xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 0fa1b3cc)
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.
292840dce1SChris Williamson  * Copyright (c) 2011, 2017 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]
33c8811bd3SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
34a4b8c9aaSAndrew Stormont  * Copyright 2017 RackTop Systems.
351702cce7SAlek Pinchuk  * Copyright (c) 2017 Datto Inc.
364445fffbSMatthew Ahrens  */
374445fffbSMatthew Ahrens 
384445fffbSMatthew Ahrens /*
394445fffbSMatthew Ahrens  * ZFS ioctls.
404445fffbSMatthew Ahrens  *
414445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
424445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
434445fffbSMatthew Ahrens  *
444445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
454445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
464445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
474445fffbSMatthew Ahrens  *
484445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
494445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
504445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
514445fffbSMatthew Ahrens  *
524445fffbSMatthew Ahrens  * The registration arguments are as follows:
534445fffbSMatthew Ahrens  *
544445fffbSMatthew Ahrens  * const char *name
554445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
564445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
574445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
584445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
594445fffbSMatthew Ahrens  *
604445fffbSMatthew Ahrens  * zfs_ioc_t ioc
614445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
624445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
634445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
644445fffbSMatthew Ahrens  *
654445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
664445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
674445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
684445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
694445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
704445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
714445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
724445fffbSMatthew Ahrens  *
734445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
744445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
754445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
764445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
774445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
784445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
794445fffbSMatthew Ahrens  *   doesn't have invalid characters).
804445fffbSMatthew Ahrens  *
814445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
824445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
834445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
844445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
854445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
864445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
874445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
884445fffbSMatthew Ahrens  *
894445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
904445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
914445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
924445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
934445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
944445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
954445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
964445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
974445fffbSMatthew Ahrens  *
984445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
994445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
1004445fffbSMatthew Ahrens  *
1014445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
1024445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
1034445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
1044445fffbSMatthew Ahrens  *   will be called with the following arguments:
1054445fffbSMatthew Ahrens  *
1064445fffbSMatthew Ahrens  *   const char *name
1074445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1084445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1094445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1104445fffbSMatthew Ahrens  *
1114445fffbSMatthew Ahrens  *   nvlist_t *innvl
1124445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1134445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1144445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1154445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1164445fffbSMatthew Ahrens  *
1174445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1184445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1194445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1204445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1214445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1224445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1234445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1244445fffbSMatthew Ahrens  *
1254445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1264445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1274445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1284445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1294445fffbSMatthew Ahrens  *         the ioctl again.
1304445fffbSMatthew Ahrens  *
1314445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1324445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1334445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1344445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1354445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1364445fffbSMatthew Ahrens  *         deserialization failing.
137e9103aaeSGarrett D'Amore  */
138fa9e4066Sahrens 
139fa9e4066Sahrens #include <sys/types.h>
140fa9e4066Sahrens #include <sys/param.h>
141fa9e4066Sahrens #include <sys/errno.h>
142fa9e4066Sahrens #include <sys/uio.h>
143fa9e4066Sahrens #include <sys/buf.h>
144fa9e4066Sahrens #include <sys/modctl.h>
145fa9e4066Sahrens #include <sys/open.h>
146fa9e4066Sahrens #include <sys/file.h>
147fa9e4066Sahrens #include <sys/kmem.h>
148fa9e4066Sahrens #include <sys/conf.h>
149fa9e4066Sahrens #include <sys/cmn_err.h>
150fa9e4066Sahrens #include <sys/stat.h>
151fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1524201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
153da6c28aaSamw #include <sys/zfs_znode.h>
154fa9e4066Sahrens #include <sys/zap.h>
155fa9e4066Sahrens #include <sys/spa.h>
156b1b8ab34Slling #include <sys/spa_impl.h>
157fa9e4066Sahrens #include <sys/vdev.h>
1584201a95eSRic Aleshire #include <sys/priv_impl.h>
159fa9e4066Sahrens #include <sys/dmu.h>
160fa9e4066Sahrens #include <sys/dsl_dir.h>
161fa9e4066Sahrens #include <sys/dsl_dataset.h>
162fa9e4066Sahrens #include <sys/dsl_prop.h>
163ecd6cf80Smarks #include <sys/dsl_deleg.h>
164ecd6cf80Smarks #include <sys/dmu_objset.h>
1654e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1663b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
167fa9e4066Sahrens #include <sys/ddi.h>
168fa9e4066Sahrens #include <sys/sunddi.h>
169fa9e4066Sahrens #include <sys/sunldi.h>
170fa9e4066Sahrens #include <sys/policy.h>
171fa9e4066Sahrens #include <sys/zone.h>
172fa9e4066Sahrens #include <sys/nvpair.h>
173fa9e4066Sahrens #include <sys/pathname.h>
174fa9e4066Sahrens #include <sys/mount.h>
175fa9e4066Sahrens #include <sys/sdt.h>
176fa9e4066Sahrens #include <sys/fs/zfs.h>
177fa9e4066Sahrens #include <sys/zfs_ctldir.h>
178da6c28aaSamw #include <sys/zfs_dir.h>
179c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
180a2eea2e1Sahrens #include <sys/zvol.h>
1813f9d6ad7SLin Ling #include <sys/dsl_scan.h>
182ecd6cf80Smarks #include <sharefs/share.h>
183f18faf3fSek #include <sys/dmu_objset.h>
184*0fa1b3ccSPaul Dagnelie #include <sys/dmu_recv.h>
1853b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1863b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18778f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1883b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
189a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
190dfc11533SChris Williamson #include <sys/zcp.h>
19145818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
1925cabbc6bSPrashanth Sreenivasa #include <sys/vdev_removal.h>
193094e47e9SGeorge Wilson #include <sys/vdev_impl.h>
194094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
195fa9e4066Sahrens 
196fa9e4066Sahrens #include "zfs_namecheck.h"
197e9dbad6fSeschrock #include "zfs_prop.h"
198ecd6cf80Smarks #include "zfs_deleg.h"
1990a586ceaSMark Shellenbaum #include "zfs_comutil.h"
200fa9e4066Sahrens 
201dfc11533SChris Williamson #include "lua.h"
202dfc11533SChris Williamson #include "lauxlib.h"
203dfc11533SChris Williamson 
204fa9e4066Sahrens extern struct modlfs zfs_modlfs;
205fa9e4066Sahrens 
206fa9e4066Sahrens extern void zfs_init(void);
207fa9e4066Sahrens extern void zfs_fini(void);
208fa9e4066Sahrens 
209fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
210fa9e4066Sahrens dev_info_t *zfs_dip;
211fa9e4066Sahrens 
2124445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
2134445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2144445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2154445fffbSMatthew Ahrens 
2164445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2174445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2184445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
219fa9e4066Sahrens 
22054d692b7SGeorge Wilson typedef enum {
22154d692b7SGeorge Wilson 	NO_NAME,
22254d692b7SGeorge Wilson 	POOL_NAME,
22354d692b7SGeorge Wilson 	DATASET_NAME
22454d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
22554d692b7SGeorge Wilson 
226f9af39baSGeorge Wilson typedef enum {
227f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
228f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2294445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
230f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
231f9af39baSGeorge Wilson 
232fa9e4066Sahrens typedef struct zfs_ioc_vec {
2334445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
234fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
235fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
23654d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2374445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
238f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2394445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2404445fffbSMatthew Ahrens 	const char		*zvec_name;
241fa9e4066Sahrens } zfs_ioc_vec_t;
242fa9e4066Sahrens 
24314843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
24414843421SMatthew Ahrens static const char *userquota_perms[] = {
24514843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
24614843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
24714843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
24814843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
24914843421SMatthew Ahrens };
25014843421SMatthew Ahrens 
25114843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
25292241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
25392241e0bSTom Erickson     cred_t *cr);
25492241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
25592241e0bSTom Erickson     nvlist_t **errors);
2560a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2570a48a24eStimh     boolean_t *);
2584445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2594445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2600a48a24eStimh 
2612acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
262a6f561b4SSašo Kiselkov 
263fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
264fa9e4066Sahrens void
265fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
266fa9e4066Sahrens {
267fa9e4066Sahrens 	const char *newfile;
2683f9d6ad7SLin Ling 	char buf[512];
269fa9e4066Sahrens 	va_list adx;
270fa9e4066Sahrens 
271fa9e4066Sahrens 	/*
272fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
273fa9e4066Sahrens 	 */
274fa9e4066Sahrens 	newfile = strrchr(file, '/');
275fa9e4066Sahrens 	if (newfile != NULL) {
276fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
277fa9e4066Sahrens 	} else {
278fa9e4066Sahrens 		newfile = file;
279fa9e4066Sahrens 	}
280fa9e4066Sahrens 
281fa9e4066Sahrens 	va_start(adx, fmt);
282fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
283fa9e4066Sahrens 	va_end(adx);
284fa9e4066Sahrens 
285fa9e4066Sahrens 	/*
286fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
287fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
288fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
289fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
290fa9e4066Sahrens 	 * arg0 = file name
291fa9e4066Sahrens 	 * arg1 = function name
292fa9e4066Sahrens 	 * arg2 = line number
293fa9e4066Sahrens 	 * arg3 = message
294fa9e4066Sahrens 	 */
295fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
296fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
297fa9e4066Sahrens }
298fa9e4066Sahrens 
299ecd6cf80Smarks static void
300228975ccSek history_str_free(char *buf)
301228975ccSek {
302228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
303228975ccSek }
304228975ccSek 
305228975ccSek static char *
306228975ccSek history_str_get(zfs_cmd_t *zc)
307ecd6cf80Smarks {
30840feaa91Sahrens 	char *buf;
309ecd6cf80Smarks 
310ecd6cf80Smarks 	if (zc->zc_history == NULL)
311228975ccSek 		return (NULL);
312e7437265Sahrens 
313ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
314ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
315ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
316228975ccSek 		history_str_free(buf);
317228975ccSek 		return (NULL);
318ecd6cf80Smarks 	}
319ecd6cf80Smarks 
320ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
321ecd6cf80Smarks 
322228975ccSek 	return (buf);
323228975ccSek }
324ecd6cf80Smarks 
32515e6edf1Sgw /*
32615e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
32715e6edf1Sgw  */
32815e6edf1Sgw static boolean_t
32915e6edf1Sgw zfs_is_bootfs(const char *name)
33015e6edf1Sgw {
331503ad85cSMatthew Ahrens 	objset_t *os;
33215e6edf1Sgw 
333503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
334503ad85cSMatthew Ahrens 		boolean_t ret;
335b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
336503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
337503ad85cSMatthew Ahrens 		return (ret);
33815e6edf1Sgw 	}
339503ad85cSMatthew Ahrens 	return (B_FALSE);
34015e6edf1Sgw }
34115e6edf1Sgw 
342c2a93d44Stimh /*
343f7170741SWill Andrews  * Return non-zero if the spa version is less than requested version.
344c2a93d44Stimh  */
345da6c28aaSamw static int
3460a48a24eStimh zfs_earlier_version(const char *name, int version)
347da6c28aaSamw {
348da6c28aaSamw 	spa_t *spa;
349da6c28aaSamw 
350da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
351da6c28aaSamw 		if (spa_version(spa) < version) {
352da6c28aaSamw 			spa_close(spa, FTAG);
353da6c28aaSamw 			return (1);
354da6c28aaSamw 		}
355da6c28aaSamw 		spa_close(spa, FTAG);
356da6c28aaSamw 	}
357da6c28aaSamw 	return (0);
358da6c28aaSamw }
359da6c28aaSamw 
3609e6eda55Smarks /*
361745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3629e6eda55Smarks  */
363745cd3c5Smaybee static boolean_t
364745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3659e6eda55Smarks {
3669e6eda55Smarks 	objset_t *os;
367745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3689e6eda55Smarks 
369503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
370745cd3c5Smaybee 		uint64_t zplversion;
3719e6eda55Smarks 
372503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
373503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
374503ad85cSMatthew Ahrens 			return (B_TRUE);
375503ad85cSMatthew Ahrens 		}
376503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
377745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
378745cd3c5Smaybee 			rc = zplversion < version;
379503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3809e6eda55Smarks 	}
3819e6eda55Smarks 	return (rc);
3829e6eda55Smarks }
3839e6eda55Smarks 
384228975ccSek static void
385228975ccSek zfs_log_history(zfs_cmd_t *zc)
386228975ccSek {
387228975ccSek 	spa_t *spa;
388228975ccSek 	char *buf;
389ecd6cf80Smarks 
390228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
391228975ccSek 		return;
392228975ccSek 
393228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
394228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
3954445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
396228975ccSek 		spa_close(spa, FTAG);
397228975ccSek 	}
398228975ccSek 	history_str_free(buf);
399ecd6cf80Smarks }
400ecd6cf80Smarks 
401fa9e4066Sahrens /*
402fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
403fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
404fa9e4066Sahrens  */
405fa9e4066Sahrens /* ARGSUSED */
406fa9e4066Sahrens static int
4074445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
408fa9e4066Sahrens {
409fa9e4066Sahrens 	return (0);
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412fa9e4066Sahrens /*
413fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
414fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
415fa9e4066Sahrens  */
416fa9e4066Sahrens /* ARGSUSED */
417fa9e4066Sahrens static int
4184445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
421ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
422fa9e4066Sahrens 		return (0);
423fa9e4066Sahrens 
424be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
425fa9e4066Sahrens }
426fa9e4066Sahrens 
427fa9e4066Sahrens static int
428a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
429fa9e4066Sahrens {
430fa9e4066Sahrens 	int writable = 1;
431fa9e4066Sahrens 
432fa9e4066Sahrens 	/*
433fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
434fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
435fa9e4066Sahrens 	 */
436fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
437fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
438be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
439fa9e4066Sahrens 
440fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
441fa9e4066Sahrens 		/*
442fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
443fa9e4066Sahrens 		 * global zone.
444fa9e4066Sahrens 		 */
445fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
446be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
447fa9e4066Sahrens 	} else {
448fa9e4066Sahrens 		/*
449fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
450fa9e4066Sahrens 		 */
451fa9e4066Sahrens 		if (!zoned)
452be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
453fa9e4066Sahrens 
454fa9e4066Sahrens 		/* must be writable by this zone */
455fa9e4066Sahrens 		if (!writable)
456be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
457fa9e4066Sahrens 	}
458fa9e4066Sahrens 	return (0);
459fa9e4066Sahrens }
460fa9e4066Sahrens 
461a7f53a56SChris Kirby static int
462a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
463a7f53a56SChris Kirby {
464a7f53a56SChris Kirby 	uint64_t zoned;
465a7f53a56SChris Kirby 
466a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
467be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
468a7f53a56SChris Kirby 
469a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
470a7f53a56SChris Kirby }
471a7f53a56SChris Kirby 
472a7f53a56SChris Kirby static int
473a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
474a7f53a56SChris Kirby {
475a7f53a56SChris Kirby 	uint64_t zoned;
476a7f53a56SChris Kirby 
4773b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
478be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
479a7f53a56SChris Kirby 
480a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
481a7f53a56SChris Kirby }
482a7f53a56SChris Kirby 
4834445fffbSMatthew Ahrens static int
4843b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4853b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
486fa9e4066Sahrens {
487fa9e4066Sahrens 	int error;
488fa9e4066Sahrens 
48919b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
490ecd6cf80Smarks 	if (error == 0) {
491ecd6cf80Smarks 		error = secpolicy_zfs(cr);
4923b2aab18SMatthew Ahrens 		if (error != 0)
4934445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
494ecd6cf80Smarks 	}
495ecd6cf80Smarks 	return (error);
496ecd6cf80Smarks }
497ecd6cf80Smarks 
4984445fffbSMatthew Ahrens static int
4993b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
500a7f53a56SChris Kirby {
501a7f53a56SChris Kirby 	int error;
5023b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
5033b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
504a7f53a56SChris Kirby 
50525f7d993SMatthew Ahrens 	/*
50625f7d993SMatthew Ahrens 	 * First do a quick check for root in the global zone, which
50725f7d993SMatthew Ahrens 	 * is allowed to do all write_perms.  This ensures that zfs_ioc_*
50825f7d993SMatthew Ahrens 	 * will get to handle nonexistent datasets.
50925f7d993SMatthew Ahrens 	 */
51025f7d993SMatthew Ahrens 	if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
51125f7d993SMatthew Ahrens 		return (0);
51225f7d993SMatthew Ahrens 
5133b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
5143b2aab18SMatthew Ahrens 	if (error != 0)
5153b2aab18SMatthew Ahrens 		return (error);
5163b2aab18SMatthew Ahrens 
5173b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
5183b2aab18SMatthew Ahrens 	if (error != 0) {
5193b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5203b2aab18SMatthew Ahrens 		return (error);
521a7f53a56SChris Kirby 	}
5223b2aab18SMatthew Ahrens 
5233b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5243b2aab18SMatthew Ahrens 
5253b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5263b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
527a7f53a56SChris Kirby 	return (error);
528a7f53a56SChris Kirby }
529a7f53a56SChris Kirby 
5304201a95eSRic Aleshire /*
5314201a95eSRic Aleshire  * Policy for setting the security label property.
5324201a95eSRic Aleshire  *
5334201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5344201a95eSRic Aleshire  */
5354201a95eSRic Aleshire static int
53692241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5374201a95eSRic Aleshire {
5384201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5394201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5404201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5414201a95eSRic Aleshire 	uint64_t	zoned;
5424201a95eSRic Aleshire 	int		needed_priv = -1;
5434201a95eSRic Aleshire 	int		error;
5444201a95eSRic Aleshire 
5454201a95eSRic Aleshire 	/* First get the existing dataset label. */
5464201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5474201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5483b2aab18SMatthew Ahrens 	if (error != 0)
549be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5504201a95eSRic Aleshire 
5514201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5524201a95eSRic Aleshire 		new_default = TRUE;
5534201a95eSRic Aleshire 
5544201a95eSRic Aleshire 	/* The label must be translatable */
5554201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
556be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5574201a95eSRic Aleshire 
5584201a95eSRic Aleshire 	/*
5594201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5604201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5614201a95eSRic Aleshire 	 * are needed.
5624201a95eSRic Aleshire 	 */
5634201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5644201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
565be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5664201a95eSRic Aleshire 		return (0);
5674201a95eSRic Aleshire 	}
5684201a95eSRic Aleshire 
5694201a95eSRic Aleshire 	/*
5704201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5714201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5724201a95eSRic Aleshire 	 * global zone.
5734201a95eSRic Aleshire 	 */
5744201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5754201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
576be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5774201a95eSRic Aleshire 	if (!zoned) {
5784201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
579be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5804201a95eSRic Aleshire 	}
5814201a95eSRic Aleshire 
5824201a95eSRic Aleshire 	/*
5834201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5844201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5854201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5864201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5874201a95eSRic Aleshire 	 */
5884201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
58992241e0bSTom Erickson 		objset_t *os;
59092241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
59192241e0bSTom Erickson 
5924201a95eSRic Aleshire 		/*
5934201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
5944201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
5954201a95eSRic Aleshire 		 */
5964201a95eSRic Aleshire 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
59792241e0bSTom Erickson 		    setsl_tag, &os);
5983b2aab18SMatthew Ahrens 		if (error != 0)
599be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
6004201a95eSRic Aleshire 
60192241e0bSTom Erickson 		dmu_objset_disown(os, setsl_tag);
60292241e0bSTom Erickson 
6034201a95eSRic Aleshire 		if (new_default) {
6044201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
6054201a95eSRic Aleshire 			goto out_check;
6064201a95eSRic Aleshire 		}
6074201a95eSRic Aleshire 
6084201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
609be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
6104201a95eSRic Aleshire 
6114201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
6124201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
6134201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
6144201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6154201a95eSRic Aleshire 	} else {
6164201a95eSRic Aleshire 		/* dataset currently has a default label */
6174201a95eSRic Aleshire 		if (!new_default)
6184201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6194201a95eSRic Aleshire 	}
6204201a95eSRic Aleshire 
6214201a95eSRic Aleshire out_check:
6224201a95eSRic Aleshire 	if (needed_priv != -1)
6234201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6244201a95eSRic Aleshire 	return (0);
6254201a95eSRic Aleshire }
6264201a95eSRic Aleshire 
627ecd6cf80Smarks static int
62892241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
62992241e0bSTom Erickson     cred_t *cr)
630ecd6cf80Smarks {
63192241e0bSTom Erickson 	char *strval;
63292241e0bSTom Erickson 
633ecd6cf80Smarks 	/*
634ecd6cf80Smarks 	 * Check permissions for special properties.
635ecd6cf80Smarks 	 */
636ecd6cf80Smarks 	switch (prop) {
637ecd6cf80Smarks 	case ZFS_PROP_ZONED:
638ecd6cf80Smarks 		/*
639ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
640ecd6cf80Smarks 		 */
641ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
642be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
643ecd6cf80Smarks 		break;
644ecd6cf80Smarks 
645ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
646a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
647a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
648ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
649ecd6cf80Smarks 			uint64_t zoned;
6509adfa60dSMatthew Ahrens 			char setpoint[ZFS_MAX_DATASET_NAME_LEN];
651ecd6cf80Smarks 			/*
652ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
653a2afb611SJerry Jelinek 			 * limit on things *under* (ie. contained by)
654ecd6cf80Smarks 			 * the thing they own.
655ecd6cf80Smarks 			 */
65692241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
657ecd6cf80Smarks 			    setpoint))
658be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
65992241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
660be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
661ecd6cf80Smarks 		}
662db870a07Sahrens 		break;
6634201a95eSRic Aleshire 
6644201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6654201a95eSRic Aleshire 		if (!is_system_labeled())
666be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
66792241e0bSTom Erickson 
66892241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
66992241e0bSTom Erickson 			int err;
67092241e0bSTom Erickson 
67192241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
67292241e0bSTom Erickson 			if (err != 0)
67392241e0bSTom Erickson 				return (err);
67492241e0bSTom Erickson 		}
6754201a95eSRic Aleshire 		break;
676ecd6cf80Smarks 	}
677ecd6cf80Smarks 
67892241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
679ecd6cf80Smarks }
680ecd6cf80Smarks 
6814445fffbSMatthew Ahrens /* ARGSUSED */
6824445fffbSMatthew Ahrens static int
6834445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
684ecd6cf80Smarks {
685ecd6cf80Smarks 	int error;
686ecd6cf80Smarks 
687ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6883b2aab18SMatthew Ahrens 	if (error != 0)
689fa9e4066Sahrens 		return (error);
690fa9e4066Sahrens 
691ecd6cf80Smarks 	/*
692ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
693ecd6cf80Smarks 	 * dsl_deleg_can_allow()
694ecd6cf80Smarks 	 */
695ecd6cf80Smarks 	return (0);
696ecd6cf80Smarks }
697ecd6cf80Smarks 
6984445fffbSMatthew Ahrens /* ARGSUSED */
6994445fffbSMatthew Ahrens static int
7004445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
701ecd6cf80Smarks {
702681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
703681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
704ecd6cf80Smarks }
705ecd6cf80Smarks 
7064445fffbSMatthew Ahrens /* ARGSUSED */
7074445fffbSMatthew Ahrens static int
7084445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
709ecd6cf80Smarks {
710a7f53a56SChris Kirby 	dsl_pool_t *dp;
711a7f53a56SChris Kirby 	dsl_dataset_t *ds;
712a7f53a56SChris Kirby 	char *cp;
713a7f53a56SChris Kirby 	int error;
714a7f53a56SChris Kirby 
715a7f53a56SChris Kirby 	/*
716a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
717a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
718a7f53a56SChris Kirby 	 */
719a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
720a7f53a56SChris Kirby 	if (cp == NULL)
721be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7223b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7233b2aab18SMatthew Ahrens 	if (error != 0)
724a7f53a56SChris Kirby 		return (error);
725a7f53a56SChris Kirby 
726a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7273b2aab18SMatthew Ahrens 	if (error != 0) {
7283b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
729a7f53a56SChris Kirby 		return (error);
7303b2aab18SMatthew Ahrens 	}
731a7f53a56SChris Kirby 
732a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
733a7f53a56SChris Kirby 
734a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
735a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
736a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
738a7f53a56SChris Kirby 
739a7f53a56SChris Kirby 	return (error);
740ecd6cf80Smarks }
741ecd6cf80Smarks 
7424445fffbSMatthew Ahrens /* ARGSUSED */
743743a77edSAlan Wright static int
7444445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7454445fffbSMatthew Ahrens {
7464445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7474445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7484445fffbSMatthew Ahrens }
7494445fffbSMatthew Ahrens 
7504445fffbSMatthew Ahrens /* ARGSUSED */
7514445fffbSMatthew Ahrens static int
7524445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
753743a77edSAlan Wright {
754743a77edSAlan Wright 	vnode_t *vp;
755743a77edSAlan Wright 	int error;
756743a77edSAlan Wright 
757743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
758743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
759743a77edSAlan Wright 		return (error);
760743a77edSAlan Wright 
761743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
762743a77edSAlan Wright 
763743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
764743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
765743a77edSAlan Wright 	    zc->zc_name) != 0)) {
766743a77edSAlan Wright 		VN_RELE(vp);
767be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
768743a77edSAlan Wright 	}
769743a77edSAlan Wright 
770743a77edSAlan Wright 	VN_RELE(vp);
771743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
772743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
773743a77edSAlan Wright }
774743a77edSAlan Wright 
775ecd6cf80Smarks int
7764445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
777ecd6cf80Smarks {
778ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
779be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
780ecd6cf80Smarks 
7813cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
782ecd6cf80Smarks 		return (0);
783ecd6cf80Smarks 	} else {
7844445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
785743a77edSAlan Wright 	}
786743a77edSAlan Wright }
787ecd6cf80Smarks 
788743a77edSAlan Wright int
7894445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
790743a77edSAlan Wright {
791743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
792be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
793ecd6cf80Smarks 
794743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
795743a77edSAlan Wright 		return (0);
796743a77edSAlan Wright 	} else {
7974445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
798ecd6cf80Smarks 	}
799fa9e4066Sahrens }
800fa9e4066Sahrens 
801fa9e4066Sahrens static int
802ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
803fa9e4066Sahrens {
804fa9e4066Sahrens 	char *cp;
805fa9e4066Sahrens 
806fa9e4066Sahrens 	/*
807fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
808fa9e4066Sahrens 	 */
809ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
810ecd6cf80Smarks 	cp = strrchr(parent, '@');
811fa9e4066Sahrens 	if (cp != NULL) {
812fa9e4066Sahrens 		cp[0] = '\0';
813fa9e4066Sahrens 	} else {
814ecd6cf80Smarks 		cp = strrchr(parent, '/');
815fa9e4066Sahrens 		if (cp == NULL)
816be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
817fa9e4066Sahrens 		cp[0] = '\0';
818ecd6cf80Smarks 	}
819ecd6cf80Smarks 
820ecd6cf80Smarks 	return (0);
821ecd6cf80Smarks }
822ecd6cf80Smarks 
823ecd6cf80Smarks int
824ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
825ecd6cf80Smarks {
826ecd6cf80Smarks 	int error;
827ecd6cf80Smarks 
828ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
829ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
830ecd6cf80Smarks 		return (error);
831ecd6cf80Smarks 
832ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
833ecd6cf80Smarks }
834ecd6cf80Smarks 
8354445fffbSMatthew Ahrens /* ARGSUSED */
836ecd6cf80Smarks static int
8374445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
838ecd6cf80Smarks {
839ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
840ecd6cf80Smarks }
841ecd6cf80Smarks 
842cbf6f6aaSWilliam Gorrell /*
843cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8444445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
845cbf6f6aaSWilliam Gorrell  */
8464445fffbSMatthew Ahrens /* ARGSUSED */
847cbf6f6aaSWilliam Gorrell static int
8484445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
849cbf6f6aaSWilliam Gorrell {
8504445fffbSMatthew Ahrens 	nvlist_t *snaps;
8514445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8524445fffbSMatthew Ahrens 	int error = 0;
853cbf6f6aaSWilliam Gorrell 
8544445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
855be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8564445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8574445fffbSMatthew Ahrens 	    pair = nextpair) {
8584445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
85978f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
86078f17100SMatthew Ahrens 		if (error == ENOENT) {
8614445fffbSMatthew Ahrens 			/*
8624445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8634445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8644445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8654445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8664445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8674445fffbSMatthew Ahrens 			 * checked for permission).
8684445fffbSMatthew Ahrens 			 */
8694445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8704445fffbSMatthew Ahrens 			error = 0;
8714445fffbSMatthew Ahrens 		}
8724445fffbSMatthew Ahrens 		if (error != 0)
8734445fffbSMatthew Ahrens 			break;
8744445fffbSMatthew Ahrens 	}
875cbf6f6aaSWilliam Gorrell 
876cbf6f6aaSWilliam Gorrell 	return (error);
877cbf6f6aaSWilliam Gorrell }
878cbf6f6aaSWilliam Gorrell 
879ecd6cf80Smarks int
880ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
881ecd6cf80Smarks {
8829adfa60dSMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
883ecd6cf80Smarks 	int	error;
884ecd6cf80Smarks 
885ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
886ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
887ecd6cf80Smarks 		return (error);
888ecd6cf80Smarks 
889ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
890ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
891ecd6cf80Smarks 		return (error);
892ecd6cf80Smarks 
893ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
894ecd6cf80Smarks 	    sizeof (parentname))) != 0)
895ecd6cf80Smarks 		return (error);
896ecd6cf80Smarks 
897ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
898ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
899ecd6cf80Smarks 		return (error);
900ecd6cf80Smarks 
901ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
902ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
903ecd6cf80Smarks 		return (error);
904ecd6cf80Smarks 
905ecd6cf80Smarks 	return (error);
906ecd6cf80Smarks }
907ecd6cf80Smarks 
9084445fffbSMatthew Ahrens /* ARGSUSED */
909ecd6cf80Smarks static int
9104445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
911ecd6cf80Smarks {
912ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
913ecd6cf80Smarks }
914ecd6cf80Smarks 
9154445fffbSMatthew Ahrens /* ARGSUSED */
916ecd6cf80Smarks static int
9174445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
918ecd6cf80Smarks {
9193b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9203b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
921ecd6cf80Smarks 	int error;
922ecd6cf80Smarks 
923ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
924ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9253b2aab18SMatthew Ahrens 	if (error != 0)
9263b2aab18SMatthew Ahrens 		return (error);
9273b2aab18SMatthew Ahrens 
9283b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9293b2aab18SMatthew Ahrens 	if (error != 0)
930ecd6cf80Smarks 		return (error);
931ecd6cf80Smarks 
9323b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
933ecd6cf80Smarks 
934ecd6cf80Smarks 	if (error == 0) {
9359adfa60dSMatthew Ahrens 		char parentname[ZFS_MAX_DATASET_NAME_LEN];
9363b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
937ecd6cf80Smarks 		dsl_dir_t *dd;
9383b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
939ecd6cf80Smarks 
940745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
941c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9423b2aab18SMatthew Ahrens 		if (error != 0) {
9433b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9443b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
945ecd6cf80Smarks 			return (error);
946ecd6cf80Smarks 		}
947ecd6cf80Smarks 
9483b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
949ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
950ecd6cf80Smarks 
9513b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9523b2aab18SMatthew Ahrens 		if (error == 0) {
9533b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
954ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9553b2aab18SMatthew Ahrens 		}
9563b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9573b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
958ecd6cf80Smarks 	}
9593b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
960ecd6cf80Smarks 	return (error);
961ecd6cf80Smarks }
962ecd6cf80Smarks 
9634445fffbSMatthew Ahrens /* ARGSUSED */
964ecd6cf80Smarks static int
9654445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
966ecd6cf80Smarks {
967ecd6cf80Smarks 	int error;
968ecd6cf80Smarks 
969ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
970ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
971ecd6cf80Smarks 		return (error);
972ecd6cf80Smarks 
973ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
974ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
975ecd6cf80Smarks 		return (error);
976ecd6cf80Smarks 
977ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
978ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
979ecd6cf80Smarks }
980ecd6cf80Smarks 
981ecd6cf80Smarks int
982ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
983ecd6cf80Smarks {
984681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
985681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
986ecd6cf80Smarks }
987ecd6cf80Smarks 
9884445fffbSMatthew Ahrens /*
9894445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
9904445fffbSMatthew Ahrens  */
9914445fffbSMatthew Ahrens /* ARGSUSED */
992ecd6cf80Smarks static int
9934445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
994ecd6cf80Smarks {
9954445fffbSMatthew Ahrens 	nvlist_t *snaps;
996d5285caeSGeorge Wilson 	int error = 0;
9974445fffbSMatthew Ahrens 	nvpair_t *pair;
998ecd6cf80Smarks 
9994445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1000be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
10014445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
10024445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
10034445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
10044445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
10054445fffbSMatthew Ahrens 
10064445fffbSMatthew Ahrens 		if (atp == NULL) {
1007be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
10084445fffbSMatthew Ahrens 			break;
10094445fffbSMatthew Ahrens 		}
10104445fffbSMatthew Ahrens 		*atp = '\0';
10114445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
10124445fffbSMatthew Ahrens 		*atp = '@';
10134445fffbSMatthew Ahrens 		if (error != 0)
10144445fffbSMatthew Ahrens 			break;
10154445fffbSMatthew Ahrens 	}
10164445fffbSMatthew Ahrens 	return (error);
1017ecd6cf80Smarks }
1018ecd6cf80Smarks 
101978f17100SMatthew Ahrens /*
102078f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
102178f17100SMatthew Ahrens  */
102278f17100SMatthew Ahrens /* ARGSUSED */
102378f17100SMatthew Ahrens static int
102478f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
102578f17100SMatthew Ahrens {
102678f17100SMatthew Ahrens 	int error = 0;
102778f17100SMatthew Ahrens 
102878f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
102978f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
103078f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
103178f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
103278f17100SMatthew Ahrens 
103378f17100SMatthew Ahrens 		if (hashp == NULL) {
103478f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
103578f17100SMatthew Ahrens 			break;
103678f17100SMatthew Ahrens 		}
103778f17100SMatthew Ahrens 		*hashp = '\0';
103878f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
103978f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
104078f17100SMatthew Ahrens 		*hashp = '#';
104178f17100SMatthew Ahrens 		if (error != 0)
104278f17100SMatthew Ahrens 			break;
104378f17100SMatthew Ahrens 	}
104478f17100SMatthew Ahrens 	return (error);
104578f17100SMatthew Ahrens }
104678f17100SMatthew Ahrens 
10475cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
10485cabbc6bSPrashanth Sreenivasa static int
10495cabbc6bSPrashanth Sreenivasa zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10505cabbc6bSPrashanth Sreenivasa {
10515cabbc6bSPrashanth Sreenivasa 	return (zfs_secpolicy_write_perms(zc->zc_name,
10525cabbc6bSPrashanth Sreenivasa 	    ZFS_DELEG_PERM_REMAP, cr));
10535cabbc6bSPrashanth Sreenivasa }
10545cabbc6bSPrashanth Sreenivasa 
105578f17100SMatthew Ahrens /* ARGSUSED */
105678f17100SMatthew Ahrens static int
105778f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
105878f17100SMatthew Ahrens {
105978f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
106078f17100SMatthew Ahrens 	int error = 0;
106178f17100SMatthew Ahrens 
106278f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
106378f17100SMatthew Ahrens 	    pair = nextpair) {
106478f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
106578f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
106678f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
106778f17100SMatthew Ahrens 
106878f17100SMatthew Ahrens 		if (hashp == NULL) {
106978f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
107078f17100SMatthew Ahrens 			break;
107178f17100SMatthew Ahrens 		}
107278f17100SMatthew Ahrens 
107378f17100SMatthew Ahrens 		*hashp = '\0';
107478f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
107578f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
107678f17100SMatthew Ahrens 		*hashp = '#';
107778f17100SMatthew Ahrens 		if (error == ENOENT) {
107878f17100SMatthew Ahrens 			/*
107978f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
108078f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
108178f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
108278f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
108378f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
108478f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
108578f17100SMatthew Ahrens 			 */
108678f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
108778f17100SMatthew Ahrens 			error = 0;
108878f17100SMatthew Ahrens 		}
108978f17100SMatthew Ahrens 		if (error != 0)
109078f17100SMatthew Ahrens 			break;
109178f17100SMatthew Ahrens 	}
109278f17100SMatthew Ahrens 
109378f17100SMatthew Ahrens 	return (error);
109478f17100SMatthew Ahrens }
109578f17100SMatthew Ahrens 
10964445fffbSMatthew Ahrens /* ARGSUSED */
1097ecd6cf80Smarks static int
10984445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10994445fffbSMatthew Ahrens {
11004445fffbSMatthew Ahrens 	/*
11014445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
11024445fffbSMatthew Ahrens 	 * to log to.
11034445fffbSMatthew Ahrens 	 */
11044445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1105be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
11064445fffbSMatthew Ahrens 	return (0);
11074445fffbSMatthew Ahrens }
11084445fffbSMatthew Ahrens 
11094445fffbSMatthew Ahrens static int
11104445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1111ecd6cf80Smarks {
11129adfa60dSMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
111392241e0bSTom Erickson 	int	error;
11144445fffbSMatthew Ahrens 	char	*origin;
1115ecd6cf80Smarks 
1116ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1117ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1118ecd6cf80Smarks 		return (error);
1119fa9e4066Sahrens 
11204445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
11214445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
11224445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
11234445fffbSMatthew Ahrens 		return (error);
1124fa9e4066Sahrens 
1125ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1126ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1127ecd6cf80Smarks 		return (error);
1128ecd6cf80Smarks 
11294445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
11304445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1131fa9e4066Sahrens }
1132fa9e4066Sahrens 
1133fa9e4066Sahrens /*
1134fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1135fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1136fa9e4066Sahrens  */
1137fa9e4066Sahrens /* ARGSUSED */
1138fa9e4066Sahrens static int
11394445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1140fa9e4066Sahrens {
1141fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1142be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1143fa9e4066Sahrens 
1144fa9e4066Sahrens 	return (0);
1145fa9e4066Sahrens }
1146fa9e4066Sahrens 
114799d5e173STim Haley /*
114899d5e173STim Haley  * Policy for object to name lookups.
114999d5e173STim Haley  */
115099d5e173STim Haley /* ARGSUSED */
115199d5e173STim Haley static int
11524445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
115399d5e173STim Haley {
115499d5e173STim Haley 	int error;
115599d5e173STim Haley 
115699d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
115799d5e173STim Haley 		return (0);
115899d5e173STim Haley 
115999d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
116099d5e173STim Haley 	return (error);
116199d5e173STim Haley }
116299d5e173STim Haley 
1163ea8dc4b6Seschrock /*
1164ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1165ea8dc4b6Seschrock  */
1166ea8dc4b6Seschrock /* ARGSUSED */
1167ea8dc4b6Seschrock static int
11684445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1169ea8dc4b6Seschrock {
1170ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1171ea8dc4b6Seschrock }
1172ea8dc4b6Seschrock 
11734445fffbSMatthew Ahrens /* ARGSUSED */
1174e45ce728Sahrens static int
11754445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1176e45ce728Sahrens {
1177e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1178e45ce728Sahrens 
1179990b4856Slling 	if (prop == ZPROP_INVAL) {
1180e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1181be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1182e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1183e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1184e45ce728Sahrens 	} else {
118592241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
118692241e0bSTom Erickson 		    NULL, cr));
1187e45ce728Sahrens 	}
1188e45ce728Sahrens }
1189e45ce728Sahrens 
119014843421SMatthew Ahrens static int
11914445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
119214843421SMatthew Ahrens {
11934445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
119414843421SMatthew Ahrens 	if (err)
119514843421SMatthew Ahrens 		return (err);
119614843421SMatthew Ahrens 
119714843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1198be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
119914843421SMatthew Ahrens 
120014843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
120114843421SMatthew Ahrens 		/*
120214843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
120314843421SMatthew Ahrens 		 * themself, allow it.
120414843421SMatthew Ahrens 		 */
120514843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
120614843421SMatthew Ahrens 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
120714843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
120814843421SMatthew Ahrens 				return (0);
120914843421SMatthew Ahrens 		} else {
121014843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
121114843421SMatthew Ahrens 				return (0);
121214843421SMatthew Ahrens 		}
121314843421SMatthew Ahrens 	}
121414843421SMatthew Ahrens 
121514843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
121614843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
121714843421SMatthew Ahrens }
121814843421SMatthew Ahrens 
121914843421SMatthew Ahrens static int
12204445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
122114843421SMatthew Ahrens {
12224445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
122314843421SMatthew Ahrens 	if (err)
122414843421SMatthew Ahrens 		return (err);
122514843421SMatthew Ahrens 
122614843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1227be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
122814843421SMatthew Ahrens 
122914843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
123014843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
123114843421SMatthew Ahrens }
123214843421SMatthew Ahrens 
12334445fffbSMatthew Ahrens /* ARGSUSED */
123414843421SMatthew Ahrens static int
12354445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
123614843421SMatthew Ahrens {
123792241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
123892241e0bSTom Erickson 	    NULL, cr));
123914843421SMatthew Ahrens }
124014843421SMatthew Ahrens 
12414445fffbSMatthew Ahrens /* ARGSUSED */
1242842727c2SChris Kirby static int
12434445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1244842727c2SChris Kirby {
12453b2aab18SMatthew Ahrens 	nvpair_t *pair;
12463b2aab18SMatthew Ahrens 	nvlist_t *holds;
12473b2aab18SMatthew Ahrens 	int error;
12483b2aab18SMatthew Ahrens 
12493b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12503b2aab18SMatthew Ahrens 	if (error != 0)
1251be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12523b2aab18SMatthew Ahrens 
12533b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12543b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
12559adfa60dSMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12563b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12573b2aab18SMatthew Ahrens 		if (error != 0)
12583b2aab18SMatthew Ahrens 			return (error);
12593b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12603b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12613b2aab18SMatthew Ahrens 		if (error != 0)
12623b2aab18SMatthew Ahrens 			return (error);
12633b2aab18SMatthew Ahrens 	}
12643b2aab18SMatthew Ahrens 	return (0);
1265842727c2SChris Kirby }
1266842727c2SChris Kirby 
12674445fffbSMatthew Ahrens /* ARGSUSED */
1268842727c2SChris Kirby static int
12694445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1270842727c2SChris Kirby {
12713b2aab18SMatthew Ahrens 	nvpair_t *pair;
12723b2aab18SMatthew Ahrens 	int error;
12733b2aab18SMatthew Ahrens 
12743b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12753b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
12769adfa60dSMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12773b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12783b2aab18SMatthew Ahrens 		if (error != 0)
12793b2aab18SMatthew Ahrens 			return (error);
12803b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12813b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12823b2aab18SMatthew Ahrens 		if (error != 0)
12833b2aab18SMatthew Ahrens 			return (error);
12843b2aab18SMatthew Ahrens 	}
12853b2aab18SMatthew Ahrens 	return (0);
1286842727c2SChris Kirby }
1287842727c2SChris Kirby 
128899d5e173STim Haley /*
128999d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
129099d5e173STim Haley  */
129199d5e173STim Haley static int
12924445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
129399d5e173STim Haley {
129499d5e173STim Haley 	/*
129599d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
129699d5e173STim Haley 	 * hold, destroy and release all rolled into one.
129799d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
129899d5e173STim Haley 	 */
129999d5e173STim Haley 	int error;
130099d5e173STim Haley 
130199d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
130299d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
130399d5e173STim Haley 		return (0);
130499d5e173STim Haley 
13054445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
13063b2aab18SMatthew Ahrens 	if (error == 0)
13074445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
13083b2aab18SMatthew Ahrens 	if (error == 0)
13094445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
13103b2aab18SMatthew Ahrens 	if (error == 0)
13114445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
131299d5e173STim Haley 	return (error);
131399d5e173STim Haley }
131499d5e173STim Haley 
1315fa9e4066Sahrens /*
1316fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1317fa9e4066Sahrens  */
1318fa9e4066Sahrens static int
1319478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1320fa9e4066Sahrens {
1321fa9e4066Sahrens 	char *packed;
1322fa9e4066Sahrens 	int error;
1323990b4856Slling 	nvlist_t *list = NULL;
1324fa9e4066Sahrens 
1325fa9e4066Sahrens 	/*
1326e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1327fa9e4066Sahrens 	 */
1328990b4856Slling 	if (size == 0)
1329be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1330fa9e4066Sahrens 
1331fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1332fa9e4066Sahrens 
1333478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1334478ed9adSEric Taylor 	    iflag)) != 0) {
1335fa9e4066Sahrens 		kmem_free(packed, size);
1336c71c00bbSRichard Yao 		return (SET_ERROR(EFAULT));
1337fa9e4066Sahrens 	}
1338fa9e4066Sahrens 
1339990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1340fa9e4066Sahrens 		kmem_free(packed, size);
1341fa9e4066Sahrens 		return (error);
1342fa9e4066Sahrens 	}
1343fa9e4066Sahrens 
1344fa9e4066Sahrens 	kmem_free(packed, size);
1345fa9e4066Sahrens 
1346990b4856Slling 	*nvp = list;
1347fa9e4066Sahrens 	return (0);
1348fa9e4066Sahrens }
1349fa9e4066Sahrens 
13504445fffbSMatthew Ahrens /*
13514445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13524445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13534445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13544445fffbSMatthew Ahrens  * removed.
13554445fffbSMatthew Ahrens  */
135692241e0bSTom Erickson static int
13574445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
135892241e0bSTom Erickson {
135992241e0bSTom Erickson 	size_t size;
136092241e0bSTom Erickson 
13614445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
136292241e0bSTom Erickson 
13634445fffbSMatthew Ahrens 	if (size > max) {
136492241e0bSTom Erickson 		nvpair_t *more_errors;
136592241e0bSTom Erickson 		int n = 0;
136692241e0bSTom Erickson 
13674445fffbSMatthew Ahrens 		if (max < 1024)
1368be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
136992241e0bSTom Erickson 
13704445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
13714445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
137292241e0bSTom Erickson 
137392241e0bSTom Erickson 		do {
13744445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
137592241e0bSTom Erickson 			    more_errors);
13764445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
137792241e0bSTom Erickson 			n++;
13784445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
13794445fffbSMatthew Ahrens 		} while (size > max);
138092241e0bSTom Erickson 
13814445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
13824445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
13834445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
138492241e0bSTom Erickson 	}
138592241e0bSTom Erickson 
138692241e0bSTom Erickson 	return (0);
138792241e0bSTom Erickson }
138892241e0bSTom Erickson 
1389e9dbad6fSeschrock static int
1390e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1391e9dbad6fSeschrock {
1392e9dbad6fSeschrock 	char *packed = NULL;
13936e27f868SSam Falkner 	int error = 0;
1394e9dbad6fSeschrock 	size_t size;
1395e9dbad6fSeschrock 
13964445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1397e9dbad6fSeschrock 
1398e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1399be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1400e9dbad6fSeschrock 	} else {
14014445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
14026e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
14036e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1404be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
14054445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1406e9dbad6fSeschrock 	}
1407e9dbad6fSeschrock 
1408e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
14094445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1410e9dbad6fSeschrock 	return (error);
1411e9dbad6fSeschrock }
1412e9dbad6fSeschrock 
1413dfc11533SChris Williamson int
1414dfc11533SChris Williamson getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
141514843421SMatthew Ahrens {
1416dfc11533SChris Williamson 	int error = 0;
1417503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1418be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1419503ad85cSMatthew Ahrens 	}
142014843421SMatthew Ahrens 
1421503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1422af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1423af4c679fSSean McEnroe 	if (*zfvp) {
1424af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
142514843421SMatthew Ahrens 	} else {
1426be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
142714843421SMatthew Ahrens 	}
1428503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1429dfc11533SChris Williamson 	return (error);
1430dfc11533SChris Williamson }
1431dfc11533SChris Williamson 
1432ed992b0aSSerapheim Dimitropoulos int
1433dfc11533SChris Williamson getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1434dfc11533SChris Williamson {
1435dfc11533SChris Williamson 	objset_t *os;
1436dfc11533SChris Williamson 	int error;
1437dfc11533SChris Williamson 
1438dfc11533SChris Williamson 	error = dmu_objset_hold(dsname, FTAG, &os);
1439dfc11533SChris Williamson 	if (error != 0)
1440dfc11533SChris Williamson 		return (error);
1441dfc11533SChris Williamson 
1442dfc11533SChris Williamson 	error = getzfsvfs_impl(os, zfvp);
1443503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
144414843421SMatthew Ahrens 	return (error);
144514843421SMatthew Ahrens }
144614843421SMatthew Ahrens 
144714843421SMatthew Ahrens /*
144814843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
144914843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1450ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1451ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
145214843421SMatthew Ahrens  */
145314843421SMatthew Ahrens static int
14541412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
145514843421SMatthew Ahrens {
145614843421SMatthew Ahrens 	int error = 0;
145714843421SMatthew Ahrens 
1458af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1459af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
146014843421SMatthew Ahrens 	if (error == 0) {
1461c9030f6cSAlexander Motin 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14621412a1a2SMark Shellenbaum 		    RW_READER, tag);
1463af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
146414843421SMatthew Ahrens 			/*
146514843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
146614843421SMatthew Ahrens 			 * thread should be just about to disassociate the
146714843421SMatthew Ahrens 			 * objset from the zfsvfs.
146814843421SMatthew Ahrens 			 */
1469c9030f6cSAlexander Motin 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1470be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
147114843421SMatthew Ahrens 		}
147214843421SMatthew Ahrens 	}
147314843421SMatthew Ahrens 	return (error);
147414843421SMatthew Ahrens }
147514843421SMatthew Ahrens 
147614843421SMatthew Ahrens static void
147714843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
147814843421SMatthew Ahrens {
1479c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
148014843421SMatthew Ahrens 
148114843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
148214843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
148314843421SMatthew Ahrens 	} else {
1484503ad85cSMatthew Ahrens 		dmu_objset_disown(zfsvfs->z_os, zfsvfs);
148514843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
148614843421SMatthew Ahrens 	}
148714843421SMatthew Ahrens }
148814843421SMatthew Ahrens 
1489fa9e4066Sahrens static int
1490fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1491fa9e4066Sahrens {
1492fa9e4066Sahrens 	int error;
1493990b4856Slling 	nvlist_t *config, *props = NULL;
14940a48a24eStimh 	nvlist_t *rootprops = NULL;
14950a48a24eStimh 	nvlist_t *zplprops = NULL;
149604e56356SAndriy Gapon 	char *spa_name = zc->zc_name;
1497fa9e4066Sahrens 
1498990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1499478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1500fa9e4066Sahrens 		return (error);
15012a6b87f0Sek 
1502990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1503478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1504478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1505990b4856Slling 		nvlist_free(config);
1506990b4856Slling 		return (error);
1507990b4856Slling 	}
1508990b4856Slling 
15090a48a24eStimh 	if (props) {
15100a48a24eStimh 		nvlist_t *nvl = NULL;
15110a48a24eStimh 		uint64_t version = SPA_VERSION;
151204e56356SAndriy Gapon 		char *tname;
15130a48a24eStimh 
15140a48a24eStimh 		(void) nvlist_lookup_uint64(props,
15150a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1516ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1517be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
15180a48a24eStimh 			goto pool_props_bad;
15190a48a24eStimh 		}
15200a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
15210a48a24eStimh 		if (nvl) {
15220a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
15230a48a24eStimh 			if (error != 0) {
15240a48a24eStimh 				nvlist_free(config);
15250a48a24eStimh 				nvlist_free(props);
15260a48a24eStimh 				return (error);
15270a48a24eStimh 			}
15280a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
15290a48a24eStimh 		}
15300a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15310a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
15320a48a24eStimh 		    zplprops, NULL);
15333b2aab18SMatthew Ahrens 		if (error != 0)
15340a48a24eStimh 			goto pool_props_bad;
153504e56356SAndriy Gapon 
153604e56356SAndriy Gapon 		if (nvlist_lookup_string(props,
153704e56356SAndriy Gapon 		    zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
153804e56356SAndriy Gapon 			spa_name = tname;
15390a48a24eStimh 	}
15400a48a24eStimh 
15414445fffbSMatthew Ahrens 	error = spa_create(zc->zc_name, config, props, zplprops);
15420a48a24eStimh 
15430a48a24eStimh 	/*
15440a48a24eStimh 	 * Set the remaining root properties
15450a48a24eStimh 	 */
154604e56356SAndriy Gapon 	if (!error && (error = zfs_set_prop_nvlist(spa_name,
154792241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
154804e56356SAndriy Gapon 		(void) spa_destroy(spa_name);
1549fa9e4066Sahrens 
15500a48a24eStimh pool_props_bad:
15510a48a24eStimh 	nvlist_free(rootprops);
15520a48a24eStimh 	nvlist_free(zplprops);
1553fa9e4066Sahrens 	nvlist_free(config);
15540a48a24eStimh 	nvlist_free(props);
1555990b4856Slling 
1556fa9e4066Sahrens 	return (error);
1557fa9e4066Sahrens }
1558fa9e4066Sahrens 
1559fa9e4066Sahrens static int
1560fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1561fa9e4066Sahrens {
1562ecd6cf80Smarks 	int error;
1563ecd6cf80Smarks 	zfs_log_history(zc);
1564ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1565681d9761SEric Taylor 	if (error == 0)
1566681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1567ecd6cf80Smarks 	return (error);
1568fa9e4066Sahrens }
1569fa9e4066Sahrens 
1570fa9e4066Sahrens static int
1571fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1572fa9e4066Sahrens {
1573990b4856Slling 	nvlist_t *config, *props = NULL;
1574fa9e4066Sahrens 	uint64_t guid;
1575468c413aSTim Haley 	int error;
1576fa9e4066Sahrens 
1577990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1578478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1579990b4856Slling 		return (error);
1580990b4856Slling 
1581990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1582478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1583478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1584990b4856Slling 		nvlist_free(config);
1585fa9e4066Sahrens 		return (error);
1586990b4856Slling 	}
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1589ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1590be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1591fa9e4066Sahrens 	else
15924b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1593fa9e4066Sahrens 
15944b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
15954b964adaSGeorge Wilson 		int err;
15964b964adaSGeorge Wilson 
15974b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
15984b964adaSGeorge Wilson 			error = err;
15994b964adaSGeorge Wilson 	}
1600468c413aSTim Haley 
1601fa9e4066Sahrens 	nvlist_free(config);
1602fa9e4066Sahrens 
1603aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(props);
1604990b4856Slling 
1605fa9e4066Sahrens 	return (error);
1606fa9e4066Sahrens }
1607fa9e4066Sahrens 
1608fa9e4066Sahrens static int
1609fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1610fa9e4066Sahrens {
1611ecd6cf80Smarks 	int error;
161289a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1613394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
161489a89ebfSlling 
1615ecd6cf80Smarks 	zfs_log_history(zc);
1616394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1617681d9761SEric Taylor 	if (error == 0)
1618681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1619ecd6cf80Smarks 	return (error);
1620fa9e4066Sahrens }
1621fa9e4066Sahrens 
1622fa9e4066Sahrens static int
1623fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1624fa9e4066Sahrens {
1625fa9e4066Sahrens 	nvlist_t *configs;
1626fa9e4066Sahrens 	int error;
1627fa9e4066Sahrens 
1628fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1629be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1630fa9e4066Sahrens 
1631e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1632fa9e4066Sahrens 
1633fa9e4066Sahrens 	nvlist_free(configs);
1634fa9e4066Sahrens 
1635fa9e4066Sahrens 	return (error);
1636fa9e4066Sahrens }
1637fa9e4066Sahrens 
1638ad135b5dSChristopher Siden /*
1639ad135b5dSChristopher Siden  * inputs:
1640ad135b5dSChristopher Siden  * zc_name		name of the pool
1641ad135b5dSChristopher Siden  *
1642ad135b5dSChristopher Siden  * outputs:
1643ad135b5dSChristopher Siden  * zc_cookie		real errno
1644ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1645ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1646ad135b5dSChristopher Siden  */
1647fa9e4066Sahrens static int
1648fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1649fa9e4066Sahrens {
1650fa9e4066Sahrens 	nvlist_t *config;
1651fa9e4066Sahrens 	int error;
1652ea8dc4b6Seschrock 	int ret = 0;
1653fa9e4066Sahrens 
1654e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1655e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1656fa9e4066Sahrens 
1657fa9e4066Sahrens 	if (config != NULL) {
1658e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1659fa9e4066Sahrens 		nvlist_free(config);
1660ea8dc4b6Seschrock 
1661ea8dc4b6Seschrock 		/*
1662ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1663ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1664ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1665ea8dc4b6Seschrock 		 */
1666ea8dc4b6Seschrock 		zc->zc_cookie = error;
1667fa9e4066Sahrens 	} else {
1668ea8dc4b6Seschrock 		ret = error;
1669fa9e4066Sahrens 	}
1670fa9e4066Sahrens 
1671ea8dc4b6Seschrock 	return (ret);
1672fa9e4066Sahrens }
1673fa9e4066Sahrens 
1674fa9e4066Sahrens /*
1675fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1676fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1677fa9e4066Sahrens  */
1678fa9e4066Sahrens static int
1679fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1680fa9e4066Sahrens {
1681fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1682fa9e4066Sahrens 	int error;
1683fa9e4066Sahrens 
1684990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1685478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1686fa9e4066Sahrens 		return (error);
1687fa9e4066Sahrens 
1688fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1689fa9e4066Sahrens 
1690fa9e4066Sahrens 	nvlist_free(tryconfig);
1691fa9e4066Sahrens 
1692fa9e4066Sahrens 	if (config == NULL)
1693be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1694fa9e4066Sahrens 
1695e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1696fa9e4066Sahrens 	nvlist_free(config);
1697fa9e4066Sahrens 
1698fa9e4066Sahrens 	return (error);
1699fa9e4066Sahrens }
1700fa9e4066Sahrens 
17013f9d6ad7SLin Ling /*
17023f9d6ad7SLin Ling  * inputs:
17033f9d6ad7SLin Ling  * zc_name              name of the pool
17043f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
17051702cce7SAlek Pinchuk  * zc_flags             scrub pause/resume flag (pool_scrub_cmd_t)
17063f9d6ad7SLin Ling  */
1707fa9e4066Sahrens static int
17083f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1709fa9e4066Sahrens {
1710fa9e4066Sahrens 	spa_t *spa;
1711fa9e4066Sahrens 	int error;
1712fa9e4066Sahrens 
171306eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
171406eeb2adSek 		return (error);
171506eeb2adSek 
17161702cce7SAlek Pinchuk 	if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
17171702cce7SAlek Pinchuk 		return (SET_ERROR(EINVAL));
17181702cce7SAlek Pinchuk 
17191702cce7SAlek Pinchuk 	if (zc->zc_flags == POOL_SCRUB_PAUSE)
17201702cce7SAlek Pinchuk 		error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
17211702cce7SAlek Pinchuk 	else if (zc->zc_cookie == POOL_SCAN_NONE)
17223f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
17233f9d6ad7SLin Ling 	else
17243f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
172506eeb2adSek 
172606eeb2adSek 	spa_close(spa, FTAG);
172706eeb2adSek 
1728fa9e4066Sahrens 	return (error);
1729fa9e4066Sahrens }
1730fa9e4066Sahrens 
1731fa9e4066Sahrens static int
1732fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1733fa9e4066Sahrens {
1734fa9e4066Sahrens 	spa_t *spa;
1735fa9e4066Sahrens 	int error;
1736fa9e4066Sahrens 
1737fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1738fa9e4066Sahrens 	if (error == 0) {
1739fa9e4066Sahrens 		spa_freeze(spa);
1740fa9e4066Sahrens 		spa_close(spa, FTAG);
1741fa9e4066Sahrens 	}
1742fa9e4066Sahrens 	return (error);
1743fa9e4066Sahrens }
1744fa9e4066Sahrens 
1745eaca9bbdSeschrock static int
1746eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1747eaca9bbdSeschrock {
1748eaca9bbdSeschrock 	spa_t *spa;
1749eaca9bbdSeschrock 	int error;
1750eaca9bbdSeschrock 
175106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
175206eeb2adSek 		return (error);
175306eeb2adSek 
1754ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1755ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1756558d2d50Slling 		spa_close(spa, FTAG);
1757be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1758558d2d50Slling 	}
1759558d2d50Slling 
1760990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
176106eeb2adSek 	spa_close(spa, FTAG);
176206eeb2adSek 
176306eeb2adSek 	return (error);
176406eeb2adSek }
176506eeb2adSek 
176606eeb2adSek static int
176706eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
176806eeb2adSek {
176906eeb2adSek 	spa_t *spa;
177006eeb2adSek 	char *hist_buf;
177106eeb2adSek 	uint64_t size;
177206eeb2adSek 	int error;
177306eeb2adSek 
177406eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1775be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
177606eeb2adSek 
177706eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
177806eeb2adSek 		return (error);
177906eeb2adSek 
1780e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1781d7306b64Sek 		spa_close(spa, FTAG);
1782be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1783d7306b64Sek 	}
1784d7306b64Sek 
178506eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
178606eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
178706eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1788478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1789478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1790478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
179106eeb2adSek 	}
179206eeb2adSek 
179306eeb2adSek 	spa_close(spa, FTAG);
179406eeb2adSek 	kmem_free(hist_buf, size);
179506eeb2adSek 	return (error);
179606eeb2adSek }
179706eeb2adSek 
1798e9103aaeSGarrett D'Amore static int
1799e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1800e9103aaeSGarrett D'Amore {
1801e9103aaeSGarrett D'Amore 	spa_t *spa;
1802e9103aaeSGarrett D'Amore 	int error;
1803e9103aaeSGarrett D'Amore 
1804e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1805e9103aaeSGarrett D'Amore 	if (error == 0) {
1806e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1807e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1808e9103aaeSGarrett D'Amore 	}
1809e9103aaeSGarrett D'Amore 	return (error);
1810e9103aaeSGarrett D'Amore }
1811e9103aaeSGarrett D'Amore 
181255434c77Sek static int
181355434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
181455434c77Sek {
18153b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
181655434c77Sek }
181755434c77Sek 
1818503ad85cSMatthew Ahrens /*
1819503ad85cSMatthew Ahrens  * inputs:
1820503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1821503ad85cSMatthew Ahrens  * zc_obj		object to find
1822503ad85cSMatthew Ahrens  *
1823503ad85cSMatthew Ahrens  * outputs:
1824503ad85cSMatthew Ahrens  * zc_value		name of object
1825503ad85cSMatthew Ahrens  */
182655434c77Sek static int
182755434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
182855434c77Sek {
1829503ad85cSMatthew Ahrens 	objset_t *os;
183055434c77Sek 	int error;
183155434c77Sek 
1832503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1833503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
183455434c77Sek 		return (error);
1835503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1836503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
1837be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1838503ad85cSMatthew Ahrens 	}
1839503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
184055434c77Sek 	    sizeof (zc->zc_value));
1841503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
184255434c77Sek 
184355434c77Sek 	return (error);
184455434c77Sek }
184555434c77Sek 
184699d5e173STim Haley /*
184799d5e173STim Haley  * inputs:
184899d5e173STim Haley  * zc_name		name of filesystem
184999d5e173STim Haley  * zc_obj		object to find
185099d5e173STim Haley  *
185199d5e173STim Haley  * outputs:
185299d5e173STim Haley  * zc_stat		stats on object
185399d5e173STim Haley  * zc_value		path to object
185499d5e173STim Haley  */
185599d5e173STim Haley static int
185699d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
185799d5e173STim Haley {
185899d5e173STim Haley 	objset_t *os;
185999d5e173STim Haley 	int error;
186099d5e173STim Haley 
186199d5e173STim Haley 	/* XXX reading from objset not owned */
186299d5e173STim Haley 	if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
186399d5e173STim Haley 		return (error);
186499d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
186599d5e173STim Haley 		dmu_objset_rele(os, FTAG);
1866be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
186799d5e173STim Haley 	}
186899d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
186999d5e173STim Haley 	    sizeof (zc->zc_value));
187099d5e173STim Haley 	dmu_objset_rele(os, FTAG);
187199d5e173STim Haley 
187299d5e173STim Haley 	return (error);
187399d5e173STim Haley }
187499d5e173STim Haley 
1875fa9e4066Sahrens static int
1876fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1877fa9e4066Sahrens {
1878fa9e4066Sahrens 	spa_t *spa;
1879fa9e4066Sahrens 	int error;
1880e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1881e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1882fa9e4066Sahrens 
1883fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1884fa9e4066Sahrens 	if (error != 0)
1885fa9e4066Sahrens 		return (error);
1886fa9e4066Sahrens 
1887fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1888478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1889fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1890fa94a07fSbrendan 	    &l2cache, &nl2cache);
1891fa94a07fSbrendan 
1892e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1893e7cbe64fSgw 	    &spares, &nspares);
1894e7cbe64fSgw 
1895b1b8ab34Slling 	/*
1896b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1897e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1898e7cbe64fSgw 	 *
1899e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1900e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1901e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1902e7cbe64fSgw 	 *
1903e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1904b1b8ab34Slling 	 */
1905b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
19061195e687SMark J Musante 		nvlist_free(config);
1907b1b8ab34Slling 		spa_close(spa, FTAG);
1908be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1909b1b8ab34Slling 	}
1910b1b8ab34Slling 
1911fa94a07fSbrendan 	if (error == 0) {
1912fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1913fa9e4066Sahrens 		nvlist_free(config);
1914fa9e4066Sahrens 	}
1915fa9e4066Sahrens 	spa_close(spa, FTAG);
1916fa9e4066Sahrens 	return (error);
1917fa9e4066Sahrens }
1918fa9e4066Sahrens 
19193f9d6ad7SLin Ling /*
19203f9d6ad7SLin Ling  * inputs:
19213f9d6ad7SLin Ling  * zc_name		name of the pool
19225cabbc6bSPrashanth Sreenivasa  * zc_guid		guid of vdev to remove
19235cabbc6bSPrashanth Sreenivasa  * zc_cookie		cancel removal
19243f9d6ad7SLin Ling  */
1925fa9e4066Sahrens static int
1926fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1927fa9e4066Sahrens {
192899653d4eSeschrock 	spa_t *spa;
192999653d4eSeschrock 	int error;
193099653d4eSeschrock 
193199653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
193299653d4eSeschrock 	if (error != 0)
193399653d4eSeschrock 		return (error);
19345cabbc6bSPrashanth Sreenivasa 	if (zc->zc_cookie != 0) {
19355cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_cancel(spa);
19365cabbc6bSPrashanth Sreenivasa 	} else {
19375cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
19385cabbc6bSPrashanth Sreenivasa 	}
193999653d4eSeschrock 	spa_close(spa, FTAG);
194099653d4eSeschrock 	return (error);
1941fa9e4066Sahrens }
1942fa9e4066Sahrens 
1943fa9e4066Sahrens static int
19443d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1945fa9e4066Sahrens {
1946fa9e4066Sahrens 	spa_t *spa;
1947fa9e4066Sahrens 	int error;
19483d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1949fa9e4066Sahrens 
195006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1951fa9e4066Sahrens 		return (error);
19523d7072f8Seschrock 	switch (zc->zc_cookie) {
19533d7072f8Seschrock 	case VDEV_STATE_ONLINE:
19543d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
19553d7072f8Seschrock 		break;
1956fa9e4066Sahrens 
19573d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
19583d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
19593d7072f8Seschrock 		break;
1960fa9e4066Sahrens 
19613d7072f8Seschrock 	case VDEV_STATE_FAULTED:
1962069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1963069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1964069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1965069f55e2SEric Schrock 
1966069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
19673d7072f8Seschrock 		break;
19683d7072f8Seschrock 
19693d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
1970069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1971069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
1972069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1973069f55e2SEric Schrock 
1974069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
19753d7072f8Seschrock 		break;
19763d7072f8Seschrock 
19773d7072f8Seschrock 	default:
1978be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
19793d7072f8Seschrock 	}
19803d7072f8Seschrock 	zc->zc_cookie = newstate;
1981fa9e4066Sahrens 	spa_close(spa, FTAG);
1982fa9e4066Sahrens 	return (error);
1983fa9e4066Sahrens }
1984fa9e4066Sahrens 
1985fa9e4066Sahrens static int
1986fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1987fa9e4066Sahrens {
1988fa9e4066Sahrens 	spa_t *spa;
1989fa9e4066Sahrens 	int replacing = zc->zc_cookie;
1990fa9e4066Sahrens 	nvlist_t *config;
1991fa9e4066Sahrens 	int error;
1992fa9e4066Sahrens 
199306eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1994fa9e4066Sahrens 		return (error);
1995fa9e4066Sahrens 
1996990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1997478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
1998ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1999fa9e4066Sahrens 		nvlist_free(config);
2000fa9e4066Sahrens 	}
2001fa9e4066Sahrens 
2002fa9e4066Sahrens 	spa_close(spa, FTAG);
2003fa9e4066Sahrens 	return (error);
2004fa9e4066Sahrens }
2005fa9e4066Sahrens 
2006fa9e4066Sahrens static int
2007fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2008fa9e4066Sahrens {
2009fa9e4066Sahrens 	spa_t *spa;
2010fa9e4066Sahrens 	int error;
2011fa9e4066Sahrens 
201206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2013fa9e4066Sahrens 		return (error);
2014fa9e4066Sahrens 
20158ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2016fa9e4066Sahrens 
2017fa9e4066Sahrens 	spa_close(spa, FTAG);
2018fa9e4066Sahrens 	return (error);
2019fa9e4066Sahrens }
2020fa9e4066Sahrens 
20211195e687SMark J Musante static int
20221195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
20231195e687SMark J Musante {
20241195e687SMark J Musante 	spa_t *spa;
20251195e687SMark J Musante 	nvlist_t *config, *props = NULL;
20261195e687SMark J Musante 	int error;
20271195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
20281195e687SMark J Musante 
20291195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
20301195e687SMark J Musante 		return (error);
20311195e687SMark J Musante 
20321195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
20331195e687SMark J Musante 	    zc->zc_iflags, &config)) {
20341195e687SMark J Musante 		spa_close(spa, FTAG);
20351195e687SMark J Musante 		return (error);
20361195e687SMark J Musante 	}
20371195e687SMark J Musante 
20381195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
20391195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20401195e687SMark J Musante 	    zc->zc_iflags, &props))) {
20411195e687SMark J Musante 		spa_close(spa, FTAG);
20421195e687SMark J Musante 		nvlist_free(config);
20431195e687SMark J Musante 		return (error);
20441195e687SMark J Musante 	}
20451195e687SMark J Musante 
20461195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
20471195e687SMark J Musante 
20481195e687SMark J Musante 	spa_close(spa, FTAG);
20491195e687SMark J Musante 
20501195e687SMark J Musante 	nvlist_free(config);
20511195e687SMark J Musante 	nvlist_free(props);
20521195e687SMark J Musante 
20531195e687SMark J Musante 	return (error);
20541195e687SMark J Musante }
20551195e687SMark J Musante 
2056c67d9675Seschrock static int
2057c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2058c67d9675Seschrock {
2059c67d9675Seschrock 	spa_t *spa;
2060e9dbad6fSeschrock 	char *path = zc->zc_value;
2061ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2062c67d9675Seschrock 	int error;
2063c67d9675Seschrock 
2064c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2065c67d9675Seschrock 	if (error != 0)
2066c67d9675Seschrock 		return (error);
2067c67d9675Seschrock 
2068c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2069c67d9675Seschrock 	spa_close(spa, FTAG);
2070c67d9675Seschrock 	return (error);
2071c67d9675Seschrock }
2072c67d9675Seschrock 
20736809eb4eSEric Schrock static int
20746809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
20756809eb4eSEric Schrock {
20766809eb4eSEric Schrock 	spa_t *spa;
20776809eb4eSEric Schrock 	char *fru = zc->zc_value;
20786809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
20796809eb4eSEric Schrock 	int error;
20806809eb4eSEric Schrock 
20816809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
20826809eb4eSEric Schrock 	if (error != 0)
20836809eb4eSEric Schrock 		return (error);
20846809eb4eSEric Schrock 
20856809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
20866809eb4eSEric Schrock 	spa_close(spa, FTAG);
20876809eb4eSEric Schrock 	return (error);
20886809eb4eSEric Schrock }
20896809eb4eSEric Schrock 
2090fa9e4066Sahrens static int
2091a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2092fa9e4066Sahrens {
2093a7f53a56SChris Kirby 	int error = 0;
20947f7322feSeschrock 	nvlist_t *nv;
2095fa9e4066Sahrens 
2096a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2097fa9e4066Sahrens 
20985ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
209992241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2100a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2101432f72fdSahrens 		/*
2102bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
2103432f72fdSahrens 		 * which we aren't supposed to do with a
2104745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2105432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2106503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2107432f72fdSahrens 		 */
210819b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
210919b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
211019b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
211119b94df9SMatthew Ahrens 			if (error == EIO)
211219b94df9SMatthew Ahrens 				return (error);
2113fb09f5aaSMadhav Suresh 			VERIFY0(error);
2114e7437265Sahrens 		}
2115e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
21167f7322feSeschrock 		nvlist_free(nv);
21177f7322feSeschrock 	}
2118fa9e4066Sahrens 
2119a7f53a56SChris Kirby 	return (error);
2120a7f53a56SChris Kirby }
2121a7f53a56SChris Kirby 
2122a7f53a56SChris Kirby /*
2123a7f53a56SChris Kirby  * inputs:
2124a7f53a56SChris Kirby  * zc_name		name of filesystem
2125a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2126a7f53a56SChris Kirby  *
2127a7f53a56SChris Kirby  * outputs:
2128a7f53a56SChris Kirby  * zc_objset_stats	stats
2129a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2130a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2131a7f53a56SChris Kirby  */
2132a7f53a56SChris Kirby static int
2133a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2134a7f53a56SChris Kirby {
21353b2aab18SMatthew Ahrens 	objset_t *os;
2136a7f53a56SChris Kirby 	int error;
2137a7f53a56SChris Kirby 
21383b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
21393b2aab18SMatthew Ahrens 	if (error == 0) {
21403b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
21413b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
21423b2aab18SMatthew Ahrens 	}
2143a7f53a56SChris Kirby 
2144fa9e4066Sahrens 	return (error);
2145fa9e4066Sahrens }
2146fa9e4066Sahrens 
214792241e0bSTom Erickson /*
214892241e0bSTom Erickson  * inputs:
214992241e0bSTom Erickson  * zc_name		name of filesystem
215092241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
215192241e0bSTom Erickson  *
215292241e0bSTom Erickson  * outputs:
215392241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
215492241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
215592241e0bSTom Erickson  *
215692241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
215792241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
215892241e0bSTom Erickson  * local property values.
215992241e0bSTom Erickson  */
216092241e0bSTom Erickson static int
216192241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
216292241e0bSTom Erickson {
21633b2aab18SMatthew Ahrens 	int error = 0;
216492241e0bSTom Erickson 	nvlist_t *nv;
216592241e0bSTom Erickson 
216692241e0bSTom Erickson 	/*
216792241e0bSTom Erickson 	 * Without this check, we would return local property values if the
216892241e0bSTom Erickson 	 * caller has not already received properties on or after
216992241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
217092241e0bSTom Erickson 	 */
21713b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2172be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
217392241e0bSTom Erickson 
217492241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
21753b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
217692241e0bSTom Erickson 		error = put_nvlist(zc, nv);
217792241e0bSTom Erickson 		nvlist_free(nv);
217892241e0bSTom Erickson 	}
217992241e0bSTom Erickson 
218092241e0bSTom Erickson 	return (error);
218192241e0bSTom Erickson }
218292241e0bSTom Erickson 
2183de8267e0Stimh static int
2184de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2185de8267e0Stimh {
2186de8267e0Stimh 	uint64_t value;
2187de8267e0Stimh 	int error;
2188de8267e0Stimh 
2189de8267e0Stimh 	/*
2190de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2191de8267e0Stimh 	 * the default value (if there is one).
2192de8267e0Stimh 	 */
2193de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2194de8267e0Stimh 		return (error);
2195de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2196de8267e0Stimh 	return (0);
2197de8267e0Stimh }
2198de8267e0Stimh 
21993cb34c60Sahrens /*
22003cb34c60Sahrens  * inputs:
22013cb34c60Sahrens  * zc_name		name of filesystem
2202de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
22033cb34c60Sahrens  *
22043cb34c60Sahrens  * outputs:
2205de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2206de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
22073cb34c60Sahrens  */
2208bd00f61bSrm static int
2209de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2210bd00f61bSrm {
2211de8267e0Stimh 	objset_t *os;
2212de8267e0Stimh 	int err;
2213bd00f61bSrm 
2214503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2215503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2216de8267e0Stimh 		return (err);
2217bd00f61bSrm 
2218bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2219bd00f61bSrm 
2220bd00f61bSrm 	/*
2221de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2222745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2223745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2224bd00f61bSrm 	 */
2225de8267e0Stimh 	if (zc->zc_nvlist_dst != NULL &&
2226de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2227de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2228de8267e0Stimh 		nvlist_t *nv;
2229de8267e0Stimh 
2230de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2231de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2232de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2233de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2234de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2235de8267e0Stimh 			err = put_nvlist(zc, nv);
2236de8267e0Stimh 		nvlist_free(nv);
2237de8267e0Stimh 	} else {
2238be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2239de8267e0Stimh 	}
2240503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2241de8267e0Stimh 	return (err);
2242bd00f61bSrm }
2243bd00f61bSrm 
224414843421SMatthew Ahrens static boolean_t
224514843421SMatthew Ahrens dataset_name_hidden(const char *name)
224614843421SMatthew Ahrens {
224714843421SMatthew Ahrens 	/*
224814843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
224914843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
225014843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
225114843421SMatthew Ahrens 	 */
225214843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
225314843421SMatthew Ahrens 		return (B_TRUE);
225414843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
225514843421SMatthew Ahrens 		return (B_TRUE);
225614843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
225714843421SMatthew Ahrens 		return (B_TRUE);
225814843421SMatthew Ahrens 	return (B_FALSE);
225914843421SMatthew Ahrens }
226014843421SMatthew Ahrens 
2261de8267e0Stimh /*
2262de8267e0Stimh  * inputs:
2263de8267e0Stimh  * zc_name		name of filesystem
2264de8267e0Stimh  * zc_cookie		zap cursor
2265de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2266de8267e0Stimh  *
2267de8267e0Stimh  * outputs:
2268de8267e0Stimh  * zc_name		name of next filesystem
226914843421SMatthew Ahrens  * zc_cookie		zap cursor
2270de8267e0Stimh  * zc_objset_stats	stats
2271de8267e0Stimh  * zc_nvlist_dst	property nvlist
2272de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2273de8267e0Stimh  */
2274fa9e4066Sahrens static int
2275fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2276fa9e4066Sahrens {
227787e5029aSahrens 	objset_t *os;
2278fa9e4066Sahrens 	int error;
2279fa9e4066Sahrens 	char *p;
2280620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2281fa9e4066Sahrens 
2282620252bcSChris Kirby top:
2283503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
228487e5029aSahrens 		if (error == ENOENT)
2285be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
228687e5029aSahrens 		return (error);
2287fa9e4066Sahrens 	}
2288fa9e4066Sahrens 
2289fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2290fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2291fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2292fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2293fa9e4066Sahrens 
2294fa9e4066Sahrens 	do {
229587e5029aSahrens 		error = dmu_dir_list_next(os,
229687e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
229787e5029aSahrens 		    NULL, &zc->zc_cookie);
2298fa9e4066Sahrens 		if (error == ENOENT)
2299be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
230019b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2301503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2302fa9e4066Sahrens 
2303681d9761SEric Taylor 	/*
2304681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2305681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2306681d9761SEric Taylor 	 */
2307620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
230887e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2309620252bcSChris Kirby 		if (error == ENOENT) {
2310620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2311620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2312620252bcSChris Kirby 			goto top;
2313620252bcSChris Kirby 		}
2314620252bcSChris Kirby 	}
2315fa9e4066Sahrens 	return (error);
2316fa9e4066Sahrens }
2317fa9e4066Sahrens 
23183cb34c60Sahrens /*
23193cb34c60Sahrens  * inputs:
23203cb34c60Sahrens  * zc_name		name of filesystem
23213cb34c60Sahrens  * zc_cookie		zap cursor
23223cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
23230d8fa8f8SMartin Matuska  * zc_simple		when set, only name is requested
23243cb34c60Sahrens  *
23253cb34c60Sahrens  * outputs:
23263cb34c60Sahrens  * zc_name		name of next snapshot
23273cb34c60Sahrens  * zc_objset_stats	stats
23283cb34c60Sahrens  * zc_nvlist_dst	property nvlist
23293cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
23303cb34c60Sahrens  */
2331fa9e4066Sahrens static int
2332fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2333fa9e4066Sahrens {
233487e5029aSahrens 	objset_t *os;
2335fa9e4066Sahrens 	int error;
2336fa9e4066Sahrens 
2337503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
23383b2aab18SMatthew Ahrens 	if (error != 0) {
2339745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
23403b2aab18SMatthew Ahrens 	}
2341fa9e4066Sahrens 
2342b81d61a6Slling 	/*
2343b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2344b81d61a6Slling 	 * so exit immediately.
2345b81d61a6Slling 	 */
23469adfa60dSMatthew Ahrens 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
23479adfa60dSMatthew Ahrens 	    ZFS_MAX_DATASET_NAME_LEN) {
2348503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2349be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2350fa9e4066Sahrens 	}
2351fa9e4066Sahrens 
235287e5029aSahrens 	error = dmu_snapshot_list_next(os,
235387e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2354a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2355a7f53a56SChris Kirby 	    NULL);
2356a7f53a56SChris Kirby 
23570d8fa8f8SMartin Matuska 	if (error == 0 && !zc->zc_simple) {
2358a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2359a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2360a7f53a56SChris Kirby 
2361a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
23623b2aab18SMatthew Ahrens 		if (error == 0) {
2363a7f53a56SChris Kirby 			objset_t *ossnap;
2364a7f53a56SChris Kirby 
2365a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2366a7f53a56SChris Kirby 			if (error == 0)
2367a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2368a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2369620252bcSChris Kirby 		}
2370620252bcSChris Kirby 	} else if (error == ENOENT) {
2371be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2372620252bcSChris Kirby 	}
2373fa9e4066Sahrens 
2374a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
23753cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
23763b2aab18SMatthew Ahrens 	if (error != 0)
23773cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2378fa9e4066Sahrens 	return (error);
2379fa9e4066Sahrens }
2380fa9e4066Sahrens 
238192241e0bSTom Erickson static int
238292241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2383fa9e4066Sahrens {
238492241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
238592241e0bSTom Erickson 	uint64_t *valary;
238692241e0bSTom Erickson 	unsigned int vallen;
238792241e0bSTom Erickson 	const char *domain;
2388eeb85002STim Haley 	char *dash;
238992241e0bSTom Erickson 	zfs_userquota_prop_t type;
239092241e0bSTom Erickson 	uint64_t rid;
239192241e0bSTom Erickson 	uint64_t quota;
239292241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
239392241e0bSTom Erickson 	int err;
239492241e0bSTom Erickson 
239592241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
239692241e0bSTom Erickson 		nvlist_t *attrs;
239792241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2398eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2399eeb85002STim Haley 		    &pair) != 0)
2400be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
240192241e0bSTom Erickson 	}
2402e9dbad6fSeschrock 
2403ecd6cf80Smarks 	/*
2404eeb85002STim Haley 	 * A correctly constructed propname is encoded as
240592241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2406ecd6cf80Smarks 	 */
2407eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2408eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2409eeb85002STim Haley 	    vallen != 3)
2410be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2411eeb85002STim Haley 
2412eeb85002STim Haley 	domain = dash + 1;
2413eeb85002STim Haley 	type = valary[0];
2414eeb85002STim Haley 	rid = valary[1];
2415eeb85002STim Haley 	quota = valary[2];
2416e9dbad6fSeschrock 
24171412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
241892241e0bSTom Erickson 	if (err == 0) {
241992241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
242092241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
242192241e0bSTom Erickson 	}
2422e9dbad6fSeschrock 
242392241e0bSTom Erickson 	return (err);
242492241e0bSTom Erickson }
242514843421SMatthew Ahrens 
242692241e0bSTom Erickson /*
242792241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
242892241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
242992241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
243092241e0bSTom Erickson  *
2431eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
243292241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
243392241e0bSTom Erickson  */
243492241e0bSTom Erickson static int
243592241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
243692241e0bSTom Erickson     nvpair_t *pair)
243792241e0bSTom Erickson {
243892241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
243992241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
244092241e0bSTom Erickson 	uint64_t intval;
2441b5152584SMatthew Ahrens 	int err = -1;
2442fa9e4066Sahrens 
244392241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
244492241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
244592241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
244692241e0bSTom Erickson 		return (-1);
244792241e0bSTom Erickson 	}
244814843421SMatthew Ahrens 
244992241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
245092241e0bSTom Erickson 		nvlist_t *attrs;
245192241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
245292241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
245392241e0bSTom Erickson 		    &pair) == 0);
245492241e0bSTom Erickson 	}
2455db870a07Sahrens 
245692241e0bSTom Erickson 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
245792241e0bSTom Erickson 		return (-1);
2458b24ab676SJeff Bonwick 
245992241e0bSTom Erickson 	VERIFY(0 == nvpair_value_uint64(pair, &intval));
246040feaa91Sahrens 
246192241e0bSTom Erickson 	switch (prop) {
246292241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
246392241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
246492241e0bSTom Erickson 		break;
246592241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
24663b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
246792241e0bSTom Erickson 		break;
2468a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2469a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2470a2afb611SJerry Jelinek 		if (intval == UINT64_MAX) {
2471a2afb611SJerry Jelinek 			/* clearing the limit, just do it */
2472a2afb611SJerry Jelinek 			err = 0;
2473a2afb611SJerry Jelinek 		} else {
2474a2afb611SJerry Jelinek 			err = dsl_dir_activate_fs_ss_limit(dsname);
2475a2afb611SJerry Jelinek 		}
2476a2afb611SJerry Jelinek 		/*
2477a2afb611SJerry Jelinek 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2478a2afb611SJerry Jelinek 		 * default path to set the value in the nvlist.
2479a2afb611SJerry Jelinek 		 */
2480a2afb611SJerry Jelinek 		if (err == 0)
2481a2afb611SJerry Jelinek 			err = -1;
2482a2afb611SJerry Jelinek 		break;
248392241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
248492241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
248592241e0bSTom Erickson 		break;
248692241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
24873b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
248892241e0bSTom Erickson 		break;
248992241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2490c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
249192241e0bSTom Erickson 		break;
249292241e0bSTom Erickson 	case ZFS_PROP_VERSION:
249392241e0bSTom Erickson 	{
249492241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
24959e6eda55Smarks 
24961412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2497b24ab676SJeff Bonwick 			break;
2498b24ab676SJeff Bonwick 
249992241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
250092241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2501d0f3f37eSMark Shellenbaum 
250292241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2503b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2504b16da2e2SGeorge Wilson 
2505b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2506b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2507b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2508b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
250940feaa91Sahrens 		}
251092241e0bSTom Erickson 		break;
2511ecd6cf80Smarks 	}
251292241e0bSTom Erickson 	default:
251392241e0bSTom Erickson 		err = -1;
251492241e0bSTom Erickson 	}
2515e9dbad6fSeschrock 
251692241e0bSTom Erickson 	return (err);
251792241e0bSTom Erickson }
2518a9799022Sck 
251992241e0bSTom Erickson /*
252092241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
25214445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
25224445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
25234445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
25244445fffbSMatthew Ahrens  * corresponding error numbers.
252592241e0bSTom Erickson  *
25264445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
25274445fffbSMatthew Ahrens  * modified.
252892241e0bSTom Erickson  */
252992241e0bSTom Erickson int
253092241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
25314445fffbSMatthew Ahrens     nvlist_t *errlist)
253292241e0bSTom Erickson {
253392241e0bSTom Erickson 	nvpair_t *pair;
253492241e0bSTom Erickson 	nvpair_t *propval;
253502e383d1STom Erickson 	int rv = 0;
253692241e0bSTom Erickson 	uint64_t intval;
253792241e0bSTom Erickson 	char *strval;
25384445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
25394445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2540a9799022Sck 
254192241e0bSTom Erickson retry:
254292241e0bSTom Erickson 	pair = NULL;
254392241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
254492241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
254592241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2546cfa69fd2STom Erickson 		int err = 0;
2547e9dbad6fSeschrock 
254892241e0bSTom Erickson 		/* decode the property value */
254992241e0bSTom Erickson 		propval = pair;
255092241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
255192241e0bSTom Erickson 			nvlist_t *attrs;
25524445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2553eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2554eeb85002STim Haley 			    &propval) != 0)
2555be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
255614843421SMatthew Ahrens 		}
2557e9dbad6fSeschrock 
255892241e0bSTom Erickson 		/* Validate value type */
2559eeb85002STim Haley 		if (err == 0 && prop == ZPROP_INVAL) {
256092241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
256192241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2562be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
256392241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
256492241e0bSTom Erickson 				if (nvpair_type(propval) !=
256592241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2566be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
256719b94df9SMatthew Ahrens 			} else {
2568be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
25694201a95eSRic Aleshire 			}
2570eeb85002STim Haley 		} else if (err == 0) {
257192241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
257292241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2573be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
257492241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2575a2eea2e1Sahrens 				const char *unused;
2576a2eea2e1Sahrens 
25774445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2578e9dbad6fSeschrock 
2579e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
258091ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2581e9dbad6fSeschrock 					break;
258291ebeef5Sahrens 				case PROP_TYPE_STRING:
2583be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
258492241e0bSTom Erickson 					break;
258591ebeef5Sahrens 				case PROP_TYPE_INDEX:
2586acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
258792241e0bSTom Erickson 					    intval, &unused) != 0)
2588be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2589e9dbad6fSeschrock 					break;
2590e9dbad6fSeschrock 				default:
2591e7437265Sahrens 					cmn_err(CE_PANIC,
2592e7437265Sahrens 					    "unknown property type");
2593e9dbad6fSeschrock 				}
2594e9dbad6fSeschrock 			} else {
2595be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2596e9dbad6fSeschrock 			}
2597e9dbad6fSeschrock 		}
259892241e0bSTom Erickson 
259992241e0bSTom Erickson 		/* Validate permissions */
260092241e0bSTom Erickson 		if (err == 0)
260192241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
260292241e0bSTom Erickson 
260392241e0bSTom Erickson 		if (err == 0) {
260492241e0bSTom Erickson 			err = zfs_prop_set_special(dsname, source, pair);
260592241e0bSTom Erickson 			if (err == -1) {
260692241e0bSTom Erickson 				/*
260792241e0bSTom Erickson 				 * For better performance we build up a list of
260892241e0bSTom Erickson 				 * properties to set in a single transaction.
260992241e0bSTom Erickson 				 */
261092241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
261192241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
261292241e0bSTom Erickson 				/*
261392241e0bSTom Erickson 				 * This may be a spurious error caused by
261492241e0bSTom Erickson 				 * receiving quota and reservation out of order.
261592241e0bSTom Erickson 				 * Try again in a second pass.
261692241e0bSTom Erickson 				 */
261792241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
261892241e0bSTom Erickson 			}
261992241e0bSTom Erickson 		}
262092241e0bSTom Erickson 
26214445fffbSMatthew Ahrens 		if (err != 0) {
26224445fffbSMatthew Ahrens 			if (errlist != NULL)
26234445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
26244445fffbSMatthew Ahrens 			rv = err;
26254445fffbSMatthew Ahrens 		}
2626e9dbad6fSeschrock 	}
2627e9dbad6fSeschrock 
262892241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
262992241e0bSTom Erickson 		nvl = retrynvl;
263092241e0bSTom Erickson 		goto retry;
263192241e0bSTom Erickson 	}
263292241e0bSTom Erickson 
263392241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
263492241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
263592241e0bSTom Erickson 		/*
263692241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
263792241e0bSTom Erickson 		 * can, so try setting them individually.
263892241e0bSTom Erickson 		 */
263992241e0bSTom Erickson 		pair = NULL;
264092241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
264192241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2642cfa69fd2STom Erickson 			int err = 0;
264392241e0bSTom Erickson 
264492241e0bSTom Erickson 			propval = pair;
264592241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
264692241e0bSTom Erickson 				nvlist_t *attrs;
26474445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
26484445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
26494445fffbSMatthew Ahrens 				    ZPROP_VALUE);
265092241e0bSTom Erickson 			}
265192241e0bSTom Erickson 
265292241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
26534445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
26543b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
26553b2aab18SMatthew Ahrens 				    source, strval);
265692241e0bSTom Erickson 			} else {
26574445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
26583b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
26593b2aab18SMatthew Ahrens 				    intval);
266092241e0bSTom Erickson 			}
266192241e0bSTom Erickson 
266292241e0bSTom Erickson 			if (err != 0) {
26634445fffbSMatthew Ahrens 				if (errlist != NULL) {
26644445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
26654445fffbSMatthew Ahrens 					    err);
26664445fffbSMatthew Ahrens 				}
26674445fffbSMatthew Ahrens 				rv = err;
266892241e0bSTom Erickson 			}
266992241e0bSTom Erickson 		}
26705c0b6a79SRich Morris 	}
26715c0b6a79SRich Morris 	nvlist_free(genericnvl);
267292241e0bSTom Erickson 	nvlist_free(retrynvl);
267392241e0bSTom Erickson 
267492241e0bSTom Erickson 	return (rv);
2675fa9e4066Sahrens }
2676fa9e4066Sahrens 
2677ea2f5b9eSMatthew Ahrens /*
2678ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2679ea2f5b9eSMatthew Ahrens  */
2680ea2f5b9eSMatthew Ahrens static int
26814445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2682ea2f5b9eSMatthew Ahrens {
268392241e0bSTom Erickson 	nvpair_t *pair = NULL;
2684ea2f5b9eSMatthew Ahrens 	int error = 0;
2685ea2f5b9eSMatthew Ahrens 
268692241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
268792241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2688ea2f5b9eSMatthew Ahrens 
2689ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
269092241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2691be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2692ea2f5b9eSMatthew Ahrens 
2693ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2694ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2695ea2f5b9eSMatthew Ahrens 			return (error);
2696ea2f5b9eSMatthew Ahrens 
2697ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2698be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2699ea2f5b9eSMatthew Ahrens 
270078f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2701ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2702ea2f5b9eSMatthew Ahrens 	}
2703ea2f5b9eSMatthew Ahrens 	return (0);
2704ea2f5b9eSMatthew Ahrens }
2705ea2f5b9eSMatthew Ahrens 
270692241e0bSTom Erickson static void
270792241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
270892241e0bSTom Erickson {
270992241e0bSTom Erickson 	nvpair_t *pair;
271092241e0bSTom Erickson 
271192241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
271292241e0bSTom Erickson 
271392241e0bSTom Erickson 	pair = NULL;
271492241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
271592241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
271692241e0bSTom Erickson 			continue;
271792241e0bSTom Erickson 
271892241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
271992241e0bSTom Erickson 	}
272092241e0bSTom Erickson }
272192241e0bSTom Erickson 
272292241e0bSTom Erickson static int
27233b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
272492241e0bSTom Erickson     nvlist_t *skipped)
272592241e0bSTom Erickson {
272692241e0bSTom Erickson 	int err = 0;
272792241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
272892241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
272992241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
273092241e0bSTom Erickson 		/*
273192241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
273292241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
273392241e0bSTom Erickson 		 */
273492241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
27353b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
27363b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
273792241e0bSTom Erickson 	}
273892241e0bSTom Erickson 	nvlist_free(cleared_props);
273992241e0bSTom Erickson 	return (err);
274092241e0bSTom Erickson }
274192241e0bSTom Erickson 
27423cb34c60Sahrens /*
27433cb34c60Sahrens  * inputs:
27443cb34c60Sahrens  * zc_name		name of filesystem
27455c0b6a79SRich Morris  * zc_value		name of property to set
27463cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
274792241e0bSTom Erickson  * zc_cookie		received properties flag
27483cb34c60Sahrens  *
274992241e0bSTom Erickson  * outputs:
275092241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
27513cb34c60Sahrens  */
2752fa9e4066Sahrens static int
2753e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2754fa9e4066Sahrens {
2755e9dbad6fSeschrock 	nvlist_t *nvl;
275692241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
275792241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
275892241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
27594445fffbSMatthew Ahrens 	nvlist_t *errors;
2760e9dbad6fSeschrock 	int error;
2761e9dbad6fSeschrock 
2762990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2763478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2764e9dbad6fSeschrock 		return (error);
2765e9dbad6fSeschrock 
276692241e0bSTom Erickson 	if (received) {
2767bb0ade09Sahrens 		nvlist_t *origprops;
2768bb0ade09Sahrens 
27693b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
27703b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
27713b2aab18SMatthew Ahrens 			    origprops, nvl);
27723b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2773bb0ade09Sahrens 		}
27743b2aab18SMatthew Ahrens 
27753b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2776bb0ade09Sahrens 	}
2777bb0ade09Sahrens 
27784445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
27793b2aab18SMatthew Ahrens 	if (error == 0)
27803b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
278192241e0bSTom Erickson 
278292241e0bSTom Erickson 	if (zc->zc_nvlist_dst != NULL && errors != NULL) {
278392241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
278492241e0bSTom Erickson 	}
2785ecd6cf80Smarks 
278692241e0bSTom Erickson 	nvlist_free(errors);
2787e9dbad6fSeschrock 	nvlist_free(nvl);
2788e9dbad6fSeschrock 	return (error);
2789fa9e4066Sahrens }
2790fa9e4066Sahrens 
27913cb34c60Sahrens /*
27923cb34c60Sahrens  * inputs:
27933cb34c60Sahrens  * zc_name		name of filesystem
27943cb34c60Sahrens  * zc_value		name of property to inherit
279592241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
27963cb34c60Sahrens  *
27973cb34c60Sahrens  * outputs:		none
27983cb34c60Sahrens  */
2799e45ce728Sahrens static int
2800e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2801e45ce728Sahrens {
280292241e0bSTom Erickson 	const char *propname = zc->zc_value;
280392241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
280492241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
280592241e0bSTom Erickson 	zprop_source_t source = (received
280692241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
280792241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
280892241e0bSTom Erickson 
280992241e0bSTom Erickson 	if (received) {
281092241e0bSTom Erickson 		nvlist_t *dummy;
281192241e0bSTom Erickson 		nvpair_t *pair;
281292241e0bSTom Erickson 		zprop_type_t type;
281392241e0bSTom Erickson 		int err;
281492241e0bSTom Erickson 
281592241e0bSTom Erickson 		/*
281692241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
281792241e0bSTom Erickson 		 * nvpair with type info.
281892241e0bSTom Erickson 		 */
281992241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
282092241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2821be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
282292241e0bSTom Erickson 
282392241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2824a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2825a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2826be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
282792241e0bSTom Erickson 		} else {
282892241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
282992241e0bSTom Erickson 		}
283092241e0bSTom Erickson 
283192241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
283292241e0bSTom Erickson 
283392241e0bSTom Erickson 		switch (type) {
283492241e0bSTom Erickson 		case PROP_TYPE_STRING:
283592241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
283692241e0bSTom Erickson 			break;
283792241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
283892241e0bSTom Erickson 		case PROP_TYPE_INDEX:
283992241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
284092241e0bSTom Erickson 			break;
284192241e0bSTom Erickson 		default:
284292241e0bSTom Erickson 			nvlist_free(dummy);
2843be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
284492241e0bSTom Erickson 		}
284592241e0bSTom Erickson 
284692241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
284792241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
284892241e0bSTom Erickson 		nvlist_free(dummy);
284992241e0bSTom Erickson 		if (err != -1)
285092241e0bSTom Erickson 			return (err); /* special property already handled */
285192241e0bSTom Erickson 	} else {
285292241e0bSTom Erickson 		/*
285392241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
285492241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
285592241e0bSTom Erickson 		 * and reservation to the received or default values even though
285692241e0bSTom Erickson 		 * they are not considered inheritable.
285792241e0bSTom Erickson 		 */
285892241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2859be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
286092241e0bSTom Erickson 	}
286192241e0bSTom Erickson 
28624445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
28633b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2864e45ce728Sahrens }
2865e45ce728Sahrens 
2866b1b8ab34Slling static int
286711a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2868b1b8ab34Slling {
2869990b4856Slling 	nvlist_t *props;
2870b1b8ab34Slling 	spa_t *spa;
2871990b4856Slling 	int error;
287292241e0bSTom Erickson 	nvpair_t *pair;
2873b1b8ab34Slling 
287492241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
287592241e0bSTom Erickson 	    zc->zc_iflags, &props))
2876b1b8ab34Slling 		return (error);
2877b1b8ab34Slling 
2878379c004dSEric Schrock 	/*
2879379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2880379c004dSEric Schrock 	 * to handle the faulted case.
2881379c004dSEric Schrock 	 */
288292241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
288392241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2884379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
288592241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2886379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2887379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2888379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
28895cabbc6bSPrashanth Sreenivasa 			spa_write_cachefile(spa, B_FALSE, B_TRUE);
2890379c004dSEric Schrock 		}
2891379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2892b693757aSEric Schrock 		if (spa != NULL) {
2893b693757aSEric Schrock 			nvlist_free(props);
2894379c004dSEric Schrock 			return (0);
2895b693757aSEric Schrock 		}
2896379c004dSEric Schrock 	}
2897379c004dSEric Schrock 
2898b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2899990b4856Slling 		nvlist_free(props);
2900b1b8ab34Slling 		return (error);
2901b1b8ab34Slling 	}
2902b1b8ab34Slling 
2903990b4856Slling 	error = spa_prop_set(spa, props);
2904b1b8ab34Slling 
2905990b4856Slling 	nvlist_free(props);
2906b1b8ab34Slling 	spa_close(spa, FTAG);
2907b1b8ab34Slling 
2908b1b8ab34Slling 	return (error);
2909b1b8ab34Slling }
2910b1b8ab34Slling 
2911b1b8ab34Slling static int
291211a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2913b1b8ab34Slling {
2914b1b8ab34Slling 	spa_t *spa;
2915b1b8ab34Slling 	int error;
2916b1b8ab34Slling 	nvlist_t *nvp = NULL;
2917b1b8ab34Slling 
2918379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2919379c004dSEric Schrock 		/*
2920379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2921379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2922379c004dSEric Schrock 		 * anyway.
2923379c004dSEric Schrock 		 */
2924379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2925379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2926379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
2927379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2928379c004dSEric Schrock 	} else {
2929379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
2930379c004dSEric Schrock 		spa_close(spa, FTAG);
2931379c004dSEric Schrock 	}
2932b1b8ab34Slling 
2933b1b8ab34Slling 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2934b1b8ab34Slling 		error = put_nvlist(zc, nvp);
2935b1b8ab34Slling 	else
2936be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
2937b1b8ab34Slling 
2938379c004dSEric Schrock 	nvlist_free(nvp);
2939b1b8ab34Slling 	return (error);
2940b1b8ab34Slling }
2941b1b8ab34Slling 
29423cb34c60Sahrens /*
29433cb34c60Sahrens  * inputs:
29443cb34c60Sahrens  * zc_name		name of filesystem
29453cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29463cb34c60Sahrens  * zc_perm_action	allow/unallow flag
29473cb34c60Sahrens  *
29483cb34c60Sahrens  * outputs:		none
29493cb34c60Sahrens  */
2950ecd6cf80Smarks static int
2951ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2952ecd6cf80Smarks {
2953ecd6cf80Smarks 	int error;
2954ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
2955ecd6cf80Smarks 
2956990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2957478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
2958ecd6cf80Smarks 		return (error);
2959ecd6cf80Smarks 
2960ecd6cf80Smarks 	/*
2961ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
2962ecd6cf80Smarks 	 */
2963ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2964ecd6cf80Smarks 		nvlist_free(fsaclnv);
2965be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2966ecd6cf80Smarks 	}
2967ecd6cf80Smarks 
2968ecd6cf80Smarks 	/*
2969ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
2970ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
2971ecd6cf80Smarks 	 * the nvlist(s)
2972ecd6cf80Smarks 	 */
2973ecd6cf80Smarks 
297491ebeef5Sahrens 	error = secpolicy_zfs(CRED());
29753b2aab18SMatthew Ahrens 	if (error != 0) {
297691ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
297791ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
297891ebeef5Sahrens 			    fsaclnv, CRED());
297991ebeef5Sahrens 		} else {
298091ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
298191ebeef5Sahrens 			    fsaclnv, CRED());
298291ebeef5Sahrens 		}
2983ecd6cf80Smarks 	}
2984ecd6cf80Smarks 
2985ecd6cf80Smarks 	if (error == 0)
2986ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2987ecd6cf80Smarks 
2988ecd6cf80Smarks 	nvlist_free(fsaclnv);
2989ecd6cf80Smarks 	return (error);
2990ecd6cf80Smarks }
2991ecd6cf80Smarks 
29923cb34c60Sahrens /*
29933cb34c60Sahrens  * inputs:
29943cb34c60Sahrens  * zc_name		name of filesystem
29953cb34c60Sahrens  *
29963cb34c60Sahrens  * outputs:
29973cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
29983cb34c60Sahrens  */
2999ecd6cf80Smarks static int
3000ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3001ecd6cf80Smarks {
3002ecd6cf80Smarks 	nvlist_t *nvp;
3003ecd6cf80Smarks 	int error;
3004ecd6cf80Smarks 
3005ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3006ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
3007ecd6cf80Smarks 		nvlist_free(nvp);
3008ecd6cf80Smarks 	}
3009ecd6cf80Smarks 
3010ecd6cf80Smarks 	return (error);
3011ecd6cf80Smarks }
3012ecd6cf80Smarks 
3013ecd6cf80Smarks /* ARGSUSED */
3014fa9e4066Sahrens static void
3015ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3016fa9e4066Sahrens {
3017da6c28aaSamw 	zfs_creat_t *zct = arg;
3018da6c28aaSamw 
3019de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3020da6c28aaSamw }
3021da6c28aaSamw 
3022de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3023da6c28aaSamw 
3024da6c28aaSamw /*
3025de8267e0Stimh  * inputs:
30260a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3027f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3028f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3029f7170741SWill Andrews  * createprops		list of properties requested by creator
3030de8267e0Stimh  *
3031de8267e0Stimh  * outputs:
3032de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
30330a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3034da6c28aaSamw  *
3035de8267e0Stimh  * Determine the settings for utf8only, normalization and
3036de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3037de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3038de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3039de8267e0Stimh  * request settings for these properties, even if the requested
3040de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3041de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3042de8267e0Stimh  * processing.
3043da6c28aaSamw  */
3044da6c28aaSamw static int
304514843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
30460a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
30470a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3048da6c28aaSamw {
3049de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3050de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3051de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3052da6c28aaSamw 
3053de8267e0Stimh 	ASSERT(zplprops != NULL);
3054da6c28aaSamw 
3055b127fe3cSAndriy Gapon 	if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3056b127fe3cSAndriy Gapon 		return (SET_ERROR(EINVAL));
3057b127fe3cSAndriy Gapon 
3058de8267e0Stimh 	/*
3059de8267e0Stimh 	 * Pull out creator prop choices, if any.
3060de8267e0Stimh 	 */
3061de8267e0Stimh 	if (createprops) {
30620a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
30630a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3064de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3065de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3066de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3067de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3068de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3069de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3070de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3071de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3072de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3073de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3074de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3075de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3076de8267e0Stimh 	}
3077da6c28aaSamw 
3078c2a93d44Stimh 	/*
30790a48a24eStimh 	 * If the zpl version requested is whacky or the file system
30800a48a24eStimh 	 * or pool is version is too "young" to support normalization
30810a48a24eStimh 	 * and the creator tried to set a value for one of the props,
30820a48a24eStimh 	 * error out.
3083c2a93d44Stimh 	 */
30840a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
30850a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
30860a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
30870a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3088de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
30890a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3090be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3091c2a93d44Stimh 
3092de8267e0Stimh 	/*
3093de8267e0Stimh 	 * Put the version in the zplprops
3094de8267e0Stimh 	 */
3095de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3096de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3097da6c28aaSamw 
3098de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3099de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3100de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3101de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3102da6c28aaSamw 
3103c2a93d44Stimh 	/*
3104de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3105c2a93d44Stimh 	 */
3106de8267e0Stimh 	if (norm)
3107de8267e0Stimh 		u8 = 1;
3108de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3109de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3110de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3111de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3112de8267e0Stimh 
3113de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3114de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3115de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3116de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3117c2a93d44Stimh 
3118ab04eb8eStimh 	if (is_ci)
3119ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3120ab04eb8eStimh 
3121da6c28aaSamw 	return (0);
3122fa9e4066Sahrens }
3123fa9e4066Sahrens 
31240a48a24eStimh static int
31250a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
31260a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31270a48a24eStimh {
31280a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
31290a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31300a48a24eStimh 	objset_t *os = NULL;
31319adfa60dSMatthew Ahrens 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
31320a48a24eStimh 	char *cp;
31330a586ceaSMark Shellenbaum 	spa_t *spa;
31340a586ceaSMark Shellenbaum 	uint64_t spa_vers;
31350a48a24eStimh 	int error;
31360a48a24eStimh 
31370a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
31380a48a24eStimh 	cp = strrchr(parentname, '/');
31390a48a24eStimh 	ASSERT(cp != NULL);
31400a48a24eStimh 	cp[0] = '\0';
31410a48a24eStimh 
31420a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
31430a586ceaSMark Shellenbaum 		return (error);
31440a586ceaSMark Shellenbaum 
31450a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
31460a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
31470a586ceaSMark Shellenbaum 
31480a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31490a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31500a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31510a48a24eStimh 
31520a48a24eStimh 	/*
31530a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
31540a48a24eStimh 	 */
3155503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
31560a48a24eStimh 		return (error);
31570a48a24eStimh 
31580a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
31590a48a24eStimh 	    zplprops, is_ci);
3160503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
31610a48a24eStimh 	return (error);
31620a48a24eStimh }
31630a48a24eStimh 
31640a48a24eStimh static int
31650a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
31660a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
31670a48a24eStimh {
31680a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
31690a586ceaSMark Shellenbaum 	boolean_t sa_ok;
31700a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
31710a48a24eStimh 	int error;
31720a48a24eStimh 
31730a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
31740a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
31750a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
31760a48a24eStimh 
31770a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
31780a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
31790a48a24eStimh 	return (error);
31800a48a24eStimh }
31810a48a24eStimh 
31823cb34c60Sahrens /*
31834445fffbSMatthew Ahrens  * innvl: {
31844445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
31854445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
31864445fffbSMatthew Ahrens  * }
31873cb34c60Sahrens  *
31884445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
31893cb34c60Sahrens  */
3190fa9e4066Sahrens static int
31914445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3192fa9e4066Sahrens {
3193fa9e4066Sahrens 	int error = 0;
31944445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3195ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3196ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
31974445fffbSMatthew Ahrens 	int32_t type32;
31984445fffbSMatthew Ahrens 	dmu_objset_type_t type;
31994445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3200fa9e4066Sahrens 
32014445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3202be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32034445fffbSMatthew Ahrens 	type = type32;
32044445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3205fa9e4066Sahrens 
32064445fffbSMatthew Ahrens 	switch (type) {
3207fa9e4066Sahrens 	case DMU_OST_ZFS:
3208fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3209fa9e4066Sahrens 		break;
3210fa9e4066Sahrens 
3211fa9e4066Sahrens 	case DMU_OST_ZVOL:
3212fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3213fa9e4066Sahrens 		break;
3214fa9e4066Sahrens 
3215fa9e4066Sahrens 	default:
32161d452cf5Sahrens 		cbfunc = NULL;
3217e7cbe64fSgw 		break;
3218fa9e4066Sahrens 	}
32194445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
32204445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3221be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3222fa9e4066Sahrens 
3223da6c28aaSamw 	zct.zct_props = nvprops;
3224da6c28aaSamw 
32254445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3226be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32274445fffbSMatthew Ahrens 
32284445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
32294445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
32304445fffbSMatthew Ahrens 
32314445fffbSMatthew Ahrens 		if (nvprops == NULL)
3232be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32334445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
32344445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3235be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3236fa9e4066Sahrens 
32374445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
32384445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
32394445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3240be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
32414445fffbSMatthew Ahrens 
32424445fffbSMatthew Ahrens 		if (error != 0)
32434445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
32444445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
32454445fffbSMatthew Ahrens 
32464445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
32474445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
32484445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
32494445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3250fa9e4066Sahrens 			return (error);
32514445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
32524445fffbSMatthew Ahrens 		int error;
3253ab04eb8eStimh 
32544445fffbSMatthew Ahrens 		/*
32554445fffbSMatthew Ahrens 		 * We have to have normalization and
32564445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
32574445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
32584445fffbSMatthew Ahrens 		 * now.
32594445fffbSMatthew Ahrens 		 */
32604445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
32614445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
32624445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
32634445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
32644445fffbSMatthew Ahrens 		if (error != 0) {
32654445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3266da6c28aaSamw 			return (error);
3267da6c28aaSamw 		}
32684445fffbSMatthew Ahrens 	}
3269ab04eb8eStimh 
32704445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
32714445fffbSMatthew Ahrens 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
32724445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
32735c5460e9Seschrock 
32744445fffbSMatthew Ahrens 	/*
32754445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
32764445fffbSMatthew Ahrens 	 */
32774445fffbSMatthew Ahrens 	if (error == 0) {
32784445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
32794445fffbSMatthew Ahrens 		    nvprops, outnvl);
32804445fffbSMatthew Ahrens 		if (error != 0)
32813b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
32824445fffbSMatthew Ahrens 	}
32834445fffbSMatthew Ahrens 	return (error);
32844445fffbSMatthew Ahrens }
3285e9dbad6fSeschrock 
32864445fffbSMatthew Ahrens /*
32874445fffbSMatthew Ahrens  * innvl: {
32884445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
32894445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
32904445fffbSMatthew Ahrens  * }
32914445fffbSMatthew Ahrens  *
32924445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32934445fffbSMatthew Ahrens  */
32944445fffbSMatthew Ahrens static int
32954445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
32964445fffbSMatthew Ahrens {
32974445fffbSMatthew Ahrens 	int error = 0;
32984445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
32994445fffbSMatthew Ahrens 	char *origin_name;
3300e9dbad6fSeschrock 
33014445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3302be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33034445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3304e9dbad6fSeschrock 
33054445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
33064445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3307be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3308e9dbad6fSeschrock 
33094445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3310be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33113b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
33123b2aab18SMatthew Ahrens 	if (error != 0)
33134445fffbSMatthew Ahrens 		return (error);
3314e9dbad6fSeschrock 
3315e9dbad6fSeschrock 	/*
3316e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3317e9dbad6fSeschrock 	 */
3318e9dbad6fSeschrock 	if (error == 0) {
33194445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33204445fffbSMatthew Ahrens 		    nvprops, outnvl);
332192241e0bSTom Erickson 		if (error != 0)
33223b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3323e9dbad6fSeschrock 	}
3324fa9e4066Sahrens 	return (error);
3325fa9e4066Sahrens }
3326fa9e4066Sahrens 
33275cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
33285cabbc6bSPrashanth Sreenivasa static int
33295cabbc6bSPrashanth Sreenivasa zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
33305cabbc6bSPrashanth Sreenivasa {
33315cabbc6bSPrashanth Sreenivasa 	if (strchr(fsname, '@') ||
33325cabbc6bSPrashanth Sreenivasa 	    strchr(fsname, '%'))
33335cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EINVAL));
33345cabbc6bSPrashanth Sreenivasa 
33355cabbc6bSPrashanth Sreenivasa 	return (dmu_objset_remap_indirects(fsname));
33365cabbc6bSPrashanth Sreenivasa }
33375cabbc6bSPrashanth Sreenivasa 
33383cb34c60Sahrens /*
33394445fffbSMatthew Ahrens  * innvl: {
33404445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
33414445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
33424445fffbSMatthew Ahrens  * }
33434445fffbSMatthew Ahrens  *
33444445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
33453cb34c60Sahrens  */
3346fa9e4066Sahrens static int
33474445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3348fa9e4066Sahrens {
33494445fffbSMatthew Ahrens 	nvlist_t *snaps;
33504445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
33514445fffbSMatthew Ahrens 	int error, poollen;
33524445fffbSMatthew Ahrens 	nvpair_t *pair;
3353bb0ade09Sahrens 
33544445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
33554445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
33564445fffbSMatthew Ahrens 		return (error);
33574445fffbSMatthew Ahrens 
33584445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
33594445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3360be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
33614445fffbSMatthew Ahrens 
33624445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3363be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33644445fffbSMatthew Ahrens 	poollen = strlen(poolname);
33654445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
33664445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
33674445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
33684445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3369bb0ade09Sahrens 
33704445fffbSMatthew Ahrens 		/*
33714445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
33724445fffbSMatthew Ahrens 		 * contain only valid characters.
33734445fffbSMatthew Ahrens 		 */
337478f17100SMatthew Ahrens 		if (cp == NULL ||
337578f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3376be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3377bb0ade09Sahrens 
33784445fffbSMatthew Ahrens 		/*
33794445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
33804445fffbSMatthew Ahrens 		 */
33814445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
33824445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3383be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
33844445fffbSMatthew Ahrens 
33854445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
33864445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
33874445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
33884445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
33894445fffbSMatthew Ahrens 			    == 0) {
3390be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
33914445fffbSMatthew Ahrens 			}
33924445fffbSMatthew Ahrens 		}
33934445fffbSMatthew Ahrens 	}
3394bb0ade09Sahrens 
33953b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
33964445fffbSMatthew Ahrens 	return (error);
33974445fffbSMatthew Ahrens }
33984445fffbSMatthew Ahrens 
33994445fffbSMatthew Ahrens /*
34004445fffbSMatthew Ahrens  * innvl: "message" -> string
34014445fffbSMatthew Ahrens  */
34024445fffbSMatthew Ahrens /* ARGSUSED */
34034445fffbSMatthew Ahrens static int
34044445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
34054445fffbSMatthew Ahrens {
34064445fffbSMatthew Ahrens 	char *message;
34074445fffbSMatthew Ahrens 	spa_t *spa;
34084445fffbSMatthew Ahrens 	int error;
34094445fffbSMatthew Ahrens 	char *poolname;
34104445fffbSMatthew Ahrens 
34114445fffbSMatthew Ahrens 	/*
34124445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
34134445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
34144445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
34154445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
34164445fffbSMatthew Ahrens 	 * we clear the TSD here.
34174445fffbSMatthew Ahrens 	 */
34184445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
34194445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
34204445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
34214445fffbSMatthew Ahrens 	strfree(poolname);
34224445fffbSMatthew Ahrens 	if (error != 0)
34234445fffbSMatthew Ahrens 		return (error);
34244445fffbSMatthew Ahrens 
34254445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
34264445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3427be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3428bb0ade09Sahrens 	}
3429ea2f5b9eSMatthew Ahrens 
34304445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
34314445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3432be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
34334445fffbSMatthew Ahrens 	}
3434ea2f5b9eSMatthew Ahrens 
34354445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
34364445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3437bb0ade09Sahrens 	return (error);
34381d452cf5Sahrens }
3439fa9e4066Sahrens 
34403b2aab18SMatthew Ahrens /*
34413b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
34423b2aab18SMatthew Ahrens  * unmount may need to write out data.
34433b2aab18SMatthew Ahrens  *
34443b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
34453b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3446fc7a6e3fSWill Andrews  *
3447fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3448fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
34493b2aab18SMatthew Ahrens  */
3450ed992b0aSSerapheim Dimitropoulos void
34513b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
34521d452cf5Sahrens {
3453ed992b0aSSerapheim Dimitropoulos 	vfs_t *vfsp = NULL;
3454ed992b0aSSerapheim Dimitropoulos 	zfsvfs_t *zfsvfs = NULL;
34551d452cf5Sahrens 
34563b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3457ed992b0aSSerapheim Dimitropoulos 		return;
34581d452cf5Sahrens 
3459ed992b0aSSerapheim Dimitropoulos 	int err = getzfsvfs(snapname, &zfsvfs);
3460ed992b0aSSerapheim Dimitropoulos 	if (err != 0) {
3461ed992b0aSSerapheim Dimitropoulos 		ASSERT3P(zfsvfs, ==, NULL);
3462ed992b0aSSerapheim Dimitropoulos 		return;
3463ed992b0aSSerapheim Dimitropoulos 	}
3464ed992b0aSSerapheim Dimitropoulos 	vfsp = zfsvfs->z_vfs;
34653b2aab18SMatthew Ahrens 
34663b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
34671d452cf5Sahrens 
3468fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
34694445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
3470fc7a6e3fSWill Andrews 	if (err != 0)
3471ed992b0aSSerapheim Dimitropoulos 		return;
34724445fffbSMatthew Ahrens 
34734445fffbSMatthew Ahrens 	/*
34744445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
34754445fffbSMatthew Ahrens 	 */
34763b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
34773b2aab18SMatthew Ahrens }
34783b2aab18SMatthew Ahrens 
34793b2aab18SMatthew Ahrens /* ARGSUSED */
34803b2aab18SMatthew Ahrens static int
34813b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
34823b2aab18SMatthew Ahrens {
3483ed992b0aSSerapheim Dimitropoulos 	zfs_unmount_snap(snapname);
3484ed992b0aSSerapheim Dimitropoulos 	return (0);
34853b2aab18SMatthew Ahrens }
34863b2aab18SMatthew Ahrens 
34873b2aab18SMatthew Ahrens /*
34883b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
34893b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
34903b2aab18SMatthew Ahrens  * if necessary.
34913b2aab18SMatthew Ahrens  */
34923b2aab18SMatthew Ahrens void
34933b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
34943b2aab18SMatthew Ahrens {
34953b2aab18SMatthew Ahrens 	int error;
34963b2aab18SMatthew Ahrens 	objset_t *os;
34973b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
34983b2aab18SMatthew Ahrens 
34993b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
35003b2aab18SMatthew Ahrens 	if (error != 0)
35013b2aab18SMatthew Ahrens 		return;
35023b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
35033b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
35049adfa60dSMatthew Ahrens 		char originname[ZFS_MAX_DATASET_NAME_LEN];
35053b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
35063b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3507ed992b0aSSerapheim Dimitropoulos 		zfs_unmount_snap(originname);
35083b2aab18SMatthew Ahrens 	} else {
35093b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
35103b2aab18SMatthew Ahrens 	}
35111d452cf5Sahrens }
35121d452cf5Sahrens 
35133cb34c60Sahrens /*
35144445fffbSMatthew Ahrens  * innvl: {
35154445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
35164445fffbSMatthew Ahrens  *     (optional boolean) "defer"
35174445fffbSMatthew Ahrens  * }
35184445fffbSMatthew Ahrens  *
35194445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
35203cb34c60Sahrens  *
35213cb34c60Sahrens  */
352278f17100SMatthew Ahrens /* ARGSUSED */
35231d452cf5Sahrens static int
35244445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
35251d452cf5Sahrens {
35264445fffbSMatthew Ahrens 	nvlist_t *snaps;
352719b94df9SMatthew Ahrens 	nvpair_t *pair;
35284445fffbSMatthew Ahrens 	boolean_t defer;
35291d452cf5Sahrens 
35304445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3531be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
35324445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
353319b94df9SMatthew Ahrens 
35344445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
35354445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
3536ed992b0aSSerapheim Dimitropoulos 		zfs_unmount_snap(nvpair_name(pair));
353778f17100SMatthew Ahrens 	}
353878f17100SMatthew Ahrens 
353978f17100SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
354078f17100SMatthew Ahrens }
354178f17100SMatthew Ahrens 
354278f17100SMatthew Ahrens /*
354378f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
354478f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
354578f17100SMatthew Ahrens  *
354678f17100SMatthew Ahrens  * innvl: {
354778f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
354878f17100SMatthew Ahrens  * }
354978f17100SMatthew Ahrens  *
355078f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
355178f17100SMatthew Ahrens  *
355278f17100SMatthew Ahrens  */
355378f17100SMatthew Ahrens /* ARGSUSED */
355478f17100SMatthew Ahrens static int
355578f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
355678f17100SMatthew Ahrens {
355778f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
355878f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
355978f17100SMatthew Ahrens 		char *snap_name;
356078f17100SMatthew Ahrens 
356178f17100SMatthew Ahrens 		/*
356278f17100SMatthew Ahrens 		 * Verify the snapshot argument.
356378f17100SMatthew Ahrens 		 */
356478f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
356578f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
356678f17100SMatthew Ahrens 
356778f17100SMatthew Ahrens 
356878f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
356978f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
357078f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
357178f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
357278f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
357378f17100SMatthew Ahrens 		}
357478f17100SMatthew Ahrens 	}
357578f17100SMatthew Ahrens 
357678f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
357778f17100SMatthew Ahrens }
357878f17100SMatthew Ahrens 
357978f17100SMatthew Ahrens /*
358078f17100SMatthew Ahrens  * innvl: {
358178f17100SMatthew Ahrens  *     property 1, property 2, ...
358278f17100SMatthew Ahrens  * }
358378f17100SMatthew Ahrens  *
358478f17100SMatthew Ahrens  * outnvl: {
358578f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
358678f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
358778f17100SMatthew Ahrens  * }
358878f17100SMatthew Ahrens  *
358978f17100SMatthew Ahrens  */
359078f17100SMatthew Ahrens static int
359178f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
359278f17100SMatthew Ahrens {
359378f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
359478f17100SMatthew Ahrens }
359578f17100SMatthew Ahrens 
359678f17100SMatthew Ahrens /*
359778f17100SMatthew Ahrens  * innvl: {
359878f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
359978f17100SMatthew Ahrens  * }
360078f17100SMatthew Ahrens  *
360178f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
360278f17100SMatthew Ahrens  *
360378f17100SMatthew Ahrens  */
360478f17100SMatthew Ahrens static int
360578f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
360678f17100SMatthew Ahrens     nvlist_t *outnvl)
360778f17100SMatthew Ahrens {
360878f17100SMatthew Ahrens 	int error, poollen;
360978f17100SMatthew Ahrens 
361078f17100SMatthew Ahrens 	poollen = strlen(poolname);
361178f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
361278f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
361319b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
361478f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
36154445fffbSMatthew Ahrens 
361619b94df9SMatthew Ahrens 		/*
361778f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
361878f17100SMatthew Ahrens 		 * must contain only valid characters.
361978f17100SMatthew Ahrens 		 */
362078f17100SMatthew Ahrens 		if (cp == NULL ||
362178f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
362278f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
362378f17100SMatthew Ahrens 
362478f17100SMatthew Ahrens 		/*
362578f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
362619b94df9SMatthew Ahrens 		 */
36274445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
362878f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
3629be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
363019b94df9SMatthew Ahrens 	}
363119b94df9SMatthew Ahrens 
363278f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
363378f17100SMatthew Ahrens 	return (error);
36341d452cf5Sahrens }
36351d452cf5Sahrens 
3636dfc11533SChris Williamson static int
3637dfc11533SChris Williamson zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3638dfc11533SChris Williamson     nvlist_t *outnvl)
3639dfc11533SChris Williamson {
3640dfc11533SChris Williamson 	char *program;
3641dfc11533SChris Williamson 	uint64_t instrlimit, memlimit;
3642a3b28680SSerapheim Dimitropoulos 	boolean_t sync_flag;
3643dfc11533SChris Williamson 	nvpair_t *nvarg = NULL;
3644dfc11533SChris Williamson 
3645dfc11533SChris Williamson 	if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3646dfc11533SChris Williamson 		return (EINVAL);
3647dfc11533SChris Williamson 	}
3648a3b28680SSerapheim Dimitropoulos 	if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3649a3b28680SSerapheim Dimitropoulos 		sync_flag = B_TRUE;
3650a3b28680SSerapheim Dimitropoulos 	}
3651dfc11533SChris Williamson 	if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3652dfc11533SChris Williamson 		instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3653dfc11533SChris Williamson 	}
3654dfc11533SChris Williamson 	if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3655dfc11533SChris Williamson 		memlimit = ZCP_DEFAULT_MEMLIMIT;
3656dfc11533SChris Williamson 	}
3657dfc11533SChris Williamson 	if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3658dfc11533SChris Williamson 		return (EINVAL);
3659dfc11533SChris Williamson 	}
3660dfc11533SChris Williamson 
3661dfc11533SChris Williamson 	if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3662dfc11533SChris Williamson 		return (EINVAL);
36632840dce1SChris Williamson 	if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3664dfc11533SChris Williamson 		return (EINVAL);
3665dfc11533SChris Williamson 
3666a3b28680SSerapheim Dimitropoulos 	return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3667dfc11533SChris Williamson 	    nvarg, outnvl));
3668dfc11533SChris Williamson }
3669dfc11533SChris Williamson 
367086714001SSerapheim Dimitropoulos /*
367186714001SSerapheim Dimitropoulos  * innvl: unused
367286714001SSerapheim Dimitropoulos  * outnvl: empty
367386714001SSerapheim Dimitropoulos  */
367486714001SSerapheim Dimitropoulos /* ARGSUSED */
367586714001SSerapheim Dimitropoulos static int
367686714001SSerapheim Dimitropoulos zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
367786714001SSerapheim Dimitropoulos {
367886714001SSerapheim Dimitropoulos 	return (spa_checkpoint(poolname));
367986714001SSerapheim Dimitropoulos }
368086714001SSerapheim Dimitropoulos 
368186714001SSerapheim Dimitropoulos /*
368286714001SSerapheim Dimitropoulos  * innvl: unused
368386714001SSerapheim Dimitropoulos  * outnvl: empty
368486714001SSerapheim Dimitropoulos  */
368586714001SSerapheim Dimitropoulos /* ARGSUSED */
368686714001SSerapheim Dimitropoulos static int
368786714001SSerapheim Dimitropoulos zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
368886714001SSerapheim Dimitropoulos     nvlist_t *outnvl)
368986714001SSerapheim Dimitropoulos {
369086714001SSerapheim Dimitropoulos 	return (spa_checkpoint_discard(poolname));
369186714001SSerapheim Dimitropoulos }
369286714001SSerapheim Dimitropoulos 
36933cb34c60Sahrens /*
36943cb34c60Sahrens  * inputs:
36953cb34c60Sahrens  * zc_name		name of dataset to destroy
3696842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
36973cb34c60Sahrens  *
36983cb34c60Sahrens  * outputs:		none
36993cb34c60Sahrens  */
37001d452cf5Sahrens static int
37011d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
37021d452cf5Sahrens {
3703049ba636SAndriy Gapon 	objset_t *os;
3704049ba636SAndriy Gapon 	dmu_objset_type_t ost;
3705681d9761SEric Taylor 	int err;
3706fc7a6e3fSWill Andrews 
3707049ba636SAndriy Gapon 	err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3708049ba636SAndriy Gapon 	if (err != 0)
3709049ba636SAndriy Gapon 		return (err);
3710049ba636SAndriy Gapon 	ost = dmu_objset_type(os);
3711049ba636SAndriy Gapon 	dmu_objset_rele(os, FTAG);
3712049ba636SAndriy Gapon 
3713049ba636SAndriy Gapon 	if (ost == DMU_OST_ZFS)
3714ed992b0aSSerapheim Dimitropoulos 		zfs_unmount_snap(zc->zc_name);
3715fa9e4066Sahrens 
37163b2aab18SMatthew Ahrens 	if (strchr(zc->zc_name, '@'))
37173b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
37183b2aab18SMatthew Ahrens 	else
37193b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
3720049ba636SAndriy Gapon 	if (ost == DMU_OST_ZVOL && err == 0)
37215c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3722681d9761SEric Taylor 	return (err);
3723fa9e4066Sahrens }
3724fa9e4066Sahrens 
3725094e47e9SGeorge Wilson /*
3726094e47e9SGeorge Wilson  * innvl: {
3727094e47e9SGeorge Wilson  *     vdevs: {
3728094e47e9SGeorge Wilson  *         guid 1, guid 2, ...
3729094e47e9SGeorge Wilson  *     },
3730094e47e9SGeorge Wilson  *     func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3731094e47e9SGeorge Wilson  * }
3732094e47e9SGeorge Wilson  *
3733094e47e9SGeorge Wilson  * outnvl: {
3734094e47e9SGeorge Wilson  *     [func: EINVAL (if provided command type didn't make sense)],
3735094e47e9SGeorge Wilson  *     [vdevs: {
3736094e47e9SGeorge Wilson  *         guid1: errno, (see function body for possible errnos)
3737094e47e9SGeorge Wilson  *         ...
3738094e47e9SGeorge Wilson  *     }]
3739094e47e9SGeorge Wilson  * }
3740094e47e9SGeorge Wilson  *
3741094e47e9SGeorge Wilson  */
3742094e47e9SGeorge Wilson static int
3743094e47e9SGeorge Wilson zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3744094e47e9SGeorge Wilson {
3745094e47e9SGeorge Wilson 	spa_t *spa;
3746094e47e9SGeorge Wilson 	int error;
3747094e47e9SGeorge Wilson 
3748094e47e9SGeorge Wilson 	error = spa_open(poolname, &spa, FTAG);
3749094e47e9SGeorge Wilson 	if (error != 0)
3750094e47e9SGeorge Wilson 		return (error);
3751094e47e9SGeorge Wilson 
3752094e47e9SGeorge Wilson 	uint64_t cmd_type;
3753094e47e9SGeorge Wilson 	if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3754094e47e9SGeorge Wilson 	    &cmd_type) != 0) {
3755094e47e9SGeorge Wilson 		spa_close(spa, FTAG);
3756094e47e9SGeorge Wilson 		return (SET_ERROR(EINVAL));
3757094e47e9SGeorge Wilson 	}
3758094e47e9SGeorge Wilson 	if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3759094e47e9SGeorge Wilson 	    cmd_type == POOL_INITIALIZE_DO ||
3760094e47e9SGeorge Wilson 	    cmd_type == POOL_INITIALIZE_SUSPEND)) {
3761094e47e9SGeorge Wilson 		spa_close(spa, FTAG);
3762094e47e9SGeorge Wilson 		return (SET_ERROR(EINVAL));
3763094e47e9SGeorge Wilson 	}
3764094e47e9SGeorge Wilson 
3765094e47e9SGeorge Wilson 	nvlist_t *vdev_guids;
3766094e47e9SGeorge Wilson 	if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3767094e47e9SGeorge Wilson 	    &vdev_guids) != 0) {
3768094e47e9SGeorge Wilson 		spa_close(spa, FTAG);
3769094e47e9SGeorge Wilson 		return (SET_ERROR(EINVAL));
3770094e47e9SGeorge Wilson 	}
3771094e47e9SGeorge Wilson 
3772094e47e9SGeorge Wilson 	nvlist_t *vdev_errlist = fnvlist_alloc();
3773094e47e9SGeorge Wilson 	int total_errors = 0;
3774094e47e9SGeorge Wilson 
3775094e47e9SGeorge Wilson 	for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3776094e47e9SGeorge Wilson 	    pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3777094e47e9SGeorge Wilson 		uint64_t vdev_guid = fnvpair_value_uint64(pair);
3778094e47e9SGeorge Wilson 
3779094e47e9SGeorge Wilson 		error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
3780094e47e9SGeorge Wilson 		if (error != 0) {
3781094e47e9SGeorge Wilson 			char guid_as_str[MAXNAMELEN];
3782094e47e9SGeorge Wilson 
3783094e47e9SGeorge Wilson 			(void) snprintf(guid_as_str, sizeof (guid_as_str),
3784094e47e9SGeorge Wilson 			    "%llu", (unsigned long long)vdev_guid);
3785094e47e9SGeorge Wilson 			fnvlist_add_int64(vdev_errlist, guid_as_str, error);
3786094e47e9SGeorge Wilson 			total_errors++;
3787094e47e9SGeorge Wilson 		}
3788094e47e9SGeorge Wilson 	}
3789094e47e9SGeorge Wilson 	if (fnvlist_size(vdev_errlist) > 0) {
3790094e47e9SGeorge Wilson 		fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3791094e47e9SGeorge Wilson 		    vdev_errlist);
3792094e47e9SGeorge Wilson 	}
3793094e47e9SGeorge Wilson 	fnvlist_free(vdev_errlist);
3794094e47e9SGeorge Wilson 
3795094e47e9SGeorge Wilson 	spa_close(spa, FTAG);
3796094e47e9SGeorge Wilson 	return (total_errors > 0 ? EINVAL : 0);
3797094e47e9SGeorge Wilson }
3798094e47e9SGeorge Wilson 
37993cb34c60Sahrens /*
3800a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
38013cb34c60Sahrens  *
380277b17137SAndriy Gapon  * innvl may contain name of expected target snapshot
3803a7027df1SMatthew Ahrens  *
3804a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3805a7027df1SMatthew Ahrens  * }
38063cb34c60Sahrens  */
3807a7027df1SMatthew Ahrens /* ARGSUSED */
3808fa9e4066Sahrens static int
380977b17137SAndriy Gapon zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3810fa9e4066Sahrens {
3811ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
381277b17137SAndriy Gapon 	char *target = NULL;
38133b2aab18SMatthew Ahrens 	int error;
3814ae46e4c7SMatthew Ahrens 
381577b17137SAndriy Gapon 	(void) nvlist_lookup_string(innvl, "target", &target);
381677b17137SAndriy Gapon 	if (target != NULL) {
381795643f75SAndriy Gapon 		const char *cp = strchr(target, '@');
381877b17137SAndriy Gapon 
381995643f75SAndriy Gapon 		/*
382095643f75SAndriy Gapon 		 * The snap name must contain an @, and the part after it must
382195643f75SAndriy Gapon 		 * contain only valid characters.
382295643f75SAndriy Gapon 		 */
382395643f75SAndriy Gapon 		if (cp == NULL ||
382495643f75SAndriy Gapon 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
382577b17137SAndriy Gapon 			return (SET_ERROR(EINVAL));
382677b17137SAndriy Gapon 	}
382777b17137SAndriy Gapon 
3828a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3829690041b9SAndriy Gapon 		dsl_dataset_t *ds;
3830690041b9SAndriy Gapon 
3831690041b9SAndriy Gapon 		ds = dmu_objset_ds(zfsvfs->z_os);
3832503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
383347f263f4Sek 		if (error == 0) {
383447f263f4Sek 			int resume_err;
38354ccbb6e7Sahrens 
383677b17137SAndriy Gapon 			error = dsl_dataset_rollback(fsname, target, zfsvfs,
383777b17137SAndriy Gapon 			    outnvl);
3838690041b9SAndriy Gapon 			resume_err = zfs_resume_fs(zfsvfs, ds);
383947f263f4Sek 			error = error ? error : resume_err;
384047f263f4Sek 		}
38414ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
38424ccbb6e7Sahrens 	} else {
384377b17137SAndriy Gapon 		error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
38444ccbb6e7Sahrens 	}
38453b2aab18SMatthew Ahrens 	return (error);
38463b2aab18SMatthew Ahrens }
38474ccbb6e7Sahrens 
38483b2aab18SMatthew Ahrens static int
38493b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
38503b2aab18SMatthew Ahrens {
38513b2aab18SMatthew Ahrens 	const char *snapname = arg;
38529adfa60dSMatthew Ahrens 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
3853ae46e4c7SMatthew Ahrens 
38543b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3855ed992b0aSSerapheim Dimitropoulos 	zfs_unmount_snap(fullname);
3856ed992b0aSSerapheim Dimitropoulos 
3857ed992b0aSSerapheim Dimitropoulos 	return (0);
3858fa9e4066Sahrens }
3859fa9e4066Sahrens 
38603cb34c60Sahrens /*
38613cb34c60Sahrens  * inputs:
38623cb34c60Sahrens  * zc_name	old name of dataset
38633cb34c60Sahrens  * zc_value	new name of dataset
38643cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
38653cb34c60Sahrens  *
38663cb34c60Sahrens  * outputs:	none
38673cb34c60Sahrens  */
3868fa9e4066Sahrens static int
3869fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3870fa9e4066Sahrens {
3871049ba636SAndriy Gapon 	objset_t *os;
3872049ba636SAndriy Gapon 	dmu_objset_type_t ost;
38737f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
38743b2aab18SMatthew Ahrens 	char *at;
3875049ba636SAndriy Gapon 	int err;
3876cdf5b4caSmmusante 
3877add927f8Sloli 	/* "zfs rename" from and to ...%recv datasets should both fail */
3878add927f8Sloli 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3879e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3880add927f8Sloli 	if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3881add927f8Sloli 	    dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3882add927f8Sloli 	    strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3883be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3884fa9e4066Sahrens 
3885049ba636SAndriy Gapon 	err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3886049ba636SAndriy Gapon 	if (err != 0)
3887049ba636SAndriy Gapon 		return (err);
3888049ba636SAndriy Gapon 	ost = dmu_objset_type(os);
3889049ba636SAndriy Gapon 	dmu_objset_rele(os, FTAG);
3890049ba636SAndriy Gapon 
38913b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
38923b2aab18SMatthew Ahrens 	if (at != NULL) {
38933b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
3894a0c1127bSSteven Hartland 		int error;
3895a0c1127bSSteven Hartland 
38963b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3897be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
38983b2aab18SMatthew Ahrens 		*at = '\0';
3899049ba636SAndriy Gapon 		if (ost == DMU_OST_ZFS) {
3900a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
39013b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
39023b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
3903a0c1127bSSteven Hartland 			if (error != 0) {
3904a0c1127bSSteven Hartland 				*at = '@';
39053b2aab18SMatthew Ahrens 				return (error);
3906a0c1127bSSteven Hartland 			}
39073b2aab18SMatthew Ahrens 		}
3908a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
3909a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3910a0c1127bSSteven Hartland 		*at = '@';
3911a0c1127bSSteven Hartland 
3912a0c1127bSSteven Hartland 		return (error);
39133b2aab18SMatthew Ahrens 	} else {
3914049ba636SAndriy Gapon 		if (ost == DMU_OST_ZVOL)
39153b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
39163b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3917fa9e4066Sahrens 	}
3918fa9e4066Sahrens }
3919fa9e4066Sahrens 
392092241e0bSTom Erickson static int
392192241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
392292241e0bSTom Erickson {
392392241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
392492241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
392592241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
392692241e0bSTom Erickson 	uint64_t intval;
392792241e0bSTom Erickson 	int err;
392892241e0bSTom Erickson 
392992241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
393092241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
393192241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
393292241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
393392241e0bSTom Erickson 				return (err);
393492241e0bSTom Erickson 			return (0);
393592241e0bSTom Erickson 		}
393692241e0bSTom Erickson 
393792241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
393892241e0bSTom Erickson 			const char *perm = NULL;
393992241e0bSTom Erickson 			const char *uq_prefix =
394092241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
394192241e0bSTom Erickson 			const char *gq_prefix =
394292241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
394392241e0bSTom Erickson 
394492241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
394592241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
394692241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
394792241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
394892241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
394992241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
395092241e0bSTom Erickson 			} else {
395192241e0bSTom Erickson 				/* USERUSED and GROUPUSED are read-only */
3952be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
395392241e0bSTom Erickson 			}
395492241e0bSTom Erickson 
395592241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
395692241e0bSTom Erickson 				return (err);
395792241e0bSTom Erickson 			return (0);
395892241e0bSTom Erickson 		}
395992241e0bSTom Erickson 
3960be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
396192241e0bSTom Erickson 	}
396292241e0bSTom Erickson 
396392241e0bSTom Erickson 	if (issnap)
3964be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
396592241e0bSTom Erickson 
396692241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
396792241e0bSTom Erickson 		/*
396892241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
396992241e0bSTom Erickson 		 * format.
397092241e0bSTom Erickson 		 */
397192241e0bSTom Erickson 		nvlist_t *attrs;
397292241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
397392241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
397492241e0bSTom Erickson 		    &pair) == 0);
397592241e0bSTom Erickson 	}
397692241e0bSTom Erickson 
397792241e0bSTom Erickson 	/*
397892241e0bSTom Erickson 	 * Check that this value is valid for this pool version
397992241e0bSTom Erickson 	 */
398092241e0bSTom Erickson 	switch (prop) {
398192241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
398292241e0bSTom Erickson 		/*
398392241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
398492241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
398592241e0bSTom Erickson 		 * we'll catch them later.
398692241e0bSTom Erickson 		 */
3987b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0) {
398892241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
398992241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
399092241e0bSTom Erickson 			    zfs_earlier_version(dsname,
399192241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
3992be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
399392241e0bSTom Erickson 			}
399492241e0bSTom Erickson 
399592241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
399692241e0bSTom Erickson 			    zfs_earlier_version(dsname,
399792241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
3998be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
399992241e0bSTom Erickson 
4000a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
4001a6f561b4SSašo Kiselkov 				spa_t *spa;
4002a6f561b4SSašo Kiselkov 
4003a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4004a6f561b4SSašo Kiselkov 					return (err);
4005a6f561b4SSašo Kiselkov 
40062acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
40072acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
4008a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
4009be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
4010a6f561b4SSašo Kiselkov 				}
4011a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
4012a6f561b4SSašo Kiselkov 			}
4013a6f561b4SSašo Kiselkov 
401492241e0bSTom Erickson 			/*
401592241e0bSTom Erickson 			 * If this is a bootable dataset then
401692241e0bSTom Erickson 			 * verify that the compression algorithm
401792241e0bSTom Erickson 			 * is supported for booting. We must return
401892241e0bSTom Erickson 			 * something other than ENOTSUP since it
401992241e0bSTom Erickson 			 * implies a downrev pool version.
402092241e0bSTom Erickson 			 */
402192241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
402292241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
4023be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
402492241e0bSTom Erickson 			}
402592241e0bSTom Erickson 		}
402692241e0bSTom Erickson 		break;
402792241e0bSTom Erickson 
402892241e0bSTom Erickson 	case ZFS_PROP_COPIES:
402992241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4030be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
403192241e0bSTom Erickson 		break;
403292241e0bSTom Erickson 
4033b5152584SMatthew Ahrens 	case ZFS_PROP_RECORDSIZE:
4034b5152584SMatthew Ahrens 		/* Record sizes above 128k need the feature to be enabled */
4035b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0 &&
4036b5152584SMatthew Ahrens 		    intval > SPA_OLD_MAXBLOCKSIZE) {
4037b5152584SMatthew Ahrens 			spa_t *spa;
4038b5152584SMatthew Ahrens 
4039b5152584SMatthew Ahrens 			/*
4040b5152584SMatthew Ahrens 			 * We don't allow setting the property above 1MB,
4041b5152584SMatthew Ahrens 			 * unless the tunable has been changed.
4042b5152584SMatthew Ahrens 			 */
4043b5152584SMatthew Ahrens 			if (intval > zfs_max_recordsize ||
4044b5152584SMatthew Ahrens 			    intval > SPA_MAXBLOCKSIZE)
40456de9bb56SMatthew Ahrens 				return (SET_ERROR(ERANGE));
4046b5152584SMatthew Ahrens 
4047b5152584SMatthew Ahrens 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4048b5152584SMatthew Ahrens 				return (err);
4049b5152584SMatthew Ahrens 
4050b5152584SMatthew Ahrens 			if (!spa_feature_is_enabled(spa,
4051b5152584SMatthew Ahrens 			    SPA_FEATURE_LARGE_BLOCKS)) {
4052b5152584SMatthew Ahrens 				spa_close(spa, FTAG);
4053b5152584SMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
4054b5152584SMatthew Ahrens 			}
4055b5152584SMatthew Ahrens 			spa_close(spa, FTAG);
4056b5152584SMatthew Ahrens 		}
4057b5152584SMatthew Ahrens 		break;
4058b5152584SMatthew Ahrens 
405954811da5SToomas Soome 	case ZFS_PROP_DNODESIZE:
406054811da5SToomas Soome 		/* Dnode sizes above 512 need the feature to be enabled */
406154811da5SToomas Soome 		if (nvpair_value_uint64(pair, &intval) == 0 &&
406254811da5SToomas Soome 		    intval != ZFS_DNSIZE_LEGACY) {
406354811da5SToomas Soome 			spa_t *spa;
406454811da5SToomas Soome 
406554811da5SToomas Soome 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
406654811da5SToomas Soome 				return (err);
406754811da5SToomas Soome 
406854811da5SToomas Soome 			if (!spa_feature_is_enabled(spa,
406954811da5SToomas Soome 			    SPA_FEATURE_LARGE_DNODE)) {
407054811da5SToomas Soome 				spa_close(spa, FTAG);
407154811da5SToomas Soome 				return (SET_ERROR(ENOTSUP));
407254811da5SToomas Soome 			}
407354811da5SToomas Soome 			spa_close(spa, FTAG);
407454811da5SToomas Soome 		}
407554811da5SToomas Soome 		break;
407654811da5SToomas Soome 
407792241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
407892241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4079be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
408092241e0bSTom Erickson 		break;
408192241e0bSTom Erickson 
408292241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
408392241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
408492241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
408592241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
408692241e0bSTom Erickson 			    zfs_earlier_version(dsname,
408792241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
4088be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
408992241e0bSTom Erickson 		}
409092241e0bSTom Erickson 		break;
409145818ee1SMatthew Ahrens 
409245818ee1SMatthew Ahrens 	case ZFS_PROP_CHECKSUM:
409345818ee1SMatthew Ahrens 	case ZFS_PROP_DEDUP:
409445818ee1SMatthew Ahrens 	{
409545818ee1SMatthew Ahrens 		spa_feature_t feature;
409645818ee1SMatthew Ahrens 		spa_t *spa;
409745818ee1SMatthew Ahrens 
409845818ee1SMatthew Ahrens 		/* dedup feature version checks */
409945818ee1SMatthew Ahrens 		if (prop == ZFS_PROP_DEDUP &&
410045818ee1SMatthew Ahrens 		    zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
410145818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
410245818ee1SMatthew Ahrens 
410345818ee1SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) != 0)
410445818ee1SMatthew Ahrens 			return (SET_ERROR(EINVAL));
410545818ee1SMatthew Ahrens 
410645818ee1SMatthew Ahrens 		/* check prop value is enabled in features */
4107971640e6Silovezfs 		feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
410845818ee1SMatthew Ahrens 		if (feature == SPA_FEATURE_NONE)
410945818ee1SMatthew Ahrens 			break;
411045818ee1SMatthew Ahrens 
411145818ee1SMatthew Ahrens 		if ((err = spa_open(dsname, &spa, FTAG)) != 0)
411245818ee1SMatthew Ahrens 			return (err);
41130dd498c0SToomas Soome 
411445818ee1SMatthew Ahrens 		if (!spa_feature_is_enabled(spa, feature)) {
411545818ee1SMatthew Ahrens 			spa_close(spa, FTAG);
411645818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
411745818ee1SMatthew Ahrens 		}
411845818ee1SMatthew Ahrens 		spa_close(spa, FTAG);
411945818ee1SMatthew Ahrens 		break;
412045818ee1SMatthew Ahrens 	}
412192241e0bSTom Erickson 	}
412292241e0bSTom Erickson 
412392241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
412492241e0bSTom Erickson }
412592241e0bSTom Erickson 
4126a6f561b4SSašo Kiselkov /*
4127a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
4128a6f561b4SSašo Kiselkov  * multiple times.
4129a6f561b4SSašo Kiselkov  */
4130a6f561b4SSašo Kiselkov static int
41313b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4132a6f561b4SSašo Kiselkov {
41333b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
41342acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
4135a6f561b4SSašo Kiselkov 
41362acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
4137a6f561b4SSašo Kiselkov 		return (0);
4138a6f561b4SSašo Kiselkov 	else
4139be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
4140a6f561b4SSašo Kiselkov }
4141a6f561b4SSašo Kiselkov 
4142a6f561b4SSašo Kiselkov /*
4143a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
4144a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
4145a6f561b4SSašo Kiselkov  */
4146a6f561b4SSašo Kiselkov static void
41473b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4148a6f561b4SSašo Kiselkov {
41493b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
41502acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
4151a6f561b4SSašo Kiselkov 
41522acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
4153a6f561b4SSašo Kiselkov }
4154a6f561b4SSašo Kiselkov 
41553b2aab18SMatthew Ahrens /*
41563b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
41573b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
41583b2aab18SMatthew Ahrens  * as being active.
41593b2aab18SMatthew Ahrens  */
41603b2aab18SMatthew Ahrens static int
41612acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
41623b2aab18SMatthew Ahrens {
41633b2aab18SMatthew Ahrens 	int err;
41643b2aab18SMatthew Ahrens 
41653b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
41663b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
41673b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
41687d46dc6cSMatthew Ahrens 	    &feature, 2, ZFS_SPACE_CHECK_RESERVED);
41693b2aab18SMatthew Ahrens 
41703b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
41713b2aab18SMatthew Ahrens 		return (err);
41723b2aab18SMatthew Ahrens 	else
41733b2aab18SMatthew Ahrens 		return (0);
41743b2aab18SMatthew Ahrens }
41753b2aab18SMatthew Ahrens 
417692241e0bSTom Erickson /*
417792241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
417892241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
417992241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
418092241e0bSTom Erickson  *
418192241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
418292241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
418392241e0bSTom Erickson  * of all the properties that failed a permission check along with the
418492241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
418592241e0bSTom Erickson  * returned errlist.
418692241e0bSTom Erickson  *
418792241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
418892241e0bSTom Erickson  * pointed at by errlist is NULL.
418992241e0bSTom Erickson  */
419092241e0bSTom Erickson static int
419192241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4192745cd3c5Smaybee {
4193745cd3c5Smaybee 	zfs_cmd_t *zc;
419492241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
419592241e0bSTom Erickson 	nvlist_t *errors;
419692241e0bSTom Erickson 	int err, rv = 0;
4197745cd3c5Smaybee 
4198745cd3c5Smaybee 	if (props == NULL)
419992241e0bSTom Erickson 		return (0);
420092241e0bSTom Erickson 
420192241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
420292241e0bSTom Erickson 
4203745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4204745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
420592241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
420692241e0bSTom Erickson 	while (pair != NULL) {
420792241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
420892241e0bSTom Erickson 
420992241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
421092241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
42114445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
421292241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
421392241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
421492241e0bSTom Erickson 			    zc->zc_value, err) == 0);
421592241e0bSTom Erickson 		}
421692241e0bSTom Erickson 		pair = next_pair;
4217745cd3c5Smaybee 	}
4218745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
421992241e0bSTom Erickson 
422092241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
422192241e0bSTom Erickson 		nvlist_free(errors);
422292241e0bSTom Erickson 		errors = NULL;
422392241e0bSTom Erickson 	} else {
422492241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
422592241e0bSTom Erickson 	}
422692241e0bSTom Erickson 
422792241e0bSTom Erickson 	if (errlist == NULL)
422892241e0bSTom Erickson 		nvlist_free(errors);
422992241e0bSTom Erickson 	else
423092241e0bSTom Erickson 		*errlist = errors;
423192241e0bSTom Erickson 
423292241e0bSTom Erickson 	return (rv);
423392241e0bSTom Erickson }
423492241e0bSTom Erickson 
423592241e0bSTom Erickson static boolean_t
423692241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
423792241e0bSTom Erickson {
423892241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
423992241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
424092241e0bSTom Erickson 		nvlist_t *attrs;
424192241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
424292241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
424392241e0bSTom Erickson 		    &p1) == 0);
424492241e0bSTom Erickson 	}
424592241e0bSTom Erickson 
424692241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
424792241e0bSTom Erickson 		nvlist_t *attrs;
424892241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
424992241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
425092241e0bSTom Erickson 		    &p2) == 0);
425192241e0bSTom Erickson 	}
425292241e0bSTom Erickson 
425392241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
425492241e0bSTom Erickson 		return (B_FALSE);
425592241e0bSTom Erickson 
425692241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
425792241e0bSTom Erickson 		char *valstr1, *valstr2;
425892241e0bSTom Erickson 
425992241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
426092241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
426192241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
426292241e0bSTom Erickson 	} else {
426392241e0bSTom Erickson 		uint64_t intval1, intval2;
426492241e0bSTom Erickson 
426592241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
426692241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
426792241e0bSTom Erickson 		return (intval1 == intval2);
426892241e0bSTom Erickson 	}
4269745cd3c5Smaybee }
4270745cd3c5Smaybee 
427192241e0bSTom Erickson /*
427292241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
427392241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
427492241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
427592241e0bSTom Erickson  */
427692241e0bSTom Erickson static void
427792241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
427892241e0bSTom Erickson {
427992241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
428092241e0bSTom Erickson 
428192241e0bSTom Erickson 	if (origprops == NULL)
428292241e0bSTom Erickson 		return; /* all props need to be received */
428392241e0bSTom Erickson 
428492241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
428592241e0bSTom Erickson 	while (pair != NULL) {
428692241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
428792241e0bSTom Erickson 		nvpair_t *match;
428892241e0bSTom Erickson 
428992241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
429092241e0bSTom Erickson 
429192241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
429292241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
429392241e0bSTom Erickson 			goto next; /* need to set received value */
429492241e0bSTom Erickson 
429592241e0bSTom Erickson 		/* don't clear the existing received value */
429692241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
429792241e0bSTom Erickson 		/* don't bother receiving the property */
429892241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
429992241e0bSTom Erickson next:
430092241e0bSTom Erickson 		pair = next_pair;
430192241e0bSTom Erickson 	}
430292241e0bSTom Erickson }
430392241e0bSTom Erickson 
43045878fad7SDan McDonald /*
43055878fad7SDan McDonald  * Extract properties that cannot be set PRIOR to the receipt of a dataset.
43065878fad7SDan McDonald  * For example, refquota cannot be set until after the receipt of a dataset,
43075878fad7SDan McDonald  * because in replication streams, an older/earlier snapshot may exceed the
43085878fad7SDan McDonald  * refquota.  We want to receive the older/earlier snapshot, but setting
43095878fad7SDan McDonald  * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
43105878fad7SDan McDonald  * the older/earlier snapshot from being received (with EDQUOT).
43115878fad7SDan McDonald  *
43125878fad7SDan McDonald  * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
43135878fad7SDan McDonald  *
43145878fad7SDan McDonald  * libzfs will need to be judicious handling errors encountered by props
43155878fad7SDan McDonald  * extracted by this function.
43165878fad7SDan McDonald  */
43175878fad7SDan McDonald static nvlist_t *
43185878fad7SDan McDonald extract_delay_props(nvlist_t *props)
43195878fad7SDan McDonald {
43205878fad7SDan McDonald 	nvlist_t *delayprops;
43215878fad7SDan McDonald 	nvpair_t *nvp, *tmp;
43225878fad7SDan McDonald 	static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
43235878fad7SDan McDonald 	int i;
43245878fad7SDan McDonald 
43255878fad7SDan McDonald 	VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
43265878fad7SDan McDonald 
43275878fad7SDan McDonald 	for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
43285878fad7SDan McDonald 	    nvp = nvlist_next_nvpair(props, nvp)) {
43295878fad7SDan McDonald 		/*
43305878fad7SDan McDonald 		 * strcmp() is safe because zfs_prop_to_name() always returns
43315878fad7SDan McDonald 		 * a bounded string.
43325878fad7SDan McDonald 		 */
43335878fad7SDan McDonald 		for (i = 0; delayable[i] != 0; i++) {
43345878fad7SDan McDonald 			if (strcmp(zfs_prop_to_name(delayable[i]),
43355878fad7SDan McDonald 			    nvpair_name(nvp)) == 0) {
43365878fad7SDan McDonald 				break;
43375878fad7SDan McDonald 			}
43385878fad7SDan McDonald 		}
43395878fad7SDan McDonald 		if (delayable[i] != 0) {
43405878fad7SDan McDonald 			tmp = nvlist_prev_nvpair(props, nvp);
43415878fad7SDan McDonald 			VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
43425878fad7SDan McDonald 			VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
43435878fad7SDan McDonald 			nvp = tmp;
43445878fad7SDan McDonald 		}
43455878fad7SDan McDonald 	}
43465878fad7SDan McDonald 
43475878fad7SDan McDonald 	if (nvlist_empty(delayprops)) {
43485878fad7SDan McDonald 		nvlist_free(delayprops);
43495878fad7SDan McDonald 		delayprops = NULL;
43505878fad7SDan McDonald 	}
43515878fad7SDan McDonald 	return (delayprops);
43525878fad7SDan McDonald }
43535878fad7SDan McDonald 
435492241e0bSTom Erickson #ifdef	DEBUG
435592241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
435692241e0bSTom Erickson #endif
435792241e0bSTom Erickson 
43583cb34c60Sahrens /*
43593cb34c60Sahrens  * inputs:
43603cb34c60Sahrens  * zc_name		name of containing filesystem
43613cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
43623cb34c60Sahrens  * zc_value		name of snapshot to create
43633cb34c60Sahrens  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
43643cb34c60Sahrens  * zc_cookie		file descriptor to recv from
43653cb34c60Sahrens  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
43663cb34c60Sahrens  * zc_guid		force flag
4367c99e4bdcSChris Kirby  * zc_cleanup_fd	cleanup-on-exit file descriptor
4368c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
43699c3fd121SMatthew Ahrens  * zc_resumable		if data is incomplete assume sender will resume
43703cb34c60Sahrens  *
43713cb34c60Sahrens  * outputs:
43723cb34c60Sahrens  * zc_cookie		number of bytes read
437392241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
437492241e0bSTom Erickson  * zc_obj		zprop_errflags_t
4375c99e4bdcSChris Kirby  * zc_action_handle	handle for this guid/ds mapping
43763cb34c60Sahrens  */
4377fa9e4066Sahrens static int
43783cb34c60Sahrens zfs_ioc_recv(zfs_cmd_t *zc)
4379fa9e4066Sahrens {
4380fa9e4066Sahrens 	file_t *fp;
43813cb34c60Sahrens 	dmu_recv_cookie_t drc;
4382f18faf3fSek 	boolean_t force = (boolean_t)zc->zc_guid;
438392241e0bSTom Erickson 	int fd;
438492241e0bSTom Erickson 	int error = 0;
438592241e0bSTom Erickson 	int props_error = 0;
438692241e0bSTom Erickson 	nvlist_t *errors;
43873cb34c60Sahrens 	offset_t off;
438892241e0bSTom Erickson 	nvlist_t *props = NULL; /* sent properties */
438992241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
43905878fad7SDan McDonald 	nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
43913b2aab18SMatthew Ahrens 	char *origin = NULL;
43923cb34c60Sahrens 	char *tosnap;
43939adfa60dSMatthew Ahrens 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
439492241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
4395fa9e4066Sahrens 
43963ccfa83cSahrens 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4397f18faf3fSek 	    strchr(zc->zc_value, '@') == NULL ||
4398f18faf3fSek 	    strchr(zc->zc_value, '%'))
4399be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
44003ccfa83cSahrens 
44013cb34c60Sahrens 	(void) strcpy(tofs, zc->zc_value);
44023cb34c60Sahrens 	tosnap = strchr(tofs, '@');
440392241e0bSTom Erickson 	*tosnap++ = '\0';
44043cb34c60Sahrens 
44053cb34c60Sahrens 	if (zc->zc_nvlist_src != NULL &&
44063cb34c60Sahrens 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4407478ed9adSEric Taylor 	    zc->zc_iflags, &props)) != 0)
44083cb34c60Sahrens 		return (error);
44093cb34c60Sahrens 
4410fa9e4066Sahrens 	fd = zc->zc_cookie;
4411fa9e4066Sahrens 	fp = getf(fd);
44123cb34c60Sahrens 	if (fp == NULL) {
44133cb34c60Sahrens 		nvlist_free(props);
4414be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
44153cb34c60Sahrens 	}
4416f18faf3fSek 
44179c3fd121SMatthew Ahrens 	errors = fnvlist_alloc();
441892241e0bSTom Erickson 
44193b2aab18SMatthew Ahrens 	if (zc->zc_string[0])
44203b2aab18SMatthew Ahrens 		origin = zc->zc_string;
44213b2aab18SMatthew Ahrens 
44223b2aab18SMatthew Ahrens 	error = dmu_recv_begin(tofs, tosnap,
44239c3fd121SMatthew Ahrens 	    &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
44243b2aab18SMatthew Ahrens 	if (error != 0)
44253b2aab18SMatthew Ahrens 		goto out;
44263b2aab18SMatthew Ahrens 
44273b2aab18SMatthew Ahrens 	/*
44283b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
44293b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
44303b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
44313b2aab18SMatthew Ahrens 	 */
44323b2aab18SMatthew Ahrens 	if (props != NULL && !drc.drc_newfs) {
44333b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
44343b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
44353b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
443692241e0bSTom Erickson 			first_recvd_props = B_TRUE;
443792241e0bSTom Erickson 
4438745cd3c5Smaybee 		/*
443992241e0bSTom Erickson 		 * If new received properties are supplied, they are to
444092241e0bSTom Erickson 		 * completely replace the existing received properties, so stash
444192241e0bSTom Erickson 		 * away the existing ones.
4442745cd3c5Smaybee 		 */
44433b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(tofs, &origprops) == 0) {
444492241e0bSTom Erickson 			nvlist_t *errlist = NULL;
444592241e0bSTom Erickson 			/*
444692241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
444792241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
444892241e0bSTom Erickson 			 *
444992241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
445092241e0bSTom Erickson 			 * special case where we blow away all local properties
445192241e0bSTom Erickson 			 * regardless.
445292241e0bSTom Erickson 			 */
445392241e0bSTom Erickson 			if (!first_recvd_props)
445492241e0bSTom Erickson 				props_reduce(props, origprops);
44553b2aab18SMatthew Ahrens 			if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
445692241e0bSTom Erickson 				(void) nvlist_merge(errors, errlist, 0);
445792241e0bSTom Erickson 			nvlist_free(errlist);
44583cb34c60Sahrens 
44593b2aab18SMatthew Ahrens 			if (clear_received_props(tofs, origprops,
44603b2aab18SMatthew Ahrens 			    first_recvd_props ? NULL : props) != 0)
446192241e0bSTom Erickson 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
44623b2aab18SMatthew Ahrens 		} else {
446392241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
446492241e0bSTom Erickson 		}
44653b2aab18SMatthew Ahrens 	}
446692241e0bSTom Erickson 
44673b2aab18SMatthew Ahrens 	if (props != NULL) {
44683b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
44693b2aab18SMatthew Ahrens 
44703b2aab18SMatthew Ahrens 		if (props_error == 0) {
44715878fad7SDan McDonald 			delayprops = extract_delay_props(props);
44723b2aab18SMatthew Ahrens 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
44733b2aab18SMatthew Ahrens 			    props, errors);
44743b2aab18SMatthew Ahrens 		}
447592241e0bSTom Erickson 	}
447692241e0bSTom Erickson 
44773cb34c60Sahrens 	off = fp->f_offset;
4478c99e4bdcSChris Kirby 	error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4479c99e4bdcSChris Kirby 	    &zc->zc_action_handle);
4480a2eea2e1Sahrens 
4481f4b94bdeSMatthew Ahrens 	if (error == 0) {
4482f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4483745cd3c5Smaybee 
4484f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4485f4b94bdeSMatthew Ahrens 			/* online recv */
4486690041b9SAndriy Gapon 			dsl_dataset_t *ds;
4487f4b94bdeSMatthew Ahrens 			int end_err;
4488745cd3c5Smaybee 
4489690041b9SAndriy Gapon 			ds = dmu_objset_ds(zfsvfs->z_os);
4490503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4491f4b94bdeSMatthew Ahrens 			/*
4492f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4493f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4494f4b94bdeSMatthew Ahrens 			 */
449591948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
44965c703fceSGeorge Wilson 			if (error == 0)
4497690041b9SAndriy Gapon 				error = zfs_resume_fs(zfsvfs, ds);
4498f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4499f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4500745cd3c5Smaybee 		} else {
450191948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
45023cb34c60Sahrens 		}
45035878fad7SDan McDonald 
45045878fad7SDan McDonald 		/* Set delayed properties now, after we're done receiving. */
45055878fad7SDan McDonald 		if (delayprops != NULL && error == 0) {
45065878fad7SDan McDonald 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
45075878fad7SDan McDonald 			    delayprops, errors);
45085878fad7SDan McDonald 		}
45095878fad7SDan McDonald 	}
45105878fad7SDan McDonald 
45115878fad7SDan McDonald 	if (delayprops != NULL) {
45125878fad7SDan McDonald 		/*
45135878fad7SDan McDonald 		 * Merge delayed props back in with initial props, in case
45145878fad7SDan McDonald 		 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
45155878fad7SDan McDonald 		 * we have to make sure clear_received_props() includes
45165878fad7SDan McDonald 		 * the delayed properties).
45175878fad7SDan McDonald 		 *
45185878fad7SDan McDonald 		 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
45195878fad7SDan McDonald 		 * using ASSERT() will be just like a VERIFY.
45205878fad7SDan McDonald 		 */
45215878fad7SDan McDonald 		ASSERT(nvlist_merge(props, delayprops, 0) == 0);
45225878fad7SDan McDonald 		nvlist_free(delayprops);
45235878fad7SDan McDonald 	}
45245878fad7SDan McDonald 
45255878fad7SDan McDonald 	/*
45265878fad7SDan McDonald 	 * Now that all props, initial and delayed, are set, report the prop
45275878fad7SDan McDonald 	 * errors to the caller.
45285878fad7SDan McDonald 	 */
45295878fad7SDan McDonald 	if (zc->zc_nvlist_dst_size != 0 &&
45305878fad7SDan McDonald 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
45315878fad7SDan McDonald 	    put_nvlist(zc, errors) != 0)) {
45325878fad7SDan McDonald 		/*
45335878fad7SDan McDonald 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
45345878fad7SDan McDonald 		 * size or supplied an invalid address.
45355878fad7SDan McDonald 		 */
45365878fad7SDan McDonald 		props_error = SET_ERROR(EINVAL);
453747f263f4Sek 	}
45383cb34c60Sahrens 
45393cb34c60Sahrens 	zc->zc_cookie = off - fp->f_offset;
45403cb34c60Sahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
45413cb34c60Sahrens 		fp->f_offset = off;
4542a2eea2e1Sahrens 
454392241e0bSTom Erickson #ifdef	DEBUG
454492241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
454592241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
454692241e0bSTom Erickson 		error = 1;
454792241e0bSTom Erickson 	}
454892241e0bSTom Erickson #endif
4549745cd3c5Smaybee 	/*
4550745cd3c5Smaybee 	 * On error, restore the original props.
4551745cd3c5Smaybee 	 */
45523b2aab18SMatthew Ahrens 	if (error != 0 && props != NULL && !drc.drc_newfs) {
45533b2aab18SMatthew Ahrens 		if (clear_received_props(tofs, props, NULL) != 0) {
45543b2aab18SMatthew Ahrens 			/*
45553b2aab18SMatthew Ahrens 			 * We failed to clear the received properties.
45563b2aab18SMatthew Ahrens 			 * Since we may have left a $recvd value on the
45573b2aab18SMatthew Ahrens 			 * system, we can't clear the $hasrecvd flag.
45583b2aab18SMatthew Ahrens 			 */
455992241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
45603b2aab18SMatthew Ahrens 		} else if (first_recvd_props) {
45613b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
456292241e0bSTom Erickson 		}
456392241e0bSTom Erickson 
456492241e0bSTom Erickson 		if (origprops == NULL && !drc.drc_newfs) {
456592241e0bSTom Erickson 			/* We failed to stash the original properties. */
456692241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
456792241e0bSTom Erickson 		}
456892241e0bSTom Erickson 
456992241e0bSTom Erickson 		/*
457092241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
457192241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
457292241e0bSTom Erickson 		 * explictly if we're restoring local properties cleared in the
457392241e0bSTom Erickson 		 * first new-style receive.
457492241e0bSTom Erickson 		 */
457592241e0bSTom Erickson 		if (origprops != NULL &&
457692241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
457792241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
457892241e0bSTom Erickson 		    origprops, NULL) != 0) {
457992241e0bSTom Erickson 			/*
458092241e0bSTom Erickson 			 * We stashed the original properties but failed to
458192241e0bSTom Erickson 			 * restore them.
458292241e0bSTom Erickson 			 */
458392241e0bSTom Erickson 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
458492241e0bSTom Erickson 		}
4585745cd3c5Smaybee 	}
4586745cd3c5Smaybee out:
4587745cd3c5Smaybee 	nvlist_free(props);
4588745cd3c5Smaybee 	nvlist_free(origprops);
458992241e0bSTom Erickson 	nvlist_free(errors);
4590fa9e4066Sahrens 	releasef(fd);
459192241e0bSTom Erickson 
459292241e0bSTom Erickson 	if (error == 0)
459392241e0bSTom Erickson 		error = props_error;
459492241e0bSTom Erickson 
4595fa9e4066Sahrens 	return (error);
4596fa9e4066Sahrens }
4597fa9e4066Sahrens 
45983cb34c60Sahrens /*
45993cb34c60Sahrens  * inputs:
46003cb34c60Sahrens  * zc_name	name of snapshot to send
46013cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4602a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4603a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4604a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
460519b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
460619b94df9SMatthew Ahrens  *		output size in zc_objset_type.
4607b5152584SMatthew Ahrens  * zc_flags	lzc_send_flags
46083cb34c60Sahrens  *
460978f17100SMatthew Ahrens  * outputs:
461078f17100SMatthew Ahrens  * zc_objset_type	estimated size, if zc_guid is set
46113cb34c60Sahrens  */
4612fa9e4066Sahrens static int
46133cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4614fa9e4066Sahrens {
4615fa9e4066Sahrens 	int error;
46163cb34c60Sahrens 	offset_t off;
461719b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
46185d7b4d43SMatthew Ahrens 	boolean_t embedok = (zc->zc_flags & 0x1);
4619b5152584SMatthew Ahrens 	boolean_t large_block_ok = (zc->zc_flags & 0x2);
46205602294fSDan Kimmel 	boolean_t compressok = (zc->zc_flags & 0x4);
4621fa9e4066Sahrens 
46223b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
46233b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
46243b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
4625a7f53a56SChris Kirby 
46263b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
46273b2aab18SMatthew Ahrens 		if (error != 0)
4628a7f53a56SChris Kirby 			return (error);
46293b2aab18SMatthew Ahrens 
46303b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
46313b2aab18SMatthew Ahrens 		if (error != 0) {
46323b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4633fa9e4066Sahrens 			return (error);
4634fa9e4066Sahrens 		}
46353b2aab18SMatthew Ahrens 
46363b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
4637c1379625SJustin T. Gibbs 			zc->zc_fromobj =
4638c1379625SJustin T. Gibbs 			    dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
46393b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
46403b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
46414445fffbSMatthew Ahrens 	}
46424445fffbSMatthew Ahrens 
46433b2aab18SMatthew Ahrens 	if (estimate) {
46443b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
46453b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
46463b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
46474445fffbSMatthew Ahrens 
46483b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
46493b2aab18SMatthew Ahrens 		if (error != 0)
46503b2aab18SMatthew Ahrens 			return (error);
46513b2aab18SMatthew Ahrens 
46523b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
46533b2aab18SMatthew Ahrens 		if (error != 0) {
46543b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
46553b2aab18SMatthew Ahrens 			return (error);
46564445fffbSMatthew Ahrens 		}
46574445fffbSMatthew Ahrens 
46583b2aab18SMatthew Ahrens 		if (zc->zc_fromobj != 0) {
46593b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
46603b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
46613b2aab18SMatthew Ahrens 			if (error != 0) {
46623b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
46633b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
46644445fffbSMatthew Ahrens 				return (error);
46654445fffbSMatthew Ahrens 			}
46664445fffbSMatthew Ahrens 		}
4667fa9e4066Sahrens 
46685602294fSDan Kimmel 		error = dmu_send_estimate(tosnap, fromsnap, compressok,
466919b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
46703b2aab18SMatthew Ahrens 
46713b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
46723b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
46733b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
46743b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
467519b94df9SMatthew Ahrens 	} else {
467619b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
46773b2aab18SMatthew Ahrens 		if (fp == NULL)
4678be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4679fa9e4066Sahrens 
468019b94df9SMatthew Ahrens 		off = fp->f_offset;
46813b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
46825602294fSDan Kimmel 		    zc->zc_fromobj, embedok, large_block_ok, compressok,
4683b5152584SMatthew Ahrens 		    zc->zc_cookie, fp->f_vnode, &off);
4684fa9e4066Sahrens 
468519b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
468619b94df9SMatthew Ahrens 			fp->f_offset = off;
468719b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
468819b94df9SMatthew Ahrens 	}
4689fa9e4066Sahrens 	return (error);
4690fa9e4066Sahrens }
4691fa9e4066Sahrens 
46924e3c9f44SBill Pijewski /*
46934e3c9f44SBill Pijewski  * inputs:
46944e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
46954e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
46964e3c9f44SBill Pijewski  *
46974e3c9f44SBill Pijewski  * outputs:
46984e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
46994e3c9f44SBill Pijewski  */
47004e3c9f44SBill Pijewski static int
47014e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
47024e3c9f44SBill Pijewski {
47033b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
47044e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
47054e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
47064e3c9f44SBill Pijewski 	int error;
47074e3c9f44SBill Pijewski 
47083b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
47093b2aab18SMatthew Ahrens 	if (error != 0)
47103b2aab18SMatthew Ahrens 		return (error);
47113b2aab18SMatthew Ahrens 
47123b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
47133b2aab18SMatthew Ahrens 	if (error != 0) {
47143b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
47154e3c9f44SBill Pijewski 		return (error);
47163b2aab18SMatthew Ahrens 	}
47174e3c9f44SBill Pijewski 
47184e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
47194e3c9f44SBill Pijewski 
47204e3c9f44SBill Pijewski 	/*
47214e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
47224e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
47234e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
47244e3c9f44SBill Pijewski 	 * that stream.
47254e3c9f44SBill Pijewski 	 */
47264e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
47274e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
47284e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
47294e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
47304e3c9f44SBill Pijewski 			break;
47314e3c9f44SBill Pijewski 	}
47324e3c9f44SBill Pijewski 
47334e3c9f44SBill Pijewski 	if (dsp != NULL)
47344e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
47354e3c9f44SBill Pijewski 	else
4736be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
47374e3c9f44SBill Pijewski 
47384e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
47394e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
47403b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
47414e3c9f44SBill Pijewski 	return (error);
47424e3c9f44SBill Pijewski }
47434e3c9f44SBill Pijewski 
4744ea8dc4b6Seschrock static int
4745ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
4746ea8dc4b6Seschrock {
4747ea8dc4b6Seschrock 	int id, error;
4748ea8dc4b6Seschrock 
4749ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4750ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4751ea8dc4b6Seschrock 
4752ea8dc4b6Seschrock 	if (error == 0)
4753ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
4754ea8dc4b6Seschrock 
4755ea8dc4b6Seschrock 	return (error);
4756ea8dc4b6Seschrock }
4757ea8dc4b6Seschrock 
4758ea8dc4b6Seschrock static int
4759ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
4760ea8dc4b6Seschrock {
4761ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
4762ea8dc4b6Seschrock }
4763ea8dc4b6Seschrock 
4764ea8dc4b6Seschrock static int
4765ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4766ea8dc4b6Seschrock {
4767ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
4768ea8dc4b6Seschrock 	int error;
4769ea8dc4b6Seschrock 
4770ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4771ea8dc4b6Seschrock 	    &zc->zc_inject_record);
4772ea8dc4b6Seschrock 
4773ea8dc4b6Seschrock 	zc->zc_guid = id;
4774ea8dc4b6Seschrock 
4775ea8dc4b6Seschrock 	return (error);
4776ea8dc4b6Seschrock }
4777ea8dc4b6Seschrock 
4778ea8dc4b6Seschrock static int
4779ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
4780ea8dc4b6Seschrock {
4781ea8dc4b6Seschrock 	spa_t *spa;
4782ea8dc4b6Seschrock 	int error;
4783e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
4784ea8dc4b6Seschrock 
4785ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4786ea8dc4b6Seschrock 		return (error);
4787ea8dc4b6Seschrock 
4788e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4789ea8dc4b6Seschrock 	    &count);
4790ea8dc4b6Seschrock 	if (error == 0)
4791e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
4792ea8dc4b6Seschrock 	else
4793e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4794ea8dc4b6Seschrock 
4795ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4796ea8dc4b6Seschrock 
4797ea8dc4b6Seschrock 	return (error);
4798ea8dc4b6Seschrock }
4799ea8dc4b6Seschrock 
4800ea8dc4b6Seschrock static int
4801ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
4802ea8dc4b6Seschrock {
4803ea8dc4b6Seschrock 	spa_t *spa;
4804ea8dc4b6Seschrock 	vdev_t *vd;
4805bb8b5132Sek 	int error;
4806ea8dc4b6Seschrock 
4807b87f3af3Sperrin 	/*
4808b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
4809b87f3af3Sperrin 	 */
4810b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
4811b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
4812b87f3af3Sperrin 	if (spa == NULL) {
4813b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
4814be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
4815b87f3af3Sperrin 	}
4816b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4817b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
4818b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
4819b87f3af3Sperrin 	}
4820468c413aSTim Haley 	spa->spa_last_open_failed = 0;
4821b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
4822b87f3af3Sperrin 
4823c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4824468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
4825468c413aSTim Haley 	} else {
4826468c413aSTim Haley 		nvlist_t *policy;
4827468c413aSTim Haley 		nvlist_t *config = NULL;
4828468c413aSTim Haley 
4829468c413aSTim Haley 		if (zc->zc_nvlist_src == NULL)
4830be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4831468c413aSTim Haley 
4832468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
4833468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4834468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4835468c413aSTim Haley 			    policy, &config);
4836468c413aSTim Haley 			if (config != NULL) {
48374b964adaSGeorge Wilson 				int err;
48384b964adaSGeorge Wilson 
48394b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
48404b964adaSGeorge Wilson 					error = err;
4841468c413aSTim Haley 				nvlist_free(config);
4842468c413aSTim Haley 			}
4843468c413aSTim Haley 			nvlist_free(policy);
4844468c413aSTim Haley 		}
4845468c413aSTim Haley 	}
4846468c413aSTim Haley 
48473b2aab18SMatthew Ahrens 	if (error != 0)
4848ea8dc4b6Seschrock 		return (error);
4849ea8dc4b6Seschrock 
48508f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4851ea8dc4b6Seschrock 
4852e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
4853ea8dc4b6Seschrock 		vd = NULL;
4854c5904d13Seschrock 	} else {
4855c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4856fa94a07fSbrendan 		if (vd == NULL) {
4857e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
4858fa94a07fSbrendan 			spa_close(spa, FTAG);
4859be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
4860fa94a07fSbrendan 		}
4861ea8dc4b6Seschrock 	}
4862ea8dc4b6Seschrock 
4863e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
4864e14bb325SJeff Bonwick 
4865e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
4866ea8dc4b6Seschrock 
4867e14bb325SJeff Bonwick 	/*
4868e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
4869e14bb325SJeff Bonwick 	 */
487054d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
4871be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
4872ea8dc4b6Seschrock 
4873ea8dc4b6Seschrock 	spa_close(spa, FTAG);
4874ea8dc4b6Seschrock 
487554d692b7SGeorge Wilson 	return (error);
4876ea8dc4b6Seschrock }
4877ea8dc4b6Seschrock 
48784263d13fSGeorge Wilson static int
48794263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
48804263d13fSGeorge Wilson {
48814263d13fSGeorge Wilson 	spa_t *spa;
48824263d13fSGeorge Wilson 	int error;
48834263d13fSGeorge Wilson 
48844263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
48853b2aab18SMatthew Ahrens 	if (error != 0)
48864263d13fSGeorge Wilson 		return (error);
48874263d13fSGeorge Wilson 
48884263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
4889d6afdce2SGeorge Wilson 
4890d6afdce2SGeorge Wilson 	/*
4891d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
4892d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4893d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
4894d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
4895d6afdce2SGeorge Wilson 	 */
4896d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
48974263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
4898d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
4899d6afdce2SGeorge Wilson 
49004263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
49014263d13fSGeorge Wilson 	spa_close(spa, FTAG);
49024263d13fSGeorge Wilson 	return (0);
49034263d13fSGeorge Wilson }
49043cb34c60Sahrens /*
49053cb34c60Sahrens  * inputs:
49063cb34c60Sahrens  * zc_name	name of filesystem
49073cb34c60Sahrens  *
4908681d9761SEric Taylor  * outputs:
4909681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
49103cb34c60Sahrens  */
491199653d4eSeschrock static int
491299653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
491399653d4eSeschrock {
4914a4b8c9aaSAndrew Stormont 	dsl_pool_t *dp;
4915a4b8c9aaSAndrew Stormont 	dsl_dataset_t *ds, *ods;
4916a4b8c9aaSAndrew Stormont 	char origin[ZFS_MAX_DATASET_NAME_LEN];
49170b69c2f0Sahrens 	char *cp;
4918a4b8c9aaSAndrew Stormont 	int error;
4919a4b8c9aaSAndrew Stormont 
4920add927f8Sloli 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4921add927f8Sloli 	if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4922add927f8Sloli 	    strchr(zc->zc_name, '%'))
4923add927f8Sloli 		return (SET_ERROR(EINVAL));
4924add927f8Sloli 
4925a4b8c9aaSAndrew Stormont 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4926a4b8c9aaSAndrew Stormont 	if (error != 0)
4927a4b8c9aaSAndrew Stormont 		return (error);
4928a4b8c9aaSAndrew Stormont 
4929a4b8c9aaSAndrew Stormont 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4930a4b8c9aaSAndrew Stormont 	if (error != 0) {
4931a4b8c9aaSAndrew Stormont 		dsl_pool_rele(dp, FTAG);
4932a4b8c9aaSAndrew Stormont 		return (error);
4933a4b8c9aaSAndrew Stormont 	}
4934a4b8c9aaSAndrew Stormont 
4935a4b8c9aaSAndrew Stormont 	if (!dsl_dir_is_clone(ds->ds_dir)) {
4936a4b8c9aaSAndrew Stormont 		dsl_dataset_rele(ds, FTAG);
4937a4b8c9aaSAndrew Stormont 		dsl_pool_rele(dp, FTAG);
4938a4b8c9aaSAndrew Stormont 		return (SET_ERROR(EINVAL));
4939a4b8c9aaSAndrew Stormont 	}
4940a4b8c9aaSAndrew Stormont 
4941a4b8c9aaSAndrew Stormont 	error = dsl_dataset_hold_obj(dp,
4942a4b8c9aaSAndrew Stormont 	    dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4943a4b8c9aaSAndrew Stormont 	if (error != 0) {
4944a4b8c9aaSAndrew Stormont 		dsl_dataset_rele(ds, FTAG);
4945a4b8c9aaSAndrew Stormont 		dsl_pool_rele(dp, FTAG);
4946a4b8c9aaSAndrew Stormont 		return (error);
4947a4b8c9aaSAndrew Stormont 	}
4948a4b8c9aaSAndrew Stormont 
4949a4b8c9aaSAndrew Stormont 	dsl_dataset_name(ods, origin);
4950a4b8c9aaSAndrew Stormont 	dsl_dataset_rele(ods, FTAG);
4951a4b8c9aaSAndrew Stormont 	dsl_dataset_rele(ds, FTAG);
4952a4b8c9aaSAndrew Stormont 	dsl_pool_rele(dp, FTAG);
49530b69c2f0Sahrens 
49540b69c2f0Sahrens 	/*
49550b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
49560b69c2f0Sahrens 	 * it's easier.
49570b69c2f0Sahrens 	 */
4958a4b8c9aaSAndrew Stormont 	cp = strchr(origin, '@');
49590b69c2f0Sahrens 	if (cp)
49600b69c2f0Sahrens 		*cp = '\0';
4961a4b8c9aaSAndrew Stormont 	(void) dmu_objset_find(origin,
49623b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4963681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
496499653d4eSeschrock }
496599653d4eSeschrock 
496614843421SMatthew Ahrens /*
496714843421SMatthew Ahrens  * Retrieve a single {user|group}{used|quota}@... property.
496814843421SMatthew Ahrens  *
496914843421SMatthew Ahrens  * inputs:
497014843421SMatthew Ahrens  * zc_name	name of filesystem
497114843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
497214843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
497314843421SMatthew Ahrens  * zc_guid	RID/UID/GID
497414843421SMatthew Ahrens  *
497514843421SMatthew Ahrens  * outputs:
497614843421SMatthew Ahrens  * zc_cookie	property value
497714843421SMatthew Ahrens  */
497814843421SMatthew Ahrens static int
497914843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
498014843421SMatthew Ahrens {
498114843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
498214843421SMatthew Ahrens 	int error;
498314843421SMatthew Ahrens 
498414843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4985be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
498614843421SMatthew Ahrens 
49871412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
49883b2aab18SMatthew Ahrens 	if (error != 0)
498914843421SMatthew Ahrens 		return (error);
499014843421SMatthew Ahrens 
499114843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
499214843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
499314843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
499414843421SMatthew Ahrens 
499514843421SMatthew Ahrens 	return (error);
499614843421SMatthew Ahrens }
499714843421SMatthew Ahrens 
499814843421SMatthew Ahrens /*
499914843421SMatthew Ahrens  * inputs:
500014843421SMatthew Ahrens  * zc_name		name of filesystem
500114843421SMatthew Ahrens  * zc_cookie		zap cursor
500214843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
500314843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
500414843421SMatthew Ahrens  *
500514843421SMatthew Ahrens  * outputs:
500614843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
500714843421SMatthew Ahrens  * zc_cookie	zap cursor
500814843421SMatthew Ahrens  */
500914843421SMatthew Ahrens static int
501014843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
501114843421SMatthew Ahrens {
501214843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
5013eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
501414843421SMatthew Ahrens 
5015eeb85002STim Haley 	if (bufsize <= 0)
5016be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
5017eeb85002STim Haley 
50181412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
50193b2aab18SMatthew Ahrens 	if (error != 0)
502014843421SMatthew Ahrens 		return (error);
502114843421SMatthew Ahrens 
502214843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
502314843421SMatthew Ahrens 
502414843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
502514843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
502614843421SMatthew Ahrens 
502714843421SMatthew Ahrens 	if (error == 0) {
502814843421SMatthew Ahrens 		error = xcopyout(buf,
502914843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
503014843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
503114843421SMatthew Ahrens 	}
503214843421SMatthew Ahrens 	kmem_free(buf, bufsize);
503314843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
503414843421SMatthew Ahrens 
503514843421SMatthew Ahrens 	return (error);
503614843421SMatthew Ahrens }
503714843421SMatthew Ahrens 
503814843421SMatthew Ahrens /*
503914843421SMatthew Ahrens  * inputs:
504014843421SMatthew Ahrens  * zc_name		name of filesystem
504114843421SMatthew Ahrens  *
504214843421SMatthew Ahrens  * outputs:
504314843421SMatthew Ahrens  * none
504414843421SMatthew Ahrens  */
504514843421SMatthew Ahrens static int
504614843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
504714843421SMatthew Ahrens {
504814843421SMatthew Ahrens 	objset_t *os;
50491195e687SMark J Musante 	int error = 0;
505014843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
505114843421SMatthew Ahrens 
505214843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5053503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
505414843421SMatthew Ahrens 			/*
505514843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
505614843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
505714843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
505814843421SMatthew Ahrens 			 */
50595f5913bbSAndriy Gapon 			dsl_dataset_t *ds, *newds;
5060690041b9SAndriy Gapon 
5061690041b9SAndriy Gapon 			ds = dmu_objset_ds(zfsvfs->z_os);
5062503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
506391948b51SKeith M Wesolowski 			if (error == 0) {
50645f5913bbSAndriy Gapon 				dmu_objset_refresh_ownership(ds, &newds,
506591948b51SKeith M Wesolowski 				    zfsvfs);
50665f5913bbSAndriy Gapon 				error = zfs_resume_fs(zfsvfs, newds);
506791948b51SKeith M Wesolowski 			}
506814843421SMatthew Ahrens 		}
506914843421SMatthew Ahrens 		if (error == 0)
507014843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
507114843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
507214843421SMatthew Ahrens 	} else {
5073503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
5074503ad85cSMatthew Ahrens 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
50753b2aab18SMatthew Ahrens 		if (error != 0)
507614843421SMatthew Ahrens 			return (error);
507714843421SMatthew Ahrens 
507814843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
5079503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
508014843421SMatthew Ahrens 	}
508114843421SMatthew Ahrens 
508214843421SMatthew Ahrens 	return (error);
508314843421SMatthew Ahrens }
508414843421SMatthew Ahrens 
5085ecd6cf80Smarks /*
5086ecd6cf80Smarks  * We don't want to have a hard dependency
5087ecd6cf80Smarks  * against some special symbols in sharefs
5088da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
5089ecd6cf80Smarks  * the first file system is shared.
5090da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
5091ecd6cf80Smarks  */
5092da6c28aaSamw int (*znfsexport_fs)(void *arg);
5093ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5094da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5095da6c28aaSamw 
5096da6c28aaSamw int zfs_nfsshare_inited;
5097da6c28aaSamw int zfs_smbshare_inited;
5098ecd6cf80Smarks 
5099ecd6cf80Smarks ddi_modhandle_t nfs_mod;
5100ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
5101da6c28aaSamw ddi_modhandle_t smbsrv_mod;
5102ecd6cf80Smarks kmutex_t zfs_share_lock;
5103ecd6cf80Smarks 
5104da6c28aaSamw static int
5105da6c28aaSamw zfs_init_sharefs()
5106da6c28aaSamw {
5107da6c28aaSamw 	int error;
5108da6c28aaSamw 
5109da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
5110da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
5111da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
5112da6c28aaSamw 	    ddi_modopen("fs/sharefs",
5113da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
5114be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
5115da6c28aaSamw 	}
5116da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
5117da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5118da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5119be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
5120da6c28aaSamw 	}
5121da6c28aaSamw 	return (0);
5122da6c28aaSamw }
5123da6c28aaSamw 
5124ecd6cf80Smarks static int
5125ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
5126ecd6cf80Smarks {
5127ecd6cf80Smarks 	int error;
5128ecd6cf80Smarks 	int opcode;
5129ecd6cf80Smarks 
5130da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
5131da6c28aaSamw 	case ZFS_SHARE_NFS:
5132da6c28aaSamw 	case ZFS_UNSHARE_NFS:
5133da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
5134da6c28aaSamw 			mutex_enter(&zfs_share_lock);
5135da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5136da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
5137da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5138be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5139da6c28aaSamw 			}
5140da6c28aaSamw 			if (znfsexport_fs == NULL &&
5141da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
5142da6c28aaSamw 			    ddi_modsym(nfs_mod,
5143da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
5144da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5145be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5146da6c28aaSamw 			}
5147da6c28aaSamw 			error = zfs_init_sharefs();
51483b2aab18SMatthew Ahrens 			if (error != 0) {
5149da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5150be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5151da6c28aaSamw 			}
5152da6c28aaSamw 			zfs_nfsshare_inited = 1;
5153ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
5154ecd6cf80Smarks 		}
5155da6c28aaSamw 		break;
5156da6c28aaSamw 	case ZFS_SHARE_SMB:
5157da6c28aaSamw 	case ZFS_UNSHARE_SMB:
5158da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
5159da6c28aaSamw 			mutex_enter(&zfs_share_lock);
5160da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
5161da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
5162da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
5163da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5164be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5165da6c28aaSamw 			}
5166da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5167da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5168faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
5169da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5170be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5171da6c28aaSamw 			}
5172da6c28aaSamw 			error = zfs_init_sharefs();
51733b2aab18SMatthew Ahrens 			if (error != 0) {
5174da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5175be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5176da6c28aaSamw 			}
5177da6c28aaSamw 			zfs_smbshare_inited = 1;
5178ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
5179ecd6cf80Smarks 		}
5180da6c28aaSamw 		break;
5181da6c28aaSamw 	default:
5182be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5183da6c28aaSamw 	}
5184ecd6cf80Smarks 
5185da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
5186da6c28aaSamw 	case ZFS_SHARE_NFS:
5187da6c28aaSamw 	case ZFS_UNSHARE_NFS:
5188da6c28aaSamw 		if (error =
5189da6c28aaSamw 		    znfsexport_fs((void *)
5190da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
5191da6c28aaSamw 			return (error);
5192da6c28aaSamw 		break;
5193da6c28aaSamw 	case ZFS_SHARE_SMB:
5194da6c28aaSamw 	case ZFS_UNSHARE_SMB:
5195da6c28aaSamw 		if (error = zsmbexport_fs((void *)
5196da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
5197da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5198743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
5199da6c28aaSamw 			return (error);
5200ecd6cf80Smarks 		}
5201da6c28aaSamw 		break;
5202ecd6cf80Smarks 	}
5203ecd6cf80Smarks 
5204da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5205da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5206ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
5207ecd6cf80Smarks 
5208da6c28aaSamw 	/*
5209da6c28aaSamw 	 * Add or remove share from sharetab
5210da6c28aaSamw 	 */
5211ecd6cf80Smarks 	error = zshare_fs(opcode,
5212ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
5213ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
5214ecd6cf80Smarks 
5215ecd6cf80Smarks 	return (error);
5216ecd6cf80Smarks 
5217ecd6cf80Smarks }
5218ecd6cf80Smarks 
5219743a77edSAlan Wright ace_t full_access[] = {
5220743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5221743a77edSAlan Wright };
5222743a77edSAlan Wright 
522399d5e173STim Haley /*
522499d5e173STim Haley  * inputs:
522599d5e173STim Haley  * zc_name		name of containing filesystem
522699d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
522799d5e173STim Haley  *
522899d5e173STim Haley  * outputs:
522999d5e173STim Haley  * zc_obj		next in-use object #
523099d5e173STim Haley  */
523199d5e173STim Haley static int
523299d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
523399d5e173STim Haley {
523499d5e173STim Haley 	objset_t *os = NULL;
523599d5e173STim Haley 	int error;
523699d5e173STim Haley 
523799d5e173STim Haley 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
52383b2aab18SMatthew Ahrens 	if (error != 0)
523999d5e173STim Haley 		return (error);
524099d5e173STim Haley 
524199d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5242c1379625SJustin T. Gibbs 	    dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
524399d5e173STim Haley 
524499d5e173STim Haley 	dmu_objset_rele(os, FTAG);
524599d5e173STim Haley 	return (error);
524699d5e173STim Haley }
524799d5e173STim Haley 
524899d5e173STim Haley /*
524999d5e173STim Haley  * inputs:
525099d5e173STim Haley  * zc_name		name of filesystem
525199d5e173STim Haley  * zc_value		prefix name for snapshot
525299d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
525399d5e173STim Haley  *
525499d5e173STim Haley  * outputs:
52554445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
525699d5e173STim Haley  */
525799d5e173STim Haley static int
525899d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
525999d5e173STim Haley {
526099d5e173STim Haley 	char *snap_name;
52613b2aab18SMatthew Ahrens 	char *hold_name;
526299d5e173STim Haley 	int error;
52633b2aab18SMatthew Ahrens 	minor_t minor;
526499d5e173STim Haley 
52653b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
52663b2aab18SMatthew Ahrens 	if (error != 0)
526799d5e173STim Haley 		return (error);
526899d5e173STim Haley 
52693b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
52703b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
52713b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
52723b2aab18SMatthew Ahrens 
52733b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
52743b2aab18SMatthew Ahrens 	    hold_name);
52753b2aab18SMatthew Ahrens 	if (error == 0)
52763b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
527799d5e173STim Haley 	strfree(snap_name);
52783b2aab18SMatthew Ahrens 	strfree(hold_name);
52793b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
52803b2aab18SMatthew Ahrens 	return (error);
528199d5e173STim Haley }
528299d5e173STim Haley 
528399d5e173STim Haley /*
528499d5e173STim Haley  * inputs:
528599d5e173STim Haley  * zc_name		name of "to" snapshot
528699d5e173STim Haley  * zc_value		name of "from" snapshot
528799d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
528899d5e173STim Haley  *
528999d5e173STim Haley  * outputs:
529099d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
529199d5e173STim Haley  */
529299d5e173STim Haley static int
529399d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
529499d5e173STim Haley {
529599d5e173STim Haley 	file_t *fp;
529699d5e173STim Haley 	offset_t off;
529799d5e173STim Haley 	int error;
529899d5e173STim Haley 
529999d5e173STim Haley 	fp = getf(zc->zc_cookie);
53003b2aab18SMatthew Ahrens 	if (fp == NULL)
5301be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
530299d5e173STim Haley 
530399d5e173STim Haley 	off = fp->f_offset;
530499d5e173STim Haley 
53053b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
530699d5e173STim Haley 
530799d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
530899d5e173STim Haley 		fp->f_offset = off;
530999d5e173STim Haley 	releasef(zc->zc_cookie);
531099d5e173STim Haley 
531199d5e173STim Haley 	return (error);
531299d5e173STim Haley }
531399d5e173STim Haley 
5314743a77edSAlan Wright /*
5315743a77edSAlan Wright  * Remove all ACL files in shares dir
5316743a77edSAlan Wright  */
5317743a77edSAlan Wright static int
5318743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
5319743a77edSAlan Wright {
5320743a77edSAlan Wright 	zap_cursor_t	zc;
5321743a77edSAlan Wright 	zap_attribute_t	zap;
5322743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5323743a77edSAlan Wright 	int error;
5324743a77edSAlan Wright 
5325743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5326743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5327743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
5328743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5329743a77edSAlan Wright 		    NULL, 0)) != 0)
5330743a77edSAlan Wright 			break;
5331743a77edSAlan Wright 	}
5332743a77edSAlan Wright 	zap_cursor_fini(&zc);
5333743a77edSAlan Wright 	return (error);
5334743a77edSAlan Wright }
5335743a77edSAlan Wright 
5336743a77edSAlan Wright static int
5337743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
5338743a77edSAlan Wright {
5339743a77edSAlan Wright 	vnode_t *vp;
5340743a77edSAlan Wright 	znode_t *dzp;
5341743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
5342743a77edSAlan Wright 	znode_t *sharedir;
5343743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
5344743a77edSAlan Wright 	nvlist_t *nvlist;
5345743a77edSAlan Wright 	char *src, *target;
5346743a77edSAlan Wright 	vattr_t vattr;
5347743a77edSAlan Wright 	vsecattr_t vsec;
5348743a77edSAlan Wright 	int error = 0;
5349743a77edSAlan Wright 
5350743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5351743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
5352743a77edSAlan Wright 		return (error);
5353743a77edSAlan Wright 
5354743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
5355743a77edSAlan Wright 
5356743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5357743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5358743a77edSAlan Wright 	    zc->zc_name) != 0)) {
5359743a77edSAlan Wright 		VN_RELE(vp);
5360be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5361743a77edSAlan Wright 	}
5362743a77edSAlan Wright 
5363743a77edSAlan Wright 	dzp = VTOZ(vp);
5364743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
5365743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
5366743a77edSAlan Wright 
53679e1320c0SMark Shellenbaum 	/*
53689e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
53699e1320c0SMark Shellenbaum 	 */
53709e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
53719e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
53729e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
53739e1320c0SMark Shellenbaum 
53749e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
53759e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
53769e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
53779e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
53789e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
53793b2aab18SMatthew Ahrens 		if (error != 0) {
53809e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
53819e1320c0SMark Shellenbaum 		} else {
53829e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
53839e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
53849e1320c0SMark Shellenbaum 		}
53853b2aab18SMatthew Ahrens 		if (error != 0) {
53869e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
53879e1320c0SMark Shellenbaum 			VN_RELE(vp);
53889e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
53899e1320c0SMark Shellenbaum 			return (error);
53909e1320c0SMark Shellenbaum 		}
53919e1320c0SMark Shellenbaum 	}
53929e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
53939e1320c0SMark Shellenbaum 
53949e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
5395743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
53969e1320c0SMark Shellenbaum 		VN_RELE(vp);
5397743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
5398743a77edSAlan Wright 		return (error);
5399743a77edSAlan Wright 	}
5400743a77edSAlan Wright 
5401743a77edSAlan Wright 	switch (zc->zc_cookie) {
5402743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
5403743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5404743a77edSAlan Wright 		vattr.va_type = VREG;
5405743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
5406743a77edSAlan Wright 		vattr.va_uid = 0;
5407743a77edSAlan Wright 		vattr.va_gid = 0;
5408743a77edSAlan Wright 
5409743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
5410743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
5411743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
5412743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
5413743a77edSAlan Wright 
5414743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5415743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5416743a77edSAlan Wright 		if (resourcevp)
5417743a77edSAlan Wright 			VN_RELE(resourcevp);
5418743a77edSAlan Wright 		break;
5419743a77edSAlan Wright 
5420743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
5421743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5422743a77edSAlan Wright 		    NULL, 0);
5423743a77edSAlan Wright 		break;
5424743a77edSAlan Wright 
5425743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
5426743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
5427478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5428743a77edSAlan Wright 			VN_RELE(vp);
54298f5190a5SDan McDonald 			VN_RELE(ZTOV(sharedir));
5430743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
5431743a77edSAlan Wright 			return (error);
5432743a77edSAlan Wright 		}
5433743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5434743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5435743a77edSAlan Wright 		    &target)) {
5436743a77edSAlan Wright 			VN_RELE(vp);
543789459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
5438743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
54391195e687SMark J Musante 			nvlist_free(nvlist);
5440743a77edSAlan Wright 			return (error);
5441743a77edSAlan Wright 		}
5442743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5443743a77edSAlan Wright 		    kcred, NULL, 0);
5444743a77edSAlan Wright 		nvlist_free(nvlist);
5445743a77edSAlan Wright 		break;
5446743a77edSAlan Wright 
5447743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
5448743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
5449743a77edSAlan Wright 		break;
5450743a77edSAlan Wright 
5451743a77edSAlan Wright 	default:
5452be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
5453743a77edSAlan Wright 		break;
5454743a77edSAlan Wright 	}
5455743a77edSAlan Wright 
5456743a77edSAlan Wright 	VN_RELE(vp);
5457743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
5458743a77edSAlan Wright 
5459743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
5460743a77edSAlan Wright 
5461743a77edSAlan Wright 	return (error);
5462743a77edSAlan Wright }
5463743a77edSAlan Wright 
5464842727c2SChris Kirby /*
54653b2aab18SMatthew Ahrens  * innvl: {
54663b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
54673b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
54683b2aab18SMatthew Ahrens  * }
5469842727c2SChris Kirby  *
54703b2aab18SMatthew Ahrens  * outnvl: {
54713b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
54723b2aab18SMatthew Ahrens  *     ...
54733b2aab18SMatthew Ahrens  * }
5474842727c2SChris Kirby  */
54753b2aab18SMatthew Ahrens /* ARGSUSED */
5476842727c2SChris Kirby static int
54773b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5478842727c2SChris Kirby {
5479752fd8daSJosef 'Jeff' Sipek 	nvpair_t *pair;
54803b2aab18SMatthew Ahrens 	nvlist_t *holds;
54813b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
5482a7f53a56SChris Kirby 	int error;
5483a7f53a56SChris Kirby 	minor_t minor = 0;
5484842727c2SChris Kirby 
54853b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
54863b2aab18SMatthew Ahrens 	if (error != 0)
5487be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5488a7f53a56SChris Kirby 
5489752fd8daSJosef 'Jeff' Sipek 	/* make sure the user didn't pass us any invalid (empty) tags */
5490752fd8daSJosef 'Jeff' Sipek 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5491752fd8daSJosef 'Jeff' Sipek 	    pair = nvlist_next_nvpair(holds, pair)) {
5492752fd8daSJosef 'Jeff' Sipek 		char *htag;
5493752fd8daSJosef 'Jeff' Sipek 
5494752fd8daSJosef 'Jeff' Sipek 		error = nvpair_value_string(pair, &htag);
5495752fd8daSJosef 'Jeff' Sipek 		if (error != 0)
5496752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(error));
5497752fd8daSJosef 'Jeff' Sipek 
5498752fd8daSJosef 'Jeff' Sipek 		if (strlen(htag) == 0)
5499752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(EINVAL));
5500752fd8daSJosef 'Jeff' Sipek 	}
5501752fd8daSJosef 'Jeff' Sipek 
55023b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
55033b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
55043b2aab18SMatthew Ahrens 		if (error != 0)
5505a7f53a56SChris Kirby 			return (error);
5506a7f53a56SChris Kirby 	}
5507a7f53a56SChris Kirby 
55083b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
55093b2aab18SMatthew Ahrens 	if (minor != 0)
55103b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
5511a7f53a56SChris Kirby 	return (error);
5512842727c2SChris Kirby }
5513842727c2SChris Kirby 
5514842727c2SChris Kirby /*
55153b2aab18SMatthew Ahrens  * innvl is not used.
5516842727c2SChris Kirby  *
55173b2aab18SMatthew Ahrens  * outnvl: {
55183b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
55193b2aab18SMatthew Ahrens  *    ...
55203b2aab18SMatthew Ahrens  * }
5521842727c2SChris Kirby  */
55223b2aab18SMatthew Ahrens /* ARGSUSED */
5523842727c2SChris Kirby static int
55243b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5525842727c2SChris Kirby {
55263b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
5527842727c2SChris Kirby }
5528842727c2SChris Kirby 
5529842727c2SChris Kirby /*
55303b2aab18SMatthew Ahrens  * innvl: {
55313b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
55323b2aab18SMatthew Ahrens  *     ...
55333b2aab18SMatthew Ahrens  * }
5534842727c2SChris Kirby  *
55353b2aab18SMatthew Ahrens  * outnvl: {
55363b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
55373b2aab18SMatthew Ahrens  *     ...
55383b2aab18SMatthew Ahrens  * }
5539842727c2SChris Kirby  */
55403b2aab18SMatthew Ahrens /* ARGSUSED */
5541842727c2SChris Kirby static int
55423b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5543842727c2SChris Kirby {
55443b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
5545842727c2SChris Kirby }
5546842727c2SChris Kirby 
554719b94df9SMatthew Ahrens /*
554819b94df9SMatthew Ahrens  * inputs:
554919b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
555019b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
555119b94df9SMatthew Ahrens  *
555219b94df9SMatthew Ahrens  * outputs:
555319b94df9SMatthew Ahrens  * zc_cookie		space in bytes
555419b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
555519b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
555619b94df9SMatthew Ahrens  */
555719b94df9SMatthew Ahrens static int
555819b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
555919b94df9SMatthew Ahrens {
556019b94df9SMatthew Ahrens 	int error;
55613b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
556219b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
556319b94df9SMatthew Ahrens 
55643b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
556519b94df9SMatthew Ahrens 	if (error != 0)
556619b94df9SMatthew Ahrens 		return (error);
55673b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
55683b2aab18SMatthew Ahrens 	if (error != 0) {
55693b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
55703b2aab18SMatthew Ahrens 		return (error);
55713b2aab18SMatthew Ahrens 	}
55723b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
557319b94df9SMatthew Ahrens 	if (error != 0) {
557419b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
55753b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
557619b94df9SMatthew Ahrens 		return (error);
557719b94df9SMatthew Ahrens 	}
557819b94df9SMatthew Ahrens 
557919b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
558019b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
558119b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
558219b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
55833b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
558419b94df9SMatthew Ahrens 	return (error);
558519b94df9SMatthew Ahrens }
55863b2aab18SMatthew Ahrens 
558719b94df9SMatthew Ahrens /*
55884445fffbSMatthew Ahrens  * innvl: {
55894445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
55904445fffbSMatthew Ahrens  * }
559119b94df9SMatthew Ahrens  *
55924445fffbSMatthew Ahrens  * outnvl: {
55934445fffbSMatthew Ahrens  *     "used" -> space in bytes
55944445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
55954445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
55964445fffbSMatthew Ahrens  * }
559719b94df9SMatthew Ahrens  */
559819b94df9SMatthew Ahrens static int
55994445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
560019b94df9SMatthew Ahrens {
560119b94df9SMatthew Ahrens 	int error;
56023b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
560319b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
56044445fffbSMatthew Ahrens 	char *firstsnap;
56054445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
560619b94df9SMatthew Ahrens 
56074445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5608be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
56094445fffbSMatthew Ahrens 
56103b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
561119b94df9SMatthew Ahrens 	if (error != 0)
561219b94df9SMatthew Ahrens 		return (error);
56133b2aab18SMatthew Ahrens 
56143b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
561524218bebSAndriy Gapon 	if (error == 0 && !new->ds_is_snapshot) {
561624218bebSAndriy Gapon 		dsl_dataset_rele(new, FTAG);
561724218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
561824218bebSAndriy Gapon 	}
56193b2aab18SMatthew Ahrens 	if (error != 0) {
56203b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
56213b2aab18SMatthew Ahrens 		return (error);
56223b2aab18SMatthew Ahrens 	}
56233b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
562424218bebSAndriy Gapon 	if (error == 0 && !old->ds_is_snapshot) {
562524218bebSAndriy Gapon 		dsl_dataset_rele(old, FTAG);
562624218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
562724218bebSAndriy Gapon 	}
562819b94df9SMatthew Ahrens 	if (error != 0) {
562919b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
56303b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
563119b94df9SMatthew Ahrens 		return (error);
563219b94df9SMatthew Ahrens 	}
563319b94df9SMatthew Ahrens 
56344445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
563519b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
563619b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
56373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
56384445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
56394445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
56404445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
564119b94df9SMatthew Ahrens 	return (error);
564219b94df9SMatthew Ahrens }
564319b94df9SMatthew Ahrens 
5644ecd6cf80Smarks /*
56454445fffbSMatthew Ahrens  * innvl: {
56464445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
56474445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
5648b5152584SMatthew Ahrens  *     (optional) "largeblockok" -> (value ignored)
5649b5152584SMatthew Ahrens  *         indicates that blocks > 128KB are permitted
56505d7b4d43SMatthew Ahrens  *     (optional) "embedok" -> (value ignored)
56515d7b4d43SMatthew Ahrens  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
56525602294fSDan Kimmel  *     (optional) "compressok" -> (value ignored)
56535602294fSDan Kimmel  *         presence indicates compressed DRR_WRITE records are permitted
56549c3fd121SMatthew Ahrens  *     (optional) "resume_object" and "resume_offset" -> (uint64)
56559c3fd121SMatthew Ahrens  *         if present, resume send stream from specified object and offset.
56564445fffbSMatthew Ahrens  * }
56574445fffbSMatthew Ahrens  *
56584445fffbSMatthew Ahrens  * outnvl is unused
5659ecd6cf80Smarks  */
56604445fffbSMatthew Ahrens /* ARGSUSED */
56614445fffbSMatthew Ahrens static int
56624445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
56634445fffbSMatthew Ahrens {
56644445fffbSMatthew Ahrens 	int error;
56654445fffbSMatthew Ahrens 	offset_t off;
56663b2aab18SMatthew Ahrens 	char *fromname = NULL;
56674445fffbSMatthew Ahrens 	int fd;
5668b5152584SMatthew Ahrens 	boolean_t largeblockok;
56695d7b4d43SMatthew Ahrens 	boolean_t embedok;
56705602294fSDan Kimmel 	boolean_t compressok;
56719c3fd121SMatthew Ahrens 	uint64_t resumeobj = 0;
56729c3fd121SMatthew Ahrens 	uint64_t resumeoff = 0;
56734445fffbSMatthew Ahrens 
56744445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
56754445fffbSMatthew Ahrens 	if (error != 0)
5676be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
56774445fffbSMatthew Ahrens 
56783b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
56794445fffbSMatthew Ahrens 
5680b5152584SMatthew Ahrens 	largeblockok = nvlist_exists(innvl, "largeblockok");
56815d7b4d43SMatthew Ahrens 	embedok = nvlist_exists(innvl, "embedok");
56825602294fSDan Kimmel 	compressok = nvlist_exists(innvl, "compressok");
56835d7b4d43SMatthew Ahrens 
56849c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
56859c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
56869c3fd121SMatthew Ahrens 
56874445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
56883b2aab18SMatthew Ahrens 	if (fp == NULL)
5689be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
56904445fffbSMatthew Ahrens 
56914445fffbSMatthew Ahrens 	off = fp->f_offset;
56925602294fSDan Kimmel 	error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
56935602294fSDan Kimmel 	    fd, resumeobj, resumeoff, fp->f_vnode, &off);
56944445fffbSMatthew Ahrens 
56954445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
56964445fffbSMatthew Ahrens 		fp->f_offset = off;
56974445fffbSMatthew Ahrens 	releasef(fd);
56984445fffbSMatthew Ahrens 	return (error);
56994445fffbSMatthew Ahrens }
57004445fffbSMatthew Ahrens 
57014445fffbSMatthew Ahrens /*
57024445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
57034445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
57044445fffbSMatthew Ahrens  *
57054445fffbSMatthew Ahrens  * innvl: {
5706643da460SMax Grossman  *     (optional) "from" -> full snap or bookmark name to send an incremental
5707643da460SMax Grossman  *                          from
57085602294fSDan Kimmel  *     (optional) "largeblockok" -> (value ignored)
57095602294fSDan Kimmel  *         indicates that blocks > 128KB are permitted
57105602294fSDan Kimmel  *     (optional) "embedok" -> (value ignored)
57115602294fSDan Kimmel  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
57125602294fSDan Kimmel  *     (optional) "compressok" -> (value ignored)
57135602294fSDan Kimmel  *         presence indicates compressed DRR_WRITE records are permitted
57144445fffbSMatthew Ahrens  * }
57154445fffbSMatthew Ahrens  *
57164445fffbSMatthew Ahrens  * outnvl: {
57174445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
57184445fffbSMatthew Ahrens  * }
57194445fffbSMatthew Ahrens  */
57204445fffbSMatthew Ahrens static int
57214445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
57224445fffbSMatthew Ahrens {
57233b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
57243b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
57254445fffbSMatthew Ahrens 	int error;
57264445fffbSMatthew Ahrens 	char *fromname;
57275602294fSDan Kimmel 	boolean_t compressok;
57284445fffbSMatthew Ahrens 	uint64_t space;
57294445fffbSMatthew Ahrens 
57303b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
57313b2aab18SMatthew Ahrens 	if (error != 0)
57323b2aab18SMatthew Ahrens 		return (error);
57333b2aab18SMatthew Ahrens 
57343b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
57353b2aab18SMatthew Ahrens 	if (error != 0) {
57363b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
57374445fffbSMatthew Ahrens 		return (error);
57383b2aab18SMatthew Ahrens 	}
57394445fffbSMatthew Ahrens 
57405602294fSDan Kimmel 	compressok = nvlist_exists(innvl, "compressok");
57415602294fSDan Kimmel 
5742643da460SMax Grossman 	error = nvlist_lookup_string(innvl, "from", &fromname);
57434445fffbSMatthew Ahrens 	if (error == 0) {
5744643da460SMax Grossman 		if (strchr(fromname, '@') != NULL) {
5745643da460SMax Grossman 			/*
5746643da460SMax Grossman 			 * If from is a snapshot, hold it and use the more
5747643da460SMax Grossman 			 * efficient dmu_send_estimate to estimate send space
5748643da460SMax Grossman 			 * size using deadlists.
5749643da460SMax Grossman 			 */
5750643da460SMax Grossman 			dsl_dataset_t *fromsnap;
5751643da460SMax Grossman 			error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5752643da460SMax Grossman 			if (error != 0)
5753643da460SMax Grossman 				goto out;
57545602294fSDan Kimmel 			error = dmu_send_estimate(tosnap, fromsnap, compressok,
57555602294fSDan Kimmel 			    &space);
5756643da460SMax Grossman 			dsl_dataset_rele(fromsnap, FTAG);
5757643da460SMax Grossman 		} else if (strchr(fromname, '#') != NULL) {
5758643da460SMax Grossman 			/*
5759643da460SMax Grossman 			 * If from is a bookmark, fetch the creation TXG of the
5760643da460SMax Grossman 			 * snapshot it was created from and use that to find
5761643da460SMax Grossman 			 * blocks that were born after it.
5762643da460SMax Grossman 			 */
5763643da460SMax Grossman 			zfs_bookmark_phys_t frombm;
5764643da460SMax Grossman 
5765643da460SMax Grossman 			error = dsl_bookmark_lookup(dp, fromname, tosnap,
5766643da460SMax Grossman 			    &frombm);
5767643da460SMax Grossman 			if (error != 0)
5768643da460SMax Grossman 				goto out;
5769643da460SMax Grossman 			error = dmu_send_estimate_from_txg(tosnap,
57705602294fSDan Kimmel 			    frombm.zbm_creation_txg, compressok, &space);
5771643da460SMax Grossman 		} else {
5772643da460SMax Grossman 			/*
5773643da460SMax Grossman 			 * from is not properly formatted as a snapshot or
5774643da460SMax Grossman 			 * bookmark
5775643da460SMax Grossman 			 */
5776643da460SMax Grossman 			error = SET_ERROR(EINVAL);
5777643da460SMax Grossman 			goto out;
57784445fffbSMatthew Ahrens 		}
5779643da460SMax Grossman 	} else {
5780b852c2f5SToomas Soome 		/*
5781b852c2f5SToomas Soome 		 * If estimating the size of a full send, use dmu_send_estimate.
5782b852c2f5SToomas Soome 		 */
57835602294fSDan Kimmel 		error = dmu_send_estimate(tosnap, NULL, compressok, &space);
57844445fffbSMatthew Ahrens 	}
57854445fffbSMatthew Ahrens 
57864445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
57874445fffbSMatthew Ahrens 
5788643da460SMax Grossman out:
57893b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
57903b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
57914445fffbSMatthew Ahrens 	return (error);
57924445fffbSMatthew Ahrens }
57934445fffbSMatthew Ahrens 
57944445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
57954445fffbSMatthew Ahrens 
57964445fffbSMatthew Ahrens static void
57974445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
57984445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
57994445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
58004445fffbSMatthew Ahrens {
58014445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
58024445fffbSMatthew Ahrens 
58034445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
58044445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
58054445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
58064445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
58074445fffbSMatthew Ahrens 
58084445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
58094445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
58104445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
58114445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
58124445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
58134445fffbSMatthew Ahrens }
58144445fffbSMatthew Ahrens 
58154445fffbSMatthew Ahrens /*
58164445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
58174445fffbSMatthew Ahrens  * each argument to this function.
58184445fffbSMatthew Ahrens  */
58194445fffbSMatthew Ahrens static void
58204445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
58214445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
58224445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
58234445fffbSMatthew Ahrens     boolean_t allow_log)
58244445fffbSMatthew Ahrens {
58254445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
58264445fffbSMatthew Ahrens 
58274445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
58284445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
58294445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
58304445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
58314445fffbSMatthew Ahrens 
58324445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
58334445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
58344445fffbSMatthew Ahrens 
58354445fffbSMatthew Ahrens 	vec->zvec_name = name;
58364445fffbSMatthew Ahrens 	vec->zvec_func = func;
58374445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
58384445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
58394445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
58404445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
58414445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
58424445fffbSMatthew Ahrens }
58434445fffbSMatthew Ahrens 
58444445fffbSMatthew Ahrens static void
58454445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
58464445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
58474445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
58484445fffbSMatthew Ahrens {
58494445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
58504445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
58514445fffbSMatthew Ahrens }
58524445fffbSMatthew Ahrens 
58534445fffbSMatthew Ahrens static void
58544445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
58554445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
58564445fffbSMatthew Ahrens {
58574445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
58584445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
58594445fffbSMatthew Ahrens }
58604445fffbSMatthew Ahrens 
58614445fffbSMatthew Ahrens static void
58624445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
58634445fffbSMatthew Ahrens {
58644445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
58654445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
58664445fffbSMatthew Ahrens }
58674445fffbSMatthew Ahrens 
58684445fffbSMatthew Ahrens static void
58694445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
58704445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
58714445fffbSMatthew Ahrens {
58724445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
58734445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
58744445fffbSMatthew Ahrens }
58754445fffbSMatthew Ahrens 
58764445fffbSMatthew Ahrens static void
58774445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
58784445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
58794445fffbSMatthew Ahrens {
58804445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
58814445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
58824445fffbSMatthew Ahrens }
58834445fffbSMatthew Ahrens 
58844445fffbSMatthew Ahrens static void
58854445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
58864445fffbSMatthew Ahrens {
58874445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
58884445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
58894445fffbSMatthew Ahrens }
58904445fffbSMatthew Ahrens 
58914445fffbSMatthew Ahrens static void
58924445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
58939a686fbcSPaul Dagnelie     zfs_secpolicy_func_t *secpolicy)
58944445fffbSMatthew Ahrens {
58954445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
58964445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
58974445fffbSMatthew Ahrens }
58984445fffbSMatthew Ahrens 
58994445fffbSMatthew Ahrens static void
59004445fffbSMatthew Ahrens zfs_ioctl_init(void)
59014445fffbSMatthew Ahrens {
59024445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
59034445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
59044445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
59054445fffbSMatthew Ahrens 
59064445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
59074445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
59084445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
59094445fffbSMatthew Ahrens 
59104445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
59114445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
59124445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
59134445fffbSMatthew Ahrens 
59144445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
59154445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
59164445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
59174445fffbSMatthew Ahrens 
59184445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
59194445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
59204445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
59214445fffbSMatthew Ahrens 
59224445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
59234445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
59244445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
59254445fffbSMatthew Ahrens 
59264445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
59274445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
59284445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
59294445fffbSMatthew Ahrens 
59305cabbc6bSPrashanth Sreenivasa 	zfs_ioctl_register("remap", ZFS_IOC_REMAP,
59315cabbc6bSPrashanth Sreenivasa 	    zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
59325cabbc6bSPrashanth Sreenivasa 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
59335cabbc6bSPrashanth Sreenivasa 
59344445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
59354445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
59364445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
59374445fffbSMatthew Ahrens 
59383b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
59393b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
59403b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
59413b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
59423b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
59433b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
59443b2aab18SMatthew Ahrens 
59453b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
59463b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
59473b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
59483b2aab18SMatthew Ahrens 
5949a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5950a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5951a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5952a7027df1SMatthew Ahrens 
595378f17100SMatthew Ahrens 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
595478f17100SMatthew Ahrens 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
595578f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
595678f17100SMatthew Ahrens 
595778f17100SMatthew Ahrens 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
595878f17100SMatthew Ahrens 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
595978f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
596078f17100SMatthew Ahrens 
596178f17100SMatthew Ahrens 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
596278f17100SMatthew Ahrens 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
596378f17100SMatthew Ahrens 	    POOL_NAME,
596478f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
596578f17100SMatthew Ahrens 
5966dfc11533SChris Williamson 	zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5967dfc11533SChris Williamson 	    zfs_ioc_channel_program, zfs_secpolicy_config,
5968dfc11533SChris Williamson 	    POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5969dfc11533SChris Williamson 	    B_TRUE);
5970dfc11533SChris Williamson 
597186714001SSerapheim Dimitropoulos 	zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
597286714001SSerapheim Dimitropoulos 	    zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
597386714001SSerapheim Dimitropoulos 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
597486714001SSerapheim Dimitropoulos 
597586714001SSerapheim Dimitropoulos 	zfs_ioctl_register("zpool_discard_checkpoint",
597686714001SSerapheim Dimitropoulos 	    ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
597786714001SSerapheim Dimitropoulos 	    zfs_secpolicy_config, POOL_NAME,
597886714001SSerapheim Dimitropoulos 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
597986714001SSerapheim Dimitropoulos 
5980094e47e9SGeorge Wilson 	zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
5981094e47e9SGeorge Wilson 	    zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
5982094e47e9SGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5983094e47e9SGeorge Wilson 
59844445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
59854445fffbSMatthew Ahrens 
59864445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
59874445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
59884445fffbSMatthew Ahrens 
59894445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
59904445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
59914445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
59924445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
59934445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
59944445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
59954445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
59964445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
59974445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
59984445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
59994445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
60004445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
60014445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
60024445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
60034445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
60044445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
60054445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
60064445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
60074445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
60084445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
60094445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
60104445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
60114445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
60124445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
60134445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
60144445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
60154445fffbSMatthew Ahrens 
60164445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
60174445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
60184445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
60194445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
60204445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
60214445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
60224445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
60234445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
60244445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
60254445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
60264445fffbSMatthew Ahrens 
60274445fffbSMatthew Ahrens 	/*
60284445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
60294445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
60304445fffbSMatthew Ahrens 	 * does the logging of those commands.
60314445fffbSMatthew Ahrens 	 */
60324445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
60334445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
60344445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
60354445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
60364445fffbSMatthew Ahrens 
60374445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
60384445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
60394445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
60404445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
60414445fffbSMatthew Ahrens 
60424445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
60434445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
60444445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
60454445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
60464445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
60474445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
60484445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
60494445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
60504445fffbSMatthew Ahrens 
60514445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
60524445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
60534445fffbSMatthew Ahrens 
60544445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6055f4c1745bSloli 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
60564445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
60574445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
60584445fffbSMatthew Ahrens 
60594445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
60604445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
60614445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
60624445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
60634445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
60644445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
60654445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
60664445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
60674445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
60684445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
60694445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
60704445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
60714445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
60724445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
60734445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
60744445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
60754445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
60764445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
60774445fffbSMatthew Ahrens 
60784445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
60794445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
60804445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
60814445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
60824445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
60834445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
60844445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
60854445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
60864445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
60874445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
60884445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
60894445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
60904445fffbSMatthew Ahrens 
60914445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
60924445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
60934445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
60944445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
60954445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
60964445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
60974445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
60984445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
60994445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
61004445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
61014445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
61024445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
61034445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
61044445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
61054445fffbSMatthew Ahrens 
61064445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
61074445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
61084445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
61094445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
61104445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
61114445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
61124445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
61134445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
61144445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
61154445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
61164445fffbSMatthew Ahrens }
6117fa9e4066Sahrens 
611854d692b7SGeorge Wilson int
6119f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6120f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
612154d692b7SGeorge Wilson {
612254d692b7SGeorge Wilson 	spa_t *spa;
612354d692b7SGeorge Wilson 	int error;
612454d692b7SGeorge Wilson 
612554d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
612654d692b7SGeorge Wilson 
6127f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
6128f9af39baSGeorge Wilson 		return (0);
6129f9af39baSGeorge Wilson 
613014843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
613154d692b7SGeorge Wilson 	if (error == 0) {
6132f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6133be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
6134f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6135be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
613654d692b7SGeorge Wilson 		spa_close(spa, FTAG);
613754d692b7SGeorge Wilson 	}
613854d692b7SGeorge Wilson 	return (error);
613954d692b7SGeorge Wilson }
614054d692b7SGeorge Wilson 
6141c99e4bdcSChris Kirby /*
6142c99e4bdcSChris Kirby  * Find a free minor number.
6143c99e4bdcSChris Kirby  */
6144c99e4bdcSChris Kirby minor_t
6145c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
6146c99e4bdcSChris Kirby {
6147c99e4bdcSChris Kirby 	static minor_t last_minor;
6148c99e4bdcSChris Kirby 	minor_t m;
6149c99e4bdcSChris Kirby 
6150c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6151c99e4bdcSChris Kirby 
6152c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
6153c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
6154c99e4bdcSChris Kirby 			m = 1;
6155c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6156c99e4bdcSChris Kirby 			last_minor = m;
6157c99e4bdcSChris Kirby 			return (m);
6158c99e4bdcSChris Kirby 		}
6159c99e4bdcSChris Kirby 	}
6160c99e4bdcSChris Kirby 
6161c99e4bdcSChris Kirby 	return (0);
6162c99e4bdcSChris Kirby }
6163c99e4bdcSChris Kirby 
6164c99e4bdcSChris Kirby static int
6165c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
6166c99e4bdcSChris Kirby {
6167c99e4bdcSChris Kirby 	minor_t minor;
6168c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
6169c99e4bdcSChris Kirby 
6170c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6171c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
6172c99e4bdcSChris Kirby 
6173c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
6174c99e4bdcSChris Kirby 	if (minor == 0)
6175be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
6176c99e4bdcSChris Kirby 
6177c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6178be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
6179c99e4bdcSChris Kirby 
6180c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
6181c99e4bdcSChris Kirby 
6182c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
6183c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
6184c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6185c99e4bdcSChris Kirby 
6186c99e4bdcSChris Kirby 	return (0);
6187c99e4bdcSChris Kirby }
6188c99e4bdcSChris Kirby 
6189c99e4bdcSChris Kirby static void
6190c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6191c99e4bdcSChris Kirby {
6192c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6193c99e4bdcSChris Kirby 
6194c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
6195c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
6196c99e4bdcSChris Kirby }
6197c99e4bdcSChris Kirby 
6198c99e4bdcSChris Kirby void *
6199c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6200c99e4bdcSChris Kirby {
6201c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
6202c99e4bdcSChris Kirby 
6203c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
6204c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
6205c99e4bdcSChris Kirby 		return (NULL);
6206c99e4bdcSChris Kirby 
6207c99e4bdcSChris Kirby 	return (zp->zss_data);
6208c99e4bdcSChris Kirby }
6209c99e4bdcSChris Kirby 
6210c99e4bdcSChris Kirby static int
6211c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6212c99e4bdcSChris Kirby {
6213c99e4bdcSChris Kirby 	int error = 0;
6214c99e4bdcSChris Kirby 
6215c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
6216c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
6217c99e4bdcSChris Kirby 
6218c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
6219c99e4bdcSChris Kirby 	if (flag & FEXCL) {
6220c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
6221c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
6222c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
6223c99e4bdcSChris Kirby 	}
6224c99e4bdcSChris Kirby 
6225c99e4bdcSChris Kirby 	return (error);
6226c99e4bdcSChris Kirby }
6227c99e4bdcSChris Kirby 
6228c99e4bdcSChris Kirby static int
6229c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6230c99e4bdcSChris Kirby {
6231c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
6232c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
6233c99e4bdcSChris Kirby 
6234c99e4bdcSChris Kirby 	if (minor == 0)
6235c99e4bdcSChris Kirby 		return (0);
6236c99e4bdcSChris Kirby 
6237c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
6238c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6239c99e4bdcSChris Kirby 	if (zo == NULL) {
6240c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
6241c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
6242c99e4bdcSChris Kirby 	}
6243c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
6244c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
6245c99e4bdcSChris Kirby 
6246c99e4bdcSChris Kirby 	return (0);
6247c99e4bdcSChris Kirby }
6248c99e4bdcSChris Kirby 
6249fa9e4066Sahrens static int
6250fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6251fa9e4066Sahrens {
6252fa9e4066Sahrens 	zfs_cmd_t *zc;
62534445fffbSMatthew Ahrens 	uint_t vecnum;
62544445fffbSMatthew Ahrens 	int error, rc, len;
6255c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
62564445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
62574445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
62584445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
6259fa9e4066Sahrens 
6260c99e4bdcSChris Kirby 	if (minor != 0 &&
6261c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6262fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6263fa9e4066Sahrens 
62644445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
626591ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6266fa9e4066Sahrens 
62674445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6268be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
62694445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
6270fa9e4066Sahrens 
6271fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6272fa9e4066Sahrens 
6273478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
62744445fffbSMatthew Ahrens 	if (error != 0) {
6275be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
62764445fffbSMatthew Ahrens 		goto out;
62774445fffbSMatthew Ahrens 	}
6278fa9e4066Sahrens 
62794445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
62804445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
62814445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
62824445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
62834445fffbSMatthew Ahrens 		if (error != 0)
62844445fffbSMatthew Ahrens 			goto out;
62854445fffbSMatthew Ahrens 	}
6286fa9e4066Sahrens 
6287fa9e4066Sahrens 	/*
6288fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
6289fa9e4066Sahrens 	 * the lower layers.
6290fa9e4066Sahrens 	 */
62914445fffbSMatthew Ahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
62924445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
62934445fffbSMatthew Ahrens 	case POOL_NAME:
62944445fffbSMatthew Ahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6295be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
62964445fffbSMatthew Ahrens 		else
6297f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
62984445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
62994445fffbSMatthew Ahrens 		break;
6300fa9e4066Sahrens 
63014445fffbSMatthew Ahrens 	case DATASET_NAME:
63024445fffbSMatthew Ahrens 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6303be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
63044445fffbSMatthew Ahrens 		else
6305f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
63064445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
63074445fffbSMatthew Ahrens 		break;
63085ad82045Snd 
63094445fffbSMatthew Ahrens 	case NO_NAME:
63104445fffbSMatthew Ahrens 		break;
6311fa9e4066Sahrens 	}
6312fa9e4066Sahrens 
6313fa9e4066Sahrens 
631445b17475SAlex Wilson 	if (error == 0)
63154445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
63164445fffbSMatthew Ahrens 
63174445fffbSMatthew Ahrens 	if (error != 0)
63184445fffbSMatthew Ahrens 		goto out;
63194445fffbSMatthew Ahrens 
63204445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
632178f17100SMatthew Ahrens 	len = strcspn(zc->zc_name, "/@#") + 1;
63224445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
63234445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
63244445fffbSMatthew Ahrens 
63254445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
63264445fffbSMatthew Ahrens 		nvlist_t *outnvl;
63274445fffbSMatthew Ahrens 		int puterror = 0;
63284445fffbSMatthew Ahrens 		spa_t *spa;
63294445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
63304445fffbSMatthew Ahrens 
63314445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
63324445fffbSMatthew Ahrens 
63334445fffbSMatthew Ahrens 		/*
63344445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
63354445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
63364445fffbSMatthew Ahrens 		 */
63374445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
63384445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
63394445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
63404445fffbSMatthew Ahrens 			    vec->zvec_name);
63414445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
63424445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
63434445fffbSMatthew Ahrens 				    innvl);
63444445fffbSMatthew Ahrens 			}
63454445fffbSMatthew Ahrens 		}
63464445fffbSMatthew Ahrens 
63474445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
63484445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
63494445fffbSMatthew Ahrens 
6350dfc11533SChris Williamson 		/*
6351d0cb1fb9SDon Brady 		 * Some commands can partially execute, modify state, and still
6352dfc11533SChris Williamson 		 * return an error.  In these cases, attempt to record what
6353dfc11533SChris Williamson 		 * was modified.
6354dfc11533SChris Williamson 		 */
6355dfc11533SChris Williamson 		if ((error == 0 ||
6356dfc11533SChris Williamson 		    (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6357dfc11533SChris Williamson 		    vec->zvec_allow_log &&
63584445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
63594445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
63604445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
63614445fffbSMatthew Ahrens 				    outnvl);
63624445fffbSMatthew Ahrens 			}
6363dfc11533SChris Williamson 			if (error != 0) {
6364dfc11533SChris Williamson 				fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6365dfc11533SChris Williamson 				    error);
6366dfc11533SChris Williamson 			}
63674445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
63684445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
63694445fffbSMatthew Ahrens 		}
63704445fffbSMatthew Ahrens 		fnvlist_free(lognv);
63714445fffbSMatthew Ahrens 
63724445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
63734445fffbSMatthew Ahrens 			int smusherror = 0;
63744445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
63754445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
63764445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
63774445fffbSMatthew Ahrens 			}
63784445fffbSMatthew Ahrens 			if (smusherror == 0)
63794445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
63804445fffbSMatthew Ahrens 		}
63814445fffbSMatthew Ahrens 
63824445fffbSMatthew Ahrens 		if (puterror != 0)
63834445fffbSMatthew Ahrens 			error = puterror;
63844445fffbSMatthew Ahrens 
63854445fffbSMatthew Ahrens 		nvlist_free(outnvl);
63864445fffbSMatthew Ahrens 	} else {
63874445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
63884445fffbSMatthew Ahrens 	}
63894445fffbSMatthew Ahrens 
63904445fffbSMatthew Ahrens out:
63914445fffbSMatthew Ahrens 	nvlist_free(innvl);
6392478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
63934445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
6394be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
63954445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
63964445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
63974445fffbSMatthew Ahrens 		if (s != NULL)
63984445fffbSMatthew Ahrens 			strfree(s);
63994445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
64004445fffbSMatthew Ahrens 	} else {
64014445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
64024445fffbSMatthew Ahrens 			strfree(saved_poolname);
6403ecd6cf80Smarks 	}
6404fa9e4066Sahrens 
6405fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
6406fa9e4066Sahrens 	return (error);
6407fa9e4066Sahrens }
6408fa9e4066Sahrens 
6409fa9e4066Sahrens static int
6410fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6411fa9e4066Sahrens {
6412fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
6413fa9e4066Sahrens 		return (DDI_FAILURE);
6414fa9e4066Sahrens 
6415fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6416fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
6417fa9e4066Sahrens 		return (DDI_FAILURE);
6418fa9e4066Sahrens 
6419fa9e4066Sahrens 	zfs_dip = dip;
6420fa9e4066Sahrens 
6421fa9e4066Sahrens 	ddi_report_dev(dip);
6422fa9e4066Sahrens 
6423fa9e4066Sahrens 	return (DDI_SUCCESS);
6424fa9e4066Sahrens }
6425fa9e4066Sahrens 
6426fa9e4066Sahrens static int
6427fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6428fa9e4066Sahrens {
6429fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
6430fa9e4066Sahrens 		return (DDI_FAILURE);
6431fa9e4066Sahrens 
6432fa9e4066Sahrens 	if (cmd != DDI_DETACH)
6433fa9e4066Sahrens 		return (DDI_FAILURE);
6434fa9e4066Sahrens 
6435fa9e4066Sahrens 	zfs_dip = NULL;
6436fa9e4066Sahrens 
6437fa9e4066Sahrens 	ddi_prop_remove_all(dip);
6438fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
6439fa9e4066Sahrens 
6440fa9e4066Sahrens 	return (DDI_SUCCESS);
6441fa9e4066Sahrens }
6442fa9e4066Sahrens 
6443fa9e4066Sahrens /*ARGSUSED*/
6444fa9e4066Sahrens static int
6445fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6446fa9e4066Sahrens {
6447fa9e4066Sahrens 	switch (infocmd) {
6448fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
6449fa9e4066Sahrens 		*result = zfs_dip;
6450fa9e4066Sahrens 		return (DDI_SUCCESS);
6451fa9e4066Sahrens 
6452fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
6453a0965f35Sbonwick 		*result = (void *)0;
6454fa9e4066Sahrens 		return (DDI_SUCCESS);
6455fa9e4066Sahrens 	}
6456fa9e4066Sahrens 
6457fa9e4066Sahrens 	return (DDI_FAILURE);
6458fa9e4066Sahrens }
6459fa9e4066Sahrens 
6460fa9e4066Sahrens /*
6461fa9e4066Sahrens  * OK, so this is a little weird.
6462fa9e4066Sahrens  *
6463fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
6464fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6465fa9e4066Sahrens  *
6466fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
6467fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
6468fa9e4066Sahrens  */
6469fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
6470c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
6471c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
6472fa9e4066Sahrens 	zvol_strategy,	/* strategy */
6473fa9e4066Sahrens 	nodev,		/* print */
6474e7cbe64fSgw 	zvol_dump,	/* dump */
6475fa9e4066Sahrens 	zvol_read,	/* read */
6476fa9e4066Sahrens 	zvol_write,	/* write */
6477fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
6478fa9e4066Sahrens 	nodev,		/* devmap */
6479fa9e4066Sahrens 	nodev,		/* mmap */
6480fa9e4066Sahrens 	nodev,		/* segmap */
6481fa9e4066Sahrens 	nochpoll,	/* poll */
6482fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
6483fa9e4066Sahrens 	NULL,		/* streamtab */
6484fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
6485fa9e4066Sahrens 	CB_REV,		/* version */
6486feb08c6bSbillm 	nodev,		/* async read */
6487feb08c6bSbillm 	nodev,		/* async write */
6488fa9e4066Sahrens };
6489fa9e4066Sahrens 
6490fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
6491fa9e4066Sahrens 	DEVO_REV,	/* version */
6492fa9e4066Sahrens 	0,		/* refcnt */
6493fa9e4066Sahrens 	zfs_info,	/* info */
6494fa9e4066Sahrens 	nulldev,	/* identify */
6495fa9e4066Sahrens 	nulldev,	/* probe */
6496fa9e4066Sahrens 	zfs_attach,	/* attach */
6497fa9e4066Sahrens 	zfs_detach,	/* detach */
6498fa9e4066Sahrens 	nodev,		/* reset */
6499fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
650019397407SSherry Moore 	NULL,		/* no bus operations */
650119397407SSherry Moore 	NULL,		/* power */
650219397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
6503fa9e4066Sahrens };
6504fa9e4066Sahrens 
6505fa9e4066Sahrens static struct modldrv zfs_modldrv = {
650619397407SSherry Moore 	&mod_driverops,
650719397407SSherry Moore 	"ZFS storage pool",
650819397407SSherry Moore 	&zfs_dev_ops
6509fa9e4066Sahrens };
6510fa9e4066Sahrens 
6511fa9e4066Sahrens static struct modlinkage modlinkage = {
6512fa9e4066Sahrens 	MODREV_1,
6513fa9e4066Sahrens 	(void *)&zfs_modlfs,
6514fa9e4066Sahrens 	(void *)&zfs_modldrv,
6515fa9e4066Sahrens 	NULL
6516fa9e4066Sahrens };
6517fa9e4066Sahrens 
65184445fffbSMatthew Ahrens static void
65194445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
65204445fffbSMatthew Ahrens {
65214445fffbSMatthew Ahrens 	char *poolname = arg;
65224445fffbSMatthew Ahrens 	strfree(poolname);
65234445fffbSMatthew Ahrens }
6524ec533521Sfr 
6525fa9e4066Sahrens int
6526fa9e4066Sahrens _init(void)
6527fa9e4066Sahrens {
6528fa9e4066Sahrens 	int error;
6529fa9e4066Sahrens 
6530a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
6531a0965f35Sbonwick 	zfs_init();
6532a0965f35Sbonwick 	zvol_init();
65334445fffbSMatthew Ahrens 	zfs_ioctl_init();
6534a0965f35Sbonwick 
6535a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
6536a0965f35Sbonwick 		zvol_fini();
6537a0965f35Sbonwick 		zfs_fini();
6538a0965f35Sbonwick 		spa_fini();
6539fa9e4066Sahrens 		return (error);
6540a0965f35Sbonwick 	}
6541fa9e4066Sahrens 
6542ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
65434445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
65444445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6545ec533521Sfr 
6546fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6547fa9e4066Sahrens 	ASSERT(error == 0);
6548ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6549fa9e4066Sahrens 
6550fa9e4066Sahrens 	return (0);
6551fa9e4066Sahrens }
6552fa9e4066Sahrens 
6553fa9e4066Sahrens int
6554fa9e4066Sahrens _fini(void)
6555fa9e4066Sahrens {
6556fa9e4066Sahrens 	int error;
6557fa9e4066Sahrens 
6558ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6559be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6560fa9e4066Sahrens 
6561fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
6562fa9e4066Sahrens 		return (error);
6563fa9e4066Sahrens 
6564fa9e4066Sahrens 	zvol_fini();
6565fa9e4066Sahrens 	zfs_fini();
6566fa9e4066Sahrens 	spa_fini();
6567da6c28aaSamw 	if (zfs_nfsshare_inited)
6568ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
6569da6c28aaSamw 	if (zfs_smbshare_inited)
6570da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
6571da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
6572ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
6573fa9e4066Sahrens 
6574ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
6575fa9e4066Sahrens 	ldi_ident_release(zfs_li);
6576fa9e4066Sahrens 	zfs_li = NULL;
6577ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
6578fa9e4066Sahrens 
6579fa9e4066Sahrens 	return (error);
6580fa9e4066Sahrens }
6581fa9e4066Sahrens 
6582fa9e4066Sahrens int
6583fa9e4066Sahrens _info(struct modinfo *modinfop)
6584fa9e4066Sahrens {
6585fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
6586fa9e4066Sahrens }
6587