xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision f67950b2)
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.
276ccda740Sloli  * Copyright 2016 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>
346ccda740Sloli  * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
35a4b8c9aaSAndrew Stormont  * Copyright 2017 RackTop Systems.
36eb633035STom Caputi  * Copyright (c) 2017, Datto, Inc. All rights reserved.
374445fffbSMatthew Ahrens  */
384445fffbSMatthew Ahrens 
394445fffbSMatthew Ahrens /*
404445fffbSMatthew Ahrens  * ZFS ioctls.
414445fffbSMatthew Ahrens  *
424445fffbSMatthew Ahrens  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
434445fffbSMatthew Ahrens  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
444445fffbSMatthew Ahrens  *
454445fffbSMatthew Ahrens  * There are two ways that we handle ioctls: the legacy way where almost
464445fffbSMatthew Ahrens  * all of the logic is in the ioctl callback, and the new way where most
474445fffbSMatthew Ahrens  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
484445fffbSMatthew Ahrens  *
494445fffbSMatthew Ahrens  * Non-legacy ioctls should be registered by calling
504445fffbSMatthew Ahrens  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
514445fffbSMatthew Ahrens  * from userland by lzc_ioctl().
524445fffbSMatthew Ahrens  *
534445fffbSMatthew Ahrens  * The registration arguments are as follows:
544445fffbSMatthew Ahrens  *
554445fffbSMatthew Ahrens  * const char *name
564445fffbSMatthew Ahrens  *   The name of the ioctl.  This is used for history logging.  If the
574445fffbSMatthew Ahrens  *   ioctl returns successfully (the callback returns 0), and allow_log
584445fffbSMatthew Ahrens  *   is true, then a history log entry will be recorded with the input &
594445fffbSMatthew Ahrens  *   output nvlists.  The log entry can be printed with "zpool history -i".
604445fffbSMatthew Ahrens  *
614445fffbSMatthew Ahrens  * zfs_ioc_t ioc
624445fffbSMatthew Ahrens  *   The ioctl request number, which userland will pass to ioctl(2).
634445fffbSMatthew Ahrens  *   The ioctl numbers can change from release to release, because
644445fffbSMatthew Ahrens  *   the caller (libzfs) must be matched to the kernel.
654445fffbSMatthew Ahrens  *
664445fffbSMatthew Ahrens  * zfs_secpolicy_func_t *secpolicy
674445fffbSMatthew Ahrens  *   This function will be called before the zfs_ioc_func_t, to
684445fffbSMatthew Ahrens  *   determine if this operation is permitted.  It should return EPERM
694445fffbSMatthew Ahrens  *   on failure, and 0 on success.  Checks include determining if the
704445fffbSMatthew Ahrens  *   dataset is visible in this zone, and if the user has either all
714445fffbSMatthew Ahrens  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
724445fffbSMatthew Ahrens  *   to do this operation on this dataset with "zfs allow".
734445fffbSMatthew Ahrens  *
744445fffbSMatthew Ahrens  * zfs_ioc_namecheck_t namecheck
754445fffbSMatthew Ahrens  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
764445fffbSMatthew Ahrens  *   name, a dataset name, or nothing.  If the name is not well-formed,
774445fffbSMatthew Ahrens  *   the ioctl will fail and the callback will not be called.
784445fffbSMatthew Ahrens  *   Therefore, the callback can assume that the name is well-formed
794445fffbSMatthew Ahrens  *   (e.g. is null-terminated, doesn't have more than one '@' character,
804445fffbSMatthew Ahrens  *   doesn't have invalid characters).
814445fffbSMatthew Ahrens  *
824445fffbSMatthew Ahrens  * zfs_ioc_poolcheck_t pool_check
834445fffbSMatthew Ahrens  *   This specifies requirements on the pool state.  If the pool does
844445fffbSMatthew Ahrens  *   not meet them (is suspended or is readonly), the ioctl will fail
854445fffbSMatthew Ahrens  *   and the callback will not be called.  If any checks are specified
864445fffbSMatthew Ahrens  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
874445fffbSMatthew Ahrens  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
884445fffbSMatthew Ahrens  *   POOL_CHECK_READONLY).
894445fffbSMatthew Ahrens  *
904445fffbSMatthew Ahrens  * boolean_t smush_outnvlist
914445fffbSMatthew Ahrens  *   If smush_outnvlist is true, then the output is presumed to be a
924445fffbSMatthew Ahrens  *   list of errors, and it will be "smushed" down to fit into the
934445fffbSMatthew Ahrens  *   caller's buffer, by removing some entries and replacing them with a
944445fffbSMatthew Ahrens  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
954445fffbSMatthew Ahrens  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
964445fffbSMatthew Ahrens  *   outnvlist does not fit into the userland-provided buffer, then the
974445fffbSMatthew Ahrens  *   ioctl will fail with ENOMEM.
984445fffbSMatthew Ahrens  *
994445fffbSMatthew Ahrens  * zfs_ioc_func_t *func
1004445fffbSMatthew Ahrens  *   The callback function that will perform the operation.
1014445fffbSMatthew Ahrens  *
1024445fffbSMatthew Ahrens  *   The callback should return 0 on success, or an error number on
1034445fffbSMatthew Ahrens  *   failure.  If the function fails, the userland ioctl will return -1,
1044445fffbSMatthew Ahrens  *   and errno will be set to the callback's return value.  The callback
1054445fffbSMatthew Ahrens  *   will be called with the following arguments:
1064445fffbSMatthew Ahrens  *
1074445fffbSMatthew Ahrens  *   const char *name
1084445fffbSMatthew Ahrens  *     The name of the pool or dataset to operate on, from
1094445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
1104445fffbSMatthew Ahrens  *     expected type (pool, dataset, or none).
1114445fffbSMatthew Ahrens  *
1124445fffbSMatthew Ahrens  *   nvlist_t *innvl
1134445fffbSMatthew Ahrens  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
1144445fffbSMatthew Ahrens  *     NULL if no input nvlist was provided.  Changes to this nvlist are
1154445fffbSMatthew Ahrens  *     ignored.  If the input nvlist could not be deserialized, the
1164445fffbSMatthew Ahrens  *     ioctl will fail and the callback will not be called.
1174445fffbSMatthew Ahrens  *
1184445fffbSMatthew Ahrens  *   nvlist_t *outnvl
1194445fffbSMatthew Ahrens  *     The output nvlist, initially empty.  The callback can fill it in,
1204445fffbSMatthew Ahrens  *     and it will be returned to userland by serializing it into
1214445fffbSMatthew Ahrens  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
1224445fffbSMatthew Ahrens  *     fails (e.g. because the caller didn't supply a large enough
1234445fffbSMatthew Ahrens  *     buffer), then the overall ioctl will fail.  See the
1244445fffbSMatthew Ahrens  *     'smush_nvlist' argument above for additional behaviors.
1254445fffbSMatthew Ahrens  *
1264445fffbSMatthew Ahrens  *     There are two typical uses of the output nvlist:
1274445fffbSMatthew Ahrens  *       - To return state, e.g. property values.  In this case,
1284445fffbSMatthew Ahrens  *         smush_outnvlist should be false.  If the buffer was not large
1294445fffbSMatthew Ahrens  *         enough, the caller will reallocate a larger buffer and try
1304445fffbSMatthew Ahrens  *         the ioctl again.
1314445fffbSMatthew Ahrens  *
1324445fffbSMatthew Ahrens  *       - To return multiple errors from an ioctl which makes on-disk
1334445fffbSMatthew Ahrens  *         changes.  In this case, smush_outnvlist should be true.
1344445fffbSMatthew Ahrens  *         Ioctls which make on-disk modifications should generally not
1354445fffbSMatthew Ahrens  *         use the outnvl if they succeed, because the caller can not
1364445fffbSMatthew Ahrens  *         distinguish between the operation failing, and
1374445fffbSMatthew Ahrens  *         deserialization failing.
138e9103aaeSGarrett D'Amore  */
139fa9e4066Sahrens 
140fa9e4066Sahrens #include <sys/types.h>
141fa9e4066Sahrens #include <sys/param.h>
142fa9e4066Sahrens #include <sys/errno.h>
143fa9e4066Sahrens #include <sys/uio.h>
144fa9e4066Sahrens #include <sys/buf.h>
145fa9e4066Sahrens #include <sys/modctl.h>
146fa9e4066Sahrens #include <sys/open.h>
147fa9e4066Sahrens #include <sys/file.h>
148fa9e4066Sahrens #include <sys/kmem.h>
149fa9e4066Sahrens #include <sys/conf.h>
150fa9e4066Sahrens #include <sys/cmn_err.h>
151fa9e4066Sahrens #include <sys/stat.h>
152fa9e4066Sahrens #include <sys/zfs_ioctl.h>
1534201a95eSRic Aleshire #include <sys/zfs_vfsops.h>
154da6c28aaSamw #include <sys/zfs_znode.h>
155fa9e4066Sahrens #include <sys/zap.h>
156fa9e4066Sahrens #include <sys/spa.h>
157b1b8ab34Slling #include <sys/spa_impl.h>
158fa9e4066Sahrens #include <sys/vdev.h>
1594201a95eSRic Aleshire #include <sys/priv_impl.h>
160fa9e4066Sahrens #include <sys/dmu.h>
161fa9e4066Sahrens #include <sys/dsl_dir.h>
162fa9e4066Sahrens #include <sys/dsl_dataset.h>
163fa9e4066Sahrens #include <sys/dsl_prop.h>
164ecd6cf80Smarks #include <sys/dsl_deleg.h>
165ecd6cf80Smarks #include <sys/dmu_objset.h>
1664e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
1673b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
168fa9e4066Sahrens #include <sys/ddi.h>
169fa9e4066Sahrens #include <sys/sunddi.h>
170fa9e4066Sahrens #include <sys/sunldi.h>
171fa9e4066Sahrens #include <sys/policy.h>
172fa9e4066Sahrens #include <sys/zone.h>
173fa9e4066Sahrens #include <sys/nvpair.h>
174fa9e4066Sahrens #include <sys/pathname.h>
175fa9e4066Sahrens #include <sys/mount.h>
176fa9e4066Sahrens #include <sys/sdt.h>
177fa9e4066Sahrens #include <sys/fs/zfs.h>
178fa9e4066Sahrens #include <sys/zfs_ctldir.h>
179da6c28aaSamw #include <sys/zfs_dir.h>
180c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
181a2eea2e1Sahrens #include <sys/zvol.h>
1823f9d6ad7SLin Ling #include <sys/dsl_scan.h>
183ecd6cf80Smarks #include <sharefs/share.h>
184f18faf3fSek #include <sys/dmu_objset.h>
1850fa1b3ccSPaul Dagnelie #include <sys/dmu_recv.h>
1863b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
1873b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
18878f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
1893b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
190a6f561b4SSašo Kiselkov #include <sys/zfeature.h>
191dfc11533SChris Williamson #include <sys/zcp.h>
19245818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
1935cabbc6bSPrashanth Sreenivasa #include <sys/vdev_removal.h>
194094e47e9SGeorge Wilson #include <sys/vdev_impl.h>
195094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
196eb633035STom Caputi #include <sys/dsl_crypt.h>
197fa9e4066Sahrens 
198fa9e4066Sahrens #include "zfs_namecheck.h"
199e9dbad6fSeschrock #include "zfs_prop.h"
200ecd6cf80Smarks #include "zfs_deleg.h"
2010a586ceaSMark Shellenbaum #include "zfs_comutil.h"
202fa9e4066Sahrens 
203dfc11533SChris Williamson #include "lua.h"
204dfc11533SChris Williamson #include "lauxlib.h"
205dfc11533SChris Williamson 
206fa9e4066Sahrens extern struct modlfs zfs_modlfs;
207fa9e4066Sahrens 
208fa9e4066Sahrens extern void zfs_init(void);
209fa9e4066Sahrens extern void zfs_fini(void);
210fa9e4066Sahrens 
211fa9e4066Sahrens ldi_ident_t zfs_li = NULL;
212fa9e4066Sahrens dev_info_t *zfs_dip;
213fa9e4066Sahrens 
2144445fffbSMatthew Ahrens uint_t zfs_fsyncer_key;
2154445fffbSMatthew Ahrens extern uint_t rrw_tsd_key;
2164445fffbSMatthew Ahrens static uint_t zfs_allow_log_key;
2174445fffbSMatthew Ahrens 
2184445fffbSMatthew Ahrens typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
2194445fffbSMatthew Ahrens typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
2204445fffbSMatthew Ahrens typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
221fa9e4066Sahrens 
22254d692b7SGeorge Wilson typedef enum {
22354d692b7SGeorge Wilson 	NO_NAME,
22454d692b7SGeorge Wilson 	POOL_NAME,
22554d692b7SGeorge Wilson 	DATASET_NAME
22654d692b7SGeorge Wilson } zfs_ioc_namecheck_t;
22754d692b7SGeorge Wilson 
228f9af39baSGeorge Wilson typedef enum {
229f9af39baSGeorge Wilson 	POOL_CHECK_NONE		= 1 << 0,
230f9af39baSGeorge Wilson 	POOL_CHECK_SUSPENDED	= 1 << 1,
2314445fffbSMatthew Ahrens 	POOL_CHECK_READONLY	= 1 << 2,
232f9af39baSGeorge Wilson } zfs_ioc_poolcheck_t;
233f9af39baSGeorge Wilson 
234fa9e4066Sahrens typedef struct zfs_ioc_vec {
2354445fffbSMatthew Ahrens 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
236fa9e4066Sahrens 	zfs_ioc_func_t		*zvec_func;
237fa9e4066Sahrens 	zfs_secpolicy_func_t	*zvec_secpolicy;
23854d692b7SGeorge Wilson 	zfs_ioc_namecheck_t	zvec_namecheck;
2394445fffbSMatthew Ahrens 	boolean_t		zvec_allow_log;
240f9af39baSGeorge Wilson 	zfs_ioc_poolcheck_t	zvec_pool_check;
2414445fffbSMatthew Ahrens 	boolean_t		zvec_smush_outnvlist;
2424445fffbSMatthew Ahrens 	const char		*zvec_name;
243fa9e4066Sahrens } zfs_ioc_vec_t;
244fa9e4066Sahrens 
24514843421SMatthew Ahrens /* This array is indexed by zfs_userquota_prop_t */
24614843421SMatthew Ahrens static const char *userquota_perms[] = {
24714843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERUSED,
24814843421SMatthew Ahrens 	ZFS_DELEG_PERM_USERQUOTA,
24914843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPUSED,
25014843421SMatthew Ahrens 	ZFS_DELEG_PERM_GROUPQUOTA,
251*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_USEROBJUSED,
252*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_USEROBJQUOTA,
253*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_GROUPOBJUSED,
254*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_GROUPOBJQUOTA,
255*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_PROJECTUSED,
256*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_PROJECTQUOTA,
257*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_PROJECTOBJUSED,
258*f67950b2SNasf-Fan 	ZFS_DELEG_PERM_PROJECTOBJQUOTA,
25914843421SMatthew Ahrens };
26014843421SMatthew Ahrens 
26114843421SMatthew Ahrens static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
262*f67950b2SNasf-Fan static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
26392241e0bSTom Erickson static int zfs_check_settable(const char *name, nvpair_t *property,
26492241e0bSTom Erickson     cred_t *cr);
26592241e0bSTom Erickson static int zfs_check_clearable(char *dataset, nvlist_t *props,
26692241e0bSTom Erickson     nvlist_t **errors);
2670a48a24eStimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
2680a48a24eStimh     boolean_t *);
2694445fffbSMatthew Ahrens int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
2704445fffbSMatthew Ahrens static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
2710a48a24eStimh 
2722acef22dSMatthew Ahrens static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
273a6f561b4SSašo Kiselkov 
274fa9e4066Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
275fa9e4066Sahrens void
276fa9e4066Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
277fa9e4066Sahrens {
278fa9e4066Sahrens 	const char *newfile;
2793f9d6ad7SLin Ling 	char buf[512];
280fa9e4066Sahrens 	va_list adx;
281fa9e4066Sahrens 
282fa9e4066Sahrens 	/*
283fa9e4066Sahrens 	 * Get rid of annoying "../common/" prefix to filename.
284fa9e4066Sahrens 	 */
285fa9e4066Sahrens 	newfile = strrchr(file, '/');
286fa9e4066Sahrens 	if (newfile != NULL) {
287fa9e4066Sahrens 		newfile = newfile + 1; /* Get rid of leading / */
288fa9e4066Sahrens 	} else {
289fa9e4066Sahrens 		newfile = file;
290fa9e4066Sahrens 	}
291fa9e4066Sahrens 
292fa9e4066Sahrens 	va_start(adx, fmt);
293fa9e4066Sahrens 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
294fa9e4066Sahrens 	va_end(adx);
295fa9e4066Sahrens 
296fa9e4066Sahrens 	/*
297fa9e4066Sahrens 	 * To get this data, use the zfs-dprintf probe as so:
298fa9e4066Sahrens 	 * dtrace -q -n 'zfs-dprintf \
299fa9e4066Sahrens 	 *	/stringof(arg0) == "dbuf.c"/ \
300fa9e4066Sahrens 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
301fa9e4066Sahrens 	 * arg0 = file name
302fa9e4066Sahrens 	 * arg1 = function name
303fa9e4066Sahrens 	 * arg2 = line number
304fa9e4066Sahrens 	 * arg3 = message
305fa9e4066Sahrens 	 */
306fa9e4066Sahrens 	DTRACE_PROBE4(zfs__dprintf,
307fa9e4066Sahrens 	    char *, newfile, char *, func, int, line, char *, buf);
308fa9e4066Sahrens }
309fa9e4066Sahrens 
310ecd6cf80Smarks static void
311228975ccSek history_str_free(char *buf)
312228975ccSek {
313228975ccSek 	kmem_free(buf, HIS_MAX_RECORD_LEN);
314228975ccSek }
315228975ccSek 
316228975ccSek static char *
317228975ccSek history_str_get(zfs_cmd_t *zc)
318ecd6cf80Smarks {
31940feaa91Sahrens 	char *buf;
320ecd6cf80Smarks 
321dd328bf6SToomas Soome 	if (zc->zc_history == 0)
322228975ccSek 		return (NULL);
323e7437265Sahrens 
324ecd6cf80Smarks 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
325ecd6cf80Smarks 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
326ecd6cf80Smarks 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
327228975ccSek 		history_str_free(buf);
328228975ccSek 		return (NULL);
329ecd6cf80Smarks 	}
330ecd6cf80Smarks 
331ecd6cf80Smarks 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
332ecd6cf80Smarks 
333228975ccSek 	return (buf);
334228975ccSek }
335ecd6cf80Smarks 
33615e6edf1Sgw /*
33715e6edf1Sgw  * Check to see if the named dataset is currently defined as bootable
33815e6edf1Sgw  */
33915e6edf1Sgw static boolean_t
34015e6edf1Sgw zfs_is_bootfs(const char *name)
34115e6edf1Sgw {
342503ad85cSMatthew Ahrens 	objset_t *os;
34315e6edf1Sgw 
344503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
345503ad85cSMatthew Ahrens 		boolean_t ret;
346b24ab676SJeff Bonwick 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
347503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
348503ad85cSMatthew Ahrens 		return (ret);
34915e6edf1Sgw 	}
350503ad85cSMatthew Ahrens 	return (B_FALSE);
35115e6edf1Sgw }
35215e6edf1Sgw 
353c2a93d44Stimh /*
354f7170741SWill Andrews  * Return non-zero if the spa version is less than requested version.
355c2a93d44Stimh  */
356da6c28aaSamw static int
3570a48a24eStimh zfs_earlier_version(const char *name, int version)
358da6c28aaSamw {
359da6c28aaSamw 	spa_t *spa;
360da6c28aaSamw 
361da6c28aaSamw 	if (spa_open(name, &spa, FTAG) == 0) {
362da6c28aaSamw 		if (spa_version(spa) < version) {
363da6c28aaSamw 			spa_close(spa, FTAG);
364da6c28aaSamw 			return (1);
365da6c28aaSamw 		}
366da6c28aaSamw 		spa_close(spa, FTAG);
367da6c28aaSamw 	}
368da6c28aaSamw 	return (0);
369da6c28aaSamw }
370da6c28aaSamw 
3719e6eda55Smarks /*
372745cd3c5Smaybee  * Return TRUE if the ZPL version is less than requested version.
3739e6eda55Smarks  */
374745cd3c5Smaybee static boolean_t
375745cd3c5Smaybee zpl_earlier_version(const char *name, int version)
3769e6eda55Smarks {
3779e6eda55Smarks 	objset_t *os;
378745cd3c5Smaybee 	boolean_t rc = B_TRUE;
3799e6eda55Smarks 
380503ad85cSMatthew Ahrens 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
381745cd3c5Smaybee 		uint64_t zplversion;
3829e6eda55Smarks 
383503ad85cSMatthew Ahrens 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
384503ad85cSMatthew Ahrens 			dmu_objset_rele(os, FTAG);
385503ad85cSMatthew Ahrens 			return (B_TRUE);
386503ad85cSMatthew Ahrens 		}
387503ad85cSMatthew Ahrens 		/* XXX reading from non-owned objset */
388745cd3c5Smaybee 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
389745cd3c5Smaybee 			rc = zplversion < version;
390503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3919e6eda55Smarks 	}
3929e6eda55Smarks 	return (rc);
3939e6eda55Smarks }
3949e6eda55Smarks 
395228975ccSek static void
396228975ccSek zfs_log_history(zfs_cmd_t *zc)
397228975ccSek {
398228975ccSek 	spa_t *spa;
399228975ccSek 	char *buf;
400ecd6cf80Smarks 
401228975ccSek 	if ((buf = history_str_get(zc)) == NULL)
402228975ccSek 		return;
403228975ccSek 
404228975ccSek 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
405228975ccSek 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
4064445fffbSMatthew Ahrens 			(void) spa_history_log(spa, buf);
407228975ccSek 		spa_close(spa, FTAG);
408228975ccSek 	}
409228975ccSek 	history_str_free(buf);
410ecd6cf80Smarks }
411ecd6cf80Smarks 
412fa9e4066Sahrens /*
413fa9e4066Sahrens  * Policy for top-level read operations (list pools).  Requires no privileges,
414fa9e4066Sahrens  * and can be used in the local zone, as there is no associated dataset.
415fa9e4066Sahrens  */
416fa9e4066Sahrens /* ARGSUSED */
417fa9e4066Sahrens static int
4184445fffbSMatthew Ahrens zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
419fa9e4066Sahrens {
420fa9e4066Sahrens 	return (0);
421fa9e4066Sahrens }
422fa9e4066Sahrens 
423fa9e4066Sahrens /*
424fa9e4066Sahrens  * Policy for dataset read operations (list children, get statistics).  Requires
425fa9e4066Sahrens  * no privileges, but must be visible in the local zone.
426fa9e4066Sahrens  */
427fa9e4066Sahrens /* ARGSUSED */
428fa9e4066Sahrens static int
4294445fffbSMatthew Ahrens zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
430fa9e4066Sahrens {
431fa9e4066Sahrens 	if (INGLOBALZONE(curproc) ||
432ecd6cf80Smarks 	    zone_dataset_visible(zc->zc_name, NULL))
433fa9e4066Sahrens 		return (0);
434fa9e4066Sahrens 
435be6fd75aSMatthew Ahrens 	return (SET_ERROR(ENOENT));
436fa9e4066Sahrens }
437fa9e4066Sahrens 
438fa9e4066Sahrens static int
439a7f53a56SChris Kirby zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
440fa9e4066Sahrens {
441fa9e4066Sahrens 	int writable = 1;
442fa9e4066Sahrens 
443fa9e4066Sahrens 	/*
444fa9e4066Sahrens 	 * The dataset must be visible by this zone -- check this first
445fa9e4066Sahrens 	 * so they don't see EPERM on something they shouldn't know about.
446fa9e4066Sahrens 	 */
447fa9e4066Sahrens 	if (!INGLOBALZONE(curproc) &&
448fa9e4066Sahrens 	    !zone_dataset_visible(dataset, &writable))
449be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
450fa9e4066Sahrens 
451fa9e4066Sahrens 	if (INGLOBALZONE(curproc)) {
452fa9e4066Sahrens 		/*
453fa9e4066Sahrens 		 * If the fs is zoned, only root can access it from the
454fa9e4066Sahrens 		 * global zone.
455fa9e4066Sahrens 		 */
456fa9e4066Sahrens 		if (secpolicy_zfs(cr) && zoned)
457be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
458fa9e4066Sahrens 	} else {
459fa9e4066Sahrens 		/*
460fa9e4066Sahrens 		 * If we are in a local zone, the 'zoned' property must be set.
461fa9e4066Sahrens 		 */
462fa9e4066Sahrens 		if (!zoned)
463be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
464fa9e4066Sahrens 
465fa9e4066Sahrens 		/* must be writable by this zone */
466fa9e4066Sahrens 		if (!writable)
467be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
468fa9e4066Sahrens 	}
469fa9e4066Sahrens 	return (0);
470fa9e4066Sahrens }
471fa9e4066Sahrens 
472a7f53a56SChris Kirby static int
473a7f53a56SChris Kirby zfs_dozonecheck(const char *dataset, cred_t *cr)
474a7f53a56SChris Kirby {
475a7f53a56SChris Kirby 	uint64_t zoned;
476a7f53a56SChris Kirby 
477a7f53a56SChris Kirby 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
478be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
479a7f53a56SChris Kirby 
480a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
481a7f53a56SChris Kirby }
482a7f53a56SChris Kirby 
483a7f53a56SChris Kirby static int
484a7f53a56SChris Kirby zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
485a7f53a56SChris Kirby {
486a7f53a56SChris Kirby 	uint64_t zoned;
487a7f53a56SChris Kirby 
4883b2aab18SMatthew Ahrens 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
489be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
490a7f53a56SChris Kirby 
491a7f53a56SChris Kirby 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
492a7f53a56SChris Kirby }
493a7f53a56SChris Kirby 
4944445fffbSMatthew Ahrens static int
4953b2aab18SMatthew Ahrens zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
4963b2aab18SMatthew Ahrens     const char *perm, cred_t *cr)
497fa9e4066Sahrens {
498fa9e4066Sahrens 	int error;
499fa9e4066Sahrens 
50019b94df9SMatthew Ahrens 	error = zfs_dozonecheck_ds(name, ds, cr);
501ecd6cf80Smarks 	if (error == 0) {
502ecd6cf80Smarks 		error = secpolicy_zfs(cr);
5033b2aab18SMatthew Ahrens 		if (error != 0)
5044445fffbSMatthew Ahrens 			error = dsl_deleg_access_impl(ds, perm, cr);
505ecd6cf80Smarks 	}
506ecd6cf80Smarks 	return (error);
507ecd6cf80Smarks }
508ecd6cf80Smarks 
5094445fffbSMatthew Ahrens static int
5103b2aab18SMatthew Ahrens zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
511a7f53a56SChris Kirby {
512a7f53a56SChris Kirby 	int error;
5133b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
5143b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
515a7f53a56SChris Kirby 
51625f7d993SMatthew Ahrens 	/*
51725f7d993SMatthew Ahrens 	 * First do a quick check for root in the global zone, which
51825f7d993SMatthew Ahrens 	 * is allowed to do all write_perms.  This ensures that zfs_ioc_*
51925f7d993SMatthew Ahrens 	 * will get to handle nonexistent datasets.
52025f7d993SMatthew Ahrens 	 */
52125f7d993SMatthew Ahrens 	if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
52225f7d993SMatthew Ahrens 		return (0);
52325f7d993SMatthew Ahrens 
5243b2aab18SMatthew Ahrens 	error = dsl_pool_hold(name, FTAG, &dp);
5253b2aab18SMatthew Ahrens 	if (error != 0)
5263b2aab18SMatthew Ahrens 		return (error);
5273b2aab18SMatthew Ahrens 
5283b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
5293b2aab18SMatthew Ahrens 	if (error != 0) {
5303b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5313b2aab18SMatthew Ahrens 		return (error);
532a7f53a56SChris Kirby 	}
5333b2aab18SMatthew Ahrens 
5343b2aab18SMatthew Ahrens 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
5353b2aab18SMatthew Ahrens 
5363b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
5373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
538a7f53a56SChris Kirby 	return (error);
539a7f53a56SChris Kirby }
540a7f53a56SChris Kirby 
5414201a95eSRic Aleshire /*
5424201a95eSRic Aleshire  * Policy for setting the security label property.
5434201a95eSRic Aleshire  *
5444201a95eSRic Aleshire  * Returns 0 for success, non-zero for access and other errors.
5454201a95eSRic Aleshire  */
5464201a95eSRic Aleshire static int
54792241e0bSTom Erickson zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
5484201a95eSRic Aleshire {
5494201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
5504201a95eSRic Aleshire 	bslabel_t	ds_sl, new_sl;
5514201a95eSRic Aleshire 	boolean_t	new_default = FALSE;
5524201a95eSRic Aleshire 	uint64_t	zoned;
5534201a95eSRic Aleshire 	int		needed_priv = -1;
5544201a95eSRic Aleshire 	int		error;
5554201a95eSRic Aleshire 
5564201a95eSRic Aleshire 	/* First get the existing dataset label. */
5574201a95eSRic Aleshire 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
5584201a95eSRic Aleshire 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
5593b2aab18SMatthew Ahrens 	if (error != 0)
560be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5614201a95eSRic Aleshire 
5624201a95eSRic Aleshire 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
5634201a95eSRic Aleshire 		new_default = TRUE;
5644201a95eSRic Aleshire 
5654201a95eSRic Aleshire 	/* The label must be translatable */
5664201a95eSRic Aleshire 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
567be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5684201a95eSRic Aleshire 
5694201a95eSRic Aleshire 	/*
5704201a95eSRic Aleshire 	 * In a non-global zone, disallow attempts to set a label that
5714201a95eSRic Aleshire 	 * doesn't match that of the zone; otherwise no other checks
5724201a95eSRic Aleshire 	 * are needed.
5734201a95eSRic Aleshire 	 */
5744201a95eSRic Aleshire 	if (!INGLOBALZONE(curproc)) {
5754201a95eSRic Aleshire 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
576be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5774201a95eSRic Aleshire 		return (0);
5784201a95eSRic Aleshire 	}
5794201a95eSRic Aleshire 
5804201a95eSRic Aleshire 	/*
5814201a95eSRic Aleshire 	 * For global-zone datasets (i.e., those whose zoned property is
5824201a95eSRic Aleshire 	 * "off", verify that the specified new label is valid for the
5834201a95eSRic Aleshire 	 * global zone.
5844201a95eSRic Aleshire 	 */
5854201a95eSRic Aleshire 	if (dsl_prop_get_integer(name,
5864201a95eSRic Aleshire 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
587be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
5884201a95eSRic Aleshire 	if (!zoned) {
5894201a95eSRic Aleshire 		if (zfs_check_global_label(name, strval) != 0)
590be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
5914201a95eSRic Aleshire 	}
5924201a95eSRic Aleshire 
5934201a95eSRic Aleshire 	/*
5944201a95eSRic Aleshire 	 * If the existing dataset label is nondefault, check if the
5954201a95eSRic Aleshire 	 * dataset is mounted (label cannot be changed while mounted).
5964201a95eSRic Aleshire 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
5974201a95eSRic Aleshire 	 * mounted (or isn't a dataset, doesn't exist, ...).
5984201a95eSRic Aleshire 	 */
5994201a95eSRic Aleshire 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
60092241e0bSTom Erickson 		objset_t *os;
60192241e0bSTom Erickson 		static char *setsl_tag = "setsl_tag";
60292241e0bSTom Erickson 
6034201a95eSRic Aleshire 		/*
6044201a95eSRic Aleshire 		 * Try to own the dataset; abort if there is any error,
6054201a95eSRic Aleshire 		 * (e.g., already mounted, in use, or other error).
6064201a95eSRic Aleshire 		 */
607eb633035STom Caputi 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
60892241e0bSTom Erickson 		    setsl_tag, &os);
6093b2aab18SMatthew Ahrens 		if (error != 0)
610be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
6114201a95eSRic Aleshire 
612eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, setsl_tag);
61392241e0bSTom Erickson 
6144201a95eSRic Aleshire 		if (new_default) {
6154201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
6164201a95eSRic Aleshire 			goto out_check;
6174201a95eSRic Aleshire 		}
6184201a95eSRic Aleshire 
6194201a95eSRic Aleshire 		if (hexstr_to_label(strval, &new_sl) != 0)
620be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
6214201a95eSRic Aleshire 
6224201a95eSRic Aleshire 		if (blstrictdom(&ds_sl, &new_sl))
6234201a95eSRic Aleshire 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
6244201a95eSRic Aleshire 		else if (blstrictdom(&new_sl, &ds_sl))
6254201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6264201a95eSRic Aleshire 	} else {
6274201a95eSRic Aleshire 		/* dataset currently has a default label */
6284201a95eSRic Aleshire 		if (!new_default)
6294201a95eSRic Aleshire 			needed_priv = PRIV_FILE_UPGRADE_SL;
6304201a95eSRic Aleshire 	}
6314201a95eSRic Aleshire 
6324201a95eSRic Aleshire out_check:
6334201a95eSRic Aleshire 	if (needed_priv != -1)
6344201a95eSRic Aleshire 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
6354201a95eSRic Aleshire 	return (0);
6364201a95eSRic Aleshire }
6374201a95eSRic Aleshire 
638ecd6cf80Smarks static int
63992241e0bSTom Erickson zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
64092241e0bSTom Erickson     cred_t *cr)
641ecd6cf80Smarks {
64292241e0bSTom Erickson 	char *strval;
64392241e0bSTom Erickson 
644ecd6cf80Smarks 	/*
645ecd6cf80Smarks 	 * Check permissions for special properties.
646ecd6cf80Smarks 	 */
647ecd6cf80Smarks 	switch (prop) {
648ecd6cf80Smarks 	case ZFS_PROP_ZONED:
649ecd6cf80Smarks 		/*
650ecd6cf80Smarks 		 * Disallow setting of 'zoned' from within a local zone.
651ecd6cf80Smarks 		 */
652ecd6cf80Smarks 		if (!INGLOBALZONE(curproc))
653be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
654ecd6cf80Smarks 		break;
655ecd6cf80Smarks 
656ecd6cf80Smarks 	case ZFS_PROP_QUOTA:
657a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
658a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
659ecd6cf80Smarks 		if (!INGLOBALZONE(curproc)) {
660ecd6cf80Smarks 			uint64_t zoned;
6619adfa60dSMatthew Ahrens 			char setpoint[ZFS_MAX_DATASET_NAME_LEN];
662ecd6cf80Smarks 			/*
663ecd6cf80Smarks 			 * Unprivileged users are allowed to modify the
664a2afb611SJerry Jelinek 			 * limit on things *under* (ie. contained by)
665ecd6cf80Smarks 			 * the thing they own.
666ecd6cf80Smarks 			 */
66792241e0bSTom Erickson 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
668ecd6cf80Smarks 			    setpoint))
669be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
67092241e0bSTom Erickson 			if (!zoned || strlen(dsname) <= strlen(setpoint))
671be6fd75aSMatthew Ahrens 				return (SET_ERROR(EPERM));
672ecd6cf80Smarks 		}
673db870a07Sahrens 		break;
6744201a95eSRic Aleshire 
6754201a95eSRic Aleshire 	case ZFS_PROP_MLSLABEL:
6764201a95eSRic Aleshire 		if (!is_system_labeled())
677be6fd75aSMatthew Ahrens 			return (SET_ERROR(EPERM));
67892241e0bSTom Erickson 
67992241e0bSTom Erickson 		if (nvpair_value_string(propval, &strval) == 0) {
68092241e0bSTom Erickson 			int err;
68192241e0bSTom Erickson 
68292241e0bSTom Erickson 			err = zfs_set_slabel_policy(dsname, strval, CRED());
68392241e0bSTom Erickson 			if (err != 0)
68492241e0bSTom Erickson 				return (err);
68592241e0bSTom Erickson 		}
6864201a95eSRic Aleshire 		break;
687ecd6cf80Smarks 	}
688ecd6cf80Smarks 
68992241e0bSTom Erickson 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
690ecd6cf80Smarks }
691ecd6cf80Smarks 
6924445fffbSMatthew Ahrens /* ARGSUSED */
6934445fffbSMatthew Ahrens static int
6944445fffbSMatthew Ahrens zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
695ecd6cf80Smarks {
696ecd6cf80Smarks 	int error;
697ecd6cf80Smarks 
698ecd6cf80Smarks 	error = zfs_dozonecheck(zc->zc_name, cr);
6993b2aab18SMatthew Ahrens 	if (error != 0)
700fa9e4066Sahrens 		return (error);
701fa9e4066Sahrens 
702ecd6cf80Smarks 	/*
703ecd6cf80Smarks 	 * permission to set permissions will be evaluated later in
704ecd6cf80Smarks 	 * dsl_deleg_can_allow()
705ecd6cf80Smarks 	 */
706ecd6cf80Smarks 	return (0);
707ecd6cf80Smarks }
708ecd6cf80Smarks 
7094445fffbSMatthew Ahrens /* ARGSUSED */
7104445fffbSMatthew Ahrens static int
7114445fffbSMatthew Ahrens zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
712ecd6cf80Smarks {
713681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(zc->zc_name,
714681d9761SEric Taylor 	    ZFS_DELEG_PERM_ROLLBACK, cr));
715ecd6cf80Smarks }
716ecd6cf80Smarks 
7174445fffbSMatthew Ahrens /* ARGSUSED */
7184445fffbSMatthew Ahrens static int
7194445fffbSMatthew Ahrens zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
720ecd6cf80Smarks {
721a7f53a56SChris Kirby 	dsl_pool_t *dp;
722a7f53a56SChris Kirby 	dsl_dataset_t *ds;
723a7f53a56SChris Kirby 	char *cp;
724a7f53a56SChris Kirby 	int error;
725a7f53a56SChris Kirby 
726a7f53a56SChris Kirby 	/*
727a7f53a56SChris Kirby 	 * Generate the current snapshot name from the given objsetid, then
728a7f53a56SChris Kirby 	 * use that name for the secpolicy/zone checks.
729a7f53a56SChris Kirby 	 */
730a7f53a56SChris Kirby 	cp = strchr(zc->zc_name, '@');
731a7f53a56SChris Kirby 	if (cp == NULL)
732be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7333b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
7343b2aab18SMatthew Ahrens 	if (error != 0)
735a7f53a56SChris Kirby 		return (error);
736a7f53a56SChris Kirby 
737a7f53a56SChris Kirby 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
7383b2aab18SMatthew Ahrens 	if (error != 0) {
7393b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
740a7f53a56SChris Kirby 		return (error);
7413b2aab18SMatthew Ahrens 	}
742a7f53a56SChris Kirby 
743a7f53a56SChris Kirby 	dsl_dataset_name(ds, zc->zc_name);
744a7f53a56SChris Kirby 
745a7f53a56SChris Kirby 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
746a7f53a56SChris Kirby 	    ZFS_DELEG_PERM_SEND, cr);
747a7f53a56SChris Kirby 	dsl_dataset_rele(ds, FTAG);
7483b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
749a7f53a56SChris Kirby 
750a7f53a56SChris Kirby 	return (error);
751ecd6cf80Smarks }
752ecd6cf80Smarks 
7534445fffbSMatthew Ahrens /* ARGSUSED */
754743a77edSAlan Wright static int
7554445fffbSMatthew Ahrens zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
7564445fffbSMatthew Ahrens {
7574445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
7584445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_SEND, cr));
7594445fffbSMatthew Ahrens }
7604445fffbSMatthew Ahrens 
7614445fffbSMatthew Ahrens /* ARGSUSED */
7624445fffbSMatthew Ahrens static int
7634445fffbSMatthew Ahrens zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
764743a77edSAlan Wright {
765743a77edSAlan Wright 	vnode_t *vp;
766743a77edSAlan Wright 	int error;
767743a77edSAlan Wright 
768743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
769743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
770743a77edSAlan Wright 		return (error);
771743a77edSAlan Wright 
772743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
773743a77edSAlan Wright 
774743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
775743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
776743a77edSAlan Wright 	    zc->zc_name) != 0)) {
777743a77edSAlan Wright 		VN_RELE(vp);
778be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
779743a77edSAlan Wright 	}
780743a77edSAlan Wright 
781743a77edSAlan Wright 	VN_RELE(vp);
782743a77edSAlan Wright 	return (dsl_deleg_access(zc->zc_name,
783743a77edSAlan Wright 	    ZFS_DELEG_PERM_SHARE, cr));
784743a77edSAlan Wright }
785743a77edSAlan Wright 
786ecd6cf80Smarks int
7874445fffbSMatthew Ahrens zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
788ecd6cf80Smarks {
789ecd6cf80Smarks 	if (!INGLOBALZONE(curproc))
790be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
791ecd6cf80Smarks 
7923cb34c60Sahrens 	if (secpolicy_nfs(cr) == 0) {
793ecd6cf80Smarks 		return (0);
794ecd6cf80Smarks 	} else {
7954445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
796743a77edSAlan Wright 	}
797743a77edSAlan Wright }
798ecd6cf80Smarks 
799743a77edSAlan Wright int
8004445fffbSMatthew Ahrens zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
801743a77edSAlan Wright {
802743a77edSAlan Wright 	if (!INGLOBALZONE(curproc))
803be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
804ecd6cf80Smarks 
805743a77edSAlan Wright 	if (secpolicy_smb(cr) == 0) {
806743a77edSAlan Wright 		return (0);
807743a77edSAlan Wright 	} else {
8084445fffbSMatthew Ahrens 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
809ecd6cf80Smarks 	}
810fa9e4066Sahrens }
811fa9e4066Sahrens 
812fa9e4066Sahrens static int
813ecd6cf80Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize)
814fa9e4066Sahrens {
815fa9e4066Sahrens 	char *cp;
816fa9e4066Sahrens 
817fa9e4066Sahrens 	/*
818fa9e4066Sahrens 	 * Remove the @bla or /bla from the end of the name to get the parent.
819fa9e4066Sahrens 	 */
820ecd6cf80Smarks 	(void) strncpy(parent, datasetname, parentsize);
821ecd6cf80Smarks 	cp = strrchr(parent, '@');
822fa9e4066Sahrens 	if (cp != NULL) {
823fa9e4066Sahrens 		cp[0] = '\0';
824fa9e4066Sahrens 	} else {
825ecd6cf80Smarks 		cp = strrchr(parent, '/');
826fa9e4066Sahrens 		if (cp == NULL)
827be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
828fa9e4066Sahrens 		cp[0] = '\0';
829ecd6cf80Smarks 	}
830ecd6cf80Smarks 
831ecd6cf80Smarks 	return (0);
832ecd6cf80Smarks }
833ecd6cf80Smarks 
834ecd6cf80Smarks int
835ecd6cf80Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
836ecd6cf80Smarks {
837ecd6cf80Smarks 	int error;
838ecd6cf80Smarks 
839ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(name,
840ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
841ecd6cf80Smarks 		return (error);
842ecd6cf80Smarks 
843ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
844ecd6cf80Smarks }
845ecd6cf80Smarks 
8464445fffbSMatthew Ahrens /* ARGSUSED */
847ecd6cf80Smarks static int
8484445fffbSMatthew Ahrens zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
849ecd6cf80Smarks {
850ecd6cf80Smarks 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
851ecd6cf80Smarks }
852ecd6cf80Smarks 
853cbf6f6aaSWilliam Gorrell /*
854cbf6f6aaSWilliam Gorrell  * Destroying snapshots with delegated permissions requires
8554445fffbSMatthew Ahrens  * descendant mount and destroy permissions.
856cbf6f6aaSWilliam Gorrell  */
8574445fffbSMatthew Ahrens /* ARGSUSED */
858cbf6f6aaSWilliam Gorrell static int
8594445fffbSMatthew Ahrens zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
860cbf6f6aaSWilliam Gorrell {
8614445fffbSMatthew Ahrens 	nvlist_t *snaps;
8624445fffbSMatthew Ahrens 	nvpair_t *pair, *nextpair;
8634445fffbSMatthew Ahrens 	int error = 0;
864cbf6f6aaSWilliam Gorrell 
8654445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
866be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8674445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
8684445fffbSMatthew Ahrens 	    pair = nextpair) {
8694445fffbSMatthew Ahrens 		nextpair = nvlist_next_nvpair(snaps, pair);
87078f17100SMatthew Ahrens 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
87178f17100SMatthew Ahrens 		if (error == ENOENT) {
8724445fffbSMatthew Ahrens 			/*
8734445fffbSMatthew Ahrens 			 * Ignore any snapshots that don't exist (we consider
8744445fffbSMatthew Ahrens 			 * them "already destroyed").  Remove the name from the
8754445fffbSMatthew Ahrens 			 * nvl here in case the snapshot is created between
8764445fffbSMatthew Ahrens 			 * now and when we try to destroy it (in which case
8774445fffbSMatthew Ahrens 			 * we don't want to destroy it since we haven't
8784445fffbSMatthew Ahrens 			 * checked for permission).
8794445fffbSMatthew Ahrens 			 */
8804445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(snaps, pair);
8814445fffbSMatthew Ahrens 			error = 0;
8824445fffbSMatthew Ahrens 		}
8834445fffbSMatthew Ahrens 		if (error != 0)
8844445fffbSMatthew Ahrens 			break;
8854445fffbSMatthew Ahrens 	}
886cbf6f6aaSWilliam Gorrell 
887cbf6f6aaSWilliam Gorrell 	return (error);
888cbf6f6aaSWilliam Gorrell }
889cbf6f6aaSWilliam Gorrell 
890ecd6cf80Smarks int
891ecd6cf80Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
892ecd6cf80Smarks {
8939adfa60dSMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
894ecd6cf80Smarks 	int	error;
895ecd6cf80Smarks 
896ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
897ecd6cf80Smarks 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
898ecd6cf80Smarks 		return (error);
899ecd6cf80Smarks 
900ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(from,
901ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
902ecd6cf80Smarks 		return (error);
903ecd6cf80Smarks 
904ecd6cf80Smarks 	if ((error = zfs_get_parent(to, parentname,
905ecd6cf80Smarks 	    sizeof (parentname))) != 0)
906ecd6cf80Smarks 		return (error);
907ecd6cf80Smarks 
908ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
909ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
910ecd6cf80Smarks 		return (error);
911ecd6cf80Smarks 
912ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
913ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
914ecd6cf80Smarks 		return (error);
915ecd6cf80Smarks 
916ecd6cf80Smarks 	return (error);
917ecd6cf80Smarks }
918ecd6cf80Smarks 
9194445fffbSMatthew Ahrens /* ARGSUSED */
920ecd6cf80Smarks static int
9214445fffbSMatthew Ahrens zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
922ecd6cf80Smarks {
923ecd6cf80Smarks 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
924ecd6cf80Smarks }
925ecd6cf80Smarks 
9264445fffbSMatthew Ahrens /* ARGSUSED */
927ecd6cf80Smarks static int
9284445fffbSMatthew Ahrens zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
929ecd6cf80Smarks {
9303b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9313b2aab18SMatthew Ahrens 	dsl_dataset_t *clone;
932ecd6cf80Smarks 	int error;
933ecd6cf80Smarks 
934ecd6cf80Smarks 	error = zfs_secpolicy_write_perms(zc->zc_name,
935ecd6cf80Smarks 	    ZFS_DELEG_PERM_PROMOTE, cr);
9363b2aab18SMatthew Ahrens 	if (error != 0)
9373b2aab18SMatthew Ahrens 		return (error);
9383b2aab18SMatthew Ahrens 
9393b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
9403b2aab18SMatthew Ahrens 	if (error != 0)
941ecd6cf80Smarks 		return (error);
942ecd6cf80Smarks 
9433b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
944ecd6cf80Smarks 
945ecd6cf80Smarks 	if (error == 0) {
9469adfa60dSMatthew Ahrens 		char parentname[ZFS_MAX_DATASET_NAME_LEN];
9473b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
948ecd6cf80Smarks 		dsl_dir_t *dd;
9493b2aab18SMatthew Ahrens 		dd = clone->ds_dir;
950ecd6cf80Smarks 
951745cd3c5Smaybee 		error = dsl_dataset_hold_obj(dd->dd_pool,
952c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
9533b2aab18SMatthew Ahrens 		if (error != 0) {
9543b2aab18SMatthew Ahrens 			dsl_dataset_rele(clone, FTAG);
9553b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
956ecd6cf80Smarks 			return (error);
957ecd6cf80Smarks 		}
958ecd6cf80Smarks 
9593b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
960ecd6cf80Smarks 		    ZFS_DELEG_PERM_MOUNT, cr);
961ecd6cf80Smarks 
9623b2aab18SMatthew Ahrens 		dsl_dataset_name(origin, parentname);
9633b2aab18SMatthew Ahrens 		if (error == 0) {
9643b2aab18SMatthew Ahrens 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
965ecd6cf80Smarks 			    ZFS_DELEG_PERM_PROMOTE, cr);
9663b2aab18SMatthew Ahrens 		}
9673b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
9683b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin, FTAG);
969ecd6cf80Smarks 	}
9703b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
971ecd6cf80Smarks 	return (error);
972ecd6cf80Smarks }
973ecd6cf80Smarks 
9744445fffbSMatthew Ahrens /* ARGSUSED */
975ecd6cf80Smarks static int
9764445fffbSMatthew Ahrens zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
977ecd6cf80Smarks {
978ecd6cf80Smarks 	int error;
979ecd6cf80Smarks 
980ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
981ecd6cf80Smarks 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
982ecd6cf80Smarks 		return (error);
983ecd6cf80Smarks 
984ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
985ecd6cf80Smarks 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
986ecd6cf80Smarks 		return (error);
987ecd6cf80Smarks 
988ecd6cf80Smarks 	return (zfs_secpolicy_write_perms(zc->zc_name,
989ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr));
990ecd6cf80Smarks }
991ecd6cf80Smarks 
992ecd6cf80Smarks int
993ecd6cf80Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
994ecd6cf80Smarks {
995681d9761SEric Taylor 	return (zfs_secpolicy_write_perms(name,
996681d9761SEric Taylor 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
997ecd6cf80Smarks }
998ecd6cf80Smarks 
9994445fffbSMatthew Ahrens /*
10004445fffbSMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
10014445fffbSMatthew Ahrens  */
10024445fffbSMatthew Ahrens /* ARGSUSED */
1003ecd6cf80Smarks static int
10044445fffbSMatthew Ahrens zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1005ecd6cf80Smarks {
10064445fffbSMatthew Ahrens 	nvlist_t *snaps;
1007d5285caeSGeorge Wilson 	int error = 0;
10084445fffbSMatthew Ahrens 	nvpair_t *pair;
1009ecd6cf80Smarks 
10104445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
1011be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
10124445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
10134445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
10144445fffbSMatthew Ahrens 		char *name = nvpair_name(pair);
10154445fffbSMatthew Ahrens 		char *atp = strchr(name, '@');
10164445fffbSMatthew Ahrens 
10174445fffbSMatthew Ahrens 		if (atp == NULL) {
1018be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
10194445fffbSMatthew Ahrens 			break;
10204445fffbSMatthew Ahrens 		}
10214445fffbSMatthew Ahrens 		*atp = '\0';
10224445fffbSMatthew Ahrens 		error = zfs_secpolicy_snapshot_perms(name, cr);
10234445fffbSMatthew Ahrens 		*atp = '@';
10244445fffbSMatthew Ahrens 		if (error != 0)
10254445fffbSMatthew Ahrens 			break;
10264445fffbSMatthew Ahrens 	}
10274445fffbSMatthew Ahrens 	return (error);
1028ecd6cf80Smarks }
1029ecd6cf80Smarks 
103078f17100SMatthew Ahrens /*
103178f17100SMatthew Ahrens  * Check for permission to create each snapshot in the nvlist.
103278f17100SMatthew Ahrens  */
103378f17100SMatthew Ahrens /* ARGSUSED */
103478f17100SMatthew Ahrens static int
103578f17100SMatthew Ahrens zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
103678f17100SMatthew Ahrens {
103778f17100SMatthew Ahrens 	int error = 0;
103878f17100SMatthew Ahrens 
103978f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
104078f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
104178f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
104278f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
104378f17100SMatthew Ahrens 
104478f17100SMatthew Ahrens 		if (hashp == NULL) {
104578f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
104678f17100SMatthew Ahrens 			break;
104778f17100SMatthew Ahrens 		}
104878f17100SMatthew Ahrens 		*hashp = '\0';
104978f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
105078f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_BOOKMARK, cr);
105178f17100SMatthew Ahrens 		*hashp = '#';
105278f17100SMatthew Ahrens 		if (error != 0)
105378f17100SMatthew Ahrens 			break;
105478f17100SMatthew Ahrens 	}
105578f17100SMatthew Ahrens 	return (error);
105678f17100SMatthew Ahrens }
105778f17100SMatthew Ahrens 
10585cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
10595cabbc6bSPrashanth Sreenivasa static int
10605cabbc6bSPrashanth Sreenivasa zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10615cabbc6bSPrashanth Sreenivasa {
10625cabbc6bSPrashanth Sreenivasa 	return (zfs_secpolicy_write_perms(zc->zc_name,
10635cabbc6bSPrashanth Sreenivasa 	    ZFS_DELEG_PERM_REMAP, cr));
10645cabbc6bSPrashanth Sreenivasa }
10655cabbc6bSPrashanth Sreenivasa 
106678f17100SMatthew Ahrens /* ARGSUSED */
106778f17100SMatthew Ahrens static int
106878f17100SMatthew Ahrens zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
106978f17100SMatthew Ahrens {
107078f17100SMatthew Ahrens 	nvpair_t *pair, *nextpair;
107178f17100SMatthew Ahrens 	int error = 0;
107278f17100SMatthew Ahrens 
107378f17100SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
107478f17100SMatthew Ahrens 	    pair = nextpair) {
107578f17100SMatthew Ahrens 		char *name = nvpair_name(pair);
107678f17100SMatthew Ahrens 		char *hashp = strchr(name, '#');
107778f17100SMatthew Ahrens 		nextpair = nvlist_next_nvpair(innvl, pair);
107878f17100SMatthew Ahrens 
107978f17100SMatthew Ahrens 		if (hashp == NULL) {
108078f17100SMatthew Ahrens 			error = SET_ERROR(EINVAL);
108178f17100SMatthew Ahrens 			break;
108278f17100SMatthew Ahrens 		}
108378f17100SMatthew Ahrens 
108478f17100SMatthew Ahrens 		*hashp = '\0';
108578f17100SMatthew Ahrens 		error = zfs_secpolicy_write_perms(name,
108678f17100SMatthew Ahrens 		    ZFS_DELEG_PERM_DESTROY, cr);
108778f17100SMatthew Ahrens 		*hashp = '#';
108878f17100SMatthew Ahrens 		if (error == ENOENT) {
108978f17100SMatthew Ahrens 			/*
109078f17100SMatthew Ahrens 			 * Ignore any filesystems that don't exist (we consider
109178f17100SMatthew Ahrens 			 * their bookmarks "already destroyed").  Remove
109278f17100SMatthew Ahrens 			 * the name from the nvl here in case the filesystem
109378f17100SMatthew Ahrens 			 * is created between now and when we try to destroy
109478f17100SMatthew Ahrens 			 * the bookmark (in which case we don't want to
109578f17100SMatthew Ahrens 			 * destroy it since we haven't checked for permission).
109678f17100SMatthew Ahrens 			 */
109778f17100SMatthew Ahrens 			fnvlist_remove_nvpair(innvl, pair);
109878f17100SMatthew Ahrens 			error = 0;
109978f17100SMatthew Ahrens 		}
110078f17100SMatthew Ahrens 		if (error != 0)
110178f17100SMatthew Ahrens 			break;
110278f17100SMatthew Ahrens 	}
110378f17100SMatthew Ahrens 
110478f17100SMatthew Ahrens 	return (error);
110578f17100SMatthew Ahrens }
110678f17100SMatthew Ahrens 
11074445fffbSMatthew Ahrens /* ARGSUSED */
1108ecd6cf80Smarks static int
11094445fffbSMatthew Ahrens zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
11104445fffbSMatthew Ahrens {
11114445fffbSMatthew Ahrens 	/*
11124445fffbSMatthew Ahrens 	 * Even root must have a proper TSD so that we know what pool
11134445fffbSMatthew Ahrens 	 * to log to.
11144445fffbSMatthew Ahrens 	 */
11154445fffbSMatthew Ahrens 	if (tsd_get(zfs_allow_log_key) == NULL)
1116be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
11174445fffbSMatthew Ahrens 	return (0);
11184445fffbSMatthew Ahrens }
11194445fffbSMatthew Ahrens 
11204445fffbSMatthew Ahrens static int
11214445fffbSMatthew Ahrens zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1122ecd6cf80Smarks {
11239adfa60dSMatthew Ahrens 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
112492241e0bSTom Erickson 	int	error;
11254445fffbSMatthew Ahrens 	char	*origin;
1126ecd6cf80Smarks 
1127ecd6cf80Smarks 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1128ecd6cf80Smarks 	    sizeof (parentname))) != 0)
1129ecd6cf80Smarks 		return (error);
1130fa9e4066Sahrens 
11314445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
11324445fffbSMatthew Ahrens 	    (error = zfs_secpolicy_write_perms(origin,
11334445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
11344445fffbSMatthew Ahrens 		return (error);
1135fa9e4066Sahrens 
1136ecd6cf80Smarks 	if ((error = zfs_secpolicy_write_perms(parentname,
1137ecd6cf80Smarks 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1138ecd6cf80Smarks 		return (error);
1139ecd6cf80Smarks 
11404445fffbSMatthew Ahrens 	return (zfs_secpolicy_write_perms(parentname,
11414445fffbSMatthew Ahrens 	    ZFS_DELEG_PERM_MOUNT, cr));
1142fa9e4066Sahrens }
1143fa9e4066Sahrens 
1144fa9e4066Sahrens /*
1145fa9e4066Sahrens  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1146fa9e4066Sahrens  * SYS_CONFIG privilege, which is not available in a local zone.
1147fa9e4066Sahrens  */
1148fa9e4066Sahrens /* ARGSUSED */
1149fa9e4066Sahrens static int
11504445fffbSMatthew Ahrens zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1151fa9e4066Sahrens {
1152fa9e4066Sahrens 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1153be6fd75aSMatthew Ahrens 		return (SET_ERROR(EPERM));
1154fa9e4066Sahrens 
1155fa9e4066Sahrens 	return (0);
1156fa9e4066Sahrens }
1157fa9e4066Sahrens 
115899d5e173STim Haley /*
115999d5e173STim Haley  * Policy for object to name lookups.
116099d5e173STim Haley  */
116199d5e173STim Haley /* ARGSUSED */
116299d5e173STim Haley static int
11634445fffbSMatthew Ahrens zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
116499d5e173STim Haley {
116599d5e173STim Haley 	int error;
116699d5e173STim Haley 
116799d5e173STim Haley 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
116899d5e173STim Haley 		return (0);
116999d5e173STim Haley 
117099d5e173STim Haley 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
117199d5e173STim Haley 	return (error);
117299d5e173STim Haley }
117399d5e173STim Haley 
1174ea8dc4b6Seschrock /*
1175ea8dc4b6Seschrock  * Policy for fault injection.  Requires all privileges.
1176ea8dc4b6Seschrock  */
1177ea8dc4b6Seschrock /* ARGSUSED */
1178ea8dc4b6Seschrock static int
11794445fffbSMatthew Ahrens zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1180ea8dc4b6Seschrock {
1181ea8dc4b6Seschrock 	return (secpolicy_zinject(cr));
1182ea8dc4b6Seschrock }
1183ea8dc4b6Seschrock 
11844445fffbSMatthew Ahrens /* ARGSUSED */
1185e45ce728Sahrens static int
11864445fffbSMatthew Ahrens zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1187e45ce728Sahrens {
1188e45ce728Sahrens 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1189e45ce728Sahrens 
1190990b4856Slling 	if (prop == ZPROP_INVAL) {
1191e45ce728Sahrens 		if (!zfs_prop_user(zc->zc_value))
1192be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1193e45ce728Sahrens 		return (zfs_secpolicy_write_perms(zc->zc_name,
1194e45ce728Sahrens 		    ZFS_DELEG_PERM_USERPROP, cr));
1195e45ce728Sahrens 	} else {
119692241e0bSTom Erickson 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
119792241e0bSTom Erickson 		    NULL, cr));
1198e45ce728Sahrens 	}
1199e45ce728Sahrens }
1200e45ce728Sahrens 
120114843421SMatthew Ahrens static int
12024445fffbSMatthew Ahrens zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
120314843421SMatthew Ahrens {
12044445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
120514843421SMatthew Ahrens 	if (err)
120614843421SMatthew Ahrens 		return (err);
120714843421SMatthew Ahrens 
120814843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1209be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
121014843421SMatthew Ahrens 
121114843421SMatthew Ahrens 	if (zc->zc_value[0] == 0) {
121214843421SMatthew Ahrens 		/*
121314843421SMatthew Ahrens 		 * They are asking about a posix uid/gid.  If it's
121414843421SMatthew Ahrens 		 * themself, allow it.
121514843421SMatthew Ahrens 		 */
121614843421SMatthew Ahrens 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1217*f67950b2SNasf-Fan 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1218*f67950b2SNasf-Fan 		    zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1219*f67950b2SNasf-Fan 		    zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
122014843421SMatthew Ahrens 			if (zc->zc_guid == crgetuid(cr))
122114843421SMatthew Ahrens 				return (0);
1222*f67950b2SNasf-Fan 		} else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1223*f67950b2SNasf-Fan 		    zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1224*f67950b2SNasf-Fan 		    zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1225*f67950b2SNasf-Fan 		    zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
122614843421SMatthew Ahrens 			if (groupmember(zc->zc_guid, cr))
122714843421SMatthew Ahrens 				return (0);
122814843421SMatthew Ahrens 		}
1229*f67950b2SNasf-Fan 		/* else is for project quota/used */
123014843421SMatthew Ahrens 	}
123114843421SMatthew Ahrens 
123214843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
123314843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
123414843421SMatthew Ahrens }
123514843421SMatthew Ahrens 
123614843421SMatthew Ahrens static int
12374445fffbSMatthew Ahrens zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
123814843421SMatthew Ahrens {
12394445fffbSMatthew Ahrens 	int err = zfs_secpolicy_read(zc, innvl, cr);
124014843421SMatthew Ahrens 	if (err)
124114843421SMatthew Ahrens 		return (err);
124214843421SMatthew Ahrens 
124314843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1244be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
124514843421SMatthew Ahrens 
124614843421SMatthew Ahrens 	return (zfs_secpolicy_write_perms(zc->zc_name,
124714843421SMatthew Ahrens 	    userquota_perms[zc->zc_objset_type], cr));
124814843421SMatthew Ahrens }
124914843421SMatthew Ahrens 
12504445fffbSMatthew Ahrens /* ARGSUSED */
125114843421SMatthew Ahrens static int
12524445fffbSMatthew Ahrens zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
125314843421SMatthew Ahrens {
125492241e0bSTom Erickson 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
125592241e0bSTom Erickson 	    NULL, cr));
125614843421SMatthew Ahrens }
125714843421SMatthew Ahrens 
12584445fffbSMatthew Ahrens /* ARGSUSED */
1259842727c2SChris Kirby static int
12604445fffbSMatthew Ahrens zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1261842727c2SChris Kirby {
12623b2aab18SMatthew Ahrens 	nvpair_t *pair;
12633b2aab18SMatthew Ahrens 	nvlist_t *holds;
12643b2aab18SMatthew Ahrens 	int error;
12653b2aab18SMatthew Ahrens 
12663b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(innvl, "holds", &holds);
12673b2aab18SMatthew Ahrens 	if (error != 0)
1268be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12693b2aab18SMatthew Ahrens 
12703b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
12713b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(holds, pair)) {
12729adfa60dSMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12733b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12743b2aab18SMatthew Ahrens 		if (error != 0)
12753b2aab18SMatthew Ahrens 			return (error);
12763b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12773b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_HOLD, cr);
12783b2aab18SMatthew Ahrens 		if (error != 0)
12793b2aab18SMatthew Ahrens 			return (error);
12803b2aab18SMatthew Ahrens 	}
12813b2aab18SMatthew Ahrens 	return (0);
1282842727c2SChris Kirby }
1283842727c2SChris Kirby 
12844445fffbSMatthew Ahrens /* ARGSUSED */
1285842727c2SChris Kirby static int
12864445fffbSMatthew Ahrens zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1287842727c2SChris Kirby {
12883b2aab18SMatthew Ahrens 	nvpair_t *pair;
12893b2aab18SMatthew Ahrens 	int error;
12903b2aab18SMatthew Ahrens 
12913b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
12923b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(innvl, pair)) {
12939adfa60dSMatthew Ahrens 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
12943b2aab18SMatthew Ahrens 		error = dmu_fsname(nvpair_name(pair), fsname);
12953b2aab18SMatthew Ahrens 		if (error != 0)
12963b2aab18SMatthew Ahrens 			return (error);
12973b2aab18SMatthew Ahrens 		error = zfs_secpolicy_write_perms(fsname,
12983b2aab18SMatthew Ahrens 		    ZFS_DELEG_PERM_RELEASE, cr);
12993b2aab18SMatthew Ahrens 		if (error != 0)
13003b2aab18SMatthew Ahrens 			return (error);
13013b2aab18SMatthew Ahrens 	}
13023b2aab18SMatthew Ahrens 	return (0);
1303842727c2SChris Kirby }
1304842727c2SChris Kirby 
1305eb633035STom Caputi /* ARGSUSED */
1306eb633035STom Caputi static int
1307eb633035STom Caputi zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1308eb633035STom Caputi {
1309eb633035STom Caputi 	return (zfs_secpolicy_write_perms(zc->zc_name,
1310eb633035STom Caputi 	    ZFS_DELEG_PERM_LOAD_KEY, cr));
1311eb633035STom Caputi }
1312eb633035STom Caputi 
1313eb633035STom Caputi /* ARGSUSED */
1314eb633035STom Caputi static int
1315eb633035STom Caputi zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1316eb633035STom Caputi {
1317eb633035STom Caputi 	return (zfs_secpolicy_write_perms(zc->zc_name,
1318eb633035STom Caputi 	    ZFS_DELEG_PERM_CHANGE_KEY, cr));
1319eb633035STom Caputi }
1320eb633035STom Caputi 
132199d5e173STim Haley /*
132299d5e173STim Haley  * Policy for allowing temporary snapshots to be taken or released
132399d5e173STim Haley  */
132499d5e173STim Haley static int
13254445fffbSMatthew Ahrens zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
132699d5e173STim Haley {
132799d5e173STim Haley 	/*
132899d5e173STim Haley 	 * A temporary snapshot is the same as a snapshot,
132999d5e173STim Haley 	 * hold, destroy and release all rolled into one.
133099d5e173STim Haley 	 * Delegated diff alone is sufficient that we allow this.
133199d5e173STim Haley 	 */
133299d5e173STim Haley 	int error;
133399d5e173STim Haley 
133499d5e173STim Haley 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
133599d5e173STim Haley 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
133699d5e173STim Haley 		return (0);
133799d5e173STim Haley 
13384445fffbSMatthew Ahrens 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
13393b2aab18SMatthew Ahrens 	if (error == 0)
13404445fffbSMatthew Ahrens 		error = zfs_secpolicy_hold(zc, innvl, cr);
13413b2aab18SMatthew Ahrens 	if (error == 0)
13424445fffbSMatthew Ahrens 		error = zfs_secpolicy_release(zc, innvl, cr);
13433b2aab18SMatthew Ahrens 	if (error == 0)
13444445fffbSMatthew Ahrens 		error = zfs_secpolicy_destroy(zc, innvl, cr);
134599d5e173STim Haley 	return (error);
134699d5e173STim Haley }
134799d5e173STim Haley 
1348fa9e4066Sahrens /*
1349fa9e4066Sahrens  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1350fa9e4066Sahrens  */
1351fa9e4066Sahrens static int
1352478ed9adSEric Taylor get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1353fa9e4066Sahrens {
1354fa9e4066Sahrens 	char *packed;
1355fa9e4066Sahrens 	int error;
1356990b4856Slling 	nvlist_t *list = NULL;
1357fa9e4066Sahrens 
1358fa9e4066Sahrens 	/*
1359e9dbad6fSeschrock 	 * Read in and unpack the user-supplied nvlist.
1360fa9e4066Sahrens 	 */
1361990b4856Slling 	if (size == 0)
1362be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1363fa9e4066Sahrens 
1364fa9e4066Sahrens 	packed = kmem_alloc(size, KM_SLEEP);
1365fa9e4066Sahrens 
1366478ed9adSEric Taylor 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1367478ed9adSEric Taylor 	    iflag)) != 0) {
1368fa9e4066Sahrens 		kmem_free(packed, size);
1369c71c00bbSRichard Yao 		return (SET_ERROR(EFAULT));
1370fa9e4066Sahrens 	}
1371fa9e4066Sahrens 
1372990b4856Slling 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1373fa9e4066Sahrens 		kmem_free(packed, size);
1374fa9e4066Sahrens 		return (error);
1375fa9e4066Sahrens 	}
1376fa9e4066Sahrens 
1377fa9e4066Sahrens 	kmem_free(packed, size);
1378fa9e4066Sahrens 
1379990b4856Slling 	*nvp = list;
1380fa9e4066Sahrens 	return (0);
1381fa9e4066Sahrens }
1382fa9e4066Sahrens 
13834445fffbSMatthew Ahrens /*
13844445fffbSMatthew Ahrens  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
13854445fffbSMatthew Ahrens  * Entries will be removed from the end of the nvlist, and one int32 entry
13864445fffbSMatthew Ahrens  * named "N_MORE_ERRORS" will be added indicating how many entries were
13874445fffbSMatthew Ahrens  * removed.
13884445fffbSMatthew Ahrens  */
138992241e0bSTom Erickson static int
13904445fffbSMatthew Ahrens nvlist_smush(nvlist_t *errors, size_t max)
139192241e0bSTom Erickson {
139292241e0bSTom Erickson 	size_t size;
139392241e0bSTom Erickson 
13944445fffbSMatthew Ahrens 	size = fnvlist_size(errors);
139592241e0bSTom Erickson 
13964445fffbSMatthew Ahrens 	if (size > max) {
139792241e0bSTom Erickson 		nvpair_t *more_errors;
139892241e0bSTom Erickson 		int n = 0;
139992241e0bSTom Erickson 
14004445fffbSMatthew Ahrens 		if (max < 1024)
1401be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOMEM));
140292241e0bSTom Erickson 
14034445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
14044445fffbSMatthew Ahrens 		more_errors = nvlist_prev_nvpair(errors, NULL);
140592241e0bSTom Erickson 
140692241e0bSTom Erickson 		do {
14074445fffbSMatthew Ahrens 			nvpair_t *pair = nvlist_prev_nvpair(errors,
140892241e0bSTom Erickson 			    more_errors);
14094445fffbSMatthew Ahrens 			fnvlist_remove_nvpair(errors, pair);
141092241e0bSTom Erickson 			n++;
14114445fffbSMatthew Ahrens 			size = fnvlist_size(errors);
14124445fffbSMatthew Ahrens 		} while (size > max);
141392241e0bSTom Erickson 
14144445fffbSMatthew Ahrens 		fnvlist_remove_nvpair(errors, more_errors);
14154445fffbSMatthew Ahrens 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
14164445fffbSMatthew Ahrens 		ASSERT3U(fnvlist_size(errors), <=, max);
141792241e0bSTom Erickson 	}
141892241e0bSTom Erickson 
141992241e0bSTom Erickson 	return (0);
142092241e0bSTom Erickson }
142192241e0bSTom Erickson 
1422e9dbad6fSeschrock static int
1423e9dbad6fSeschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1424e9dbad6fSeschrock {
1425e9dbad6fSeschrock 	char *packed = NULL;
14266e27f868SSam Falkner 	int error = 0;
1427e9dbad6fSeschrock 	size_t size;
1428e9dbad6fSeschrock 
14294445fffbSMatthew Ahrens 	size = fnvlist_size(nvl);
1430e9dbad6fSeschrock 
1431e9dbad6fSeschrock 	if (size > zc->zc_nvlist_dst_size) {
1432be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOMEM);
1433e9dbad6fSeschrock 	} else {
14344445fffbSMatthew Ahrens 		packed = fnvlist_pack(nvl, &size);
14356e27f868SSam Falkner 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
14366e27f868SSam Falkner 		    size, zc->zc_iflags) != 0)
1437be6fd75aSMatthew Ahrens 			error = SET_ERROR(EFAULT);
14384445fffbSMatthew Ahrens 		fnvlist_pack_free(packed, size);
1439e9dbad6fSeschrock 	}
1440e9dbad6fSeschrock 
1441e9dbad6fSeschrock 	zc->zc_nvlist_dst_size = size;
14424445fffbSMatthew Ahrens 	zc->zc_nvlist_dst_filled = B_TRUE;
1443e9dbad6fSeschrock 	return (error);
1444e9dbad6fSeschrock }
1445e9dbad6fSeschrock 
1446dfc11533SChris Williamson int
1447dfc11533SChris Williamson getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
144814843421SMatthew Ahrens {
1449dfc11533SChris Williamson 	int error = 0;
1450503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1451be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1452503ad85cSMatthew Ahrens 	}
145314843421SMatthew Ahrens 
1454503ad85cSMatthew Ahrens 	mutex_enter(&os->os_user_ptr_lock);
1455af4c679fSSean McEnroe 	*zfvp = dmu_objset_get_user(os);
1456af4c679fSSean McEnroe 	if (*zfvp) {
1457af4c679fSSean McEnroe 		VFS_HOLD((*zfvp)->z_vfs);
145814843421SMatthew Ahrens 	} else {
1459be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
146014843421SMatthew Ahrens 	}
1461503ad85cSMatthew Ahrens 	mutex_exit(&os->os_user_ptr_lock);
1462dfc11533SChris Williamson 	return (error);
1463dfc11533SChris Williamson }
1464dfc11533SChris Williamson 
1465ed992b0aSSerapheim Dimitropoulos int
1466dfc11533SChris Williamson getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1467dfc11533SChris Williamson {
1468dfc11533SChris Williamson 	objset_t *os;
1469dfc11533SChris Williamson 	int error;
1470dfc11533SChris Williamson 
1471dfc11533SChris Williamson 	error = dmu_objset_hold(dsname, FTAG, &os);
1472dfc11533SChris Williamson 	if (error != 0)
1473dfc11533SChris Williamson 		return (error);
1474dfc11533SChris Williamson 
1475dfc11533SChris Williamson 	error = getzfsvfs_impl(os, zfvp);
1476503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
147714843421SMatthew Ahrens 	return (error);
147814843421SMatthew Ahrens }
147914843421SMatthew Ahrens 
148014843421SMatthew Ahrens /*
148114843421SMatthew Ahrens  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
148214843421SMatthew Ahrens  * case its z_vfs will be NULL, and it will be opened as the owner.
1483ad135b5dSChristopher Siden  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1484ad135b5dSChristopher Siden  * which prevents all vnode ops from running.
148514843421SMatthew Ahrens  */
148614843421SMatthew Ahrens static int
14871412a1a2SMark Shellenbaum zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
148814843421SMatthew Ahrens {
148914843421SMatthew Ahrens 	int error = 0;
149014843421SMatthew Ahrens 
1491af4c679fSSean McEnroe 	if (getzfsvfs(name, zfvp) != 0)
1492af4c679fSSean McEnroe 		error = zfsvfs_create(name, zfvp);
149314843421SMatthew Ahrens 	if (error == 0) {
1494c9030f6cSAlexander Motin 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
14951412a1a2SMark Shellenbaum 		    RW_READER, tag);
1496af4c679fSSean McEnroe 		if ((*zfvp)->z_unmounted) {
149714843421SMatthew Ahrens 			/*
149814843421SMatthew Ahrens 			 * XXX we could probably try again, since the unmounting
149914843421SMatthew Ahrens 			 * thread should be just about to disassociate the
150014843421SMatthew Ahrens 			 * objset from the zfsvfs.
150114843421SMatthew Ahrens 			 */
1502c9030f6cSAlexander Motin 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1503be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
150414843421SMatthew Ahrens 		}
150514843421SMatthew Ahrens 	}
150614843421SMatthew Ahrens 	return (error);
150714843421SMatthew Ahrens }
150814843421SMatthew Ahrens 
150914843421SMatthew Ahrens static void
151014843421SMatthew Ahrens zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
151114843421SMatthew Ahrens {
1512c9030f6cSAlexander Motin 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
151314843421SMatthew Ahrens 
151414843421SMatthew Ahrens 	if (zfsvfs->z_vfs) {
151514843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
151614843421SMatthew Ahrens 	} else {
1517eb633035STom Caputi 		dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
151814843421SMatthew Ahrens 		zfsvfs_free(zfsvfs);
151914843421SMatthew Ahrens 	}
152014843421SMatthew Ahrens }
152114843421SMatthew Ahrens 
1522fa9e4066Sahrens static int
1523fa9e4066Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc)
1524fa9e4066Sahrens {
1525fa9e4066Sahrens 	int error;
1526990b4856Slling 	nvlist_t *config, *props = NULL;
15270a48a24eStimh 	nvlist_t *rootprops = NULL;
15280a48a24eStimh 	nvlist_t *zplprops = NULL;
152904e56356SAndriy Gapon 	char *spa_name = zc->zc_name;
1530eb633035STom Caputi 	dsl_crypto_params_t *dcp = NULL;
1531fa9e4066Sahrens 
1532990b4856Slling 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1533478ed9adSEric Taylor 	    zc->zc_iflags, &config))
1534fa9e4066Sahrens 		return (error);
15352a6b87f0Sek 
1536990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1537478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1538478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1539990b4856Slling 		nvlist_free(config);
1540990b4856Slling 		return (error);
1541990b4856Slling 	}
1542990b4856Slling 
15430a48a24eStimh 	if (props) {
15440a48a24eStimh 		nvlist_t *nvl = NULL;
1545eb633035STom Caputi 		nvlist_t *hidden_args = NULL;
15460a48a24eStimh 		uint64_t version = SPA_VERSION;
154704e56356SAndriy Gapon 		char *tname;
15480a48a24eStimh 
15490a48a24eStimh 		(void) nvlist_lookup_uint64(props,
15500a48a24eStimh 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1551ad135b5dSChristopher Siden 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1552be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
15530a48a24eStimh 			goto pool_props_bad;
15540a48a24eStimh 		}
15550a48a24eStimh 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
15560a48a24eStimh 		if (nvl) {
15570a48a24eStimh 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
15580a48a24eStimh 			if (error != 0) {
15590a48a24eStimh 				nvlist_free(config);
15600a48a24eStimh 				nvlist_free(props);
15610a48a24eStimh 				return (error);
15620a48a24eStimh 			}
15630a48a24eStimh 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
15640a48a24eStimh 		}
1565eb633035STom Caputi 
1566eb633035STom Caputi 		(void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1567eb633035STom Caputi 		    &hidden_args);
1568eb633035STom Caputi 		error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1569eb633035STom Caputi 		    rootprops, hidden_args, &dcp);
1570eb633035STom Caputi 		if (error != 0) {
1571eb633035STom Caputi 			nvlist_free(config);
1572eb633035STom Caputi 			nvlist_free(props);
1573eb633035STom Caputi 			return (error);
1574eb633035STom Caputi 		}
1575eb633035STom Caputi 		(void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1576eb633035STom Caputi 
15770a48a24eStimh 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
15780a48a24eStimh 		error = zfs_fill_zplprops_root(version, rootprops,
15790a48a24eStimh 		    zplprops, NULL);
15803b2aab18SMatthew Ahrens 		if (error != 0)
15810a48a24eStimh 			goto pool_props_bad;
158204e56356SAndriy Gapon 
158304e56356SAndriy Gapon 		if (nvlist_lookup_string(props,
158404e56356SAndriy Gapon 		    zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
158504e56356SAndriy Gapon 			spa_name = tname;
15860a48a24eStimh 	}
15870a48a24eStimh 
1588eb633035STom Caputi 	error = spa_create(zc->zc_name, config, props, zplprops, dcp);
15890a48a24eStimh 
15900a48a24eStimh 	/*
15910a48a24eStimh 	 * Set the remaining root properties
15920a48a24eStimh 	 */
159304e56356SAndriy Gapon 	if (!error && (error = zfs_set_prop_nvlist(spa_name,
159492241e0bSTom Erickson 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
159504e56356SAndriy Gapon 		(void) spa_destroy(spa_name);
1596fa9e4066Sahrens 
15970a48a24eStimh pool_props_bad:
15980a48a24eStimh 	nvlist_free(rootprops);
15990a48a24eStimh 	nvlist_free(zplprops);
1600fa9e4066Sahrens 	nvlist_free(config);
16010a48a24eStimh 	nvlist_free(props);
1602eb633035STom Caputi 	dsl_crypto_params_free(dcp, !!error);
1603990b4856Slling 
1604fa9e4066Sahrens 	return (error);
1605fa9e4066Sahrens }
1606fa9e4066Sahrens 
1607fa9e4066Sahrens static int
1608fa9e4066Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1609fa9e4066Sahrens {
1610ecd6cf80Smarks 	int error;
1611ecd6cf80Smarks 	zfs_log_history(zc);
1612ecd6cf80Smarks 	error = spa_destroy(zc->zc_name);
1613681d9761SEric Taylor 	if (error == 0)
1614681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1615ecd6cf80Smarks 	return (error);
1616fa9e4066Sahrens }
1617fa9e4066Sahrens 
1618fa9e4066Sahrens static int
1619fa9e4066Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc)
1620fa9e4066Sahrens {
1621990b4856Slling 	nvlist_t *config, *props = NULL;
1622fa9e4066Sahrens 	uint64_t guid;
1623468c413aSTim Haley 	int error;
1624fa9e4066Sahrens 
1625990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1626478ed9adSEric Taylor 	    zc->zc_iflags, &config)) != 0)
1627990b4856Slling 		return (error);
1628990b4856Slling 
1629990b4856Slling 	if (zc->zc_nvlist_src_size != 0 && (error =
1630478ed9adSEric Taylor 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1631478ed9adSEric Taylor 	    zc->zc_iflags, &props))) {
1632990b4856Slling 		nvlist_free(config);
1633fa9e4066Sahrens 		return (error);
1634990b4856Slling 	}
1635fa9e4066Sahrens 
1636fa9e4066Sahrens 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1637ea8dc4b6Seschrock 	    guid != zc->zc_guid)
1638be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
1639fa9e4066Sahrens 	else
16404b964adaSGeorge Wilson 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1641fa9e4066Sahrens 
16424b964adaSGeorge Wilson 	if (zc->zc_nvlist_dst != 0) {
16434b964adaSGeorge Wilson 		int err;
16444b964adaSGeorge Wilson 
16454b964adaSGeorge Wilson 		if ((err = put_nvlist(zc, config)) != 0)
16464b964adaSGeorge Wilson 			error = err;
16474b964adaSGeorge Wilson 	}
1648468c413aSTim Haley 
1649fa9e4066Sahrens 	nvlist_free(config);
1650fa9e4066Sahrens 
1651aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(props);
1652990b4856Slling 
1653fa9e4066Sahrens 	return (error);
1654fa9e4066Sahrens }
1655fa9e4066Sahrens 
1656fa9e4066Sahrens static int
1657fa9e4066Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc)
1658fa9e4066Sahrens {
1659ecd6cf80Smarks 	int error;
166089a89ebfSlling 	boolean_t force = (boolean_t)zc->zc_cookie;
1661394ab0cbSGeorge Wilson 	boolean_t hardforce = (boolean_t)zc->zc_guid;
166289a89ebfSlling 
1663ecd6cf80Smarks 	zfs_log_history(zc);
1664394ab0cbSGeorge Wilson 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1665681d9761SEric Taylor 	if (error == 0)
1666681d9761SEric Taylor 		zvol_remove_minors(zc->zc_name);
1667ecd6cf80Smarks 	return (error);
1668fa9e4066Sahrens }
1669fa9e4066Sahrens 
1670fa9e4066Sahrens static int
1671fa9e4066Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc)
1672fa9e4066Sahrens {
1673fa9e4066Sahrens 	nvlist_t *configs;
1674fa9e4066Sahrens 	int error;
1675fa9e4066Sahrens 
1676fa9e4066Sahrens 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1677be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
1678fa9e4066Sahrens 
1679e9dbad6fSeschrock 	error = put_nvlist(zc, configs);
1680fa9e4066Sahrens 
1681fa9e4066Sahrens 	nvlist_free(configs);
1682fa9e4066Sahrens 
1683fa9e4066Sahrens 	return (error);
1684fa9e4066Sahrens }
1685fa9e4066Sahrens 
1686ad135b5dSChristopher Siden /*
1687ad135b5dSChristopher Siden  * inputs:
1688ad135b5dSChristopher Siden  * zc_name		name of the pool
1689ad135b5dSChristopher Siden  *
1690ad135b5dSChristopher Siden  * outputs:
1691ad135b5dSChristopher Siden  * zc_cookie		real errno
1692ad135b5dSChristopher Siden  * zc_nvlist_dst	config nvlist
1693ad135b5dSChristopher Siden  * zc_nvlist_dst_size	size of config nvlist
1694ad135b5dSChristopher Siden  */
1695fa9e4066Sahrens static int
1696fa9e4066Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc)
1697fa9e4066Sahrens {
1698fa9e4066Sahrens 	nvlist_t *config;
1699fa9e4066Sahrens 	int error;
1700ea8dc4b6Seschrock 	int ret = 0;
1701fa9e4066Sahrens 
1702e9dbad6fSeschrock 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1703e9dbad6fSeschrock 	    sizeof (zc->zc_value));
1704fa9e4066Sahrens 
1705fa9e4066Sahrens 	if (config != NULL) {
1706e9dbad6fSeschrock 		ret = put_nvlist(zc, config);
1707fa9e4066Sahrens 		nvlist_free(config);
1708ea8dc4b6Seschrock 
1709ea8dc4b6Seschrock 		/*
1710ea8dc4b6Seschrock 		 * The config may be present even if 'error' is non-zero.
1711ea8dc4b6Seschrock 		 * In this case we return success, and preserve the real errno
1712ea8dc4b6Seschrock 		 * in 'zc_cookie'.
1713ea8dc4b6Seschrock 		 */
1714ea8dc4b6Seschrock 		zc->zc_cookie = error;
1715fa9e4066Sahrens 	} else {
1716ea8dc4b6Seschrock 		ret = error;
1717fa9e4066Sahrens 	}
1718fa9e4066Sahrens 
1719ea8dc4b6Seschrock 	return (ret);
1720fa9e4066Sahrens }
1721fa9e4066Sahrens 
1722fa9e4066Sahrens /*
1723fa9e4066Sahrens  * Try to import the given pool, returning pool stats as appropriate so that
1724fa9e4066Sahrens  * user land knows which devices are available and overall pool health.
1725fa9e4066Sahrens  */
1726fa9e4066Sahrens static int
1727fa9e4066Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1728fa9e4066Sahrens {
1729fa9e4066Sahrens 	nvlist_t *tryconfig, *config;
1730fa9e4066Sahrens 	int error;
1731fa9e4066Sahrens 
1732990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1733478ed9adSEric Taylor 	    zc->zc_iflags, &tryconfig)) != 0)
1734fa9e4066Sahrens 		return (error);
1735fa9e4066Sahrens 
1736fa9e4066Sahrens 	config = spa_tryimport(tryconfig);
1737fa9e4066Sahrens 
1738fa9e4066Sahrens 	nvlist_free(tryconfig);
1739fa9e4066Sahrens 
1740fa9e4066Sahrens 	if (config == NULL)
1741be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1742fa9e4066Sahrens 
1743e9dbad6fSeschrock 	error = put_nvlist(zc, config);
1744fa9e4066Sahrens 	nvlist_free(config);
1745fa9e4066Sahrens 
1746fa9e4066Sahrens 	return (error);
1747fa9e4066Sahrens }
1748fa9e4066Sahrens 
17493f9d6ad7SLin Ling /*
17503f9d6ad7SLin Ling  * inputs:
17513f9d6ad7SLin Ling  * zc_name              name of the pool
17523f9d6ad7SLin Ling  * zc_cookie            scan func (pool_scan_func_t)
17531702cce7SAlek Pinchuk  * zc_flags             scrub pause/resume flag (pool_scrub_cmd_t)
17543f9d6ad7SLin Ling  */
1755fa9e4066Sahrens static int
17563f9d6ad7SLin Ling zfs_ioc_pool_scan(zfs_cmd_t *zc)
1757fa9e4066Sahrens {
1758fa9e4066Sahrens 	spa_t *spa;
1759fa9e4066Sahrens 	int error;
1760fa9e4066Sahrens 
176106eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
176206eeb2adSek 		return (error);
176306eeb2adSek 
17641702cce7SAlek Pinchuk 	if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
17651702cce7SAlek Pinchuk 		return (SET_ERROR(EINVAL));
17661702cce7SAlek Pinchuk 
17671702cce7SAlek Pinchuk 	if (zc->zc_flags == POOL_SCRUB_PAUSE)
17681702cce7SAlek Pinchuk 		error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
17691702cce7SAlek Pinchuk 	else if (zc->zc_cookie == POOL_SCAN_NONE)
17703f9d6ad7SLin Ling 		error = spa_scan_stop(spa);
17713f9d6ad7SLin Ling 	else
17723f9d6ad7SLin Ling 		error = spa_scan(spa, zc->zc_cookie);
177306eeb2adSek 
177406eeb2adSek 	spa_close(spa, FTAG);
177506eeb2adSek 
1776fa9e4066Sahrens 	return (error);
1777fa9e4066Sahrens }
1778fa9e4066Sahrens 
1779fa9e4066Sahrens static int
1780fa9e4066Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1781fa9e4066Sahrens {
1782fa9e4066Sahrens 	spa_t *spa;
1783fa9e4066Sahrens 	int error;
1784fa9e4066Sahrens 
1785fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1786fa9e4066Sahrens 	if (error == 0) {
1787fa9e4066Sahrens 		spa_freeze(spa);
1788fa9e4066Sahrens 		spa_close(spa, FTAG);
1789fa9e4066Sahrens 	}
1790fa9e4066Sahrens 	return (error);
1791fa9e4066Sahrens }
1792fa9e4066Sahrens 
1793eaca9bbdSeschrock static int
1794eaca9bbdSeschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1795eaca9bbdSeschrock {
1796eaca9bbdSeschrock 	spa_t *spa;
1797eaca9bbdSeschrock 	int error;
1798eaca9bbdSeschrock 
179906eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
180006eeb2adSek 		return (error);
180106eeb2adSek 
1802ad135b5dSChristopher Siden 	if (zc->zc_cookie < spa_version(spa) ||
1803ad135b5dSChristopher Siden 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1804558d2d50Slling 		spa_close(spa, FTAG);
1805be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1806558d2d50Slling 	}
1807558d2d50Slling 
1808990b4856Slling 	spa_upgrade(spa, zc->zc_cookie);
180906eeb2adSek 	spa_close(spa, FTAG);
181006eeb2adSek 
181106eeb2adSek 	return (error);
181206eeb2adSek }
181306eeb2adSek 
181406eeb2adSek static int
181506eeb2adSek zfs_ioc_pool_get_history(zfs_cmd_t *zc)
181606eeb2adSek {
181706eeb2adSek 	spa_t *spa;
181806eeb2adSek 	char *hist_buf;
181906eeb2adSek 	uint64_t size;
182006eeb2adSek 	int error;
182106eeb2adSek 
182206eeb2adSek 	if ((size = zc->zc_history_len) == 0)
1823be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
182406eeb2adSek 
182506eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
182606eeb2adSek 		return (error);
182706eeb2adSek 
1828e7437265Sahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1829d7306b64Sek 		spa_close(spa, FTAG);
1830be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1831d7306b64Sek 	}
1832d7306b64Sek 
183306eeb2adSek 	hist_buf = kmem_alloc(size, KM_SLEEP);
183406eeb2adSek 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
183506eeb2adSek 	    &zc->zc_history_len, hist_buf)) == 0) {
1836478ed9adSEric Taylor 		error = ddi_copyout(hist_buf,
1837478ed9adSEric Taylor 		    (void *)(uintptr_t)zc->zc_history,
1838478ed9adSEric Taylor 		    zc->zc_history_len, zc->zc_iflags);
183906eeb2adSek 	}
184006eeb2adSek 
184106eeb2adSek 	spa_close(spa, FTAG);
184206eeb2adSek 	kmem_free(hist_buf, size);
184306eeb2adSek 	return (error);
184406eeb2adSek }
184506eeb2adSek 
1846e9103aaeSGarrett D'Amore static int
1847e9103aaeSGarrett D'Amore zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1848e9103aaeSGarrett D'Amore {
1849e9103aaeSGarrett D'Amore 	spa_t *spa;
1850e9103aaeSGarrett D'Amore 	int error;
1851e9103aaeSGarrett D'Amore 
1852e9103aaeSGarrett D'Amore 	error = spa_open(zc->zc_name, &spa, FTAG);
1853e9103aaeSGarrett D'Amore 	if (error == 0) {
1854e9103aaeSGarrett D'Amore 		error = spa_change_guid(spa);
1855e9103aaeSGarrett D'Amore 		spa_close(spa, FTAG);
1856e9103aaeSGarrett D'Amore 	}
1857e9103aaeSGarrett D'Amore 	return (error);
1858e9103aaeSGarrett D'Amore }
1859e9103aaeSGarrett D'Amore 
186055434c77Sek static int
186155434c77Sek zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
186255434c77Sek {
18633b2aab18SMatthew Ahrens 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
186455434c77Sek }
186555434c77Sek 
1866503ad85cSMatthew Ahrens /*
1867503ad85cSMatthew Ahrens  * inputs:
1868503ad85cSMatthew Ahrens  * zc_name		name of filesystem
1869503ad85cSMatthew Ahrens  * zc_obj		object to find
1870503ad85cSMatthew Ahrens  *
1871503ad85cSMatthew Ahrens  * outputs:
1872503ad85cSMatthew Ahrens  * zc_value		name of object
1873503ad85cSMatthew Ahrens  */
187455434c77Sek static int
187555434c77Sek zfs_ioc_obj_to_path(zfs_cmd_t *zc)
187655434c77Sek {
1877503ad85cSMatthew Ahrens 	objset_t *os;
187855434c77Sek 	int error;
187955434c77Sek 
1880503ad85cSMatthew Ahrens 	/* XXX reading from objset not owned */
1881eb633035STom Caputi 	if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1882eb633035STom Caputi 	    FTAG, &os)) != 0)
188355434c77Sek 		return (error);
1884503ad85cSMatthew Ahrens 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1885eb633035STom Caputi 		dmu_objset_rele_flags(os, B_TRUE, FTAG);
1886be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1887503ad85cSMatthew Ahrens 	}
1888503ad85cSMatthew Ahrens 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
188955434c77Sek 	    sizeof (zc->zc_value));
1890eb633035STom Caputi 	dmu_objset_rele_flags(os, B_TRUE, FTAG);
189155434c77Sek 
189255434c77Sek 	return (error);
189355434c77Sek }
189455434c77Sek 
189599d5e173STim Haley /*
189699d5e173STim Haley  * inputs:
189799d5e173STim Haley  * zc_name		name of filesystem
189899d5e173STim Haley  * zc_obj		object to find
189999d5e173STim Haley  *
190099d5e173STim Haley  * outputs:
190199d5e173STim Haley  * zc_stat		stats on object
190299d5e173STim Haley  * zc_value		path to object
190399d5e173STim Haley  */
190499d5e173STim Haley static int
190599d5e173STim Haley zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
190699d5e173STim Haley {
190799d5e173STim Haley 	objset_t *os;
190899d5e173STim Haley 	int error;
190999d5e173STim Haley 
191099d5e173STim Haley 	/* XXX reading from objset not owned */
1911eb633035STom Caputi 	if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1912eb633035STom Caputi 	    FTAG, &os)) != 0)
191399d5e173STim Haley 		return (error);
191499d5e173STim Haley 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1915eb633035STom Caputi 		dmu_objset_rele_flags(os, B_TRUE, FTAG);
1916be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
191799d5e173STim Haley 	}
191899d5e173STim Haley 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
191999d5e173STim Haley 	    sizeof (zc->zc_value));
1920eb633035STom Caputi 	dmu_objset_rele_flags(os, B_TRUE, FTAG);
192199d5e173STim Haley 
192299d5e173STim Haley 	return (error);
192399d5e173STim Haley }
192499d5e173STim Haley 
1925fa9e4066Sahrens static int
1926fa9e4066Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc)
1927fa9e4066Sahrens {
1928fa9e4066Sahrens 	spa_t *spa;
1929fa9e4066Sahrens 	int error;
1930e7cbe64fSgw 	nvlist_t *config, **l2cache, **spares;
1931e7cbe64fSgw 	uint_t nl2cache = 0, nspares = 0;
1932fa9e4066Sahrens 
1933fa9e4066Sahrens 	error = spa_open(zc->zc_name, &spa, FTAG);
1934fa9e4066Sahrens 	if (error != 0)
1935fa9e4066Sahrens 		return (error);
1936fa9e4066Sahrens 
1937fa94a07fSbrendan 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1938478ed9adSEric Taylor 	    zc->zc_iflags, &config);
1939fa94a07fSbrendan 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1940fa94a07fSbrendan 	    &l2cache, &nl2cache);
1941fa94a07fSbrendan 
1942e7cbe64fSgw 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1943e7cbe64fSgw 	    &spares, &nspares);
1944e7cbe64fSgw 
1945b1b8ab34Slling 	/*
1946b1b8ab34Slling 	 * A root pool with concatenated devices is not supported.
1947e7cbe64fSgw 	 * Thus, can not add a device to a root pool.
1948e7cbe64fSgw 	 *
1949e7cbe64fSgw 	 * Intent log device can not be added to a rootpool because
1950e7cbe64fSgw 	 * during mountroot, zil is replayed, a seperated log device
1951e7cbe64fSgw 	 * can not be accessed during the mountroot time.
1952e7cbe64fSgw 	 *
1953e7cbe64fSgw 	 * l2cache and spare devices are ok to be added to a rootpool.
1954b1b8ab34Slling 	 */
1955b24ab676SJeff Bonwick 	if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
19561195e687SMark J Musante 		nvlist_free(config);
1957b1b8ab34Slling 		spa_close(spa, FTAG);
1958be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDOM));
1959b1b8ab34Slling 	}
1960b1b8ab34Slling 
1961fa94a07fSbrendan 	if (error == 0) {
1962fa9e4066Sahrens 		error = spa_vdev_add(spa, config);
1963fa9e4066Sahrens 		nvlist_free(config);
1964fa9e4066Sahrens 	}
1965fa9e4066Sahrens 	spa_close(spa, FTAG);
1966fa9e4066Sahrens 	return (error);
1967fa9e4066Sahrens }
1968fa9e4066Sahrens 
19693f9d6ad7SLin Ling /*
19703f9d6ad7SLin Ling  * inputs:
19713f9d6ad7SLin Ling  * zc_name		name of the pool
19725cabbc6bSPrashanth Sreenivasa  * zc_guid		guid of vdev to remove
19735cabbc6bSPrashanth Sreenivasa  * zc_cookie		cancel removal
19743f9d6ad7SLin Ling  */
1975fa9e4066Sahrens static int
1976fa9e4066Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1977fa9e4066Sahrens {
197899653d4eSeschrock 	spa_t *spa;
197999653d4eSeschrock 	int error;
198099653d4eSeschrock 
198199653d4eSeschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
198299653d4eSeschrock 	if (error != 0)
198399653d4eSeschrock 		return (error);
19845cabbc6bSPrashanth Sreenivasa 	if (zc->zc_cookie != 0) {
19855cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove_cancel(spa);
19865cabbc6bSPrashanth Sreenivasa 	} else {
19875cabbc6bSPrashanth Sreenivasa 		error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
19885cabbc6bSPrashanth Sreenivasa 	}
198999653d4eSeschrock 	spa_close(spa, FTAG);
199099653d4eSeschrock 	return (error);
1991fa9e4066Sahrens }
1992fa9e4066Sahrens 
1993fa9e4066Sahrens static int
19943d7072f8Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1995fa9e4066Sahrens {
1996fa9e4066Sahrens 	spa_t *spa;
1997fa9e4066Sahrens 	int error;
19983d7072f8Seschrock 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1999fa9e4066Sahrens 
200006eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2001fa9e4066Sahrens 		return (error);
20023d7072f8Seschrock 	switch (zc->zc_cookie) {
20033d7072f8Seschrock 	case VDEV_STATE_ONLINE:
20043d7072f8Seschrock 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
20053d7072f8Seschrock 		break;
2006fa9e4066Sahrens 
20073d7072f8Seschrock 	case VDEV_STATE_OFFLINE:
20083d7072f8Seschrock 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
20093d7072f8Seschrock 		break;
2010fa9e4066Sahrens 
20113d7072f8Seschrock 	case VDEV_STATE_FAULTED:
2012069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2013069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
2014069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2015069f55e2SEric Schrock 
2016069f55e2SEric Schrock 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
20173d7072f8Seschrock 		break;
20183d7072f8Seschrock 
20193d7072f8Seschrock 	case VDEV_STATE_DEGRADED:
2020069f55e2SEric Schrock 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2021069f55e2SEric Schrock 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
2022069f55e2SEric Schrock 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2023069f55e2SEric Schrock 
2024069f55e2SEric Schrock 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
20253d7072f8Seschrock 		break;
20263d7072f8Seschrock 
20273d7072f8Seschrock 	default:
2028be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
20293d7072f8Seschrock 	}
20303d7072f8Seschrock 	zc->zc_cookie = newstate;
2031fa9e4066Sahrens 	spa_close(spa, FTAG);
2032fa9e4066Sahrens 	return (error);
2033fa9e4066Sahrens }
2034fa9e4066Sahrens 
2035fa9e4066Sahrens static int
2036fa9e4066Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2037fa9e4066Sahrens {
2038fa9e4066Sahrens 	spa_t *spa;
2039fa9e4066Sahrens 	int replacing = zc->zc_cookie;
2040fa9e4066Sahrens 	nvlist_t *config;
2041fa9e4066Sahrens 	int error;
2042fa9e4066Sahrens 
204306eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2044fa9e4066Sahrens 		return (error);
2045fa9e4066Sahrens 
2046990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2047478ed9adSEric Taylor 	    zc->zc_iflags, &config)) == 0) {
2048ea8dc4b6Seschrock 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2049fa9e4066Sahrens 		nvlist_free(config);
2050fa9e4066Sahrens 	}
2051fa9e4066Sahrens 
2052fa9e4066Sahrens 	spa_close(spa, FTAG);
2053fa9e4066Sahrens 	return (error);
2054fa9e4066Sahrens }
2055fa9e4066Sahrens 
2056fa9e4066Sahrens static int
2057fa9e4066Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2058fa9e4066Sahrens {
2059fa9e4066Sahrens 	spa_t *spa;
2060fa9e4066Sahrens 	int error;
2061fa9e4066Sahrens 
206206eeb2adSek 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2063fa9e4066Sahrens 		return (error);
2064fa9e4066Sahrens 
20658ad4d6ddSJeff Bonwick 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2066fa9e4066Sahrens 
2067fa9e4066Sahrens 	spa_close(spa, FTAG);
2068fa9e4066Sahrens 	return (error);
2069fa9e4066Sahrens }
2070fa9e4066Sahrens 
20711195e687SMark J Musante static int
20721195e687SMark J Musante zfs_ioc_vdev_split(zfs_cmd_t *zc)
20731195e687SMark J Musante {
20741195e687SMark J Musante 	spa_t *spa;
20751195e687SMark J Musante 	nvlist_t *config, *props = NULL;
20761195e687SMark J Musante 	int error;
20771195e687SMark J Musante 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
20781195e687SMark J Musante 
20791195e687SMark J Musante 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
20801195e687SMark J Musante 		return (error);
20811195e687SMark J Musante 
20821195e687SMark J Musante 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
20831195e687SMark J Musante 	    zc->zc_iflags, &config)) {
20841195e687SMark J Musante 		spa_close(spa, FTAG);
20851195e687SMark J Musante 		return (error);
20861195e687SMark J Musante 	}
20871195e687SMark J Musante 
20881195e687SMark J Musante 	if (zc->zc_nvlist_src_size != 0 && (error =
20891195e687SMark J Musante 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
20901195e687SMark J Musante 	    zc->zc_iflags, &props))) {
20911195e687SMark J Musante 		spa_close(spa, FTAG);
20921195e687SMark J Musante 		nvlist_free(config);
20931195e687SMark J Musante 		return (error);
20941195e687SMark J Musante 	}
20951195e687SMark J Musante 
20961195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
20971195e687SMark J Musante 
20981195e687SMark J Musante 	spa_close(spa, FTAG);
20991195e687SMark J Musante 
21001195e687SMark J Musante 	nvlist_free(config);
21011195e687SMark J Musante 	nvlist_free(props);
21021195e687SMark J Musante 
21031195e687SMark J Musante 	return (error);
21041195e687SMark J Musante }
21051195e687SMark J Musante 
2106c67d9675Seschrock static int
2107c67d9675Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2108c67d9675Seschrock {
2109c67d9675Seschrock 	spa_t *spa;
2110e9dbad6fSeschrock 	char *path = zc->zc_value;
2111ea8dc4b6Seschrock 	uint64_t guid = zc->zc_guid;
2112c67d9675Seschrock 	int error;
2113c67d9675Seschrock 
2114c67d9675Seschrock 	error = spa_open(zc->zc_name, &spa, FTAG);
2115c67d9675Seschrock 	if (error != 0)
2116c67d9675Seschrock 		return (error);
2117c67d9675Seschrock 
2118c67d9675Seschrock 	error = spa_vdev_setpath(spa, guid, path);
2119c67d9675Seschrock 	spa_close(spa, FTAG);
2120c67d9675Seschrock 	return (error);
2121c67d9675Seschrock }
2122c67d9675Seschrock 
21236809eb4eSEric Schrock static int
21246809eb4eSEric Schrock zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
21256809eb4eSEric Schrock {
21266809eb4eSEric Schrock 	spa_t *spa;
21276809eb4eSEric Schrock 	char *fru = zc->zc_value;
21286809eb4eSEric Schrock 	uint64_t guid = zc->zc_guid;
21296809eb4eSEric Schrock 	int error;
21306809eb4eSEric Schrock 
21316809eb4eSEric Schrock 	error = spa_open(zc->zc_name, &spa, FTAG);
21326809eb4eSEric Schrock 	if (error != 0)
21336809eb4eSEric Schrock 		return (error);
21346809eb4eSEric Schrock 
21356809eb4eSEric Schrock 	error = spa_vdev_setfru(spa, guid, fru);
21366809eb4eSEric Schrock 	spa_close(spa, FTAG);
21376809eb4eSEric Schrock 	return (error);
21386809eb4eSEric Schrock }
21396809eb4eSEric Schrock 
2140fa9e4066Sahrens static int
2141a7f53a56SChris Kirby zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2142fa9e4066Sahrens {
2143a7f53a56SChris Kirby 	int error = 0;
21447f7322feSeschrock 	nvlist_t *nv;
2145fa9e4066Sahrens 
2146a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2147fa9e4066Sahrens 
21485ad82045Snd 	if (zc->zc_nvlist_dst != 0 &&
214992241e0bSTom Erickson 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2150a2eea2e1Sahrens 		dmu_objset_stats(os, nv);
2151432f72fdSahrens 		/*
2152bd00f61bSrm 		 * NB: zvol_get_stats() will read the objset contents,
2153432f72fdSahrens 		 * which we aren't supposed to do with a
2154745cd3c5Smaybee 		 * DS_MODE_USER hold, because it could be
2155432f72fdSahrens 		 * inconsistent.  So this is a bit of a workaround...
2156503ad85cSMatthew Ahrens 		 * XXX reading with out owning
2157432f72fdSahrens 		 */
215819b94df9SMatthew Ahrens 		if (!zc->zc_objset_stats.dds_inconsistent &&
215919b94df9SMatthew Ahrens 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
216019b94df9SMatthew Ahrens 			error = zvol_get_stats(os, nv);
216119b94df9SMatthew Ahrens 			if (error == EIO)
216219b94df9SMatthew Ahrens 				return (error);
2163fb09f5aaSMadhav Suresh 			VERIFY0(error);
2164e7437265Sahrens 		}
2165e9dbad6fSeschrock 		error = put_nvlist(zc, nv);
21667f7322feSeschrock 		nvlist_free(nv);
21677f7322feSeschrock 	}
2168fa9e4066Sahrens 
2169a7f53a56SChris Kirby 	return (error);
2170a7f53a56SChris Kirby }
2171a7f53a56SChris Kirby 
2172a7f53a56SChris Kirby /*
2173a7f53a56SChris Kirby  * inputs:
2174a7f53a56SChris Kirby  * zc_name		name of filesystem
2175a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of buffer for property nvlist
2176a7f53a56SChris Kirby  *
2177a7f53a56SChris Kirby  * outputs:
2178a7f53a56SChris Kirby  * zc_objset_stats	stats
2179a7f53a56SChris Kirby  * zc_nvlist_dst	property nvlist
2180a7f53a56SChris Kirby  * zc_nvlist_dst_size	size of property nvlist
2181a7f53a56SChris Kirby  */
2182a7f53a56SChris Kirby static int
2183a7f53a56SChris Kirby zfs_ioc_objset_stats(zfs_cmd_t *zc)
2184a7f53a56SChris Kirby {
21853b2aab18SMatthew Ahrens 	objset_t *os;
2186a7f53a56SChris Kirby 	int error;
2187a7f53a56SChris Kirby 
21883b2aab18SMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
21893b2aab18SMatthew Ahrens 	if (error == 0) {
21903b2aab18SMatthew Ahrens 		error = zfs_ioc_objset_stats_impl(zc, os);
21913b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
21923b2aab18SMatthew Ahrens 	}
2193a7f53a56SChris Kirby 
2194fa9e4066Sahrens 	return (error);
2195fa9e4066Sahrens }
2196fa9e4066Sahrens 
219792241e0bSTom Erickson /*
219892241e0bSTom Erickson  * inputs:
219992241e0bSTom Erickson  * zc_name		name of filesystem
220092241e0bSTom Erickson  * zc_nvlist_dst_size	size of buffer for property nvlist
220192241e0bSTom Erickson  *
220292241e0bSTom Erickson  * outputs:
220392241e0bSTom Erickson  * zc_nvlist_dst	received property nvlist
220492241e0bSTom Erickson  * zc_nvlist_dst_size	size of received property nvlist
220592241e0bSTom Erickson  *
220692241e0bSTom Erickson  * Gets received properties (distinct from local properties on or after
220792241e0bSTom Erickson  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
220892241e0bSTom Erickson  * local property values.
220992241e0bSTom Erickson  */
221092241e0bSTom Erickson static int
221192241e0bSTom Erickson zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
221292241e0bSTom Erickson {
22133b2aab18SMatthew Ahrens 	int error = 0;
221492241e0bSTom Erickson 	nvlist_t *nv;
221592241e0bSTom Erickson 
221692241e0bSTom Erickson 	/*
221792241e0bSTom Erickson 	 * Without this check, we would return local property values if the
221892241e0bSTom Erickson 	 * caller has not already received properties on or after
221992241e0bSTom Erickson 	 * SPA_VERSION_RECVD_PROPS.
222092241e0bSTom Erickson 	 */
22213b2aab18SMatthew Ahrens 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2222be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
222392241e0bSTom Erickson 
222492241e0bSTom Erickson 	if (zc->zc_nvlist_dst != 0 &&
22253b2aab18SMatthew Ahrens 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
222692241e0bSTom Erickson 		error = put_nvlist(zc, nv);
222792241e0bSTom Erickson 		nvlist_free(nv);
222892241e0bSTom Erickson 	}
222992241e0bSTom Erickson 
223092241e0bSTom Erickson 	return (error);
223192241e0bSTom Erickson }
223292241e0bSTom Erickson 
2233de8267e0Stimh static int
2234de8267e0Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2235de8267e0Stimh {
2236de8267e0Stimh 	uint64_t value;
2237de8267e0Stimh 	int error;
2238de8267e0Stimh 
2239de8267e0Stimh 	/*
2240de8267e0Stimh 	 * zfs_get_zplprop() will either find a value or give us
2241de8267e0Stimh 	 * the default value (if there is one).
2242de8267e0Stimh 	 */
2243de8267e0Stimh 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2244de8267e0Stimh 		return (error);
2245de8267e0Stimh 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2246de8267e0Stimh 	return (0);
2247de8267e0Stimh }
2248de8267e0Stimh 
22493cb34c60Sahrens /*
22503cb34c60Sahrens  * inputs:
22513cb34c60Sahrens  * zc_name		name of filesystem
2252de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
22533cb34c60Sahrens  *
22543cb34c60Sahrens  * outputs:
2255de8267e0Stimh  * zc_nvlist_dst	zpl property nvlist
2256de8267e0Stimh  * zc_nvlist_dst_size	size of zpl property nvlist
22573cb34c60Sahrens  */
2258bd00f61bSrm static int
2259de8267e0Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2260bd00f61bSrm {
2261de8267e0Stimh 	objset_t *os;
2262de8267e0Stimh 	int err;
2263bd00f61bSrm 
2264503ad85cSMatthew Ahrens 	/* XXX reading without owning */
2265503ad85cSMatthew Ahrens 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2266de8267e0Stimh 		return (err);
2267bd00f61bSrm 
2268bd00f61bSrm 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2269bd00f61bSrm 
2270bd00f61bSrm 	/*
2271de8267e0Stimh 	 * NB: nvl_add_zplprop() will read the objset contents,
2272745cd3c5Smaybee 	 * which we aren't supposed to do with a DS_MODE_USER
2273745cd3c5Smaybee 	 * hold, because it could be inconsistent.
2274bd00f61bSrm 	 */
2275dd328bf6SToomas Soome 	if (zc->zc_nvlist_dst != 0 &&
2276de8267e0Stimh 	    !zc->zc_objset_stats.dds_inconsistent &&
2277de8267e0Stimh 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2278de8267e0Stimh 		nvlist_t *nv;
2279de8267e0Stimh 
2280de8267e0Stimh 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2281de8267e0Stimh 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2282de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2283de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2284de8267e0Stimh 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2285de8267e0Stimh 			err = put_nvlist(zc, nv);
2286de8267e0Stimh 		nvlist_free(nv);
2287de8267e0Stimh 	} else {
2288be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
2289de8267e0Stimh 	}
2290503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2291de8267e0Stimh 	return (err);
2292bd00f61bSrm }
2293bd00f61bSrm 
229414843421SMatthew Ahrens static boolean_t
229514843421SMatthew Ahrens dataset_name_hidden(const char *name)
229614843421SMatthew Ahrens {
229714843421SMatthew Ahrens 	/*
229814843421SMatthew Ahrens 	 * Skip over datasets that are not visible in this zone,
229914843421SMatthew Ahrens 	 * internal datasets (which have a $ in their name), and
230014843421SMatthew Ahrens 	 * temporary datasets (which have a % in their name).
230114843421SMatthew Ahrens 	 */
230214843421SMatthew Ahrens 	if (strchr(name, '$') != NULL)
230314843421SMatthew Ahrens 		return (B_TRUE);
230414843421SMatthew Ahrens 	if (strchr(name, '%') != NULL)
230514843421SMatthew Ahrens 		return (B_TRUE);
230614843421SMatthew Ahrens 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
230714843421SMatthew Ahrens 		return (B_TRUE);
230814843421SMatthew Ahrens 	return (B_FALSE);
230914843421SMatthew Ahrens }
231014843421SMatthew Ahrens 
2311de8267e0Stimh /*
2312de8267e0Stimh  * inputs:
2313de8267e0Stimh  * zc_name		name of filesystem
2314de8267e0Stimh  * zc_cookie		zap cursor
2315de8267e0Stimh  * zc_nvlist_dst_size	size of buffer for property nvlist
2316de8267e0Stimh  *
2317de8267e0Stimh  * outputs:
2318de8267e0Stimh  * zc_name		name of next filesystem
231914843421SMatthew Ahrens  * zc_cookie		zap cursor
2320de8267e0Stimh  * zc_objset_stats	stats
2321de8267e0Stimh  * zc_nvlist_dst	property nvlist
2322de8267e0Stimh  * zc_nvlist_dst_size	size of property nvlist
2323de8267e0Stimh  */
2324fa9e4066Sahrens static int
2325fa9e4066Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2326fa9e4066Sahrens {
232787e5029aSahrens 	objset_t *os;
2328fa9e4066Sahrens 	int error;
2329fa9e4066Sahrens 	char *p;
2330620252bcSChris Kirby 	size_t orig_len = strlen(zc->zc_name);
2331fa9e4066Sahrens 
2332620252bcSChris Kirby top:
2333503ad85cSMatthew Ahrens 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
233487e5029aSahrens 		if (error == ENOENT)
2335be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
233687e5029aSahrens 		return (error);
2337fa9e4066Sahrens 	}
2338fa9e4066Sahrens 
2339fa9e4066Sahrens 	p = strrchr(zc->zc_name, '/');
2340fa9e4066Sahrens 	if (p == NULL || p[1] != '\0')
2341fa9e4066Sahrens 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2342fa9e4066Sahrens 	p = zc->zc_name + strlen(zc->zc_name);
2343fa9e4066Sahrens 
2344fa9e4066Sahrens 	do {
234587e5029aSahrens 		error = dmu_dir_list_next(os,
234687e5029aSahrens 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
234787e5029aSahrens 		    NULL, &zc->zc_cookie);
2348fa9e4066Sahrens 		if (error == ENOENT)
2349be6fd75aSMatthew Ahrens 			error = SET_ERROR(ESRCH);
235019b94df9SMatthew Ahrens 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2351503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
2352fa9e4066Sahrens 
2353681d9761SEric Taylor 	/*
2354681d9761SEric Taylor 	 * If it's an internal dataset (ie. with a '$' in its name),
2355681d9761SEric Taylor 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2356681d9761SEric Taylor 	 */
2357620252bcSChris Kirby 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
235887e5029aSahrens 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2359620252bcSChris Kirby 		if (error == ENOENT) {
2360620252bcSChris Kirby 			/* We lost a race with destroy, get the next one. */
2361620252bcSChris Kirby 			zc->zc_name[orig_len] = '\0';
2362620252bcSChris Kirby 			goto top;
2363620252bcSChris Kirby 		}
2364620252bcSChris Kirby 	}
2365fa9e4066Sahrens 	return (error);
2366fa9e4066Sahrens }
2367fa9e4066Sahrens 
23683cb34c60Sahrens /*
23693cb34c60Sahrens  * inputs:
23703cb34c60Sahrens  * zc_name		name of filesystem
23713cb34c60Sahrens  * zc_cookie		zap cursor
23723cb34c60Sahrens  * zc_nvlist_dst_size	size of buffer for property nvlist
23730d8fa8f8SMartin Matuska  * zc_simple		when set, only name is requested
23743cb34c60Sahrens  *
23753cb34c60Sahrens  * outputs:
23763cb34c60Sahrens  * zc_name		name of next snapshot
23773cb34c60Sahrens  * zc_objset_stats	stats
23783cb34c60Sahrens  * zc_nvlist_dst	property nvlist
23793cb34c60Sahrens  * zc_nvlist_dst_size	size of property nvlist
23803cb34c60Sahrens  */
2381fa9e4066Sahrens static int
2382fa9e4066Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2383fa9e4066Sahrens {
238487e5029aSahrens 	objset_t *os;
2385fa9e4066Sahrens 	int error;
2386fa9e4066Sahrens 
2387503ad85cSMatthew Ahrens 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
23883b2aab18SMatthew Ahrens 	if (error != 0) {
2389745cd3c5Smaybee 		return (error == ENOENT ? ESRCH : error);
23903b2aab18SMatthew Ahrens 	}
2391fa9e4066Sahrens 
2392b81d61a6Slling 	/*
2393b81d61a6Slling 	 * A dataset name of maximum length cannot have any snapshots,
2394b81d61a6Slling 	 * so exit immediately.
2395b81d61a6Slling 	 */
23969adfa60dSMatthew Ahrens 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
23979adfa60dSMatthew Ahrens 	    ZFS_MAX_DATASET_NAME_LEN) {
2398503ad85cSMatthew Ahrens 		dmu_objset_rele(os, FTAG);
2399be6fd75aSMatthew Ahrens 		return (SET_ERROR(ESRCH));
2400fa9e4066Sahrens 	}
2401fa9e4066Sahrens 
240287e5029aSahrens 	error = dmu_snapshot_list_next(os,
240387e5029aSahrens 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2404a7f53a56SChris Kirby 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2405a7f53a56SChris Kirby 	    NULL);
2406a7f53a56SChris Kirby 
24070d8fa8f8SMartin Matuska 	if (error == 0 && !zc->zc_simple) {
2408a7f53a56SChris Kirby 		dsl_dataset_t *ds;
2409a7f53a56SChris Kirby 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2410a7f53a56SChris Kirby 
2411a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
24123b2aab18SMatthew Ahrens 		if (error == 0) {
2413a7f53a56SChris Kirby 			objset_t *ossnap;
2414a7f53a56SChris Kirby 
2415a7f53a56SChris Kirby 			error = dmu_objset_from_ds(ds, &ossnap);
2416a7f53a56SChris Kirby 			if (error == 0)
2417a7f53a56SChris Kirby 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2418a7f53a56SChris Kirby 			dsl_dataset_rele(ds, FTAG);
2419620252bcSChris Kirby 		}
2420620252bcSChris Kirby 	} else if (error == ENOENT) {
2421be6fd75aSMatthew Ahrens 		error = SET_ERROR(ESRCH);
2422620252bcSChris Kirby 	}
2423fa9e4066Sahrens 
2424a7f53a56SChris Kirby 	dmu_objset_rele(os, FTAG);
24253cb34c60Sahrens 	/* if we failed, undo the @ that we tacked on to zc_name */
24263b2aab18SMatthew Ahrens 	if (error != 0)
24273cb34c60Sahrens 		*strchr(zc->zc_name, '@') = '\0';
2428fa9e4066Sahrens 	return (error);
2429fa9e4066Sahrens }
2430fa9e4066Sahrens 
243192241e0bSTom Erickson static int
243292241e0bSTom Erickson zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2433fa9e4066Sahrens {
243492241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
243592241e0bSTom Erickson 	uint64_t *valary;
243692241e0bSTom Erickson 	unsigned int vallen;
243792241e0bSTom Erickson 	const char *domain;
2438eeb85002STim Haley 	char *dash;
243992241e0bSTom Erickson 	zfs_userquota_prop_t type;
244092241e0bSTom Erickson 	uint64_t rid;
244192241e0bSTom Erickson 	uint64_t quota;
244292241e0bSTom Erickson 	zfsvfs_t *zfsvfs;
244392241e0bSTom Erickson 	int err;
244492241e0bSTom Erickson 
244592241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
244692241e0bSTom Erickson 		nvlist_t *attrs;
244792241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2448eeb85002STim Haley 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2449eeb85002STim Haley 		    &pair) != 0)
2450be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
245192241e0bSTom Erickson 	}
2452e9dbad6fSeschrock 
2453ecd6cf80Smarks 	/*
2454eeb85002STim Haley 	 * A correctly constructed propname is encoded as
245592241e0bSTom Erickson 	 * userquota@<rid>-<domain>.
2456ecd6cf80Smarks 	 */
2457eeb85002STim Haley 	if ((dash = strchr(propname, '-')) == NULL ||
2458eeb85002STim Haley 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2459eeb85002STim Haley 	    vallen != 3)
2460be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2461eeb85002STim Haley 
2462eeb85002STim Haley 	domain = dash + 1;
2463eeb85002STim Haley 	type = valary[0];
2464eeb85002STim Haley 	rid = valary[1];
2465eeb85002STim Haley 	quota = valary[2];
2466e9dbad6fSeschrock 
24671412a1a2SMark Shellenbaum 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
246892241e0bSTom Erickson 	if (err == 0) {
246992241e0bSTom Erickson 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
247092241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
247192241e0bSTom Erickson 	}
2472e9dbad6fSeschrock 
247392241e0bSTom Erickson 	return (err);
247492241e0bSTom Erickson }
247514843421SMatthew Ahrens 
247692241e0bSTom Erickson /*
247792241e0bSTom Erickson  * If the named property is one that has a special function to set its value,
247892241e0bSTom Erickson  * return 0 on success and a positive error code on failure; otherwise if it is
247992241e0bSTom Erickson  * not one of the special properties handled by this function, return -1.
248092241e0bSTom Erickson  *
2481eeb85002STim Haley  * XXX: It would be better for callers of the property interface if we handled
248292241e0bSTom Erickson  * these special cases in dsl_prop.c (in the dsl layer).
248392241e0bSTom Erickson  */
248492241e0bSTom Erickson static int
248592241e0bSTom Erickson zfs_prop_set_special(const char *dsname, zprop_source_t source,
248692241e0bSTom Erickson     nvpair_t *pair)
248792241e0bSTom Erickson {
248892241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
248992241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
2490eb633035STom Caputi 	uint64_t intval = 0;
2491eb633035STom Caputi 	char *strval = NULL;
2492b5152584SMatthew Ahrens 	int err = -1;
2493fa9e4066Sahrens 
249492241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
249592241e0bSTom Erickson 		if (zfs_prop_userquota(propname))
249692241e0bSTom Erickson 			return (zfs_prop_set_userquota(dsname, pair));
249792241e0bSTom Erickson 		return (-1);
249892241e0bSTom Erickson 	}
249914843421SMatthew Ahrens 
250092241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
250192241e0bSTom Erickson 		nvlist_t *attrs;
250292241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
250392241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
250492241e0bSTom Erickson 		    &pair) == 0);
250592241e0bSTom Erickson 	}
2506db870a07Sahrens 
2507eb633035STom Caputi 	/* all special properties are numeric except for keylocation */
2508eb633035STom Caputi 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2509eb633035STom Caputi 		strval = fnvpair_value_string(pair);
2510eb633035STom Caputi 	} else {
2511eb633035STom Caputi 		intval = fnvpair_value_uint64(pair);
2512eb633035STom Caputi 	}
251340feaa91Sahrens 
251492241e0bSTom Erickson 	switch (prop) {
251592241e0bSTom Erickson 	case ZFS_PROP_QUOTA:
251692241e0bSTom Erickson 		err = dsl_dir_set_quota(dsname, source, intval);
251792241e0bSTom Erickson 		break;
251892241e0bSTom Erickson 	case ZFS_PROP_REFQUOTA:
25193b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refquota(dsname, source, intval);
252092241e0bSTom Erickson 		break;
2521a2afb611SJerry Jelinek 	case ZFS_PROP_FILESYSTEM_LIMIT:
2522a2afb611SJerry Jelinek 	case ZFS_PROP_SNAPSHOT_LIMIT:
2523a2afb611SJerry Jelinek 		if (intval == UINT64_MAX) {
2524a2afb611SJerry Jelinek 			/* clearing the limit, just do it */
2525a2afb611SJerry Jelinek 			err = 0;
2526a2afb611SJerry Jelinek 		} else {
2527a2afb611SJerry Jelinek 			err = dsl_dir_activate_fs_ss_limit(dsname);
2528a2afb611SJerry Jelinek 		}
2529eb633035STom Caputi 		/*
2530eb633035STom Caputi 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2531eb633035STom Caputi 		 * default path to set the value in the nvlist.
2532eb633035STom Caputi 		 */
2533eb633035STom Caputi 		if (err == 0)
2534eb633035STom Caputi 			err = -1;
2535eb633035STom Caputi 		break;
2536eb633035STom Caputi 	case ZFS_PROP_KEYLOCATION:
2537eb633035STom Caputi 		err = dsl_crypto_can_set_keylocation(dsname, strval);
2538eb633035STom Caputi 
2539a2afb611SJerry Jelinek 		/*
2540a2afb611SJerry Jelinek 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2541a2afb611SJerry Jelinek 		 * default path to set the value in the nvlist.
2542a2afb611SJerry Jelinek 		 */
2543a2afb611SJerry Jelinek 		if (err == 0)
2544a2afb611SJerry Jelinek 			err = -1;
2545a2afb611SJerry Jelinek 		break;
254692241e0bSTom Erickson 	case ZFS_PROP_RESERVATION:
254792241e0bSTom Erickson 		err = dsl_dir_set_reservation(dsname, source, intval);
254892241e0bSTom Erickson 		break;
254992241e0bSTom Erickson 	case ZFS_PROP_REFRESERVATION:
25503b2aab18SMatthew Ahrens 		err = dsl_dataset_set_refreservation(dsname, source, intval);
255192241e0bSTom Erickson 		break;
255292241e0bSTom Erickson 	case ZFS_PROP_VOLSIZE:
2553c61ea566SGeorge Wilson 		err = zvol_set_volsize(dsname, intval);
255492241e0bSTom Erickson 		break;
255592241e0bSTom Erickson 	case ZFS_PROP_VERSION:
255692241e0bSTom Erickson 	{
255792241e0bSTom Erickson 		zfsvfs_t *zfsvfs;
25589e6eda55Smarks 
25591412a1a2SMark Shellenbaum 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2560b24ab676SJeff Bonwick 			break;
2561b24ab676SJeff Bonwick 
256292241e0bSTom Erickson 		err = zfs_set_version(zfsvfs, intval);
256392241e0bSTom Erickson 		zfsvfs_rele(zfsvfs, FTAG);
2564d0f3f37eSMark Shellenbaum 
256592241e0bSTom Erickson 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2566b16da2e2SGeorge Wilson 			zfs_cmd_t *zc;
2567b16da2e2SGeorge Wilson 
2568b16da2e2SGeorge Wilson 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2569b16da2e2SGeorge Wilson 			(void) strcpy(zc->zc_name, dsname);
2570b16da2e2SGeorge Wilson 			(void) zfs_ioc_userspace_upgrade(zc);
2571*f67950b2SNasf-Fan 			(void) zfs_ioc_id_quota_upgrade(zc);
2572b16da2e2SGeorge Wilson 			kmem_free(zc, sizeof (zfs_cmd_t));
257340feaa91Sahrens 		}
257492241e0bSTom Erickson 		break;
2575ecd6cf80Smarks 	}
257692241e0bSTom Erickson 	default:
257792241e0bSTom Erickson 		err = -1;
257892241e0bSTom Erickson 	}
2579e9dbad6fSeschrock 
258092241e0bSTom Erickson 	return (err);
258192241e0bSTom Erickson }
2582a9799022Sck 
258392241e0bSTom Erickson /*
258492241e0bSTom Erickson  * This function is best effort. If it fails to set any of the given properties,
25854445fffbSMatthew Ahrens  * it continues to set as many as it can and returns the last error
25864445fffbSMatthew Ahrens  * encountered. If the caller provides a non-NULL errlist, it will be filled in
25874445fffbSMatthew Ahrens  * with the list of names of all the properties that failed along with the
25884445fffbSMatthew Ahrens  * corresponding error numbers.
258992241e0bSTom Erickson  *
25904445fffbSMatthew Ahrens  * If every property is set successfully, zero is returned and errlist is not
25914445fffbSMatthew Ahrens  * modified.
259292241e0bSTom Erickson  */
259392241e0bSTom Erickson int
259492241e0bSTom Erickson zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
25954445fffbSMatthew Ahrens     nvlist_t *errlist)
259692241e0bSTom Erickson {
259792241e0bSTom Erickson 	nvpair_t *pair;
259892241e0bSTom Erickson 	nvpair_t *propval;
259902e383d1STom Erickson 	int rv = 0;
260092241e0bSTom Erickson 	uint64_t intval;
260192241e0bSTom Erickson 	char *strval;
26024445fffbSMatthew Ahrens 	nvlist_t *genericnvl = fnvlist_alloc();
26034445fffbSMatthew Ahrens 	nvlist_t *retrynvl = fnvlist_alloc();
2604a9799022Sck 
260592241e0bSTom Erickson retry:
260692241e0bSTom Erickson 	pair = NULL;
260792241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
260892241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
260992241e0bSTom Erickson 		zfs_prop_t prop = zfs_name_to_prop(propname);
2610cfa69fd2STom Erickson 		int err = 0;
2611e9dbad6fSeschrock 
261292241e0bSTom Erickson 		/* decode the property value */
261392241e0bSTom Erickson 		propval = pair;
261492241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
261592241e0bSTom Erickson 			nvlist_t *attrs;
26164445fffbSMatthew Ahrens 			attrs = fnvpair_value_nvlist(pair);
2617eeb85002STim Haley 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2618eeb85002STim Haley 			    &propval) != 0)
2619be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
262014843421SMatthew Ahrens 		}
2621e9dbad6fSeschrock 
262292241e0bSTom Erickson 		/* Validate value type */
26236ccda740Sloli 		if (err == 0 && source == ZPROP_SRC_INHERITED) {
26246ccda740Sloli 			/* inherited properties are expected to be booleans */
26256ccda740Sloli 			if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
26266ccda740Sloli 				err = SET_ERROR(EINVAL);
26276ccda740Sloli 		} else if (err == 0 && prop == ZPROP_INVAL) {
262892241e0bSTom Erickson 			if (zfs_prop_user(propname)) {
262992241e0bSTom Erickson 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2630be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
263192241e0bSTom Erickson 			} else if (zfs_prop_userquota(propname)) {
263292241e0bSTom Erickson 				if (nvpair_type(propval) !=
263392241e0bSTom Erickson 				    DATA_TYPE_UINT64_ARRAY)
2634be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
263519b94df9SMatthew Ahrens 			} else {
2636be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
26374201a95eSRic Aleshire 			}
2638eeb85002STim Haley 		} else if (err == 0) {
263992241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
264092241e0bSTom Erickson 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2641be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
264292241e0bSTom Erickson 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2643a2eea2e1Sahrens 				const char *unused;
2644a2eea2e1Sahrens 
26454445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
2646e9dbad6fSeschrock 
2647e9dbad6fSeschrock 				switch (zfs_prop_get_type(prop)) {
264891ebeef5Sahrens 				case PROP_TYPE_NUMBER:
2649e9dbad6fSeschrock 					break;
265091ebeef5Sahrens 				case PROP_TYPE_STRING:
2651be6fd75aSMatthew Ahrens 					err = SET_ERROR(EINVAL);
265292241e0bSTom Erickson 					break;
265391ebeef5Sahrens 				case PROP_TYPE_INDEX:
2654acd76fe5Seschrock 					if (zfs_prop_index_to_string(prop,
265592241e0bSTom Erickson 					    intval, &unused) != 0)
2656be6fd75aSMatthew Ahrens 						err = SET_ERROR(EINVAL);
2657e9dbad6fSeschrock 					break;
2658e9dbad6fSeschrock 				default:
2659e7437265Sahrens 					cmn_err(CE_PANIC,
2660e7437265Sahrens 					    "unknown property type");
2661e9dbad6fSeschrock 				}
2662e9dbad6fSeschrock 			} else {
2663be6fd75aSMatthew Ahrens 				err = SET_ERROR(EINVAL);
2664e9dbad6fSeschrock 			}
2665e9dbad6fSeschrock 		}
266692241e0bSTom Erickson 
266792241e0bSTom Erickson 		/* Validate permissions */
266892241e0bSTom Erickson 		if (err == 0)
266992241e0bSTom Erickson 			err = zfs_check_settable(dsname, pair, CRED());
267092241e0bSTom Erickson 
267192241e0bSTom Erickson 		if (err == 0) {
26726ccda740Sloli 			if (source == ZPROP_SRC_INHERITED)
26736ccda740Sloli 				err = -1; /* does not need special handling */
26746ccda740Sloli 			else
26756ccda740Sloli 				err = zfs_prop_set_special(dsname, source,
26766ccda740Sloli 				    pair);
267792241e0bSTom Erickson 			if (err == -1) {
267892241e0bSTom Erickson 				/*
267992241e0bSTom Erickson 				 * For better performance we build up a list of
268092241e0bSTom Erickson 				 * properties to set in a single transaction.
268192241e0bSTom Erickson 				 */
268292241e0bSTom Erickson 				err = nvlist_add_nvpair(genericnvl, pair);
268392241e0bSTom Erickson 			} else if (err != 0 && nvl != retrynvl) {
268492241e0bSTom Erickson 				/*
268592241e0bSTom Erickson 				 * This may be a spurious error caused by
268692241e0bSTom Erickson 				 * receiving quota and reservation out of order.
268792241e0bSTom Erickson 				 * Try again in a second pass.
268892241e0bSTom Erickson 				 */
268992241e0bSTom Erickson 				err = nvlist_add_nvpair(retrynvl, pair);
269092241e0bSTom Erickson 			}
269192241e0bSTom Erickson 		}
269292241e0bSTom Erickson 
26934445fffbSMatthew Ahrens 		if (err != 0) {
26944445fffbSMatthew Ahrens 			if (errlist != NULL)
26954445fffbSMatthew Ahrens 				fnvlist_add_int32(errlist, propname, err);
26964445fffbSMatthew Ahrens 			rv = err;
26974445fffbSMatthew Ahrens 		}
2698e9dbad6fSeschrock 	}
2699e9dbad6fSeschrock 
270092241e0bSTom Erickson 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
270192241e0bSTom Erickson 		nvl = retrynvl;
270292241e0bSTom Erickson 		goto retry;
270392241e0bSTom Erickson 	}
270492241e0bSTom Erickson 
270592241e0bSTom Erickson 	if (!nvlist_empty(genericnvl) &&
270692241e0bSTom Erickson 	    dsl_props_set(dsname, source, genericnvl) != 0) {
270792241e0bSTom Erickson 		/*
270892241e0bSTom Erickson 		 * If this fails, we still want to set as many properties as we
270992241e0bSTom Erickson 		 * can, so try setting them individually.
271092241e0bSTom Erickson 		 */
271192241e0bSTom Erickson 		pair = NULL;
271292241e0bSTom Erickson 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
271392241e0bSTom Erickson 			const char *propname = nvpair_name(pair);
2714cfa69fd2STom Erickson 			int err = 0;
271592241e0bSTom Erickson 
271692241e0bSTom Erickson 			propval = pair;
271792241e0bSTom Erickson 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
271892241e0bSTom Erickson 				nvlist_t *attrs;
27194445fffbSMatthew Ahrens 				attrs = fnvpair_value_nvlist(pair);
27204445fffbSMatthew Ahrens 				propval = fnvlist_lookup_nvpair(attrs,
27214445fffbSMatthew Ahrens 				    ZPROP_VALUE);
272292241e0bSTom Erickson 			}
272392241e0bSTom Erickson 
272492241e0bSTom Erickson 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
27254445fffbSMatthew Ahrens 				strval = fnvpair_value_string(propval);
27263b2aab18SMatthew Ahrens 				err = dsl_prop_set_string(dsname, propname,
27273b2aab18SMatthew Ahrens 				    source, strval);
27286ccda740Sloli 			} else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
27296ccda740Sloli 				err = dsl_prop_inherit(dsname, propname,
27306ccda740Sloli 				    source);
273192241e0bSTom Erickson 			} else {
27324445fffbSMatthew Ahrens 				intval = fnvpair_value_uint64(propval);
27333b2aab18SMatthew Ahrens 				err = dsl_prop_set_int(dsname, propname, source,
27343b2aab18SMatthew Ahrens 				    intval);
273592241e0bSTom Erickson 			}
273692241e0bSTom Erickson 
273792241e0bSTom Erickson 			if (err != 0) {
27384445fffbSMatthew Ahrens 				if (errlist != NULL) {
27394445fffbSMatthew Ahrens 					fnvlist_add_int32(errlist, propname,
27404445fffbSMatthew Ahrens 					    err);
27414445fffbSMatthew Ahrens 				}
27424445fffbSMatthew Ahrens 				rv = err;
274392241e0bSTom Erickson 			}
274492241e0bSTom Erickson 		}
27455c0b6a79SRich Morris 	}
27465c0b6a79SRich Morris 	nvlist_free(genericnvl);
274792241e0bSTom Erickson 	nvlist_free(retrynvl);
274892241e0bSTom Erickson 
274992241e0bSTom Erickson 	return (rv);
2750fa9e4066Sahrens }
2751fa9e4066Sahrens 
2752ea2f5b9eSMatthew Ahrens /*
2753ea2f5b9eSMatthew Ahrens  * Check that all the properties are valid user properties.
2754ea2f5b9eSMatthew Ahrens  */
2755ea2f5b9eSMatthew Ahrens static int
27564445fffbSMatthew Ahrens zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2757ea2f5b9eSMatthew Ahrens {
275892241e0bSTom Erickson 	nvpair_t *pair = NULL;
2759ea2f5b9eSMatthew Ahrens 	int error = 0;
2760ea2f5b9eSMatthew Ahrens 
276192241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
276292241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
2763ea2f5b9eSMatthew Ahrens 
2764ea2f5b9eSMatthew Ahrens 		if (!zfs_prop_user(propname) ||
276592241e0bSTom Erickson 		    nvpair_type(pair) != DATA_TYPE_STRING)
2766be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
2767ea2f5b9eSMatthew Ahrens 
2768ea2f5b9eSMatthew Ahrens 		if (error = zfs_secpolicy_write_perms(fsname,
2769ea2f5b9eSMatthew Ahrens 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2770ea2f5b9eSMatthew Ahrens 			return (error);
2771ea2f5b9eSMatthew Ahrens 
2772ea2f5b9eSMatthew Ahrens 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2773be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
2774ea2f5b9eSMatthew Ahrens 
277578f17100SMatthew Ahrens 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2776ea2f5b9eSMatthew Ahrens 			return (E2BIG);
2777ea2f5b9eSMatthew Ahrens 	}
2778ea2f5b9eSMatthew Ahrens 	return (0);
2779ea2f5b9eSMatthew Ahrens }
2780ea2f5b9eSMatthew Ahrens 
278192241e0bSTom Erickson static void
278292241e0bSTom Erickson props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
278392241e0bSTom Erickson {
278492241e0bSTom Erickson 	nvpair_t *pair;
278592241e0bSTom Erickson 
278692241e0bSTom Erickson 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
278792241e0bSTom Erickson 
278892241e0bSTom Erickson 	pair = NULL;
278992241e0bSTom Erickson 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
279092241e0bSTom Erickson 		if (nvlist_exists(skipped, nvpair_name(pair)))
279192241e0bSTom Erickson 			continue;
279292241e0bSTom Erickson 
279392241e0bSTom Erickson 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
279492241e0bSTom Erickson 	}
279592241e0bSTom Erickson }
279692241e0bSTom Erickson 
279792241e0bSTom Erickson static int
27983b2aab18SMatthew Ahrens clear_received_props(const char *dsname, nvlist_t *props,
279992241e0bSTom Erickson     nvlist_t *skipped)
280092241e0bSTom Erickson {
280192241e0bSTom Erickson 	int err = 0;
280292241e0bSTom Erickson 	nvlist_t *cleared_props = NULL;
280392241e0bSTom Erickson 	props_skip(props, skipped, &cleared_props);
280492241e0bSTom Erickson 	if (!nvlist_empty(cleared_props)) {
280592241e0bSTom Erickson 		/*
280692241e0bSTom Erickson 		 * Acts on local properties until the dataset has received
280792241e0bSTom Erickson 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
280892241e0bSTom Erickson 		 */
280992241e0bSTom Erickson 		zprop_source_t flags = (ZPROP_SRC_NONE |
28103b2aab18SMatthew Ahrens 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
28113b2aab18SMatthew Ahrens 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
281292241e0bSTom Erickson 	}
281392241e0bSTom Erickson 	nvlist_free(cleared_props);
281492241e0bSTom Erickson 	return (err);
281592241e0bSTom Erickson }
281692241e0bSTom Erickson 
28173cb34c60Sahrens /*
28183cb34c60Sahrens  * inputs:
28193cb34c60Sahrens  * zc_name		name of filesystem
28205c0b6a79SRich Morris  * zc_value		name of property to set
28213cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of properties to apply
282292241e0bSTom Erickson  * zc_cookie		received properties flag
28233cb34c60Sahrens  *
282492241e0bSTom Erickson  * outputs:
282592241e0bSTom Erickson  * zc_nvlist_dst{_size} error for each unapplied received property
28263cb34c60Sahrens  */
2827fa9e4066Sahrens static int
2828e9dbad6fSeschrock zfs_ioc_set_prop(zfs_cmd_t *zc)
2829fa9e4066Sahrens {
2830e9dbad6fSeschrock 	nvlist_t *nvl;
283192241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
283292241e0bSTom Erickson 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
283392241e0bSTom Erickson 	    ZPROP_SRC_LOCAL);
28344445fffbSMatthew Ahrens 	nvlist_t *errors;
2835e9dbad6fSeschrock 	int error;
2836e9dbad6fSeschrock 
2837990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2838478ed9adSEric Taylor 	    zc->zc_iflags, &nvl)) != 0)
2839e9dbad6fSeschrock 		return (error);
2840e9dbad6fSeschrock 
284192241e0bSTom Erickson 	if (received) {
2842bb0ade09Sahrens 		nvlist_t *origprops;
2843bb0ade09Sahrens 
28443b2aab18SMatthew Ahrens 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
28453b2aab18SMatthew Ahrens 			(void) clear_received_props(zc->zc_name,
28463b2aab18SMatthew Ahrens 			    origprops, nvl);
28473b2aab18SMatthew Ahrens 			nvlist_free(origprops);
2848bb0ade09Sahrens 		}
28493b2aab18SMatthew Ahrens 
28503b2aab18SMatthew Ahrens 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2851bb0ade09Sahrens 	}
2852bb0ade09Sahrens 
28534445fffbSMatthew Ahrens 	errors = fnvlist_alloc();
28543b2aab18SMatthew Ahrens 	if (error == 0)
28553b2aab18SMatthew Ahrens 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
285692241e0bSTom Erickson 
2857dd328bf6SToomas Soome 	if (zc->zc_nvlist_dst != 0 && errors != NULL) {
285892241e0bSTom Erickson 		(void) put_nvlist(zc, errors);
285992241e0bSTom Erickson 	}
2860ecd6cf80Smarks 
286192241e0bSTom Erickson 	nvlist_free(errors);
2862e9dbad6fSeschrock 	nvlist_free(nvl);
2863e9dbad6fSeschrock 	return (error);
2864fa9e4066Sahrens }
2865fa9e4066Sahrens 
28663cb34c60Sahrens /*
28673cb34c60Sahrens  * inputs:
28683cb34c60Sahrens  * zc_name		name of filesystem
28693cb34c60Sahrens  * zc_value		name of property to inherit
287092241e0bSTom Erickson  * zc_cookie		revert to received value if TRUE
28713cb34c60Sahrens  *
28723cb34c60Sahrens  * outputs:		none
28733cb34c60Sahrens  */
2874e45ce728Sahrens static int
2875e45ce728Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2876e45ce728Sahrens {
287792241e0bSTom Erickson 	const char *propname = zc->zc_value;
287892241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
287992241e0bSTom Erickson 	boolean_t received = zc->zc_cookie;
288092241e0bSTom Erickson 	zprop_source_t source = (received
288192241e0bSTom Erickson 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
288292241e0bSTom Erickson 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
288392241e0bSTom Erickson 
288492241e0bSTom Erickson 	if (received) {
288592241e0bSTom Erickson 		nvlist_t *dummy;
288692241e0bSTom Erickson 		nvpair_t *pair;
288792241e0bSTom Erickson 		zprop_type_t type;
288892241e0bSTom Erickson 		int err;
288992241e0bSTom Erickson 
289092241e0bSTom Erickson 		/*
289192241e0bSTom Erickson 		 * zfs_prop_set_special() expects properties in the form of an
289292241e0bSTom Erickson 		 * nvpair with type info.
289392241e0bSTom Erickson 		 */
289492241e0bSTom Erickson 		if (prop == ZPROP_INVAL) {
289592241e0bSTom Erickson 			if (!zfs_prop_user(propname))
2896be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
289792241e0bSTom Erickson 
289892241e0bSTom Erickson 			type = PROP_TYPE_STRING;
2899a79992aaSTom Erickson 		} else if (prop == ZFS_PROP_VOLSIZE ||
2900a79992aaSTom Erickson 		    prop == ZFS_PROP_VERSION) {
2901be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
290292241e0bSTom Erickson 		} else {
290392241e0bSTom Erickson 			type = zfs_prop_get_type(prop);
290492241e0bSTom Erickson 		}
290592241e0bSTom Erickson 
290692241e0bSTom Erickson 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
290792241e0bSTom Erickson 
290892241e0bSTom Erickson 		switch (type) {
290992241e0bSTom Erickson 		case PROP_TYPE_STRING:
291092241e0bSTom Erickson 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
291192241e0bSTom Erickson 			break;
291292241e0bSTom Erickson 		case PROP_TYPE_NUMBER:
291392241e0bSTom Erickson 		case PROP_TYPE_INDEX:
291492241e0bSTom Erickson 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
291592241e0bSTom Erickson 			break;
291692241e0bSTom Erickson 		default:
291792241e0bSTom Erickson 			nvlist_free(dummy);
2918be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
291992241e0bSTom Erickson 		}
292092241e0bSTom Erickson 
292192241e0bSTom Erickson 		pair = nvlist_next_nvpair(dummy, NULL);
292292241e0bSTom Erickson 		err = zfs_prop_set_special(zc->zc_name, source, pair);
292392241e0bSTom Erickson 		nvlist_free(dummy);
292492241e0bSTom Erickson 		if (err != -1)
292592241e0bSTom Erickson 			return (err); /* special property already handled */
292692241e0bSTom Erickson 	} else {
292792241e0bSTom Erickson 		/*
292892241e0bSTom Erickson 		 * Only check this in the non-received case. We want to allow
292992241e0bSTom Erickson 		 * 'inherit -S' to revert non-inheritable properties like quota
293092241e0bSTom Erickson 		 * and reservation to the received or default values even though
293192241e0bSTom Erickson 		 * they are not considered inheritable.
293292241e0bSTom Erickson 		 */
293392241e0bSTom Erickson 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2934be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
293592241e0bSTom Erickson 	}
293692241e0bSTom Erickson 
29374445fffbSMatthew Ahrens 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
29383b2aab18SMatthew Ahrens 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2939e45ce728Sahrens }
2940e45ce728Sahrens 
2941b1b8ab34Slling static int
294211a41203Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2943b1b8ab34Slling {
2944990b4856Slling 	nvlist_t *props;
2945b1b8ab34Slling 	spa_t *spa;
2946990b4856Slling 	int error;
294792241e0bSTom Erickson 	nvpair_t *pair;
2948b1b8ab34Slling 
294992241e0bSTom Erickson 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
295092241e0bSTom Erickson 	    zc->zc_iflags, &props))
2951b1b8ab34Slling 		return (error);
2952b1b8ab34Slling 
2953379c004dSEric Schrock 	/*
2954379c004dSEric Schrock 	 * If the only property is the configfile, then just do a spa_lookup()
2955379c004dSEric Schrock 	 * to handle the faulted case.
2956379c004dSEric Schrock 	 */
295792241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
295892241e0bSTom Erickson 	if (pair != NULL && strcmp(nvpair_name(pair),
2959379c004dSEric Schrock 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
296092241e0bSTom Erickson 	    nvlist_next_nvpair(props, pair) == NULL) {
2961379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
2962379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2963379c004dSEric Schrock 			spa_configfile_set(spa, props, B_FALSE);
29645cabbc6bSPrashanth Sreenivasa 			spa_write_cachefile(spa, B_FALSE, B_TRUE);
2965379c004dSEric Schrock 		}
2966379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
2967b693757aSEric Schrock 		if (spa != NULL) {
2968b693757aSEric Schrock 			nvlist_free(props);
2969379c004dSEric Schrock 			return (0);
2970b693757aSEric Schrock 		}
2971379c004dSEric Schrock 	}
2972379c004dSEric Schrock 
2973b1b8ab34Slling 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2974990b4856Slling 		nvlist_free(props);
2975b1b8ab34Slling 		return (error);
2976b1b8ab34Slling 	}
2977b1b8ab34Slling 
2978990b4856Slling 	error = spa_prop_set(spa, props);
2979b1b8ab34Slling 
2980990b4856Slling 	nvlist_free(props);
2981b1b8ab34Slling 	spa_close(spa, FTAG);
2982b1b8ab34Slling 
2983b1b8ab34Slling 	return (error);
2984b1b8ab34Slling }
2985b1b8ab34Slling 
2986b1b8ab34Slling static int
298711a41203Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2988b1b8ab34Slling {
2989b1b8ab34Slling 	spa_t *spa;
2990b1b8ab34Slling 	int error;
2991b1b8ab34Slling 	nvlist_t *nvp = NULL;
2992b1b8ab34Slling 
2993379c004dSEric Schrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2994379c004dSEric Schrock 		/*
2995379c004dSEric Schrock 		 * If the pool is faulted, there may be properties we can still
2996379c004dSEric Schrock 		 * get (such as altroot and cachefile), so attempt to get them
2997379c004dSEric Schrock 		 * anyway.
2998379c004dSEric Schrock 		 */
2999379c004dSEric Schrock 		mutex_enter(&spa_namespace_lock);
3000379c004dSEric Schrock 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
3001379c004dSEric Schrock 			error = spa_prop_get(spa, &nvp);
3002379c004dSEric Schrock 		mutex_exit(&spa_namespace_lock);
3003379c004dSEric Schrock 	} else {
3004379c004dSEric Schrock 		error = spa_prop_get(spa, &nvp);
3005379c004dSEric Schrock 		spa_close(spa, FTAG);
3006379c004dSEric Schrock 	}
3007b1b8ab34Slling 
3008dd328bf6SToomas Soome 	if (error == 0 && zc->zc_nvlist_dst != 0)
3009b1b8ab34Slling 		error = put_nvlist(zc, nvp);
3010b1b8ab34Slling 	else
3011be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
3012b1b8ab34Slling 
3013379c004dSEric Schrock 	nvlist_free(nvp);
3014b1b8ab34Slling 	return (error);
3015b1b8ab34Slling }
3016b1b8ab34Slling 
30173cb34c60Sahrens /*
30183cb34c60Sahrens  * inputs:
30193cb34c60Sahrens  * zc_name		name of filesystem
30203cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
30213cb34c60Sahrens  * zc_perm_action	allow/unallow flag
30223cb34c60Sahrens  *
30233cb34c60Sahrens  * outputs:		none
30243cb34c60Sahrens  */
3025ecd6cf80Smarks static int
3026ecd6cf80Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3027ecd6cf80Smarks {
3028ecd6cf80Smarks 	int error;
3029ecd6cf80Smarks 	nvlist_t *fsaclnv = NULL;
3030ecd6cf80Smarks 
3031990b4856Slling 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3032478ed9adSEric Taylor 	    zc->zc_iflags, &fsaclnv)) != 0)
3033ecd6cf80Smarks 		return (error);
3034ecd6cf80Smarks 
3035ecd6cf80Smarks 	/*
3036ecd6cf80Smarks 	 * Verify nvlist is constructed correctly
3037ecd6cf80Smarks 	 */
3038ecd6cf80Smarks 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3039ecd6cf80Smarks 		nvlist_free(fsaclnv);
3040be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3041ecd6cf80Smarks 	}
3042ecd6cf80Smarks 
3043ecd6cf80Smarks 	/*
3044ecd6cf80Smarks 	 * If we don't have PRIV_SYS_MOUNT, then validate
3045ecd6cf80Smarks 	 * that user is allowed to hand out each permission in
3046ecd6cf80Smarks 	 * the nvlist(s)
3047ecd6cf80Smarks 	 */
3048ecd6cf80Smarks 
304991ebeef5Sahrens 	error = secpolicy_zfs(CRED());
30503b2aab18SMatthew Ahrens 	if (error != 0) {
305191ebeef5Sahrens 		if (zc->zc_perm_action == B_FALSE) {
305291ebeef5Sahrens 			error = dsl_deleg_can_allow(zc->zc_name,
305391ebeef5Sahrens 			    fsaclnv, CRED());
305491ebeef5Sahrens 		} else {
305591ebeef5Sahrens 			error = dsl_deleg_can_unallow(zc->zc_name,
305691ebeef5Sahrens 			    fsaclnv, CRED());
305791ebeef5Sahrens 		}
3058ecd6cf80Smarks 	}
3059ecd6cf80Smarks 
3060ecd6cf80Smarks 	if (error == 0)
3061ecd6cf80Smarks 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3062ecd6cf80Smarks 
3063ecd6cf80Smarks 	nvlist_free(fsaclnv);
3064ecd6cf80Smarks 	return (error);
3065ecd6cf80Smarks }
3066ecd6cf80Smarks 
30673cb34c60Sahrens /*
30683cb34c60Sahrens  * inputs:
30693cb34c60Sahrens  * zc_name		name of filesystem
30703cb34c60Sahrens  *
30713cb34c60Sahrens  * outputs:
30723cb34c60Sahrens  * zc_nvlist_src{_size}	nvlist of delegated permissions
30733cb34c60Sahrens  */
3074ecd6cf80Smarks static int
3075ecd6cf80Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3076ecd6cf80Smarks {
3077ecd6cf80Smarks 	nvlist_t *nvp;
3078ecd6cf80Smarks 	int error;
3079ecd6cf80Smarks 
3080ecd6cf80Smarks 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3081ecd6cf80Smarks 		error = put_nvlist(zc, nvp);
3082ecd6cf80Smarks 		nvlist_free(nvp);
3083ecd6cf80Smarks 	}
3084ecd6cf80Smarks 
3085ecd6cf80Smarks 	return (error);
3086ecd6cf80Smarks }
3087ecd6cf80Smarks 
3088ecd6cf80Smarks /* ARGSUSED */
3089fa9e4066Sahrens static void
3090ecd6cf80Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3091fa9e4066Sahrens {
3092da6c28aaSamw 	zfs_creat_t *zct = arg;
3093da6c28aaSamw 
3094de8267e0Stimh 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3095da6c28aaSamw }
3096da6c28aaSamw 
3097de8267e0Stimh #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3098da6c28aaSamw 
3099da6c28aaSamw /*
3100de8267e0Stimh  * inputs:
31010a48a24eStimh  * os			parent objset pointer (NULL if root fs)
3102f7170741SWill Andrews  * fuids_ok		fuids allowed in this version of the spa?
3103f7170741SWill Andrews  * sa_ok		SAs allowed in this version of the spa?
3104f7170741SWill Andrews  * createprops		list of properties requested by creator
3105de8267e0Stimh  *
3106de8267e0Stimh  * outputs:
3107de8267e0Stimh  * zplprops	values for the zplprops we attach to the master node object
31080a48a24eStimh  * is_ci	true if requested file system will be purely case-insensitive
3109da6c28aaSamw  *
3110de8267e0Stimh  * Determine the settings for utf8only, normalization and
3111de8267e0Stimh  * casesensitivity.  Specific values may have been requested by the
3112de8267e0Stimh  * creator and/or we can inherit values from the parent dataset.  If
3113de8267e0Stimh  * the file system is of too early a vintage, a creator can not
3114de8267e0Stimh  * request settings for these properties, even if the requested
3115de8267e0Stimh  * setting is the default value.  We don't actually want to create dsl
3116de8267e0Stimh  * properties for these, so remove them from the source nvlist after
3117de8267e0Stimh  * processing.
3118da6c28aaSamw  */
3119da6c28aaSamw static int
312014843421SMatthew Ahrens zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
31210a586ceaSMark Shellenbaum     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
31220a586ceaSMark Shellenbaum     nvlist_t *zplprops, boolean_t *is_ci)
3123da6c28aaSamw {
3124de8267e0Stimh 	uint64_t sense = ZFS_PROP_UNDEFINED;
3125de8267e0Stimh 	uint64_t norm = ZFS_PROP_UNDEFINED;
3126de8267e0Stimh 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3127da6c28aaSamw 
3128de8267e0Stimh 	ASSERT(zplprops != NULL);
3129da6c28aaSamw 
3130b127fe3cSAndriy Gapon 	if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3131b127fe3cSAndriy Gapon 		return (SET_ERROR(EINVAL));
3132b127fe3cSAndriy Gapon 
3133de8267e0Stimh 	/*
3134de8267e0Stimh 	 * Pull out creator prop choices, if any.
3135de8267e0Stimh 	 */
3136de8267e0Stimh 	if (createprops) {
31370a48a24eStimh 		(void) nvlist_lookup_uint64(createprops,
31380a48a24eStimh 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3139de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3140de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3141de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3142de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3143de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3144de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3145de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3146de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3147de8267e0Stimh 		(void) nvlist_lookup_uint64(createprops,
3148de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3149de8267e0Stimh 		(void) nvlist_remove_all(createprops,
3150de8267e0Stimh 		    zfs_prop_to_name(ZFS_PROP_CASE));
3151de8267e0Stimh 	}
3152da6c28aaSamw 
3153c2a93d44Stimh 	/*
31540a48a24eStimh 	 * If the zpl version requested is whacky or the file system
31550a48a24eStimh 	 * or pool is version is too "young" to support normalization
31560a48a24eStimh 	 * and the creator tried to set a value for one of the props,
31570a48a24eStimh 	 * error out.
3158c2a93d44Stimh 	 */
31590a48a24eStimh 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
31600a48a24eStimh 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
31610a586ceaSMark Shellenbaum 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
31620a48a24eStimh 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3163de8267e0Stimh 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
31640a48a24eStimh 	    sense != ZFS_PROP_UNDEFINED)))
3165be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3166c2a93d44Stimh 
3167de8267e0Stimh 	/*
3168de8267e0Stimh 	 * Put the version in the zplprops
3169de8267e0Stimh 	 */
3170de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3171de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3172da6c28aaSamw 
3173de8267e0Stimh 	if (norm == ZFS_PROP_UNDEFINED)
3174de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3175de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3176de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3177da6c28aaSamw 
3178c2a93d44Stimh 	/*
3179de8267e0Stimh 	 * If we're normalizing, names must always be valid UTF-8 strings.
3180c2a93d44Stimh 	 */
3181de8267e0Stimh 	if (norm)
3182de8267e0Stimh 		u8 = 1;
3183de8267e0Stimh 	if (u8 == ZFS_PROP_UNDEFINED)
3184de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3185de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3186de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3187de8267e0Stimh 
3188de8267e0Stimh 	if (sense == ZFS_PROP_UNDEFINED)
3189de8267e0Stimh 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3190de8267e0Stimh 	VERIFY(nvlist_add_uint64(zplprops,
3191de8267e0Stimh 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3192c2a93d44Stimh 
3193ab04eb8eStimh 	if (is_ci)
3194ab04eb8eStimh 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3195ab04eb8eStimh 
3196da6c28aaSamw 	return (0);
3197fa9e4066Sahrens }
3198fa9e4066Sahrens 
31990a48a24eStimh static int
32000a48a24eStimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
32010a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
32020a48a24eStimh {
32030a586ceaSMark Shellenbaum 	boolean_t fuids_ok, sa_ok;
32040a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
32050a48a24eStimh 	objset_t *os = NULL;
32069adfa60dSMatthew Ahrens 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
32070a48a24eStimh 	char *cp;
32080a586ceaSMark Shellenbaum 	spa_t *spa;
32090a586ceaSMark Shellenbaum 	uint64_t spa_vers;
32100a48a24eStimh 	int error;
32110a48a24eStimh 
32120a48a24eStimh 	(void) strlcpy(parentname, dataset, sizeof (parentname));
32130a48a24eStimh 	cp = strrchr(parentname, '/');
32140a48a24eStimh 	ASSERT(cp != NULL);
32150a48a24eStimh 	cp[0] = '\0';
32160a48a24eStimh 
32170a586ceaSMark Shellenbaum 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
32180a586ceaSMark Shellenbaum 		return (error);
32190a586ceaSMark Shellenbaum 
32200a586ceaSMark Shellenbaum 	spa_vers = spa_version(spa);
32210a586ceaSMark Shellenbaum 	spa_close(spa, FTAG);
32220a586ceaSMark Shellenbaum 
32230a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
32240a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
32250a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
32260a48a24eStimh 
32270a48a24eStimh 	/*
32280a48a24eStimh 	 * Open parent object set so we can inherit zplprop values.
32290a48a24eStimh 	 */
3230503ad85cSMatthew Ahrens 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
32310a48a24eStimh 		return (error);
32320a48a24eStimh 
32330a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
32340a48a24eStimh 	    zplprops, is_ci);
3235503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
32360a48a24eStimh 	return (error);
32370a48a24eStimh }
32380a48a24eStimh 
32390a48a24eStimh static int
32400a48a24eStimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
32410a48a24eStimh     nvlist_t *zplprops, boolean_t *is_ci)
32420a48a24eStimh {
32430a586ceaSMark Shellenbaum 	boolean_t fuids_ok;
32440a586ceaSMark Shellenbaum 	boolean_t sa_ok;
32450a48a24eStimh 	uint64_t zplver = ZPL_VERSION;
32460a48a24eStimh 	int error;
32470a48a24eStimh 
32480a586ceaSMark Shellenbaum 	zplver = zfs_zpl_version_map(spa_vers);
32490a586ceaSMark Shellenbaum 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
32500a586ceaSMark Shellenbaum 	sa_ok = (zplver >= ZPL_VERSION_SA);
32510a48a24eStimh 
32520a586ceaSMark Shellenbaum 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
32530a586ceaSMark Shellenbaum 	    createprops, zplprops, is_ci);
32540a48a24eStimh 	return (error);
32550a48a24eStimh }
32560a48a24eStimh 
32573cb34c60Sahrens /*
32584445fffbSMatthew Ahrens  * innvl: {
32594445fffbSMatthew Ahrens  *     "type" -> dmu_objset_type_t (int32)
32604445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
3261eb633035STom Caputi  *     (optional) "hidden_args" -> { "wkeydata" -> value }
3262eb633035STom Caputi  *         raw uint8_t array of encryption wrapping key data (32 bytes)
32634445fffbSMatthew Ahrens  * }
32643cb34c60Sahrens  *
32654445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
32663cb34c60Sahrens  */
3267fa9e4066Sahrens static int
32684445fffbSMatthew Ahrens zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3269fa9e4066Sahrens {
3270fa9e4066Sahrens 	int error = 0;
32714445fffbSMatthew Ahrens 	zfs_creat_t zct = { 0 };
3272ecd6cf80Smarks 	nvlist_t *nvprops = NULL;
3273eb633035STom Caputi 	nvlist_t *hidden_args = NULL;
3274ecd6cf80Smarks 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
32754445fffbSMatthew Ahrens 	int32_t type32;
32764445fffbSMatthew Ahrens 	dmu_objset_type_t type;
32774445fffbSMatthew Ahrens 	boolean_t is_insensitive = B_FALSE;
3278eb633035STom Caputi 	dsl_crypto_params_t *dcp = NULL;
3279fa9e4066Sahrens 
32804445fffbSMatthew Ahrens 	if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3281be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32824445fffbSMatthew Ahrens 	type = type32;
32834445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3284eb633035STom Caputi 	(void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3285fa9e4066Sahrens 
32864445fffbSMatthew Ahrens 	switch (type) {
3287fa9e4066Sahrens 	case DMU_OST_ZFS:
3288fa9e4066Sahrens 		cbfunc = zfs_create_cb;
3289fa9e4066Sahrens 		break;
3290fa9e4066Sahrens 
3291fa9e4066Sahrens 	case DMU_OST_ZVOL:
3292fa9e4066Sahrens 		cbfunc = zvol_create_cb;
3293fa9e4066Sahrens 		break;
3294fa9e4066Sahrens 
3295fa9e4066Sahrens 	default:
32961d452cf5Sahrens 		cbfunc = NULL;
3297e7cbe64fSgw 		break;
3298fa9e4066Sahrens 	}
32994445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
33004445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3301be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3302fa9e4066Sahrens 
3303da6c28aaSamw 	zct.zct_props = nvprops;
3304da6c28aaSamw 
33054445fffbSMatthew Ahrens 	if (cbfunc == NULL)
3306be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33074445fffbSMatthew Ahrens 
33084445fffbSMatthew Ahrens 	if (type == DMU_OST_ZVOL) {
33094445fffbSMatthew Ahrens 		uint64_t volsize, volblocksize;
33104445fffbSMatthew Ahrens 
33114445fffbSMatthew Ahrens 		if (nvprops == NULL)
3312be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
33134445fffbSMatthew Ahrens 		if (nvlist_lookup_uint64(nvprops,
33144445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3315be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3316fa9e4066Sahrens 
33174445fffbSMatthew Ahrens 		if ((error = nvlist_lookup_uint64(nvprops,
33184445fffbSMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
33194445fffbSMatthew Ahrens 		    &volblocksize)) != 0 && error != ENOENT)
3320be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
33214445fffbSMatthew Ahrens 
33224445fffbSMatthew Ahrens 		if (error != 0)
33234445fffbSMatthew Ahrens 			volblocksize = zfs_prop_default_numeric(
33244445fffbSMatthew Ahrens 			    ZFS_PROP_VOLBLOCKSIZE);
33254445fffbSMatthew Ahrens 
33264445fffbSMatthew Ahrens 		if ((error = zvol_check_volblocksize(
33274445fffbSMatthew Ahrens 		    volblocksize)) != 0 ||
33284445fffbSMatthew Ahrens 		    (error = zvol_check_volsize(volsize,
33294445fffbSMatthew Ahrens 		    volblocksize)) != 0)
3330fa9e4066Sahrens 			return (error);
33314445fffbSMatthew Ahrens 	} else if (type == DMU_OST_ZFS) {
33324445fffbSMatthew Ahrens 		int error;
3333ab04eb8eStimh 
33344445fffbSMatthew Ahrens 		/*
33354445fffbSMatthew Ahrens 		 * We have to have normalization and
33364445fffbSMatthew Ahrens 		 * case-folding flags correct when we do the
33374445fffbSMatthew Ahrens 		 * file system creation, so go figure them out
33384445fffbSMatthew Ahrens 		 * now.
33394445fffbSMatthew Ahrens 		 */
33404445fffbSMatthew Ahrens 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
33414445fffbSMatthew Ahrens 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
33424445fffbSMatthew Ahrens 		error = zfs_fill_zplprops(fsname, nvprops,
33434445fffbSMatthew Ahrens 		    zct.zct_zplprops, &is_insensitive);
33444445fffbSMatthew Ahrens 		if (error != 0) {
33454445fffbSMatthew Ahrens 			nvlist_free(zct.zct_zplprops);
3346da6c28aaSamw 			return (error);
3347da6c28aaSamw 		}
33484445fffbSMatthew Ahrens 	}
3349ab04eb8eStimh 
3350eb633035STom Caputi 	error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3351eb633035STom Caputi 	    hidden_args, &dcp);
3352eb633035STom Caputi 	if (error != 0) {
3353eb633035STom Caputi 		nvlist_free(zct.zct_zplprops);
3354eb633035STom Caputi 		return (error);
3355eb633035STom Caputi 	}
3356eb633035STom Caputi 
33574445fffbSMatthew Ahrens 	error = dmu_objset_create(fsname, type,
3358eb633035STom Caputi 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3359eb633035STom Caputi 
33604445fffbSMatthew Ahrens 	nvlist_free(zct.zct_zplprops);
3361eb633035STom Caputi 	dsl_crypto_params_free(dcp, !!error);
33625c5460e9Seschrock 
33634445fffbSMatthew Ahrens 	/*
33644445fffbSMatthew Ahrens 	 * It would be nice to do this atomically.
33654445fffbSMatthew Ahrens 	 */
33664445fffbSMatthew Ahrens 	if (error == 0) {
33674445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
33684445fffbSMatthew Ahrens 		    nvprops, outnvl);
33694445fffbSMatthew Ahrens 		if (error != 0)
33703b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
33714445fffbSMatthew Ahrens 	}
33724445fffbSMatthew Ahrens 	return (error);
33734445fffbSMatthew Ahrens }
3374e9dbad6fSeschrock 
33754445fffbSMatthew Ahrens /*
33764445fffbSMatthew Ahrens  * innvl: {
33774445fffbSMatthew Ahrens  *     "origin" -> name of origin snapshot
33784445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value }
3379eb633035STom Caputi  *     (optional) "hidden_args" -> { "wkeydata" -> value }
3380eb633035STom Caputi  *         raw uint8_t array of encryption wrapping key data (32 bytes)
33814445fffbSMatthew Ahrens  * }
33824445fffbSMatthew Ahrens  *
33834445fffbSMatthew Ahrens  * outnvl: propname -> error code (int32)
33844445fffbSMatthew Ahrens  */
33854445fffbSMatthew Ahrens static int
33864445fffbSMatthew Ahrens zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
33874445fffbSMatthew Ahrens {
33884445fffbSMatthew Ahrens 	int error = 0;
33894445fffbSMatthew Ahrens 	nvlist_t *nvprops = NULL;
33904445fffbSMatthew Ahrens 	char *origin_name;
3391e9dbad6fSeschrock 
33924445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3393be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33944445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3395e9dbad6fSeschrock 
33964445fffbSMatthew Ahrens 	if (strchr(fsname, '@') ||
33974445fffbSMatthew Ahrens 	    strchr(fsname, '%'))
3398be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3399e9dbad6fSeschrock 
34004445fffbSMatthew Ahrens 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3401be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3402eb633035STom Caputi 
34033b2aab18SMatthew Ahrens 	error = dmu_objset_clone(fsname, origin_name);
3404e9dbad6fSeschrock 
3405e9dbad6fSeschrock 	/*
3406e9dbad6fSeschrock 	 * It would be nice to do this atomically.
3407e9dbad6fSeschrock 	 */
3408e9dbad6fSeschrock 	if (error == 0) {
34094445fffbSMatthew Ahrens 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
34104445fffbSMatthew Ahrens 		    nvprops, outnvl);
341192241e0bSTom Erickson 		if (error != 0)
34123b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(fsname);
3413e9dbad6fSeschrock 	}
3414fa9e4066Sahrens 	return (error);
3415fa9e4066Sahrens }
3416fa9e4066Sahrens 
34175cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
34185cabbc6bSPrashanth Sreenivasa static int
34195cabbc6bSPrashanth Sreenivasa zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34205cabbc6bSPrashanth Sreenivasa {
34215cabbc6bSPrashanth Sreenivasa 	if (strchr(fsname, '@') ||
34225cabbc6bSPrashanth Sreenivasa 	    strchr(fsname, '%'))
34235cabbc6bSPrashanth Sreenivasa 		return (SET_ERROR(EINVAL));
34245cabbc6bSPrashanth Sreenivasa 
34255cabbc6bSPrashanth Sreenivasa 	return (dmu_objset_remap_indirects(fsname));
34265cabbc6bSPrashanth Sreenivasa }
34275cabbc6bSPrashanth Sreenivasa 
34283cb34c60Sahrens /*
34294445fffbSMatthew Ahrens  * innvl: {
34304445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
34314445fffbSMatthew Ahrens  *     (optional) "props" -> { prop -> value (string) }
34324445fffbSMatthew Ahrens  * }
34334445fffbSMatthew Ahrens  *
34344445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
34353cb34c60Sahrens  */
3436fa9e4066Sahrens static int
34374445fffbSMatthew Ahrens zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3438fa9e4066Sahrens {
34394445fffbSMatthew Ahrens 	nvlist_t *snaps;
34404445fffbSMatthew Ahrens 	nvlist_t *props = NULL;
34414445fffbSMatthew Ahrens 	int error, poollen;
34424445fffbSMatthew Ahrens 	nvpair_t *pair;
3443bb0ade09Sahrens 
34444445fffbSMatthew Ahrens 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
34454445fffbSMatthew Ahrens 	if ((error = zfs_check_userprops(poolname, props)) != 0)
34464445fffbSMatthew Ahrens 		return (error);
34474445fffbSMatthew Ahrens 
34484445fffbSMatthew Ahrens 	if (!nvlist_empty(props) &&
34494445fffbSMatthew Ahrens 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3450be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
34514445fffbSMatthew Ahrens 
34524445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3453be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
34544445fffbSMatthew Ahrens 	poollen = strlen(poolname);
34554445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
34564445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
34574445fffbSMatthew Ahrens 		const char *name = nvpair_name(pair);
34584445fffbSMatthew Ahrens 		const char *cp = strchr(name, '@');
3459bb0ade09Sahrens 
34604445fffbSMatthew Ahrens 		/*
34614445fffbSMatthew Ahrens 		 * The snap name must contain an @, and the part after it must
34624445fffbSMatthew Ahrens 		 * contain only valid characters.
34634445fffbSMatthew Ahrens 		 */
346478f17100SMatthew Ahrens 		if (cp == NULL ||
346578f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3466be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3467bb0ade09Sahrens 
34684445fffbSMatthew Ahrens 		/*
34694445fffbSMatthew Ahrens 		 * The snap must be in the specified pool.
34704445fffbSMatthew Ahrens 		 */
34714445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
34724445fffbSMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '@'))
3473be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
34744445fffbSMatthew Ahrens 
34754445fffbSMatthew Ahrens 		/* This must be the only snap of this fs. */
34764445fffbSMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
34774445fffbSMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
34784445fffbSMatthew Ahrens 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
34794445fffbSMatthew Ahrens 			    == 0) {
3480be6fd75aSMatthew Ahrens 				return (SET_ERROR(EXDEV));
34814445fffbSMatthew Ahrens 			}
34824445fffbSMatthew Ahrens 		}
34834445fffbSMatthew Ahrens 	}
3484bb0ade09Sahrens 
34853b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot(snaps, props, outnvl);
34864445fffbSMatthew Ahrens 	return (error);
34874445fffbSMatthew Ahrens }
34884445fffbSMatthew Ahrens 
34894445fffbSMatthew Ahrens /*
34904445fffbSMatthew Ahrens  * innvl: "message" -> string
34914445fffbSMatthew Ahrens  */
34924445fffbSMatthew Ahrens /* ARGSUSED */
34934445fffbSMatthew Ahrens static int
34944445fffbSMatthew Ahrens zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
34954445fffbSMatthew Ahrens {
34964445fffbSMatthew Ahrens 	char *message;
34974445fffbSMatthew Ahrens 	spa_t *spa;
34984445fffbSMatthew Ahrens 	int error;
34994445fffbSMatthew Ahrens 	char *poolname;
35004445fffbSMatthew Ahrens 
35014445fffbSMatthew Ahrens 	/*
35024445fffbSMatthew Ahrens 	 * The poolname in the ioctl is not set, we get it from the TSD,
35034445fffbSMatthew Ahrens 	 * which was set at the end of the last successful ioctl that allows
35044445fffbSMatthew Ahrens 	 * logging.  The secpolicy func already checked that it is set.
35054445fffbSMatthew Ahrens 	 * Only one log ioctl is allowed after each successful ioctl, so
35064445fffbSMatthew Ahrens 	 * we clear the TSD here.
35074445fffbSMatthew Ahrens 	 */
35084445fffbSMatthew Ahrens 	poolname = tsd_get(zfs_allow_log_key);
35094445fffbSMatthew Ahrens 	(void) tsd_set(zfs_allow_log_key, NULL);
35104445fffbSMatthew Ahrens 	error = spa_open(poolname, &spa, FTAG);
35114445fffbSMatthew Ahrens 	strfree(poolname);
35124445fffbSMatthew Ahrens 	if (error != 0)
35134445fffbSMatthew Ahrens 		return (error);
35144445fffbSMatthew Ahrens 
35154445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "message", &message) != 0)  {
35164445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3517be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3518bb0ade09Sahrens 	}
3519ea2f5b9eSMatthew Ahrens 
35204445fffbSMatthew Ahrens 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
35214445fffbSMatthew Ahrens 		spa_close(spa, FTAG);
3522be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
35234445fffbSMatthew Ahrens 	}
3524ea2f5b9eSMatthew Ahrens 
35254445fffbSMatthew Ahrens 	error = spa_history_log(spa, message);
35264445fffbSMatthew Ahrens 	spa_close(spa, FTAG);
3527bb0ade09Sahrens 	return (error);
35281d452cf5Sahrens }
3529fa9e4066Sahrens 
35303b2aab18SMatthew Ahrens /*
35313b2aab18SMatthew Ahrens  * The dp_config_rwlock must not be held when calling this, because the
35323b2aab18SMatthew Ahrens  * unmount may need to write out data.
35333b2aab18SMatthew Ahrens  *
35343b2aab18SMatthew Ahrens  * This function is best-effort.  Callers must deal gracefully if it
35353b2aab18SMatthew Ahrens  * remains mounted (or is remounted after this call).
3536fc7a6e3fSWill Andrews  *
3537fc7a6e3fSWill Andrews  * Returns 0 if the argument is not a snapshot, or it is not currently a
3538fc7a6e3fSWill Andrews  * filesystem, or we were able to unmount it.  Returns error code otherwise.
35393b2aab18SMatthew Ahrens  */
3540ed992b0aSSerapheim Dimitropoulos void
35413b2aab18SMatthew Ahrens zfs_unmount_snap(const char *snapname)
35421d452cf5Sahrens {
3543ed992b0aSSerapheim Dimitropoulos 	vfs_t *vfsp = NULL;
3544ed992b0aSSerapheim Dimitropoulos 	zfsvfs_t *zfsvfs = NULL;
35451d452cf5Sahrens 
35463b2aab18SMatthew Ahrens 	if (strchr(snapname, '@') == NULL)
3547ed992b0aSSerapheim Dimitropoulos 		return;
35481d452cf5Sahrens 
3549ed992b0aSSerapheim Dimitropoulos 	int err = getzfsvfs(snapname, &zfsvfs);
3550ed992b0aSSerapheim Dimitropoulos 	if (err != 0) {
3551ed992b0aSSerapheim Dimitropoulos 		ASSERT3P(zfsvfs, ==, NULL);
3552ed992b0aSSerapheim Dimitropoulos 		return;
3553ed992b0aSSerapheim Dimitropoulos 	}
3554ed992b0aSSerapheim Dimitropoulos 	vfsp = zfsvfs->z_vfs;
35553b2aab18SMatthew Ahrens 
35563b2aab18SMatthew Ahrens 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
35571d452cf5Sahrens 
3558fc7a6e3fSWill Andrews 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
35594445fffbSMatthew Ahrens 	VFS_RELE(vfsp);
3560fc7a6e3fSWill Andrews 	if (err != 0)
3561ed992b0aSSerapheim Dimitropoulos 		return;
35624445fffbSMatthew Ahrens 
35634445fffbSMatthew Ahrens 	/*
35644445fffbSMatthew Ahrens 	 * Always force the unmount for snapshots.
35654445fffbSMatthew Ahrens 	 */
35663b2aab18SMatthew Ahrens 	(void) dounmount(vfsp, MS_FORCE, kcred);
35673b2aab18SMatthew Ahrens }
35683b2aab18SMatthew Ahrens 
35693b2aab18SMatthew Ahrens /* ARGSUSED */
35703b2aab18SMatthew Ahrens static int
35713b2aab18SMatthew Ahrens zfs_unmount_snap_cb(const char *snapname, void *arg)
35723b2aab18SMatthew Ahrens {
3573ed992b0aSSerapheim Dimitropoulos 	zfs_unmount_snap(snapname);
3574ed992b0aSSerapheim Dimitropoulos 	return (0);
35753b2aab18SMatthew Ahrens }
35763b2aab18SMatthew Ahrens 
35773b2aab18SMatthew Ahrens /*
35783b2aab18SMatthew Ahrens  * When a clone is destroyed, its origin may also need to be destroyed,
35793b2aab18SMatthew Ahrens  * in which case it must be unmounted.  This routine will do that unmount
35803b2aab18SMatthew Ahrens  * if necessary.
35813b2aab18SMatthew Ahrens  */
35823b2aab18SMatthew Ahrens void
35833b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(const char *fsname)
35843b2aab18SMatthew Ahrens {
35853b2aab18SMatthew Ahrens 	int error;
35863b2aab18SMatthew Ahrens 	objset_t *os;
35873b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
35883b2aab18SMatthew Ahrens 
35893b2aab18SMatthew Ahrens 	error = dmu_objset_hold(fsname, FTAG, &os);
35903b2aab18SMatthew Ahrens 	if (error != 0)
35913b2aab18SMatthew Ahrens 		return;
35923b2aab18SMatthew Ahrens 	ds = dmu_objset_ds(os);
35933b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
35949adfa60dSMatthew Ahrens 		char originname[ZFS_MAX_DATASET_NAME_LEN];
35953b2aab18SMatthew Ahrens 		dsl_dataset_name(ds->ds_prev, originname);
35963b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
3597ed992b0aSSerapheim Dimitropoulos 		zfs_unmount_snap(originname);
35983b2aab18SMatthew Ahrens 	} else {
35993b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
36003b2aab18SMatthew Ahrens 	}
36011d452cf5Sahrens }
36021d452cf5Sahrens 
36033cb34c60Sahrens /*
36044445fffbSMatthew Ahrens  * innvl: {
36054445fffbSMatthew Ahrens  *     "snaps" -> { snapshot1, snapshot2 }
36064445fffbSMatthew Ahrens  *     (optional boolean) "defer"
36074445fffbSMatthew Ahrens  * }
36084445fffbSMatthew Ahrens  *
36094445fffbSMatthew Ahrens  * outnvl: snapshot -> error code (int32)
36103cb34c60Sahrens  *
36113cb34c60Sahrens  */
361278f17100SMatthew Ahrens /* ARGSUSED */
36131d452cf5Sahrens static int
36144445fffbSMatthew Ahrens zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
36151d452cf5Sahrens {
36164445fffbSMatthew Ahrens 	nvlist_t *snaps;
361719b94df9SMatthew Ahrens 	nvpair_t *pair;
36184445fffbSMatthew Ahrens 	boolean_t defer;
36191d452cf5Sahrens 
36204445fffbSMatthew Ahrens 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3621be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
36224445fffbSMatthew Ahrens 	defer = nvlist_exists(innvl, "defer");
362319b94df9SMatthew Ahrens 
36244445fffbSMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
36254445fffbSMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
3626ed992b0aSSerapheim Dimitropoulos 		zfs_unmount_snap(nvpair_name(pair));
362778f17100SMatthew Ahrens 	}
362878f17100SMatthew Ahrens 
362978f17100SMatthew Ahrens 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
363078f17100SMatthew Ahrens }
363178f17100SMatthew Ahrens 
363278f17100SMatthew Ahrens /*
363378f17100SMatthew Ahrens  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
363478f17100SMatthew Ahrens  * All bookmarks must be in the same pool.
363578f17100SMatthew Ahrens  *
363678f17100SMatthew Ahrens  * innvl: {
363778f17100SMatthew Ahrens  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
363878f17100SMatthew Ahrens  * }
363978f17100SMatthew Ahrens  *
364078f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
364178f17100SMatthew Ahrens  *
364278f17100SMatthew Ahrens  */
364378f17100SMatthew Ahrens /* ARGSUSED */
364478f17100SMatthew Ahrens static int
364578f17100SMatthew Ahrens zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
364678f17100SMatthew Ahrens {
364778f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
364878f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
364978f17100SMatthew Ahrens 		char *snap_name;
365078f17100SMatthew Ahrens 
365178f17100SMatthew Ahrens 		/*
365278f17100SMatthew Ahrens 		 * Verify the snapshot argument.
365378f17100SMatthew Ahrens 		 */
365478f17100SMatthew Ahrens 		if (nvpair_value_string(pair, &snap_name) != 0)
365578f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
365678f17100SMatthew Ahrens 
365778f17100SMatthew Ahrens 
365878f17100SMatthew Ahrens 		/* Verify that the keys (bookmarks) are unique */
365978f17100SMatthew Ahrens 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
366078f17100SMatthew Ahrens 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
366178f17100SMatthew Ahrens 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
366278f17100SMatthew Ahrens 				return (SET_ERROR(EINVAL));
366378f17100SMatthew Ahrens 		}
366478f17100SMatthew Ahrens 	}
366578f17100SMatthew Ahrens 
366678f17100SMatthew Ahrens 	return (dsl_bookmark_create(innvl, outnvl));
366778f17100SMatthew Ahrens }
366878f17100SMatthew Ahrens 
366978f17100SMatthew Ahrens /*
367078f17100SMatthew Ahrens  * innvl: {
367178f17100SMatthew Ahrens  *     property 1, property 2, ...
367278f17100SMatthew Ahrens  * }
367378f17100SMatthew Ahrens  *
367478f17100SMatthew Ahrens  * outnvl: {
367578f17100SMatthew Ahrens  *     bookmark name 1 -> { property 1, property 2, ... },
367678f17100SMatthew Ahrens  *     bookmark name 2 -> { property 1, property 2, ... }
367778f17100SMatthew Ahrens  * }
367878f17100SMatthew Ahrens  *
367978f17100SMatthew Ahrens  */
368078f17100SMatthew Ahrens static int
368178f17100SMatthew Ahrens zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
368278f17100SMatthew Ahrens {
368378f17100SMatthew Ahrens 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
368478f17100SMatthew Ahrens }
368578f17100SMatthew Ahrens 
368678f17100SMatthew Ahrens /*
368778f17100SMatthew Ahrens  * innvl: {
368878f17100SMatthew Ahrens  *     bookmark name 1, bookmark name 2
368978f17100SMatthew Ahrens  * }
369078f17100SMatthew Ahrens  *
369178f17100SMatthew Ahrens  * outnvl: bookmark -> error code (int32)
369278f17100SMatthew Ahrens  *
369378f17100SMatthew Ahrens  */
369478f17100SMatthew Ahrens static int
369578f17100SMatthew Ahrens zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
369678f17100SMatthew Ahrens     nvlist_t *outnvl)
369778f17100SMatthew Ahrens {
369878f17100SMatthew Ahrens 	int error, poollen;
369978f17100SMatthew Ahrens 
370078f17100SMatthew Ahrens 	poollen = strlen(poolname);
370178f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
370278f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
370319b94df9SMatthew Ahrens 		const char *name = nvpair_name(pair);
370478f17100SMatthew Ahrens 		const char *cp = strchr(name, '#');
37054445fffbSMatthew Ahrens 
370619b94df9SMatthew Ahrens 		/*
370778f17100SMatthew Ahrens 		 * The bookmark name must contain an #, and the part after it
370878f17100SMatthew Ahrens 		 * must contain only valid characters.
370978f17100SMatthew Ahrens 		 */
371078f17100SMatthew Ahrens 		if (cp == NULL ||
371178f17100SMatthew Ahrens 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
371278f17100SMatthew Ahrens 			return (SET_ERROR(EINVAL));
371378f17100SMatthew Ahrens 
371478f17100SMatthew Ahrens 		/*
371578f17100SMatthew Ahrens 		 * The bookmark must be in the specified pool.
371619b94df9SMatthew Ahrens 		 */
37174445fffbSMatthew Ahrens 		if (strncmp(name, poolname, poollen) != 0 ||
371878f17100SMatthew Ahrens 		    (name[poollen] != '/' && name[poollen] != '#'))
3719be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
372019b94df9SMatthew Ahrens 	}
372119b94df9SMatthew Ahrens 
372278f17100SMatthew Ahrens 	error = dsl_bookmark_destroy(innvl, outnvl);
372378f17100SMatthew Ahrens 	return (error);
37241d452cf5Sahrens }
37251d452cf5Sahrens 
3726dfc11533SChris Williamson static int
3727dfc11533SChris Williamson zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3728dfc11533SChris Williamson     nvlist_t *outnvl)
3729dfc11533SChris Williamson {
3730dfc11533SChris Williamson 	char *program;
3731dfc11533SChris Williamson 	uint64_t instrlimit, memlimit;
3732a3b28680SSerapheim Dimitropoulos 	boolean_t sync_flag;
3733dfc11533SChris Williamson 	nvpair_t *nvarg = NULL;
3734dfc11533SChris Williamson 
3735dfc11533SChris Williamson 	if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3736dfc11533SChris Williamson 		return (EINVAL);
3737dfc11533SChris Williamson 	}
3738a3b28680SSerapheim Dimitropoulos 	if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3739a3b28680SSerapheim Dimitropoulos 		sync_flag = B_TRUE;
3740a3b28680SSerapheim Dimitropoulos 	}
3741dfc11533SChris Williamson 	if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3742dfc11533SChris Williamson 		instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3743dfc11533SChris Williamson 	}
3744dfc11533SChris Williamson 	if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3745dfc11533SChris Williamson 		memlimit = ZCP_DEFAULT_MEMLIMIT;
3746dfc11533SChris Williamson 	}
3747dfc11533SChris Williamson 	if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3748dfc11533SChris Williamson 		return (EINVAL);
3749dfc11533SChris Williamson 	}
3750dfc11533SChris Williamson 
3751dfc11533SChris Williamson 	if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3752dfc11533SChris Williamson 		return (EINVAL);
37532840dce1SChris Williamson 	if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3754dfc11533SChris Williamson 		return (EINVAL);
3755dfc11533SChris Williamson 
3756a3b28680SSerapheim Dimitropoulos 	return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3757dfc11533SChris Williamson 	    nvarg, outnvl));
3758dfc11533SChris Williamson }
3759dfc11533SChris Williamson 
376086714001SSerapheim Dimitropoulos /*
376186714001SSerapheim Dimitropoulos  * innvl: unused
376286714001SSerapheim Dimitropoulos  * outnvl: empty
376386714001SSerapheim Dimitropoulos  */
376486714001SSerapheim Dimitropoulos /* ARGSUSED */
376586714001SSerapheim Dimitropoulos static int
376686714001SSerapheim Dimitropoulos zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
376786714001SSerapheim Dimitropoulos {
376886714001SSerapheim Dimitropoulos 	return (spa_checkpoint(poolname));
376986714001SSerapheim Dimitropoulos }
377086714001SSerapheim Dimitropoulos 
377186714001SSerapheim Dimitropoulos /*
377286714001SSerapheim Dimitropoulos  * innvl: unused
377386714001SSerapheim Dimitropoulos  * outnvl: empty
377486714001SSerapheim Dimitropoulos  */
377586714001SSerapheim Dimitropoulos /* ARGSUSED */
377686714001SSerapheim Dimitropoulos static int
377786714001SSerapheim Dimitropoulos zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
377886714001SSerapheim Dimitropoulos     nvlist_t *outnvl)
377986714001SSerapheim Dimitropoulos {
378086714001SSerapheim Dimitropoulos 	return (spa_checkpoint_discard(poolname));
378186714001SSerapheim Dimitropoulos }
378286714001SSerapheim Dimitropoulos 
37833cb34c60Sahrens /*
37843cb34c60Sahrens  * inputs:
37853cb34c60Sahrens  * zc_name		name of dataset to destroy
3786842727c2SChris Kirby  * zc_defer_destroy	mark for deferred destroy
37873cb34c60Sahrens  *
37883cb34c60Sahrens  * outputs:		none
37893cb34c60Sahrens  */
37901d452cf5Sahrens static int
37911d452cf5Sahrens zfs_ioc_destroy(zfs_cmd_t *zc)
37921d452cf5Sahrens {
3793049ba636SAndriy Gapon 	objset_t *os;
3794049ba636SAndriy Gapon 	dmu_objset_type_t ost;
3795681d9761SEric Taylor 	int err;
3796fc7a6e3fSWill Andrews 
3797049ba636SAndriy Gapon 	err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3798049ba636SAndriy Gapon 	if (err != 0)
3799049ba636SAndriy Gapon 		return (err);
3800049ba636SAndriy Gapon 	ost = dmu_objset_type(os);
3801049ba636SAndriy Gapon 	dmu_objset_rele(os, FTAG);
3802049ba636SAndriy Gapon 
3803049ba636SAndriy Gapon 	if (ost == DMU_OST_ZFS)
3804ed992b0aSSerapheim Dimitropoulos 		zfs_unmount_snap(zc->zc_name);
3805fa9e4066Sahrens 
38066ccda740Sloli 	if (strchr(zc->zc_name, '@')) {
38073b2aab18SMatthew Ahrens 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
38086ccda740Sloli 	} else {
38093b2aab18SMatthew Ahrens 		err = dsl_destroy_head(zc->zc_name);
38106ccda740Sloli 		if (err == EEXIST) {
38116ccda740Sloli 			/*
38126ccda740Sloli 			 * It is possible that the given DS may have
38136ccda740Sloli 			 * hidden child (%recv) datasets - "leftovers"
38146ccda740Sloli 			 * resulting from the previously interrupted
38156ccda740Sloli 			 * 'zfs receive'.
38166ccda740Sloli 			 *
38176ccda740Sloli 			 * 6 extra bytes for /%recv
38186ccda740Sloli 			 */
38196ccda740Sloli 			char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
38206ccda740Sloli 
38216ccda740Sloli 			if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
38226ccda740Sloli 			    zc->zc_name, recv_clone_name) >=
38236ccda740Sloli 			    sizeof (namebuf))
38246ccda740Sloli 				return (SET_ERROR(EINVAL));
38256ccda740Sloli 
38266ccda740Sloli 			/*
38276ccda740Sloli 			 * Try to remove the hidden child (%recv) and after
38286ccda740Sloli 			 * that try to remove the target dataset.
38296ccda740Sloli 			 * If the hidden child (%recv) does not exist
38306ccda740Sloli 			 * the original error (EEXIST) will be returned
38316ccda740Sloli 			 */
38326ccda740Sloli 			err = dsl_destroy_head(namebuf);
38336ccda740Sloli 			if (err == 0)
38346ccda740Sloli 				err = dsl_destroy_head(zc->zc_name);
38356ccda740Sloli 			else if (err == ENOENT)
38366ccda740Sloli 				err = SET_ERROR(EEXIST);
38376ccda740Sloli 		}
38386ccda740Sloli 	}
3839049ba636SAndriy Gapon 	if (ost == DMU_OST_ZVOL && err == 0)
38405c987a37SChris Kirby 		(void) zvol_remove_minor(zc->zc_name);
3841681d9761SEric Taylor 	return (err);
3842fa9e4066Sahrens }
3843fa9e4066Sahrens 
3844094e47e9SGeorge Wilson /*
3845094e47e9SGeorge Wilson  * innvl: {
3846094e47e9SGeorge Wilson  *     vdevs: {
3847094e47e9SGeorge Wilson  *         guid 1, guid 2, ...
3848094e47e9SGeorge Wilson  *     },
3849094e47e9SGeorge Wilson  *     func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3850094e47e9SGeorge Wilson  * }
3851094e47e9SGeorge Wilson  *
3852094e47e9SGeorge Wilson  * outnvl: {
3853094e47e9SGeorge Wilson  *     [func: EINVAL (if provided command type didn't make sense)],
3854094e47e9SGeorge Wilson  *     [vdevs: {
3855094e47e9SGeorge Wilson  *         guid1: errno, (see function body for possible errnos)
3856094e47e9SGeorge Wilson  *         ...
3857094e47e9SGeorge Wilson  *     }]
3858094e47e9SGeorge Wilson  * }
3859094e47e9SGeorge Wilson  *
3860094e47e9SGeorge Wilson  */
3861094e47e9SGeorge Wilson static int
3862094e47e9SGeorge Wilson zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3863094e47e9SGeorge Wilson {
3864094e47e9SGeorge Wilson 	spa_t *spa;
3865094e47e9SGeorge Wilson 	int error;
3866094e47e9SGeorge Wilson 
3867094e47e9SGeorge Wilson 	error = spa_open(poolname, &spa, FTAG);
3868094e47e9SGeorge Wilson 	if (error != 0)
3869094e47e9SGeorge Wilson 		return (error);
3870094e47e9SGeorge Wilson 
3871094e47e9SGeorge Wilson 	uint64_t cmd_type;
3872094e47e9SGeorge Wilson 	if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3873094e47e9SGeorge Wilson 	    &cmd_type) != 0) {
3874094e47e9SGeorge Wilson 		spa_close(spa, FTAG);
3875094e47e9SGeorge Wilson 		return (SET_ERROR(EINVAL));
3876094e47e9SGeorge Wilson 	}
3877094e47e9SGeorge Wilson 	if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3878094e47e9SGeorge Wilson 	    cmd_type == POOL_INITIALIZE_DO ||
3879094e47e9SGeorge Wilson 	    cmd_type == POOL_INITIALIZE_SUSPEND)) {
3880094e47e9SGeorge Wilson 		spa_close(spa, FTAG);
3881094e47e9SGeorge Wilson 		return (SET_ERROR(EINVAL));
3882094e47e9SGeorge Wilson 	}
3883094e47e9SGeorge Wilson 
3884094e47e9SGeorge Wilson 	nvlist_t *vdev_guids;
3885094e47e9SGeorge Wilson 	if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3886094e47e9SGeorge Wilson 	    &vdev_guids) != 0) {
3887094e47e9SGeorge Wilson 		spa_close(spa, FTAG);
3888094e47e9SGeorge Wilson 		return (SET_ERROR(EINVAL));
3889094e47e9SGeorge Wilson 	}
3890094e47e9SGeorge Wilson 
3891094e47e9SGeorge Wilson 	nvlist_t *vdev_errlist = fnvlist_alloc();
3892094e47e9SGeorge Wilson 	int total_errors = 0;
3893094e47e9SGeorge Wilson 
3894094e47e9SGeorge Wilson 	for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3895094e47e9SGeorge Wilson 	    pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3896094e47e9SGeorge Wilson 		uint64_t vdev_guid = fnvpair_value_uint64(pair);
3897094e47e9SGeorge Wilson 
3898094e47e9SGeorge Wilson 		error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
3899094e47e9SGeorge Wilson 		if (error != 0) {
3900094e47e9SGeorge Wilson 			char guid_as_str[MAXNAMELEN];
3901094e47e9SGeorge Wilson 
3902094e47e9SGeorge Wilson 			(void) snprintf(guid_as_str, sizeof (guid_as_str),
3903094e47e9SGeorge Wilson 			    "%llu", (unsigned long long)vdev_guid);
3904094e47e9SGeorge Wilson 			fnvlist_add_int64(vdev_errlist, guid_as_str, error);
3905094e47e9SGeorge Wilson 			total_errors++;
3906094e47e9SGeorge Wilson 		}
3907094e47e9SGeorge Wilson 	}
3908094e47e9SGeorge Wilson 	if (fnvlist_size(vdev_errlist) > 0) {
3909094e47e9SGeorge Wilson 		fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3910094e47e9SGeorge Wilson 		    vdev_errlist);
3911094e47e9SGeorge Wilson 	}
3912094e47e9SGeorge Wilson 	fnvlist_free(vdev_errlist);
3913094e47e9SGeorge Wilson 
3914094e47e9SGeorge Wilson 	spa_close(spa, FTAG);
3915094e47e9SGeorge Wilson 	return (total_errors > 0 ? EINVAL : 0);
3916094e47e9SGeorge Wilson }
3917094e47e9SGeorge Wilson 
39183cb34c60Sahrens /*
3919a7027df1SMatthew Ahrens  * fsname is name of dataset to rollback (to most recent snapshot)
39203cb34c60Sahrens  *
392177b17137SAndriy Gapon  * innvl may contain name of expected target snapshot
3922a7027df1SMatthew Ahrens  *
3923a7027df1SMatthew Ahrens  * outnvl: "target" -> name of most recent snapshot
3924a7027df1SMatthew Ahrens  * }
39253cb34c60Sahrens  */
3926a7027df1SMatthew Ahrens /* ARGSUSED */
3927fa9e4066Sahrens static int
392877b17137SAndriy Gapon zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3929fa9e4066Sahrens {
3930ae46e4c7SMatthew Ahrens 	zfsvfs_t *zfsvfs;
393177b17137SAndriy Gapon 	char *target = NULL;
39323b2aab18SMatthew Ahrens 	int error;
3933ae46e4c7SMatthew Ahrens 
393477b17137SAndriy Gapon 	(void) nvlist_lookup_string(innvl, "target", &target);
393577b17137SAndriy Gapon 	if (target != NULL) {
393695643f75SAndriy Gapon 		const char *cp = strchr(target, '@');
393777b17137SAndriy Gapon 
393895643f75SAndriy Gapon 		/*
393995643f75SAndriy Gapon 		 * The snap name must contain an @, and the part after it must
394095643f75SAndriy Gapon 		 * contain only valid characters.
394195643f75SAndriy Gapon 		 */
394295643f75SAndriy Gapon 		if (cp == NULL ||
394395643f75SAndriy Gapon 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
394477b17137SAndriy Gapon 			return (SET_ERROR(EINVAL));
394577b17137SAndriy Gapon 	}
394677b17137SAndriy Gapon 
3947a7027df1SMatthew Ahrens 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
3948690041b9SAndriy Gapon 		dsl_dataset_t *ds;
3949690041b9SAndriy Gapon 
3950690041b9SAndriy Gapon 		ds = dmu_objset_ds(zfsvfs->z_os);
3951503ad85cSMatthew Ahrens 		error = zfs_suspend_fs(zfsvfs);
395247f263f4Sek 		if (error == 0) {
395347f263f4Sek 			int resume_err;
39544ccbb6e7Sahrens 
395577b17137SAndriy Gapon 			error = dsl_dataset_rollback(fsname, target, zfsvfs,
395677b17137SAndriy Gapon 			    outnvl);
3957690041b9SAndriy Gapon 			resume_err = zfs_resume_fs(zfsvfs, ds);
395847f263f4Sek 			error = error ? error : resume_err;
395947f263f4Sek 		}
39604ccbb6e7Sahrens 		VFS_RELE(zfsvfs->z_vfs);
39614ccbb6e7Sahrens 	} else {
396277b17137SAndriy Gapon 		error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
39634ccbb6e7Sahrens 	}
39643b2aab18SMatthew Ahrens 	return (error);
39653b2aab18SMatthew Ahrens }
39664ccbb6e7Sahrens 
39673b2aab18SMatthew Ahrens static int
39683b2aab18SMatthew Ahrens recursive_unmount(const char *fsname, void *arg)
39693b2aab18SMatthew Ahrens {
39703b2aab18SMatthew Ahrens 	const char *snapname = arg;
39719adfa60dSMatthew Ahrens 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
3972ae46e4c7SMatthew Ahrens 
39733b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3974ed992b0aSSerapheim Dimitropoulos 	zfs_unmount_snap(fullname);
3975ed992b0aSSerapheim Dimitropoulos 
3976ed992b0aSSerapheim Dimitropoulos 	return (0);
3977fa9e4066Sahrens }
3978fa9e4066Sahrens 
39793cb34c60Sahrens /*
39803cb34c60Sahrens  * inputs:
39813cb34c60Sahrens  * zc_name	old name of dataset
39823cb34c60Sahrens  * zc_value	new name of dataset
39833cb34c60Sahrens  * zc_cookie	recursive flag (only valid for snapshots)
39843cb34c60Sahrens  *
39853cb34c60Sahrens  * outputs:	none
39863cb34c60Sahrens  */
3987fa9e4066Sahrens static int
3988fa9e4066Sahrens zfs_ioc_rename(zfs_cmd_t *zc)
3989fa9e4066Sahrens {
3990049ba636SAndriy Gapon 	objset_t *os;
3991049ba636SAndriy Gapon 	dmu_objset_type_t ost;
39927f1f55eaSvb 	boolean_t recursive = zc->zc_cookie & 1;
39933b2aab18SMatthew Ahrens 	char *at;
3994049ba636SAndriy Gapon 	int err;
3995cdf5b4caSmmusante 
3996add927f8Sloli 	/* "zfs rename" from and to ...%recv datasets should both fail */
3997add927f8Sloli 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3998e9dbad6fSeschrock 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3999add927f8Sloli 	if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4000add927f8Sloli 	    dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4001add927f8Sloli 	    strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4002be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
4003fa9e4066Sahrens 
4004049ba636SAndriy Gapon 	err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4005049ba636SAndriy Gapon 	if (err != 0)
4006049ba636SAndriy Gapon 		return (err);
4007049ba636SAndriy Gapon 	ost = dmu_objset_type(os);
4008049ba636SAndriy Gapon 	dmu_objset_rele(os, FTAG);
4009049ba636SAndriy Gapon 
40103b2aab18SMatthew Ahrens 	at = strchr(zc->zc_name, '@');
40113b2aab18SMatthew Ahrens 	if (at != NULL) {
40123b2aab18SMatthew Ahrens 		/* snaps must be in same fs */
4013a0c1127bSSteven Hartland 		int error;
4014a0c1127bSSteven Hartland 
40153b2aab18SMatthew Ahrens 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4016be6fd75aSMatthew Ahrens 			return (SET_ERROR(EXDEV));
40173b2aab18SMatthew Ahrens 		*at = '\0';
4018049ba636SAndriy Gapon 		if (ost == DMU_OST_ZFS) {
4019a0c1127bSSteven Hartland 			error = dmu_objset_find(zc->zc_name,
40203b2aab18SMatthew Ahrens 			    recursive_unmount, at + 1,
40213b2aab18SMatthew Ahrens 			    recursive ? DS_FIND_CHILDREN : 0);
4022a0c1127bSSteven Hartland 			if (error != 0) {
4023a0c1127bSSteven Hartland 				*at = '@';
40243b2aab18SMatthew Ahrens 				return (error);
4025a0c1127bSSteven Hartland 			}
40263b2aab18SMatthew Ahrens 		}
4027a0c1127bSSteven Hartland 		error = dsl_dataset_rename_snapshot(zc->zc_name,
4028a0c1127bSSteven Hartland 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4029a0c1127bSSteven Hartland 		*at = '@';
4030a0c1127bSSteven Hartland 
4031a0c1127bSSteven Hartland 		return (error);
40323b2aab18SMatthew Ahrens 	} else {
4033049ba636SAndriy Gapon 		if (ost == DMU_OST_ZVOL)
40343b2aab18SMatthew Ahrens 			(void) zvol_remove_minor(zc->zc_name);
40353b2aab18SMatthew Ahrens 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4036fa9e4066Sahrens 	}
4037fa9e4066Sahrens }
4038fa9e4066Sahrens 
403992241e0bSTom Erickson static int
404092241e0bSTom Erickson zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
404192241e0bSTom Erickson {
404292241e0bSTom Erickson 	const char *propname = nvpair_name(pair);
404392241e0bSTom Erickson 	boolean_t issnap = (strchr(dsname, '@') != NULL);
404492241e0bSTom Erickson 	zfs_prop_t prop = zfs_name_to_prop(propname);
404592241e0bSTom Erickson 	uint64_t intval;
404692241e0bSTom Erickson 	int err;
404792241e0bSTom Erickson 
404892241e0bSTom Erickson 	if (prop == ZPROP_INVAL) {
404992241e0bSTom Erickson 		if (zfs_prop_user(propname)) {
405092241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname,
405192241e0bSTom Erickson 			    ZFS_DELEG_PERM_USERPROP, cr))
405292241e0bSTom Erickson 				return (err);
405392241e0bSTom Erickson 			return (0);
405492241e0bSTom Erickson 		}
405592241e0bSTom Erickson 
405692241e0bSTom Erickson 		if (!issnap && zfs_prop_userquota(propname)) {
405792241e0bSTom Erickson 			const char *perm = NULL;
405892241e0bSTom Erickson 			const char *uq_prefix =
405992241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
406092241e0bSTom Erickson 			const char *gq_prefix =
406192241e0bSTom Erickson 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4062*f67950b2SNasf-Fan 			const char *uiq_prefix =
4063*f67950b2SNasf-Fan 			    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4064*f67950b2SNasf-Fan 			const char *giq_prefix =
4065*f67950b2SNasf-Fan 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4066*f67950b2SNasf-Fan 			const char *pq_prefix =
4067*f67950b2SNasf-Fan 			    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4068*f67950b2SNasf-Fan 			const char *piq_prefix = zfs_userquota_prop_prefixes[\
4069*f67950b2SNasf-Fan 			    ZFS_PROP_PROJECTOBJQUOTA];
407092241e0bSTom Erickson 
407192241e0bSTom Erickson 			if (strncmp(propname, uq_prefix,
407292241e0bSTom Erickson 			    strlen(uq_prefix)) == 0) {
407392241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_USERQUOTA;
4074*f67950b2SNasf-Fan 			} else if (strncmp(propname, uiq_prefix,
4075*f67950b2SNasf-Fan 			    strlen(uiq_prefix)) == 0) {
4076*f67950b2SNasf-Fan 				perm = ZFS_DELEG_PERM_USEROBJQUOTA;
407792241e0bSTom Erickson 			} else if (strncmp(propname, gq_prefix,
407892241e0bSTom Erickson 			    strlen(gq_prefix)) == 0) {
407992241e0bSTom Erickson 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
4080*f67950b2SNasf-Fan 			} else if (strncmp(propname, giq_prefix,
4081*f67950b2SNasf-Fan 			    strlen(giq_prefix)) == 0) {
4082*f67950b2SNasf-Fan 				perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4083*f67950b2SNasf-Fan 			} else if (strncmp(propname, pq_prefix,
4084*f67950b2SNasf-Fan 			    strlen(pq_prefix)) == 0) {
4085*f67950b2SNasf-Fan 				perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4086*f67950b2SNasf-Fan 			} else if (strncmp(propname, piq_prefix,
4087*f67950b2SNasf-Fan 			    strlen(piq_prefix)) == 0) {
4088*f67950b2SNasf-Fan 				perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
408992241e0bSTom Erickson 			} else {
4090*f67950b2SNasf-Fan 				/* {USER|GROUP|PROJECT}USED are read-only */
4091be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
409292241e0bSTom Erickson 			}
409392241e0bSTom Erickson 
409492241e0bSTom Erickson 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
409592241e0bSTom Erickson 				return (err);
409692241e0bSTom Erickson 			return (0);
409792241e0bSTom Erickson 		}
409892241e0bSTom Erickson 
4099be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
410092241e0bSTom Erickson 	}
410192241e0bSTom Erickson 
410292241e0bSTom Erickson 	if (issnap)
4103be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
410492241e0bSTom Erickson 
410592241e0bSTom Erickson 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
410692241e0bSTom Erickson 		/*
410792241e0bSTom Erickson 		 * dsl_prop_get_all_impl() returns properties in this
410892241e0bSTom Erickson 		 * format.
410992241e0bSTom Erickson 		 */
411092241e0bSTom Erickson 		nvlist_t *attrs;
411192241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
411292241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
411392241e0bSTom Erickson 		    &pair) == 0);
411492241e0bSTom Erickson 	}
411592241e0bSTom Erickson 
411692241e0bSTom Erickson 	/*
411792241e0bSTom Erickson 	 * Check that this value is valid for this pool version
411892241e0bSTom Erickson 	 */
411992241e0bSTom Erickson 	switch (prop) {
412092241e0bSTom Erickson 	case ZFS_PROP_COMPRESSION:
412192241e0bSTom Erickson 		/*
412292241e0bSTom Erickson 		 * If the user specified gzip compression, make sure
412392241e0bSTom Erickson 		 * the SPA supports it. We ignore any errors here since
412492241e0bSTom Erickson 		 * we'll catch them later.
412592241e0bSTom Erickson 		 */
4126b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0) {
412792241e0bSTom Erickson 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
412892241e0bSTom Erickson 			    intval <= ZIO_COMPRESS_GZIP_9 &&
412992241e0bSTom Erickson 			    zfs_earlier_version(dsname,
413092241e0bSTom Erickson 			    SPA_VERSION_GZIP_COMPRESSION)) {
4131be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
413292241e0bSTom Erickson 			}
413392241e0bSTom Erickson 
413492241e0bSTom Erickson 			if (intval == ZIO_COMPRESS_ZLE &&
413592241e0bSTom Erickson 			    zfs_earlier_version(dsname,
413692241e0bSTom Erickson 			    SPA_VERSION_ZLE_COMPRESSION))
4137be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
413892241e0bSTom Erickson 
4139a6f561b4SSašo Kiselkov 			if (intval == ZIO_COMPRESS_LZ4) {
4140a6f561b4SSašo Kiselkov 				spa_t *spa;
4141a6f561b4SSašo Kiselkov 
4142a6f561b4SSašo Kiselkov 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4143a6f561b4SSašo Kiselkov 					return (err);
4144a6f561b4SSašo Kiselkov 
41452acef22dSMatthew Ahrens 				if (!spa_feature_is_enabled(spa,
41462acef22dSMatthew Ahrens 				    SPA_FEATURE_LZ4_COMPRESS)) {
4147a6f561b4SSašo Kiselkov 					spa_close(spa, FTAG);
4148be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENOTSUP));
4149a6f561b4SSašo Kiselkov 				}
4150a6f561b4SSašo Kiselkov 				spa_close(spa, FTAG);
4151a6f561b4SSašo Kiselkov 			}
4152a6f561b4SSašo Kiselkov 
415392241e0bSTom Erickson 			/*
415492241e0bSTom Erickson 			 * If this is a bootable dataset then
415592241e0bSTom Erickson 			 * verify that the compression algorithm
415692241e0bSTom Erickson 			 * is supported for booting. We must return
415792241e0bSTom Erickson 			 * something other than ENOTSUP since it
415892241e0bSTom Erickson 			 * implies a downrev pool version.
415992241e0bSTom Erickson 			 */
416092241e0bSTom Erickson 			if (zfs_is_bootfs(dsname) &&
416192241e0bSTom Erickson 			    !BOOTFS_COMPRESS_VALID(intval)) {
4162be6fd75aSMatthew Ahrens 				return (SET_ERROR(ERANGE));
416392241e0bSTom Erickson 			}
416492241e0bSTom Erickson 		}
416592241e0bSTom Erickson 		break;
416692241e0bSTom Erickson 
416792241e0bSTom Erickson 	case ZFS_PROP_COPIES:
416892241e0bSTom Erickson 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4169be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
417092241e0bSTom Erickson 		break;
417192241e0bSTom Erickson 
4172b5152584SMatthew Ahrens 	case ZFS_PROP_RECORDSIZE:
4173b5152584SMatthew Ahrens 		/* Record sizes above 128k need the feature to be enabled */
4174b5152584SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) == 0 &&
4175b5152584SMatthew Ahrens 		    intval > SPA_OLD_MAXBLOCKSIZE) {
4176b5152584SMatthew Ahrens 			spa_t *spa;
4177b5152584SMatthew Ahrens 
4178b5152584SMatthew Ahrens 			/*
4179b5152584SMatthew Ahrens 			 * We don't allow setting the property above 1MB,
4180b5152584SMatthew Ahrens 			 * unless the tunable has been changed.
4181b5152584SMatthew Ahrens 			 */
4182b5152584SMatthew Ahrens 			if (intval > zfs_max_recordsize ||
4183b5152584SMatthew Ahrens 			    intval > SPA_MAXBLOCKSIZE)
41846de9bb56SMatthew Ahrens 				return (SET_ERROR(ERANGE));
4185b5152584SMatthew Ahrens 
4186b5152584SMatthew Ahrens 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4187b5152584SMatthew Ahrens 				return (err);
4188b5152584SMatthew Ahrens 
4189b5152584SMatthew Ahrens 			if (!spa_feature_is_enabled(spa,
4190b5152584SMatthew Ahrens 			    SPA_FEATURE_LARGE_BLOCKS)) {
4191b5152584SMatthew Ahrens 				spa_close(spa, FTAG);
4192b5152584SMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
4193b5152584SMatthew Ahrens 			}
4194b5152584SMatthew Ahrens 			spa_close(spa, FTAG);
4195b5152584SMatthew Ahrens 		}
4196b5152584SMatthew Ahrens 		break;
4197b5152584SMatthew Ahrens 
419854811da5SToomas Soome 	case ZFS_PROP_DNODESIZE:
419954811da5SToomas Soome 		/* Dnode sizes above 512 need the feature to be enabled */
420054811da5SToomas Soome 		if (nvpair_value_uint64(pair, &intval) == 0 &&
420154811da5SToomas Soome 		    intval != ZFS_DNSIZE_LEGACY) {
420254811da5SToomas Soome 			spa_t *spa;
420354811da5SToomas Soome 
420454811da5SToomas Soome 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
420554811da5SToomas Soome 				return (err);
420654811da5SToomas Soome 
420754811da5SToomas Soome 			if (!spa_feature_is_enabled(spa,
420854811da5SToomas Soome 			    SPA_FEATURE_LARGE_DNODE)) {
420954811da5SToomas Soome 				spa_close(spa, FTAG);
421054811da5SToomas Soome 				return (SET_ERROR(ENOTSUP));
421154811da5SToomas Soome 			}
421254811da5SToomas Soome 			spa_close(spa, FTAG);
421354811da5SToomas Soome 		}
421454811da5SToomas Soome 		break;
421554811da5SToomas Soome 
4216663207adSDon Brady 	case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4217663207adSDon Brady 		/*
4218663207adSDon Brady 		 * This property could require the allocation classes
4219663207adSDon Brady 		 * feature to be active for setting, however we allow
4220663207adSDon Brady 		 * it so that tests of settable properties succeed.
4221663207adSDon Brady 		 * The CLI will issue a warning in this case.
4222663207adSDon Brady 		 */
4223663207adSDon Brady 		break;
4224663207adSDon Brady 
422592241e0bSTom Erickson 	case ZFS_PROP_SHARESMB:
422692241e0bSTom Erickson 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4227be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
422892241e0bSTom Erickson 		break;
422992241e0bSTom Erickson 
423092241e0bSTom Erickson 	case ZFS_PROP_ACLINHERIT:
423192241e0bSTom Erickson 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
423292241e0bSTom Erickson 		    nvpair_value_uint64(pair, &intval) == 0) {
423392241e0bSTom Erickson 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
423492241e0bSTom Erickson 			    zfs_earlier_version(dsname,
423592241e0bSTom Erickson 			    SPA_VERSION_PASSTHROUGH_X))
4236be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOTSUP));
423792241e0bSTom Erickson 		}
423892241e0bSTom Erickson 		break;
423945818ee1SMatthew Ahrens 
424045818ee1SMatthew Ahrens 	case ZFS_PROP_CHECKSUM:
424145818ee1SMatthew Ahrens 	case ZFS_PROP_DEDUP:
424245818ee1SMatthew Ahrens 	{
424345818ee1SMatthew Ahrens 		spa_feature_t feature;
424445818ee1SMatthew Ahrens 		spa_t *spa;
424545818ee1SMatthew Ahrens 
424645818ee1SMatthew Ahrens 		/* dedup feature version checks */
424745818ee1SMatthew Ahrens 		if (prop == ZFS_PROP_DEDUP &&
424845818ee1SMatthew Ahrens 		    zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
424945818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
425045818ee1SMatthew Ahrens 
425145818ee1SMatthew Ahrens 		if (nvpair_value_uint64(pair, &intval) != 0)
425245818ee1SMatthew Ahrens 			return (SET_ERROR(EINVAL));
425345818ee1SMatthew Ahrens 
425445818ee1SMatthew Ahrens 		/* check prop value is enabled in features */
4255971640e6Silovezfs 		feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
425645818ee1SMatthew Ahrens 		if (feature == SPA_FEATURE_NONE)
425745818ee1SMatthew Ahrens 			break;
425845818ee1SMatthew Ahrens 
425945818ee1SMatthew Ahrens 		if ((err = spa_open(dsname, &spa, FTAG)) != 0)
426045818ee1SMatthew Ahrens 			return (err);
42610dd498c0SToomas Soome 
426245818ee1SMatthew Ahrens 		if (!spa_feature_is_enabled(spa, feature)) {
426345818ee1SMatthew Ahrens 			spa_close(spa, FTAG);
426445818ee1SMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
426545818ee1SMatthew Ahrens 		}
426645818ee1SMatthew Ahrens 		spa_close(spa, FTAG);
426745818ee1SMatthew Ahrens 		break;
426845818ee1SMatthew Ahrens 	}
426992241e0bSTom Erickson 	}
427092241e0bSTom Erickson 
427192241e0bSTom Erickson 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
427292241e0bSTom Erickson }
427392241e0bSTom Erickson 
4274a6f561b4SSašo Kiselkov /*
4275a6f561b4SSašo Kiselkov  * Checks for a race condition to make sure we don't increment a feature flag
4276a6f561b4SSašo Kiselkov  * multiple times.
4277a6f561b4SSašo Kiselkov  */
4278a6f561b4SSašo Kiselkov static int
42793b2aab18SMatthew Ahrens zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4280a6f561b4SSašo Kiselkov {
42813b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
42822acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
4283a6f561b4SSašo Kiselkov 
42842acef22dSMatthew Ahrens 	if (!spa_feature_is_active(spa, *featurep))
4285a6f561b4SSašo Kiselkov 		return (0);
4286a6f561b4SSašo Kiselkov 	else
4287be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
4288a6f561b4SSašo Kiselkov }
4289a6f561b4SSašo Kiselkov 
4290a6f561b4SSašo Kiselkov /*
4291a6f561b4SSašo Kiselkov  * The callback invoked on feature activation in the sync task caused by
4292a6f561b4SSašo Kiselkov  * zfs_prop_activate_feature.
4293a6f561b4SSašo Kiselkov  */
4294a6f561b4SSašo Kiselkov static void
42953b2aab18SMatthew Ahrens zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4296a6f561b4SSašo Kiselkov {
42973b2aab18SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
42982acef22dSMatthew Ahrens 	spa_feature_t *featurep = arg;
4299a6f561b4SSašo Kiselkov 
43002acef22dSMatthew Ahrens 	spa_feature_incr(spa, *featurep, tx);
4301a6f561b4SSašo Kiselkov }
4302a6f561b4SSašo Kiselkov 
43033b2aab18SMatthew Ahrens /*
43043b2aab18SMatthew Ahrens  * Activates a feature on a pool in response to a property setting. This
43053b2aab18SMatthew Ahrens  * creates a new sync task which modifies the pool to reflect the feature
43063b2aab18SMatthew Ahrens  * as being active.
43073b2aab18SMatthew Ahrens  */
43083b2aab18SMatthew Ahrens static int
43092acef22dSMatthew Ahrens zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
43103b2aab18SMatthew Ahrens {
43113b2aab18SMatthew Ahrens 	int err;
43123b2aab18SMatthew Ahrens 
43133b2aab18SMatthew Ahrens 	/* EBUSY here indicates that the feature is already active */
43143b2aab18SMatthew Ahrens 	err = dsl_sync_task(spa_name(spa),
43153b2aab18SMatthew Ahrens 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
43167d46dc6cSMatthew Ahrens 	    &feature, 2, ZFS_SPACE_CHECK_RESERVED);
43173b2aab18SMatthew Ahrens 
43183b2aab18SMatthew Ahrens 	if (err != 0 && err != EBUSY)
43193b2aab18SMatthew Ahrens 		return (err);
43203b2aab18SMatthew Ahrens 	else
43213b2aab18SMatthew Ahrens 		return (0);
43223b2aab18SMatthew Ahrens }
43233b2aab18SMatthew Ahrens 
432492241e0bSTom Erickson /*
432592241e0bSTom Erickson  * Removes properties from the given props list that fail permission checks
432692241e0bSTom Erickson  * needed to clear them and to restore them in case of a receive error. For each
432792241e0bSTom Erickson  * property, make sure we have both set and inherit permissions.
432892241e0bSTom Erickson  *
432992241e0bSTom Erickson  * Returns the first error encountered if any permission checks fail. If the
433092241e0bSTom Erickson  * caller provides a non-NULL errlist, it also gives the complete list of names
433192241e0bSTom Erickson  * of all the properties that failed a permission check along with the
433292241e0bSTom Erickson  * corresponding error numbers. The caller is responsible for freeing the
433392241e0bSTom Erickson  * returned errlist.
433492241e0bSTom Erickson  *
433592241e0bSTom Erickson  * If every property checks out successfully, zero is returned and the list
433692241e0bSTom Erickson  * pointed at by errlist is NULL.
433792241e0bSTom Erickson  */
433892241e0bSTom Erickson static int
433992241e0bSTom Erickson zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4340745cd3c5Smaybee {
4341745cd3c5Smaybee 	zfs_cmd_t *zc;
434292241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
434392241e0bSTom Erickson 	nvlist_t *errors;
434492241e0bSTom Erickson 	int err, rv = 0;
4345745cd3c5Smaybee 
4346745cd3c5Smaybee 	if (props == NULL)
434792241e0bSTom Erickson 		return (0);
434892241e0bSTom Erickson 
434992241e0bSTom Erickson 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
435092241e0bSTom Erickson 
4351745cd3c5Smaybee 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4352745cd3c5Smaybee 	(void) strcpy(zc->zc_name, dataset);
435392241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
435492241e0bSTom Erickson 	while (pair != NULL) {
435592241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
435692241e0bSTom Erickson 
435792241e0bSTom Erickson 		(void) strcpy(zc->zc_value, nvpair_name(pair));
435892241e0bSTom Erickson 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
43594445fffbSMatthew Ahrens 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
436092241e0bSTom Erickson 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
436192241e0bSTom Erickson 			VERIFY(nvlist_add_int32(errors,
436292241e0bSTom Erickson 			    zc->zc_value, err) == 0);
436392241e0bSTom Erickson 		}
436492241e0bSTom Erickson 		pair = next_pair;
4365745cd3c5Smaybee 	}
4366745cd3c5Smaybee 	kmem_free(zc, sizeof (zfs_cmd_t));
436792241e0bSTom Erickson 
436892241e0bSTom Erickson 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
436992241e0bSTom Erickson 		nvlist_free(errors);
437092241e0bSTom Erickson 		errors = NULL;
437192241e0bSTom Erickson 	} else {
437292241e0bSTom Erickson 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
437392241e0bSTom Erickson 	}
437492241e0bSTom Erickson 
437592241e0bSTom Erickson 	if (errlist == NULL)
437692241e0bSTom Erickson 		nvlist_free(errors);
437792241e0bSTom Erickson 	else
437892241e0bSTom Erickson 		*errlist = errors;
437992241e0bSTom Erickson 
438092241e0bSTom Erickson 	return (rv);
438192241e0bSTom Erickson }
438292241e0bSTom Erickson 
438392241e0bSTom Erickson static boolean_t
438492241e0bSTom Erickson propval_equals(nvpair_t *p1, nvpair_t *p2)
438592241e0bSTom Erickson {
438692241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
438792241e0bSTom Erickson 		/* dsl_prop_get_all_impl() format */
438892241e0bSTom Erickson 		nvlist_t *attrs;
438992241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
439092241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
439192241e0bSTom Erickson 		    &p1) == 0);
439292241e0bSTom Erickson 	}
439392241e0bSTom Erickson 
439492241e0bSTom Erickson 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
439592241e0bSTom Erickson 		nvlist_t *attrs;
439692241e0bSTom Erickson 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
439792241e0bSTom Erickson 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
439892241e0bSTom Erickson 		    &p2) == 0);
439992241e0bSTom Erickson 	}
440092241e0bSTom Erickson 
440192241e0bSTom Erickson 	if (nvpair_type(p1) != nvpair_type(p2))
440292241e0bSTom Erickson 		return (B_FALSE);
440392241e0bSTom Erickson 
440492241e0bSTom Erickson 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
440592241e0bSTom Erickson 		char *valstr1, *valstr2;
440692241e0bSTom Erickson 
440792241e0bSTom Erickson 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
440892241e0bSTom Erickson 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
440992241e0bSTom Erickson 		return (strcmp(valstr1, valstr2) == 0);
441092241e0bSTom Erickson 	} else {
441192241e0bSTom Erickson 		uint64_t intval1, intval2;
441292241e0bSTom Erickson 
441392241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
441492241e0bSTom Erickson 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
441592241e0bSTom Erickson 		return (intval1 == intval2);
441692241e0bSTom Erickson 	}
4417745cd3c5Smaybee }
4418745cd3c5Smaybee 
441992241e0bSTom Erickson /*
442092241e0bSTom Erickson  * Remove properties from props if they are not going to change (as determined
442192241e0bSTom Erickson  * by comparison with origprops). Remove them from origprops as well, since we
442292241e0bSTom Erickson  * do not need to clear or restore properties that won't change.
442392241e0bSTom Erickson  */
442492241e0bSTom Erickson static void
442592241e0bSTom Erickson props_reduce(nvlist_t *props, nvlist_t *origprops)
442692241e0bSTom Erickson {
442792241e0bSTom Erickson 	nvpair_t *pair, *next_pair;
442892241e0bSTom Erickson 
442992241e0bSTom Erickson 	if (origprops == NULL)
443092241e0bSTom Erickson 		return; /* all props need to be received */
443192241e0bSTom Erickson 
443292241e0bSTom Erickson 	pair = nvlist_next_nvpair(props, NULL);
443392241e0bSTom Erickson 	while (pair != NULL) {
443492241e0bSTom Erickson 		const char *propname = nvpair_name(pair);
443592241e0bSTom Erickson 		nvpair_t *match;
443692241e0bSTom Erickson 
443792241e0bSTom Erickson 		next_pair = nvlist_next_nvpair(props, pair);
443892241e0bSTom Erickson 
443992241e0bSTom Erickson 		if ((nvlist_lookup_nvpair(origprops, propname,
444092241e0bSTom Erickson 		    &match) != 0) || !propval_equals(pair, match))
444192241e0bSTom Erickson 			goto next; /* need to set received value */
444292241e0bSTom Erickson 
444392241e0bSTom Erickson 		/* don't clear the existing received value */
444492241e0bSTom Erickson 		(void) nvlist_remove_nvpair(origprops, match);
444592241e0bSTom Erickson 		/* don't bother receiving the property */
444692241e0bSTom Erickson 		(void) nvlist_remove_nvpair(props, pair);
444792241e0bSTom Erickson next:
444892241e0bSTom Erickson 		pair = next_pair;
444992241e0bSTom Erickson 	}
445092241e0bSTom Erickson }
445192241e0bSTom Erickson 
44525878fad7SDan McDonald /*
44535878fad7SDan McDonald  * Extract properties that cannot be set PRIOR to the receipt of a dataset.
44545878fad7SDan McDonald  * For example, refquota cannot be set until after the receipt of a dataset,
44555878fad7SDan McDonald  * because in replication streams, an older/earlier snapshot may exceed the
44565878fad7SDan McDonald  * refquota.  We want to receive the older/earlier snapshot, but setting
44575878fad7SDan McDonald  * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
44585878fad7SDan McDonald  * the older/earlier snapshot from being received (with EDQUOT).
44595878fad7SDan McDonald  *
44605878fad7SDan McDonald  * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
44615878fad7SDan McDonald  *
44625878fad7SDan McDonald  * libzfs will need to be judicious handling errors encountered by props
44635878fad7SDan McDonald  * extracted by this function.
44645878fad7SDan McDonald  */
44655878fad7SDan McDonald static nvlist_t *
44665878fad7SDan McDonald extract_delay_props(nvlist_t *props)
44675878fad7SDan McDonald {
44685878fad7SDan McDonald 	nvlist_t *delayprops;
44695878fad7SDan McDonald 	nvpair_t *nvp, *tmp;
4470eb633035STom Caputi 	static const zfs_prop_t delayable[] = {
4471eb633035STom Caputi 		ZFS_PROP_REFQUOTA,
4472eb633035STom Caputi 		ZFS_PROP_KEYLOCATION,
4473eb633035STom Caputi 		0
4474eb633035STom Caputi 	};
44755878fad7SDan McDonald 	int i;
44765878fad7SDan McDonald 
44775878fad7SDan McDonald 	VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
44785878fad7SDan McDonald 
44795878fad7SDan McDonald 	for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
44805878fad7SDan McDonald 	    nvp = nvlist_next_nvpair(props, nvp)) {
44815878fad7SDan McDonald 		/*
44825878fad7SDan McDonald 		 * strcmp() is safe because zfs_prop_to_name() always returns
44835878fad7SDan McDonald 		 * a bounded string.
44845878fad7SDan McDonald 		 */
44855878fad7SDan McDonald 		for (i = 0; delayable[i] != 0; i++) {
44865878fad7SDan McDonald 			if (strcmp(zfs_prop_to_name(delayable[i]),
44875878fad7SDan McDonald 			    nvpair_name(nvp)) == 0) {
44885878fad7SDan McDonald 				break;
44895878fad7SDan McDonald 			}
44905878fad7SDan McDonald 		}
44915878fad7SDan McDonald 		if (delayable[i] != 0) {
44925878fad7SDan McDonald 			tmp = nvlist_prev_nvpair(props, nvp);
44935878fad7SDan McDonald 			VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
44945878fad7SDan McDonald 			VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
44955878fad7SDan McDonald 			nvp = tmp;
44965878fad7SDan McDonald 		}
44975878fad7SDan McDonald 	}
44985878fad7SDan McDonald 
44995878fad7SDan McDonald 	if (nvlist_empty(delayprops)) {
45005878fad7SDan McDonald 		nvlist_free(delayprops);
45015878fad7SDan McDonald 		delayprops = NULL;
45025878fad7SDan McDonald 	}
45035878fad7SDan McDonald 	return (delayprops);
45045878fad7SDan McDonald }
45055878fad7SDan McDonald 
450692241e0bSTom Erickson #ifdef	DEBUG
450792241e0bSTom Erickson static boolean_t zfs_ioc_recv_inject_err;
450892241e0bSTom Erickson #endif
450992241e0bSTom Erickson 
45103cb34c60Sahrens /*
45116ccda740Sloli  * nvlist 'errors' is always allocated. It will contain descriptions of
45126ccda740Sloli  * encountered errors, if any. It's the callers responsibility to free.
45133cb34c60Sahrens  */
4514fa9e4066Sahrens static int
45156ccda740Sloli zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
45166ccda740Sloli     nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
45176ccda740Sloli     boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
45186ccda740Sloli     int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
45196ccda740Sloli     uint64_t *action_handle, nvlist_t **errors)
4520fa9e4066Sahrens {
45213cb34c60Sahrens 	dmu_recv_cookie_t drc;
452292241e0bSTom Erickson 	int error = 0;
452392241e0bSTom Erickson 	int props_error = 0;
45243cb34c60Sahrens 	offset_t off;
45256ccda740Sloli 	nvlist_t *local_delayprops = NULL;
45266ccda740Sloli 	nvlist_t *recv_delayprops = NULL;
452792241e0bSTom Erickson 	nvlist_t *origprops = NULL; /* existing properties */
45286ccda740Sloli 	nvlist_t *origrecvd = NULL; /* existing received properties */
452992241e0bSTom Erickson 	boolean_t first_recvd_props = B_FALSE;
45306ccda740Sloli 	file_t *input_fp;
4531fa9e4066Sahrens 
45326ccda740Sloli 	*read_bytes = 0;
45336ccda740Sloli 	*errflags = 0;
45346ccda740Sloli 	*errors = fnvlist_alloc();
45353cb34c60Sahrens 
45366ccda740Sloli 	input_fp = getf(input_fd);
45376ccda740Sloli 	if (input_fp == NULL)
4538be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
4539f18faf3fSek 
45406ccda740Sloli 	error = dmu_recv_begin(tofs, tosnap, begin_record, force,
45416ccda740Sloli 	    resumable, localprops, hidden_args, origin, &drc);
45423b2aab18SMatthew Ahrens 	if (error != 0)
45433b2aab18SMatthew Ahrens 		goto out;
45443b2aab18SMatthew Ahrens 
45453b2aab18SMatthew Ahrens 	/*
45463b2aab18SMatthew Ahrens 	 * Set properties before we receive the stream so that they are applied
45473b2aab18SMatthew Ahrens 	 * to the new data. Note that we must call dmu_recv_stream() if
45483b2aab18SMatthew Ahrens 	 * dmu_recv_begin() succeeds.
45493b2aab18SMatthew Ahrens 	 */
45506ccda740Sloli 	if (recvprops != NULL && !drc.drc_newfs) {
45513b2aab18SMatthew Ahrens 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
45523b2aab18SMatthew Ahrens 		    SPA_VERSION_RECVD_PROPS &&
45533b2aab18SMatthew Ahrens 		    !dsl_prop_get_hasrecvd(tofs))
455492241e0bSTom Erickson 			first_recvd_props = B_TRUE;
455592241e0bSTom Erickson 
4556745cd3c5Smaybee 		/*
455792241e0bSTom Erickson 		 * If new received properties are supplied, they are to
45586ccda740Sloli 		 * completely replace the existing received properties,
45596ccda740Sloli 		 * so stash away the existing ones.
4560745cd3c5Smaybee 		 */
45616ccda740Sloli 		if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
456292241e0bSTom Erickson 			nvlist_t *errlist = NULL;
456392241e0bSTom Erickson 			/*
456492241e0bSTom Erickson 			 * Don't bother writing a property if its value won't
456592241e0bSTom Erickson 			 * change (and avoid the unnecessary security checks).
456692241e0bSTom Erickson 			 *
456792241e0bSTom Erickson 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
456892241e0bSTom Erickson 			 * special case where we blow away all local properties
456992241e0bSTom Erickson 			 * regardless.
457092241e0bSTom Erickson 			 */
457192241e0bSTom Erickson 			if (!first_recvd_props)
45726ccda740Sloli 				props_reduce(recvprops, origrecvd);
45736ccda740Sloli 			if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
45746ccda740Sloli 				(void) nvlist_merge(*errors, errlist, 0);
457592241e0bSTom Erickson 			nvlist_free(errlist);
45763cb34c60Sahrens 
45776ccda740Sloli 			if (clear_received_props(tofs, origrecvd,
45786ccda740Sloli 			    first_recvd_props ? NULL : recvprops) != 0)
45796ccda740Sloli 				*errflags |= ZPROP_ERR_NOCLEAR;
45806ccda740Sloli 		} else {
45816ccda740Sloli 			*errflags |= ZPROP_ERR_NOCLEAR;
45826ccda740Sloli 		}
45836ccda740Sloli 	}
45846ccda740Sloli 
45856ccda740Sloli 	/*
45866ccda740Sloli 	 * Stash away existing properties so we can restore them on error unless
45876ccda740Sloli 	 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
45886ccda740Sloli 	 * case "origrecvd" will take care of that.
45896ccda740Sloli 	 */
45906ccda740Sloli 	if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
45916ccda740Sloli 		objset_t *os;
45926ccda740Sloli 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
45936ccda740Sloli 			if (dsl_prop_get_all(os, &origprops) != 0) {
45946ccda740Sloli 				*errflags |= ZPROP_ERR_NOCLEAR;
45956ccda740Sloli 			}
45966ccda740Sloli 			dmu_objset_rele(os, FTAG);
45973b2aab18SMatthew Ahrens 		} else {
45986ccda740Sloli 			*errflags |= ZPROP_ERR_NOCLEAR;
459992241e0bSTom Erickson 		}
46003b2aab18SMatthew Ahrens 	}
460192241e0bSTom Erickson 
46026ccda740Sloli 	if (recvprops != NULL) {
46033b2aab18SMatthew Ahrens 		props_error = dsl_prop_set_hasrecvd(tofs);
46043b2aab18SMatthew Ahrens 
46053b2aab18SMatthew Ahrens 		if (props_error == 0) {
46066ccda740Sloli 			recv_delayprops = extract_delay_props(recvprops);
46073b2aab18SMatthew Ahrens 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
46086ccda740Sloli 			    recvprops, *errors);
46093b2aab18SMatthew Ahrens 		}
461092241e0bSTom Erickson 	}
461192241e0bSTom Erickson 
46126ccda740Sloli 	if (localprops != NULL) {
46136ccda740Sloli 		nvlist_t *oprops = fnvlist_alloc();
46146ccda740Sloli 		nvlist_t *xprops = fnvlist_alloc();
46156ccda740Sloli 		nvpair_t *nvp = NULL;
46166ccda740Sloli 
46176ccda740Sloli 		while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
46186ccda740Sloli 			if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
46196ccda740Sloli 				/* -x property */
46206ccda740Sloli 				const char *name = nvpair_name(nvp);
46216ccda740Sloli 				zfs_prop_t prop = zfs_name_to_prop(name);
46226ccda740Sloli 				if (prop != ZPROP_INVAL) {
46236ccda740Sloli 					if (!zfs_prop_inheritable(prop))
46246ccda740Sloli 						continue;
46256ccda740Sloli 				} else if (!zfs_prop_user(name))
46266ccda740Sloli 					continue;
46276ccda740Sloli 				fnvlist_add_boolean(xprops, name);
46286ccda740Sloli 			} else {
46296ccda740Sloli 				/* -o property=value */
46306ccda740Sloli 				fnvlist_add_nvpair(oprops, nvp);
46316ccda740Sloli 			}
46326ccda740Sloli 		}
46336ccda740Sloli 
46346ccda740Sloli 		local_delayprops = extract_delay_props(oprops);
46356ccda740Sloli 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
46366ccda740Sloli 		    oprops, *errors);
46376ccda740Sloli 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
46386ccda740Sloli 		    xprops, *errors);
46396ccda740Sloli 
46406ccda740Sloli 		nvlist_free(oprops);
46416ccda740Sloli 		nvlist_free(xprops);
46426ccda740Sloli 	}
46436ccda740Sloli 
46446ccda740Sloli 	off = input_fp->f_offset;
46456ccda740Sloli 	error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
46466ccda740Sloli 	    action_handle);
4647a2eea2e1Sahrens 
4648f4b94bdeSMatthew Ahrens 	if (error == 0) {
4649f4b94bdeSMatthew Ahrens 		zfsvfs_t *zfsvfs = NULL;
4650745cd3c5Smaybee 
4651f4b94bdeSMatthew Ahrens 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4652f4b94bdeSMatthew Ahrens 			/* online recv */
4653690041b9SAndriy Gapon 			dsl_dataset_t *ds;
4654f4b94bdeSMatthew Ahrens 			int end_err;
4655745cd3c5Smaybee 
4656690041b9SAndriy Gapon 			ds = dmu_objset_ds(zfsvfs->z_os);
4657503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
4658f4b94bdeSMatthew Ahrens 			/*
4659f4b94bdeSMatthew Ahrens 			 * If the suspend fails, then the recv_end will
4660f4b94bdeSMatthew Ahrens 			 * likely also fail, and clean up after itself.
4661f4b94bdeSMatthew Ahrens 			 */
466291948b51SKeith M Wesolowski 			end_err = dmu_recv_end(&drc, zfsvfs);
46635c703fceSGeorge Wilson 			if (error == 0)
4664690041b9SAndriy Gapon 				error = zfs_resume_fs(zfsvfs, ds);
4665f4b94bdeSMatthew Ahrens 			error = error ? error : end_err;
4666f4b94bdeSMatthew Ahrens 			VFS_RELE(zfsvfs->z_vfs);
4667745cd3c5Smaybee 		} else {
466891948b51SKeith M Wesolowski 			error = dmu_recv_end(&drc, NULL);
46693cb34c60Sahrens 		}
46705878fad7SDan McDonald 
46715878fad7SDan McDonald 		/* Set delayed properties now, after we're done receiving. */
46726ccda740Sloli 		if (recv_delayprops != NULL && error == 0) {
46735878fad7SDan McDonald 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
46746ccda740Sloli 			    recv_delayprops, *errors);
46756ccda740Sloli 		}
46766ccda740Sloli 		if (local_delayprops != NULL && error == 0) {
46776ccda740Sloli 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
46786ccda740Sloli 			    local_delayprops, *errors);
46795878fad7SDan McDonald 		}
46805878fad7SDan McDonald 	}
46815878fad7SDan McDonald 
46825878fad7SDan McDonald 	/*
46836ccda740Sloli 	 * Merge delayed props back in with initial props, in case
46846ccda740Sloli 	 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
46856ccda740Sloli 	 * we have to make sure clear_received_props() includes
46866ccda740Sloli 	 * the delayed properties).
46876ccda740Sloli 	 *
46886ccda740Sloli 	 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
46896ccda740Sloli 	 * using ASSERT() will be just like a VERIFY.
46905878fad7SDan McDonald 	 */
46916ccda740Sloli 	if (recv_delayprops != NULL) {
46926ccda740Sloli 		ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
46936ccda740Sloli 		nvlist_free(recv_delayprops);
46946ccda740Sloli 	}
46956ccda740Sloli 	if (local_delayprops != NULL) {
46966ccda740Sloli 		ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
46976ccda740Sloli 		nvlist_free(local_delayprops);
469847f263f4Sek 	}
46993cb34c60Sahrens 
47006ccda740Sloli 	*read_bytes = off - input_fp->f_offset;
47016ccda740Sloli 	if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
47026ccda740Sloli 		input_fp->f_offset = off;
4703a2eea2e1Sahrens 
470492241e0bSTom Erickson #ifdef	DEBUG
470592241e0bSTom Erickson 	if (zfs_ioc_recv_inject_err) {
470692241e0bSTom Erickson 		zfs_ioc_recv_inject_err = B_FALSE;
470792241e0bSTom Erickson 		error = 1;
470892241e0bSTom Erickson 	}
470992241e0bSTom Erickson #endif
47106ccda740Sloli 
4711745cd3c5Smaybee 	/*
4712745cd3c5Smaybee 	 * On error, restore the original props.
4713745cd3c5Smaybee 	 */
47146ccda740Sloli 	if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
47156ccda740Sloli 		if (clear_received_props(tofs, recvprops, NULL) != 0) {
47163b2aab18SMatthew Ahrens 			/*
47173b2aab18SMatthew Ahrens 			 * We failed to clear the received properties.
47183b2aab18SMatthew Ahrens 			 * Since we may have left a $recvd value on the
47193b2aab18SMatthew Ahrens 			 * system, we can't clear the $hasrecvd flag.
47203b2aab18SMatthew Ahrens 			 */
47216ccda740Sloli 			*errflags |= ZPROP_ERR_NORESTORE;
47223b2aab18SMatthew Ahrens 		} else if (first_recvd_props) {
47233b2aab18SMatthew Ahrens 			dsl_prop_unset_hasrecvd(tofs);
472492241e0bSTom Erickson 		}
472592241e0bSTom Erickson 
47266ccda740Sloli 		if (origrecvd == NULL && !drc.drc_newfs) {
472792241e0bSTom Erickson 			/* We failed to stash the original properties. */
47286ccda740Sloli 			*errflags |= ZPROP_ERR_NORESTORE;
472992241e0bSTom Erickson 		}
473092241e0bSTom Erickson 
473192241e0bSTom Erickson 		/*
473292241e0bSTom Erickson 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
473392241e0bSTom Erickson 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
47346ccda740Sloli 		 * explicitly if we're restoring local properties cleared in the
473592241e0bSTom Erickson 		 * first new-style receive.
473692241e0bSTom Erickson 		 */
47376ccda740Sloli 		if (origrecvd != NULL &&
473892241e0bSTom Erickson 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
473992241e0bSTom Erickson 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
47406ccda740Sloli 		    origrecvd, NULL) != 0) {
474192241e0bSTom Erickson 			/*
474292241e0bSTom Erickson 			 * We stashed the original properties but failed to
474392241e0bSTom Erickson 			 * restore them.
474492241e0bSTom Erickson 			 */
47456ccda740Sloli 			*errflags |= ZPROP_ERR_NORESTORE;
474692241e0bSTom Erickson 		}
4747745cd3c5Smaybee 	}
47486ccda740Sloli 	if (error != 0 && localprops != NULL && !drc.drc_newfs &&
47496ccda740Sloli 	    !first_recvd_props) {
47506ccda740Sloli 		nvlist_t *setprops;
47516ccda740Sloli 		nvlist_t *inheritprops;
47526ccda740Sloli 		nvpair_t *nvp;
47536ccda740Sloli 
47546ccda740Sloli 		if (origprops == NULL) {
47556ccda740Sloli 			/* We failed to stash the original properties. */
47566ccda740Sloli 			*errflags |= ZPROP_ERR_NORESTORE;
47576ccda740Sloli 			goto out;
47586ccda740Sloli 		}
47596ccda740Sloli 
47606ccda740Sloli 		/* Restore original props */
47616ccda740Sloli 		setprops = fnvlist_alloc();
47626ccda740Sloli 		inheritprops = fnvlist_alloc();
47636ccda740Sloli 		nvp = NULL;
47646ccda740Sloli 		while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
47656ccda740Sloli 			const char *name = nvpair_name(nvp);
47666ccda740Sloli 			const char *source;
47676ccda740Sloli 			nvlist_t *attrs;
47686ccda740Sloli 
47696ccda740Sloli 			if (!nvlist_exists(origprops, name)) {
47706ccda740Sloli 				/*
47716ccda740Sloli 				 * Property was not present or was explicitly
47726ccda740Sloli 				 * inherited before the receive, restore this.
47736ccda740Sloli 				 */
47746ccda740Sloli 				fnvlist_add_boolean(inheritprops, name);
47756ccda740Sloli 				continue;
47766ccda740Sloli 			}
47776ccda740Sloli 			attrs = fnvlist_lookup_nvlist(origprops, name);
47786ccda740Sloli 			source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
47796ccda740Sloli 
47806ccda740Sloli 			/* Skip received properties */
47816ccda740Sloli 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
47826ccda740Sloli 				continue;
47836ccda740Sloli 
47846ccda740Sloli 			if (strcmp(source, tofs) == 0) {
47856ccda740Sloli 				/* Property was locally set */
47866ccda740Sloli 				fnvlist_add_nvlist(setprops, name, attrs);
47876ccda740Sloli 			} else {
47886ccda740Sloli 				/* Property was implicitly inherited */
47896ccda740Sloli 				fnvlist_add_boolean(inheritprops, name);
47906ccda740Sloli 			}
47916ccda740Sloli 		}
47926ccda740Sloli 
47936ccda740Sloli 		if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
47946ccda740Sloli 		    NULL) != 0)
47956ccda740Sloli 			*errflags |= ZPROP_ERR_NORESTORE;
47966ccda740Sloli 		if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
47976ccda740Sloli 		    NULL) != 0)
47986ccda740Sloli 			*errflags |= ZPROP_ERR_NORESTORE;
47996ccda740Sloli 
48006ccda740Sloli 		nvlist_free(setprops);
48016ccda740Sloli 		nvlist_free(inheritprops);
48026ccda740Sloli 	}
4803745cd3c5Smaybee out:
48046ccda740Sloli 	releasef(input_fd);
48056ccda740Sloli 	nvlist_free(origrecvd);
4806745cd3c5Smaybee 	nvlist_free(origprops);
480792241e0bSTom Erickson 
480892241e0bSTom Erickson 	if (error == 0)
480992241e0bSTom Erickson 		error = props_error;
481092241e0bSTom Erickson 
4811fa9e4066Sahrens 	return (error);
4812fa9e4066Sahrens }
4813fa9e4066Sahrens 
48146ccda740Sloli /*
48156ccda740Sloli  * inputs:
48166ccda740Sloli  * zc_name		name of containing filesystem
48176ccda740Sloli  * zc_nvlist_src{_size}	nvlist of received properties to apply
48186ccda740Sloli  * zc_nvlist_conf{_size} nvlist of local properties to apply
48196ccda740Sloli  * zc_history_offset{_len} nvlist of hidden args { "wkeydata" -> value }
48206ccda740Sloli  * zc_value		name of snapshot to create
48216ccda740Sloli  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
48226ccda740Sloli  * zc_cookie		file descriptor to recv from
48236ccda740Sloli  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
48246ccda740Sloli  * zc_guid		force flag
48256ccda740Sloli  * zc_cleanup_fd	cleanup-on-exit file descriptor
48266ccda740Sloli  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
48276ccda740Sloli  * zc_resumable		if data is incomplete assume sender will resume
48286ccda740Sloli  *
48296ccda740Sloli  * outputs:
48306ccda740Sloli  * zc_cookie		number of bytes read
48316ccda740Sloli  * zc_nvlist_dst{_size} error for each unapplied received property
48326ccda740Sloli  * zc_obj		zprop_errflags_t
48336ccda740Sloli  * zc_action_handle	handle for this guid/ds mapping
48346ccda740Sloli  */
48356ccda740Sloli static int
48366ccda740Sloli zfs_ioc_recv(zfs_cmd_t *zc)
48376ccda740Sloli {
48386ccda740Sloli 	dmu_replay_record_t begin_record;
48396ccda740Sloli 	nvlist_t *errors = NULL;
48406ccda740Sloli 	nvlist_t *recvdprops = NULL;
48416ccda740Sloli 	nvlist_t *localprops = NULL;
48426ccda740Sloli 	nvlist_t *hidden_args = NULL;
48436ccda740Sloli 	char *origin = NULL;
48446ccda740Sloli 	char *tosnap;
48456ccda740Sloli 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
48466ccda740Sloli 	int error = 0;
48476ccda740Sloli 
48486ccda740Sloli 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
48496ccda740Sloli 	    strchr(zc->zc_value, '@') == NULL ||
48506ccda740Sloli 	    strchr(zc->zc_value, '%'))
48516ccda740Sloli 		return (SET_ERROR(EINVAL));
48526ccda740Sloli 
48536ccda740Sloli 	(void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
48546ccda740Sloli 	tosnap = strchr(tofs, '@');
48556ccda740Sloli 	*tosnap++ = '\0';
48566ccda740Sloli 
48576ccda740Sloli 	if (zc->zc_nvlist_src != 0 &&
48586ccda740Sloli 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
48596ccda740Sloli 	    zc->zc_iflags, &recvdprops)) != 0)
48606ccda740Sloli 		return (error);
48616ccda740Sloli 
48626ccda740Sloli 	if (zc->zc_nvlist_conf != 0 &&
48636ccda740Sloli 	    (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
48646ccda740Sloli 	    zc->zc_iflags, &localprops)) != 0)
48656ccda740Sloli 		return (error);
48666ccda740Sloli 
48676ccda740Sloli 	if (zc->zc_history_offset != 0 &&
48686ccda740Sloli 	    (error = get_nvlist(zc->zc_history_offset, zc->zc_history_len,
48696ccda740Sloli 	    zc->zc_iflags, &hidden_args)) != 0)
48706ccda740Sloli 		return (error);
48716ccda740Sloli 
48726ccda740Sloli 	if (zc->zc_string[0])
48736ccda740Sloli 		origin = zc->zc_string;
48746ccda740Sloli 
48756ccda740Sloli 	begin_record.drr_type = DRR_BEGIN;
48766ccda740Sloli 	begin_record.drr_payloadlen = zc->zc_begin_record.drr_payloadlen;
48776ccda740Sloli 	begin_record.drr_u.drr_begin = zc->zc_begin_record.drr_u.drr_begin;
48786ccda740Sloli 
48796ccda740Sloli 	error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
48806ccda740Sloli 	    hidden_args, zc->zc_guid, zc->zc_resumable, zc->zc_cookie,
48816ccda740Sloli 	    &begin_record, zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
48826ccda740Sloli 	    &zc->zc_action_handle, &errors);
48836ccda740Sloli 	nvlist_free(recvdprops);
48846ccda740Sloli 	nvlist_free(localprops);
48856ccda740Sloli 
48866ccda740Sloli 	/*
48876ccda740Sloli 	 * Now that all props, initial and delayed, are set, report the prop
48886ccda740Sloli 	 * errors to the caller.
48896ccda740Sloli 	 */
48906ccda740Sloli 	if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
48916ccda740Sloli 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
48926ccda740Sloli 	    put_nvlist(zc, errors) != 0)) {
48936ccda740Sloli 		/*
48946ccda740Sloli 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
48956ccda740Sloli 		 * size or supplied an invalid address.
48966ccda740Sloli 		 */
48976ccda740Sloli 		error = SET_ERROR(EINVAL);
48986ccda740Sloli 	}
48996ccda740Sloli 
49006ccda740Sloli 	nvlist_free(errors);
49016ccda740Sloli 
49026ccda740Sloli 	return (error);
49036ccda740Sloli }
49046ccda740Sloli 
49053cb34c60Sahrens /*
49063cb34c60Sahrens  * inputs:
49073cb34c60Sahrens  * zc_name	name of snapshot to send
49083cb34c60Sahrens  * zc_cookie	file descriptor to send stream to
4909a7f53a56SChris Kirby  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
4910a7f53a56SChris Kirby  * zc_sendobj	objsetid of snapshot to send
4911a7f53a56SChris Kirby  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
491219b94df9SMatthew Ahrens  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
491319b94df9SMatthew Ahrens  *		output size in zc_objset_type.
4914b5152584SMatthew Ahrens  * zc_flags	lzc_send_flags
49153cb34c60Sahrens  *
491678f17100SMatthew Ahrens  * outputs:
491778f17100SMatthew Ahrens  * zc_objset_type	estimated size, if zc_guid is set
49183cb34c60Sahrens  */
4919fa9e4066Sahrens static int
49203cb34c60Sahrens zfs_ioc_send(zfs_cmd_t *zc)
4921fa9e4066Sahrens {
4922fa9e4066Sahrens 	int error;
49233cb34c60Sahrens 	offset_t off;
492419b94df9SMatthew Ahrens 	boolean_t estimate = (zc->zc_guid != 0);
49255d7b4d43SMatthew Ahrens 	boolean_t embedok = (zc->zc_flags & 0x1);
4926b5152584SMatthew Ahrens 	boolean_t large_block_ok = (zc->zc_flags & 0x2);
49275602294fSDan Kimmel 	boolean_t compressok = (zc->zc_flags & 0x4);
4928eb633035STom Caputi 	boolean_t rawok = (zc->zc_flags & 0x8);
4929fa9e4066Sahrens 
49303b2aab18SMatthew Ahrens 	if (zc->zc_obj != 0) {
49313b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
49323b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
4933a7f53a56SChris Kirby 
49343b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
49353b2aab18SMatthew Ahrens 		if (error != 0)
4936a7f53a56SChris Kirby 			return (error);
49373b2aab18SMatthew Ahrens 
49383b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
49393b2aab18SMatthew Ahrens 		if (error != 0) {
49403b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
4941fa9e4066Sahrens 			return (error);
4942fa9e4066Sahrens 		}
49433b2aab18SMatthew Ahrens 
49443b2aab18SMatthew Ahrens 		if (dsl_dir_is_clone(tosnap->ds_dir))
4945c1379625SJustin T. Gibbs 			zc->zc_fromobj =
4946c1379625SJustin T. Gibbs 			    dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
49473b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
49483b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
49494445fffbSMatthew Ahrens 	}
49504445fffbSMatthew Ahrens 
49513b2aab18SMatthew Ahrens 	if (estimate) {
49523b2aab18SMatthew Ahrens 		dsl_pool_t *dp;
49533b2aab18SMatthew Ahrens 		dsl_dataset_t *tosnap;
49543b2aab18SMatthew Ahrens 		dsl_dataset_t *fromsnap = NULL;
49554445fffbSMatthew Ahrens 
49563b2aab18SMatthew Ahrens 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
49573b2aab18SMatthew Ahrens 		if (error != 0)
49583b2aab18SMatthew Ahrens 			return (error);
49593b2aab18SMatthew Ahrens 
4960eb633035STom Caputi 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
4961eb633035STom Caputi 		    FTAG, &tosnap);
49623b2aab18SMatthew Ahrens 		if (error != 0) {
49633b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
49643b2aab18SMatthew Ahrens 			return (error);
49654445fffbSMatthew Ahrens 		}
49664445fffbSMatthew Ahrens 
49673b2aab18SMatthew Ahrens 		if (zc->zc_fromobj != 0) {
49683b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
49693b2aab18SMatthew Ahrens 			    FTAG, &fromsnap);
49703b2aab18SMatthew Ahrens 			if (error != 0) {
49713b2aab18SMatthew Ahrens 				dsl_dataset_rele(tosnap, FTAG);
49723b2aab18SMatthew Ahrens 				dsl_pool_rele(dp, FTAG);
49734445fffbSMatthew Ahrens 				return (error);
49744445fffbSMatthew Ahrens 			}
49754445fffbSMatthew Ahrens 		}
4976fa9e4066Sahrens 
4977eb633035STom Caputi 		error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
497819b94df9SMatthew Ahrens 		    &zc->zc_objset_type);
49793b2aab18SMatthew Ahrens 
49803b2aab18SMatthew Ahrens 		if (fromsnap != NULL)
49813b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromsnap, FTAG);
49823b2aab18SMatthew Ahrens 		dsl_dataset_rele(tosnap, FTAG);
49833b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
498419b94df9SMatthew Ahrens 	} else {
498519b94df9SMatthew Ahrens 		file_t *fp = getf(zc->zc_cookie);
49863b2aab18SMatthew Ahrens 		if (fp == NULL)
4987be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBADF));
4988fa9e4066Sahrens 
498919b94df9SMatthew Ahrens 		off = fp->f_offset;
49903b2aab18SMatthew Ahrens 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4991eb633035STom Caputi 		    zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
4992b5152584SMatthew Ahrens 		    zc->zc_cookie, fp->f_vnode, &off);
4993fa9e4066Sahrens 
499419b94df9SMatthew Ahrens 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
499519b94df9SMatthew Ahrens 			fp->f_offset = off;
499619b94df9SMatthew Ahrens 		releasef(zc->zc_cookie);
499719b94df9SMatthew Ahrens 	}
4998fa9e4066Sahrens 	return (error);
4999fa9e4066Sahrens }
5000fa9e4066Sahrens 
50014e3c9f44SBill Pijewski /*
50024e3c9f44SBill Pijewski  * inputs:
50034e3c9f44SBill Pijewski  * zc_name	name of snapshot on which to report progress
50044e3c9f44SBill Pijewski  * zc_cookie	file descriptor of send stream
50054e3c9f44SBill Pijewski  *
50064e3c9f44SBill Pijewski  * outputs:
50074e3c9f44SBill Pijewski  * zc_cookie	number of bytes written in send stream thus far
50084e3c9f44SBill Pijewski  */
50094e3c9f44SBill Pijewski static int
50104e3c9f44SBill Pijewski zfs_ioc_send_progress(zfs_cmd_t *zc)
50114e3c9f44SBill Pijewski {
50123b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
50134e3c9f44SBill Pijewski 	dsl_dataset_t *ds;
50144e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = NULL;
50154e3c9f44SBill Pijewski 	int error;
50164e3c9f44SBill Pijewski 
50173b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
50183b2aab18SMatthew Ahrens 	if (error != 0)
50193b2aab18SMatthew Ahrens 		return (error);
50203b2aab18SMatthew Ahrens 
50213b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
50223b2aab18SMatthew Ahrens 	if (error != 0) {
50233b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
50244e3c9f44SBill Pijewski 		return (error);
50253b2aab18SMatthew Ahrens 	}
50264e3c9f44SBill Pijewski 
50274e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
50284e3c9f44SBill Pijewski 
50294e3c9f44SBill Pijewski 	/*
50304e3c9f44SBill Pijewski 	 * Iterate over all the send streams currently active on this dataset.
50314e3c9f44SBill Pijewski 	 * If there's one which matches the specified file descriptor _and_ the
50324e3c9f44SBill Pijewski 	 * stream was started by the current process, return the progress of
50334e3c9f44SBill Pijewski 	 * that stream.
50344e3c9f44SBill Pijewski 	 */
50354e3c9f44SBill Pijewski 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
50364e3c9f44SBill Pijewski 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
50374e3c9f44SBill Pijewski 		if (dsp->dsa_outfd == zc->zc_cookie &&
50384e3c9f44SBill Pijewski 		    dsp->dsa_proc == curproc)
50394e3c9f44SBill Pijewski 			break;
50404e3c9f44SBill Pijewski 	}
50414e3c9f44SBill Pijewski 
50424e3c9f44SBill Pijewski 	if (dsp != NULL)
50434e3c9f44SBill Pijewski 		zc->zc_cookie = *(dsp->dsa_off);
50444e3c9f44SBill Pijewski 	else
5045be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOENT);
50464e3c9f44SBill Pijewski 
50474e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
50484e3c9f44SBill Pijewski 	dsl_dataset_rele(ds, FTAG);
50493b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
50504e3c9f44SBill Pijewski 	return (error);
50514e3c9f44SBill Pijewski }
50524e3c9f44SBill Pijewski 
5053ea8dc4b6Seschrock static int
5054ea8dc4b6Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc)
5055ea8dc4b6Seschrock {
5056ea8dc4b6Seschrock 	int id, error;
5057ea8dc4b6Seschrock 
5058ea8dc4b6Seschrock 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5059ea8dc4b6Seschrock 	    &zc->zc_inject_record);
5060ea8dc4b6Seschrock 
5061ea8dc4b6Seschrock 	if (error == 0)
5062ea8dc4b6Seschrock 		zc->zc_guid = (uint64_t)id;
5063ea8dc4b6Seschrock 
5064ea8dc4b6Seschrock 	return (error);
5065ea8dc4b6Seschrock }
5066ea8dc4b6Seschrock 
5067ea8dc4b6Seschrock static int
5068ea8dc4b6Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc)
5069ea8dc4b6Seschrock {
5070ea8dc4b6Seschrock 	return (zio_clear_fault((int)zc->zc_guid));
5071ea8dc4b6Seschrock }
5072ea8dc4b6Seschrock 
5073ea8dc4b6Seschrock static int
5074ea8dc4b6Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5075ea8dc4b6Seschrock {
5076ea8dc4b6Seschrock 	int id = (int)zc->zc_guid;
5077ea8dc4b6Seschrock 	int error;
5078ea8dc4b6Seschrock 
5079ea8dc4b6Seschrock 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5080ea8dc4b6Seschrock 	    &zc->zc_inject_record);
5081ea8dc4b6Seschrock 
5082ea8dc4b6Seschrock 	zc->zc_guid = id;
5083ea8dc4b6Seschrock 
5084ea8dc4b6Seschrock 	return (error);
5085ea8dc4b6Seschrock }
5086ea8dc4b6Seschrock 
5087ea8dc4b6Seschrock static int
5088ea8dc4b6Seschrock zfs_ioc_error_log(zfs_cmd_t *zc)
5089ea8dc4b6Seschrock {
5090ea8dc4b6Seschrock 	spa_t *spa;
5091ea8dc4b6Seschrock 	int error;
5092e9dbad6fSeschrock 	size_t count = (size_t)zc->zc_nvlist_dst_size;
5093ea8dc4b6Seschrock 
5094ea8dc4b6Seschrock 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5095ea8dc4b6Seschrock 		return (error);
5096ea8dc4b6Seschrock 
5097e9dbad6fSeschrock 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5098ea8dc4b6Seschrock 	    &count);
5099ea8dc4b6Seschrock 	if (error == 0)
5100e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = count;
5101ea8dc4b6Seschrock 	else
5102e9dbad6fSeschrock 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5103ea8dc4b6Seschrock 
5104ea8dc4b6Seschrock 	spa_close(spa, FTAG);
5105ea8dc4b6Seschrock 
5106ea8dc4b6Seschrock 	return (error);
5107ea8dc4b6Seschrock }
5108ea8dc4b6Seschrock 
5109ea8dc4b6Seschrock static int
5110ea8dc4b6Seschrock zfs_ioc_clear(zfs_cmd_t *zc)
5111ea8dc4b6Seschrock {
5112ea8dc4b6Seschrock 	spa_t *spa;
5113ea8dc4b6Seschrock 	vdev_t *vd;
5114bb8b5132Sek 	int error;
5115ea8dc4b6Seschrock 
5116b87f3af3Sperrin 	/*
5117b87f3af3Sperrin 	 * On zpool clear we also fix up missing slogs
5118b87f3af3Sperrin 	 */
5119b87f3af3Sperrin 	mutex_enter(&spa_namespace_lock);
5120b87f3af3Sperrin 	spa = spa_lookup(zc->zc_name);
5121b87f3af3Sperrin 	if (spa == NULL) {
5122b87f3af3Sperrin 		mutex_exit(&spa_namespace_lock);
5123be6fd75aSMatthew Ahrens 		return (SET_ERROR(EIO));
5124b87f3af3Sperrin 	}
5125b24ab676SJeff Bonwick 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5126b87f3af3Sperrin 		/* we need to let spa_open/spa_load clear the chains */
5127b24ab676SJeff Bonwick 		spa_set_log_state(spa, SPA_LOG_CLEAR);
5128b87f3af3Sperrin 	}
5129468c413aSTim Haley 	spa->spa_last_open_failed = 0;
5130b87f3af3Sperrin 	mutex_exit(&spa_namespace_lock);
5131b87f3af3Sperrin 
5132c8ee1847SVictor Latushkin 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5133468c413aSTim Haley 		error = spa_open(zc->zc_name, &spa, FTAG);
5134468c413aSTim Haley 	} else {
5135468c413aSTim Haley 		nvlist_t *policy;
5136468c413aSTim Haley 		nvlist_t *config = NULL;
5137468c413aSTim Haley 
5138dd328bf6SToomas Soome 		if (zc->zc_nvlist_src == 0)
5139be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
5140468c413aSTim Haley 
5141468c413aSTim Haley 		if ((error = get_nvlist(zc->zc_nvlist_src,
5142468c413aSTim Haley 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5143468c413aSTim Haley 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5144468c413aSTim Haley 			    policy, &config);
5145468c413aSTim Haley 			if (config != NULL) {
51464b964adaSGeorge Wilson 				int err;
51474b964adaSGeorge Wilson 
51484b964adaSGeorge Wilson 				if ((err = put_nvlist(zc, config)) != 0)
51494b964adaSGeorge Wilson 					error = err;
5150468c413aSTim Haley 				nvlist_free(config);
5151468c413aSTim Haley 			}
5152468c413aSTim Haley 			nvlist_free(policy);
5153468c413aSTim Haley 		}
5154468c413aSTim Haley 	}
5155468c413aSTim Haley 
51563b2aab18SMatthew Ahrens 	if (error != 0)
5157ea8dc4b6Seschrock 		return (error);
5158ea8dc4b6Seschrock 
5159e0f1c0afSOlaf Faaland 	/*
5160e0f1c0afSOlaf Faaland 	 * If multihost is enabled, resuming I/O is unsafe as another
5161e0f1c0afSOlaf Faaland 	 * host may have imported the pool.
5162e0f1c0afSOlaf Faaland 	 */
5163e0f1c0afSOlaf Faaland 	if (spa_multihost(spa) && spa_suspended(spa))
5164e0f1c0afSOlaf Faaland 		return (SET_ERROR(EINVAL));
5165e0f1c0afSOlaf Faaland 
51668f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
5167ea8dc4b6Seschrock 
5168e9dbad6fSeschrock 	if (zc->zc_guid == 0) {
5169ea8dc4b6Seschrock 		vd = NULL;
5170c5904d13Seschrock 	} else {
5171c5904d13Seschrock 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5172fa94a07fSbrendan 		if (vd == NULL) {
5173e14bb325SJeff Bonwick 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
5174fa94a07fSbrendan 			spa_close(spa, FTAG);
5175be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
5176fa94a07fSbrendan 		}
5177ea8dc4b6Seschrock 	}
5178ea8dc4b6Seschrock 
5179e14bb325SJeff Bonwick 	vdev_clear(spa, vd);
5180e14bb325SJeff Bonwick 
5181e14bb325SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
5182ea8dc4b6Seschrock 
5183e14bb325SJeff Bonwick 	/*
5184e14bb325SJeff Bonwick 	 * Resume any suspended I/Os.
5185e14bb325SJeff Bonwick 	 */
518654d692b7SGeorge Wilson 	if (zio_resume(spa) != 0)
5187be6fd75aSMatthew Ahrens 		error = SET_ERROR(EIO);
5188ea8dc4b6Seschrock 
5189ea8dc4b6Seschrock 	spa_close(spa, FTAG);
5190ea8dc4b6Seschrock 
519154d692b7SGeorge Wilson 	return (error);
5192ea8dc4b6Seschrock }
5193ea8dc4b6Seschrock 
51944263d13fSGeorge Wilson static int
51954263d13fSGeorge Wilson zfs_ioc_pool_reopen(zfs_cmd_t *zc)
51964263d13fSGeorge Wilson {
51974263d13fSGeorge Wilson 	spa_t *spa;
51984263d13fSGeorge Wilson 	int error;
51994263d13fSGeorge Wilson 
52004263d13fSGeorge Wilson 	error = spa_open(zc->zc_name, &spa, FTAG);
52013b2aab18SMatthew Ahrens 	if (error != 0)
52024263d13fSGeorge Wilson 		return (error);
52034263d13fSGeorge Wilson 
52044263d13fSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
5205d6afdce2SGeorge Wilson 
5206d6afdce2SGeorge Wilson 	/*
5207d6afdce2SGeorge Wilson 	 * If a resilver is already in progress then set the
5208d6afdce2SGeorge Wilson 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
5209d6afdce2SGeorge Wilson 	 * the scan as a side effect of the reopen. Otherwise, let
5210d6afdce2SGeorge Wilson 	 * vdev_open() decided if a resilver is required.
5211d6afdce2SGeorge Wilson 	 */
5212d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
52134263d13fSGeorge Wilson 	vdev_reopen(spa->spa_root_vdev);
5214d6afdce2SGeorge Wilson 	spa->spa_scrub_reopen = B_FALSE;
5215d6afdce2SGeorge Wilson 
52164263d13fSGeorge Wilson 	(void) spa_vdev_state_exit(spa, NULL, 0);
52174263d13fSGeorge Wilson 	spa_close(spa, FTAG);
52184263d13fSGeorge Wilson 	return (0);
52194263d13fSGeorge Wilson }
52203cb34c60Sahrens /*
52213cb34c60Sahrens  * inputs:
52223cb34c60Sahrens  * zc_name	name of filesystem
52233cb34c60Sahrens  *
5224681d9761SEric Taylor  * outputs:
5225681d9761SEric Taylor  * zc_string	name of conflicting snapshot, if there is one
52263cb34c60Sahrens  */
522799653d4eSeschrock static int
522899653d4eSeschrock zfs_ioc_promote(zfs_cmd_t *zc)
522999653d4eSeschrock {
5230a4b8c9aaSAndrew Stormont 	dsl_pool_t *dp;
5231a4b8c9aaSAndrew Stormont 	dsl_dataset_t *ds, *ods;
5232a4b8c9aaSAndrew Stormont 	char origin[ZFS_MAX_DATASET_NAME_LEN];
52330b69c2f0Sahrens 	char *cp;
5234a4b8c9aaSAndrew Stormont 	int error;
5235a4b8c9aaSAndrew Stormont 
5236add927f8Sloli 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5237add927f8Sloli 	if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5238add927f8Sloli 	    strchr(zc->zc_name, '%'))
5239add927f8Sloli 		return (SET_ERROR(EINVAL));
5240add927f8Sloli 
5241a4b8c9aaSAndrew Stormont 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5242a4b8c9aaSAndrew Stormont 	if (error != 0)
5243a4b8c9aaSAndrew Stormont 		return (error);
5244a4b8c9aaSAndrew Stormont 
5245a4b8c9aaSAndrew Stormont 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5246a4b8c9aaSAndrew Stormont 	if (error != 0) {
5247a4b8c9aaSAndrew Stormont 		dsl_pool_rele(dp, FTAG);
5248a4b8c9aaSAndrew Stormont 		return (error);
5249a4b8c9aaSAndrew Stormont 	}
5250a4b8c9aaSAndrew Stormont 
5251a4b8c9aaSAndrew Stormont 	if (!dsl_dir_is_clone(ds->ds_dir)) {
5252a4b8c9aaSAndrew Stormont 		dsl_dataset_rele(ds, FTAG);
5253a4b8c9aaSAndrew Stormont 		dsl_pool_rele(dp, FTAG);
5254a4b8c9aaSAndrew Stormont 		return (SET_ERROR(EINVAL));
5255a4b8c9aaSAndrew Stormont 	}
5256a4b8c9aaSAndrew Stormont 
5257a4b8c9aaSAndrew Stormont 	error = dsl_dataset_hold_obj(dp,
5258a4b8c9aaSAndrew Stormont 	    dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5259a4b8c9aaSAndrew Stormont 	if (error != 0) {
5260a4b8c9aaSAndrew Stormont 		dsl_dataset_rele(ds, FTAG);
5261a4b8c9aaSAndrew Stormont 		dsl_pool_rele(dp, FTAG);
5262a4b8c9aaSAndrew Stormont 		return (error);
5263a4b8c9aaSAndrew Stormont 	}
5264a4b8c9aaSAndrew Stormont 
5265a4b8c9aaSAndrew Stormont 	dsl_dataset_name(ods, origin);
5266a4b8c9aaSAndrew Stormont 	dsl_dataset_rele(ods, FTAG);
5267a4b8c9aaSAndrew Stormont 	dsl_dataset_rele(ds, FTAG);
5268a4b8c9aaSAndrew Stormont 	dsl_pool_rele(dp, FTAG);
52690b69c2f0Sahrens 
52700b69c2f0Sahrens 	/*
52710b69c2f0Sahrens 	 * We don't need to unmount *all* the origin fs's snapshots, but
52720b69c2f0Sahrens 	 * it's easier.
52730b69c2f0Sahrens 	 */
5274a4b8c9aaSAndrew Stormont 	cp = strchr(origin, '@');
52750b69c2f0Sahrens 	if (cp)
52760b69c2f0Sahrens 		*cp = '\0';
5277a4b8c9aaSAndrew Stormont 	(void) dmu_objset_find(origin,
52783b2aab18SMatthew Ahrens 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5279681d9761SEric Taylor 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
528099653d4eSeschrock }
528199653d4eSeschrock 
528214843421SMatthew Ahrens /*
5283*f67950b2SNasf-Fan  * Retrieve a single {user|group|project}{used|quota}@... property.
528414843421SMatthew Ahrens  *
528514843421SMatthew Ahrens  * inputs:
528614843421SMatthew Ahrens  * zc_name	name of filesystem
528714843421SMatthew Ahrens  * zc_objset_type zfs_userquota_prop_t
528814843421SMatthew Ahrens  * zc_value	domain name (eg. "S-1-234-567-89")
528914843421SMatthew Ahrens  * zc_guid	RID/UID/GID
529014843421SMatthew Ahrens  *
529114843421SMatthew Ahrens  * outputs:
529214843421SMatthew Ahrens  * zc_cookie	property value
529314843421SMatthew Ahrens  */
529414843421SMatthew Ahrens static int
529514843421SMatthew Ahrens zfs_ioc_userspace_one(zfs_cmd_t *zc)
529614843421SMatthew Ahrens {
529714843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
529814843421SMatthew Ahrens 	int error;
529914843421SMatthew Ahrens 
530014843421SMatthew Ahrens 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5301be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
530214843421SMatthew Ahrens 
53031412a1a2SMark Shellenbaum 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
53043b2aab18SMatthew Ahrens 	if (error != 0)
530514843421SMatthew Ahrens 		return (error);
530614843421SMatthew Ahrens 
530714843421SMatthew Ahrens 	error = zfs_userspace_one(zfsvfs,
530814843421SMatthew Ahrens 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
530914843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
531014843421SMatthew Ahrens 
531114843421SMatthew Ahrens 	return (error);
531214843421SMatthew Ahrens }
531314843421SMatthew Ahrens 
531414843421SMatthew Ahrens /*
531514843421SMatthew Ahrens  * inputs:
531614843421SMatthew Ahrens  * zc_name		name of filesystem
531714843421SMatthew Ahrens  * zc_cookie		zap cursor
531814843421SMatthew Ahrens  * zc_objset_type	zfs_userquota_prop_t
531914843421SMatthew Ahrens  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
532014843421SMatthew Ahrens  *
532114843421SMatthew Ahrens  * outputs:
532214843421SMatthew Ahrens  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
532314843421SMatthew Ahrens  * zc_cookie	zap cursor
532414843421SMatthew Ahrens  */
532514843421SMatthew Ahrens static int
532614843421SMatthew Ahrens zfs_ioc_userspace_many(zfs_cmd_t *zc)
532714843421SMatthew Ahrens {
532814843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
5329eeb85002STim Haley 	int bufsize = zc->zc_nvlist_dst_size;
533014843421SMatthew Ahrens 
5331eeb85002STim Haley 	if (bufsize <= 0)
5332be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOMEM));
5333eeb85002STim Haley 
53341412a1a2SMark Shellenbaum 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
53353b2aab18SMatthew Ahrens 	if (error != 0)
533614843421SMatthew Ahrens 		return (error);
533714843421SMatthew Ahrens 
533814843421SMatthew Ahrens 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
533914843421SMatthew Ahrens 
534014843421SMatthew Ahrens 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
534114843421SMatthew Ahrens 	    buf, &zc->zc_nvlist_dst_size);
534214843421SMatthew Ahrens 
534314843421SMatthew Ahrens 	if (error == 0) {
534414843421SMatthew Ahrens 		error = xcopyout(buf,
534514843421SMatthew Ahrens 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
534614843421SMatthew Ahrens 		    zc->zc_nvlist_dst_size);
534714843421SMatthew Ahrens 	}
534814843421SMatthew Ahrens 	kmem_free(buf, bufsize);
534914843421SMatthew Ahrens 	zfsvfs_rele(zfsvfs, FTAG);
535014843421SMatthew Ahrens 
535114843421SMatthew Ahrens 	return (error);
535214843421SMatthew Ahrens }
535314843421SMatthew Ahrens 
535414843421SMatthew Ahrens /*
535514843421SMatthew Ahrens  * inputs:
535614843421SMatthew Ahrens  * zc_name		name of filesystem
535714843421SMatthew Ahrens  *
535814843421SMatthew Ahrens  * outputs:
535914843421SMatthew Ahrens  * none
536014843421SMatthew Ahrens  */
536114843421SMatthew Ahrens static int
536214843421SMatthew Ahrens zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
536314843421SMatthew Ahrens {
536414843421SMatthew Ahrens 	objset_t *os;
53651195e687SMark J Musante 	int error = 0;
536614843421SMatthew Ahrens 	zfsvfs_t *zfsvfs;
536714843421SMatthew Ahrens 
536814843421SMatthew Ahrens 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5369503ad85cSMatthew Ahrens 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
537014843421SMatthew Ahrens 			/*
537114843421SMatthew Ahrens 			 * If userused is not enabled, it may be because the
537214843421SMatthew Ahrens 			 * objset needs to be closed & reopened (to grow the
537314843421SMatthew Ahrens 			 * objset_phys_t).  Suspend/resume the fs will do that.
537414843421SMatthew Ahrens 			 */
53755f5913bbSAndriy Gapon 			dsl_dataset_t *ds, *newds;
5376690041b9SAndriy Gapon 
5377690041b9SAndriy Gapon 			ds = dmu_objset_ds(zfsvfs->z_os);
5378503ad85cSMatthew Ahrens 			error = zfs_suspend_fs(zfsvfs);
537991948b51SKeith M Wesolowski 			if (error == 0) {
53805f5913bbSAndriy Gapon 				dmu_objset_refresh_ownership(ds, &newds,
5381eb633035STom Caputi 				    B_TRUE, zfsvfs);
53825f5913bbSAndriy Gapon 				error = zfs_resume_fs(zfsvfs, newds);
538391948b51SKeith M Wesolowski 			}
538414843421SMatthew Ahrens 		}
538514843421SMatthew Ahrens 		if (error == 0)
538614843421SMatthew Ahrens 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
538714843421SMatthew Ahrens 		VFS_RELE(zfsvfs->z_vfs);
538814843421SMatthew Ahrens 	} else {
5389503ad85cSMatthew Ahrens 		/* XXX kind of reading contents without owning */
5390eb633035STom Caputi 		error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
53913b2aab18SMatthew Ahrens 		if (error != 0)
539214843421SMatthew Ahrens 			return (error);
539314843421SMatthew Ahrens 
539414843421SMatthew Ahrens 		error = dmu_objset_userspace_upgrade(os);
5395eb633035STom Caputi 		dmu_objset_rele_flags(os, B_TRUE, FTAG);
539614843421SMatthew Ahrens 	}
539714843421SMatthew Ahrens 
539814843421SMatthew Ahrens 	return (error);
539914843421SMatthew Ahrens }
540014843421SMatthew Ahrens 
5401*f67950b2SNasf-Fan /*
5402*f67950b2SNasf-Fan  * inputs:
5403*f67950b2SNasf-Fan  * zc_name		name of filesystem
5404*f67950b2SNasf-Fan  *
5405*f67950b2SNasf-Fan  * outputs:
5406*f67950b2SNasf-Fan  * none
5407*f67950b2SNasf-Fan  */
5408*f67950b2SNasf-Fan static int
5409*f67950b2SNasf-Fan zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5410*f67950b2SNasf-Fan {
5411*f67950b2SNasf-Fan 	objset_t *os;
5412*f67950b2SNasf-Fan 	int error;
5413*f67950b2SNasf-Fan 
5414*f67950b2SNasf-Fan 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5415*f67950b2SNasf-Fan 	if (error != 0)
5416*f67950b2SNasf-Fan 		return (error);
5417*f67950b2SNasf-Fan 
5418*f67950b2SNasf-Fan 	dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
5419*f67950b2SNasf-Fan 	dsl_pool_rele(dmu_objset_pool(os), FTAG);
5420*f67950b2SNasf-Fan 
5421*f67950b2SNasf-Fan 	if (dmu_objset_userobjspace_upgradable(os) ||
5422*f67950b2SNasf-Fan 	    dmu_objset_projectquota_upgradable(os)) {
5423*f67950b2SNasf-Fan 		mutex_enter(&os->os_upgrade_lock);
5424*f67950b2SNasf-Fan 		if (os->os_upgrade_id == 0) {
5425*f67950b2SNasf-Fan 			/* clear potential error code and retry */
5426*f67950b2SNasf-Fan 			os->os_upgrade_status = 0;
5427*f67950b2SNasf-Fan 			mutex_exit(&os->os_upgrade_lock);
5428*f67950b2SNasf-Fan 
5429*f67950b2SNasf-Fan 			dmu_objset_id_quota_upgrade(os);
5430*f67950b2SNasf-Fan 		} else {
5431*f67950b2SNasf-Fan 			mutex_exit(&os->os_upgrade_lock);
5432*f67950b2SNasf-Fan 		}
5433*f67950b2SNasf-Fan 
5434*f67950b2SNasf-Fan 		taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5435*f67950b2SNasf-Fan 		error = os->os_upgrade_status;
5436*f67950b2SNasf-Fan 	}
5437*f67950b2SNasf-Fan 
5438*f67950b2SNasf-Fan 	dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
5439*f67950b2SNasf-Fan 	dsl_dataset_rele(dmu_objset_ds(os), FTAG);
5440*f67950b2SNasf-Fan 
5441*f67950b2SNasf-Fan 	return (error);
5442*f67950b2SNasf-Fan }
5443*f67950b2SNasf-Fan 
5444ecd6cf80Smarks /*
5445ecd6cf80Smarks  * We don't want to have a hard dependency
5446ecd6cf80Smarks  * against some special symbols in sharefs
5447da6c28aaSamw  * nfs, and smbsrv.  Determine them if needed when
5448ecd6cf80Smarks  * the first file system is shared.
5449da6c28aaSamw  * Neither sharefs, nfs or smbsrv are unloadable modules.
5450ecd6cf80Smarks  */
5451da6c28aaSamw int (*znfsexport_fs)(void *arg);
5452ecd6cf80Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5453da6c28aaSamw int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5454da6c28aaSamw 
5455da6c28aaSamw int zfs_nfsshare_inited;
5456da6c28aaSamw int zfs_smbshare_inited;
5457ecd6cf80Smarks 
5458ecd6cf80Smarks ddi_modhandle_t nfs_mod;
5459ecd6cf80Smarks ddi_modhandle_t sharefs_mod;
5460da6c28aaSamw ddi_modhandle_t smbsrv_mod;
5461ecd6cf80Smarks kmutex_t zfs_share_lock;
5462ecd6cf80Smarks 
5463da6c28aaSamw static int
5464da6c28aaSamw zfs_init_sharefs()
5465da6c28aaSamw {
5466da6c28aaSamw 	int error;
5467da6c28aaSamw 
5468da6c28aaSamw 	ASSERT(MUTEX_HELD(&zfs_share_lock));
5469da6c28aaSamw 	/* Both NFS and SMB shares also require sharetab support. */
5470da6c28aaSamw 	if (sharefs_mod == NULL && ((sharefs_mod =
5471da6c28aaSamw 	    ddi_modopen("fs/sharefs",
5472da6c28aaSamw 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
5473be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
5474da6c28aaSamw 	}
5475da6c28aaSamw 	if (zshare_fs == NULL && ((zshare_fs =
5476da6c28aaSamw 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5477da6c28aaSamw 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5478be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSYS));
5479da6c28aaSamw 	}
5480da6c28aaSamw 	return (0);
5481da6c28aaSamw }
5482da6c28aaSamw 
5483ecd6cf80Smarks static int
5484ecd6cf80Smarks zfs_ioc_share(zfs_cmd_t *zc)
5485ecd6cf80Smarks {
5486ecd6cf80Smarks 	int error;
5487ecd6cf80Smarks 	int opcode;
5488ecd6cf80Smarks 
5489da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
5490da6c28aaSamw 	case ZFS_SHARE_NFS:
5491da6c28aaSamw 	case ZFS_UNSHARE_NFS:
5492da6c28aaSamw 		if (zfs_nfsshare_inited == 0) {
5493da6c28aaSamw 			mutex_enter(&zfs_share_lock);
5494da6c28aaSamw 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5495da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
5496da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5497be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5498da6c28aaSamw 			}
5499da6c28aaSamw 			if (znfsexport_fs == NULL &&
5500da6c28aaSamw 			    ((znfsexport_fs = (int (*)(void *))
5501da6c28aaSamw 			    ddi_modsym(nfs_mod,
5502da6c28aaSamw 			    "nfs_export", &error)) == NULL)) {
5503da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5504be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5505da6c28aaSamw 			}
5506da6c28aaSamw 			error = zfs_init_sharefs();
55073b2aab18SMatthew Ahrens 			if (error != 0) {
5508da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5509be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5510da6c28aaSamw 			}
5511da6c28aaSamw 			zfs_nfsshare_inited = 1;
5512ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
5513ecd6cf80Smarks 		}
5514da6c28aaSamw 		break;
5515da6c28aaSamw 	case ZFS_SHARE_SMB:
5516da6c28aaSamw 	case ZFS_UNSHARE_SMB:
5517da6c28aaSamw 		if (zfs_smbshare_inited == 0) {
5518da6c28aaSamw 			mutex_enter(&zfs_share_lock);
5519da6c28aaSamw 			if (smbsrv_mod == NULL && ((smbsrv_mod =
5520da6c28aaSamw 			    ddi_modopen("drv/smbsrv",
5521da6c28aaSamw 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
5522da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5523be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5524da6c28aaSamw 			}
5525da6c28aaSamw 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5526da6c28aaSamw 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5527faa1795aSjb 			    "smb_server_share", &error)) == NULL)) {
5528da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5529be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5530da6c28aaSamw 			}
5531da6c28aaSamw 			error = zfs_init_sharefs();
55323b2aab18SMatthew Ahrens 			if (error != 0) {
5533da6c28aaSamw 				mutex_exit(&zfs_share_lock);
5534be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENOSYS));
5535da6c28aaSamw 			}
5536da6c28aaSamw 			zfs_smbshare_inited = 1;
5537ecd6cf80Smarks 			mutex_exit(&zfs_share_lock);
5538ecd6cf80Smarks 		}
5539da6c28aaSamw 		break;
5540da6c28aaSamw 	default:
5541be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5542da6c28aaSamw 	}
5543ecd6cf80Smarks 
5544da6c28aaSamw 	switch (zc->zc_share.z_sharetype) {
5545da6c28aaSamw 	case ZFS_SHARE_NFS:
5546da6c28aaSamw 	case ZFS_UNSHARE_NFS:
5547da6c28aaSamw 		if (error =
5548da6c28aaSamw 		    znfsexport_fs((void *)
5549da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata))
5550da6c28aaSamw 			return (error);
5551da6c28aaSamw 		break;
5552da6c28aaSamw 	case ZFS_SHARE_SMB:
5553da6c28aaSamw 	case ZFS_UNSHARE_SMB:
5554da6c28aaSamw 		if (error = zsmbexport_fs((void *)
5555da6c28aaSamw 		    (uintptr_t)zc->zc_share.z_exportdata,
5556da6c28aaSamw 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5557743a77edSAlan Wright 		    B_TRUE: B_FALSE)) {
5558da6c28aaSamw 			return (error);
5559ecd6cf80Smarks 		}
5560da6c28aaSamw 		break;
5561ecd6cf80Smarks 	}
5562ecd6cf80Smarks 
5563da6c28aaSamw 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5564da6c28aaSamw 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5565ecd6cf80Smarks 	    SHAREFS_ADD : SHAREFS_REMOVE;
5566ecd6cf80Smarks 
5567da6c28aaSamw 	/*
5568da6c28aaSamw 	 * Add or remove share from sharetab
5569da6c28aaSamw 	 */
5570ecd6cf80Smarks 	error = zshare_fs(opcode,
5571ecd6cf80Smarks 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
5572ecd6cf80Smarks 	    zc->zc_share.z_sharemax);
5573ecd6cf80Smarks 
5574ecd6cf80Smarks 	return (error);
5575ecd6cf80Smarks 
5576ecd6cf80Smarks }
5577ecd6cf80Smarks 
5578743a77edSAlan Wright ace_t full_access[] = {
5579743a77edSAlan Wright 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5580743a77edSAlan Wright };
5581743a77edSAlan Wright 
558299d5e173STim Haley /*
558399d5e173STim Haley  * inputs:
558499d5e173STim Haley  * zc_name		name of containing filesystem
558599d5e173STim Haley  * zc_obj		object # beyond which we want next in-use object #
558699d5e173STim Haley  *
558799d5e173STim Haley  * outputs:
558899d5e173STim Haley  * zc_obj		next in-use object #
558999d5e173STim Haley  */
559099d5e173STim Haley static int
559199d5e173STim Haley zfs_ioc_next_obj(zfs_cmd_t *zc)
559299d5e173STim Haley {
559399d5e173STim Haley 	objset_t *os = NULL;
559499d5e173STim Haley 	int error;
559599d5e173STim Haley 
5596eb633035STom Caputi 	error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
55973b2aab18SMatthew Ahrens 	if (error != 0)
559899d5e173STim Haley 		return (error);
559999d5e173STim Haley 
560099d5e173STim Haley 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5601c1379625SJustin T. Gibbs 	    dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
560299d5e173STim Haley 
560399d5e173STim Haley 	dmu_objset_rele(os, FTAG);
560499d5e173STim Haley 	return (error);
560599d5e173STim Haley }
560699d5e173STim Haley 
560799d5e173STim Haley /*
560899d5e173STim Haley  * inputs:
560999d5e173STim Haley  * zc_name		name of filesystem
561099d5e173STim Haley  * zc_value		prefix name for snapshot
561199d5e173STim Haley  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
561299d5e173STim Haley  *
561399d5e173STim Haley  * outputs:
56144445fffbSMatthew Ahrens  * zc_value		short name of new snapshot
561599d5e173STim Haley  */
561699d5e173STim Haley static int
561799d5e173STim Haley zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
561899d5e173STim Haley {
561999d5e173STim Haley 	char *snap_name;
56203b2aab18SMatthew Ahrens 	char *hold_name;
562199d5e173STim Haley 	int error;
56223b2aab18SMatthew Ahrens 	minor_t minor;
562399d5e173STim Haley 
56243b2aab18SMatthew Ahrens 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
56253b2aab18SMatthew Ahrens 	if (error != 0)
562699d5e173STim Haley 		return (error);
562799d5e173STim Haley 
56283b2aab18SMatthew Ahrens 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
56293b2aab18SMatthew Ahrens 	    (u_longlong_t)ddi_get_lbolt64());
56303b2aab18SMatthew Ahrens 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
56313b2aab18SMatthew Ahrens 
56323b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
56333b2aab18SMatthew Ahrens 	    hold_name);
56343b2aab18SMatthew Ahrens 	if (error == 0)
56353b2aab18SMatthew Ahrens 		(void) strcpy(zc->zc_value, snap_name);
563699d5e173STim Haley 	strfree(snap_name);
56373b2aab18SMatthew Ahrens 	strfree(hold_name);
56383b2aab18SMatthew Ahrens 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
56393b2aab18SMatthew Ahrens 	return (error);
564099d5e173STim Haley }
564199d5e173STim Haley 
564299d5e173STim Haley /*
564399d5e173STim Haley  * inputs:
564499d5e173STim Haley  * zc_name		name of "to" snapshot
564599d5e173STim Haley  * zc_value		name of "from" snapshot
564699d5e173STim Haley  * zc_cookie		file descriptor to write diff data on
564799d5e173STim Haley  *
564899d5e173STim Haley  * outputs:
564999d5e173STim Haley  * dmu_diff_record_t's to the file descriptor
565099d5e173STim Haley  */
565199d5e173STim Haley static int
565299d5e173STim Haley zfs_ioc_diff(zfs_cmd_t *zc)
565399d5e173STim Haley {
565499d5e173STim Haley 	file_t *fp;
565599d5e173STim Haley 	offset_t off;
565699d5e173STim Haley 	int error;
565799d5e173STim Haley 
565899d5e173STim Haley 	fp = getf(zc->zc_cookie);
56593b2aab18SMatthew Ahrens 	if (fp == NULL)
5660be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
566199d5e173STim Haley 
566299d5e173STim Haley 	off = fp->f_offset;
566399d5e173STim Haley 
56643b2aab18SMatthew Ahrens 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
566599d5e173STim Haley 
566699d5e173STim Haley 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
566799d5e173STim Haley 		fp->f_offset = off;
566899d5e173STim Haley 	releasef(zc->zc_cookie);
566999d5e173STim Haley 
567099d5e173STim Haley 	return (error);
567199d5e173STim Haley }
567299d5e173STim Haley 
5673743a77edSAlan Wright /*
5674743a77edSAlan Wright  * Remove all ACL files in shares dir
5675743a77edSAlan Wright  */
5676743a77edSAlan Wright static int
5677743a77edSAlan Wright zfs_smb_acl_purge(znode_t *dzp)
5678743a77edSAlan Wright {
5679743a77edSAlan Wright 	zap_cursor_t	zc;
5680743a77edSAlan Wright 	zap_attribute_t	zap;
5681743a77edSAlan Wright 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5682743a77edSAlan Wright 	int error;
5683743a77edSAlan Wright 
5684743a77edSAlan Wright 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5685743a77edSAlan Wright 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5686743a77edSAlan Wright 	    zap_cursor_advance(&zc)) {
5687743a77edSAlan Wright 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5688743a77edSAlan Wright 		    NULL, 0)) != 0)
5689743a77edSAlan Wright 			break;
5690743a77edSAlan Wright 	}
5691743a77edSAlan Wright 	zap_cursor_fini(&zc);
5692743a77edSAlan Wright 	return (error);
5693743a77edSAlan Wright }
5694743a77edSAlan Wright 
5695743a77edSAlan Wright static int
5696743a77edSAlan Wright zfs_ioc_smb_acl(zfs_cmd_t *zc)
5697743a77edSAlan Wright {
5698743a77edSAlan Wright 	vnode_t *vp;
5699743a77edSAlan Wright 	znode_t *dzp;
5700743a77edSAlan Wright 	vnode_t *resourcevp = NULL;
5701743a77edSAlan Wright 	znode_t *sharedir;
5702743a77edSAlan Wright 	zfsvfs_t *zfsvfs;
5703743a77edSAlan Wright 	nvlist_t *nvlist;
5704743a77edSAlan Wright 	char *src, *target;
5705743a77edSAlan Wright 	vattr_t vattr;
5706743a77edSAlan Wright 	vsecattr_t vsec;
5707743a77edSAlan Wright 	int error = 0;
5708743a77edSAlan Wright 
5709743a77edSAlan Wright 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5710743a77edSAlan Wright 	    NO_FOLLOW, NULL, &vp)) != 0)
5711743a77edSAlan Wright 		return (error);
5712743a77edSAlan Wright 
5713743a77edSAlan Wright 	/* Now make sure mntpnt and dataset are ZFS */
5714743a77edSAlan Wright 
5715743a77edSAlan Wright 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5716743a77edSAlan Wright 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5717743a77edSAlan Wright 	    zc->zc_name) != 0)) {
5718743a77edSAlan Wright 		VN_RELE(vp);
5719be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5720743a77edSAlan Wright 	}
5721743a77edSAlan Wright 
5722743a77edSAlan Wright 	dzp = VTOZ(vp);
5723743a77edSAlan Wright 	zfsvfs = dzp->z_zfsvfs;
5724743a77edSAlan Wright 	ZFS_ENTER(zfsvfs);
5725743a77edSAlan Wright 
57269e1320c0SMark Shellenbaum 	/*
57279e1320c0SMark Shellenbaum 	 * Create share dir if its missing.
57289e1320c0SMark Shellenbaum 	 */
57299e1320c0SMark Shellenbaum 	mutex_enter(&zfsvfs->z_lock);
57309e1320c0SMark Shellenbaum 	if (zfsvfs->z_shares_dir == 0) {
57319e1320c0SMark Shellenbaum 		dmu_tx_t *tx;
57329e1320c0SMark Shellenbaum 
57339e1320c0SMark Shellenbaum 		tx = dmu_tx_create(zfsvfs->z_os);
57349e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
57359e1320c0SMark Shellenbaum 		    ZFS_SHARES_DIR);
57369e1320c0SMark Shellenbaum 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
57379e1320c0SMark Shellenbaum 		error = dmu_tx_assign(tx, TXG_WAIT);
57383b2aab18SMatthew Ahrens 		if (error != 0) {
57399e1320c0SMark Shellenbaum 			dmu_tx_abort(tx);
57409e1320c0SMark Shellenbaum 		} else {
57419e1320c0SMark Shellenbaum 			error = zfs_create_share_dir(zfsvfs, tx);
57429e1320c0SMark Shellenbaum 			dmu_tx_commit(tx);
57439e1320c0SMark Shellenbaum 		}
57443b2aab18SMatthew Ahrens 		if (error != 0) {
57459e1320c0SMark Shellenbaum 			mutex_exit(&zfsvfs->z_lock);
57469e1320c0SMark Shellenbaum 			VN_RELE(vp);
57479e1320c0SMark Shellenbaum 			ZFS_EXIT(zfsvfs);
57489e1320c0SMark Shellenbaum 			return (error);
57499e1320c0SMark Shellenbaum 		}
57509e1320c0SMark Shellenbaum 	}
57519e1320c0SMark Shellenbaum 	mutex_exit(&zfsvfs->z_lock);
57529e1320c0SMark Shellenbaum 
57539e1320c0SMark Shellenbaum 	ASSERT(zfsvfs->z_shares_dir);
5754743a77edSAlan Wright 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
57559e1320c0SMark Shellenbaum 		VN_RELE(vp);
5756743a77edSAlan Wright 		ZFS_EXIT(zfsvfs);
5757743a77edSAlan Wright 		return (error);
5758743a77edSAlan Wright 	}
5759743a77edSAlan Wright 
5760743a77edSAlan Wright 	switch (zc->zc_cookie) {
5761743a77edSAlan Wright 	case ZFS_SMB_ACL_ADD:
5762743a77edSAlan Wright 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5763743a77edSAlan Wright 		vattr.va_type = VREG;
5764743a77edSAlan Wright 		vattr.va_mode = S_IFREG|0777;
5765743a77edSAlan Wright 		vattr.va_uid = 0;
5766743a77edSAlan Wright 		vattr.va_gid = 0;
5767743a77edSAlan Wright 
5768743a77edSAlan Wright 		vsec.vsa_mask = VSA_ACE;
5769743a77edSAlan Wright 		vsec.vsa_aclentp = &full_access;
5770743a77edSAlan Wright 		vsec.vsa_aclentsz = sizeof (full_access);
5771743a77edSAlan Wright 		vsec.vsa_aclcnt = 1;
5772743a77edSAlan Wright 
5773743a77edSAlan Wright 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5774743a77edSAlan Wright 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5775743a77edSAlan Wright 		if (resourcevp)
5776743a77edSAlan Wright 			VN_RELE(resourcevp);
5777743a77edSAlan Wright 		break;
5778743a77edSAlan Wright 
5779743a77edSAlan Wright 	case ZFS_SMB_ACL_REMOVE:
5780743a77edSAlan Wright 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5781743a77edSAlan Wright 		    NULL, 0);
5782743a77edSAlan Wright 		break;
5783743a77edSAlan Wright 
5784743a77edSAlan Wright 	case ZFS_SMB_ACL_RENAME:
5785743a77edSAlan Wright 		if ((error = get_nvlist(zc->zc_nvlist_src,
5786478ed9adSEric Taylor 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5787743a77edSAlan Wright 			VN_RELE(vp);
57888f5190a5SDan McDonald 			VN_RELE(ZTOV(sharedir));
5789743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
5790743a77edSAlan Wright 			return (error);
5791743a77edSAlan Wright 		}
5792743a77edSAlan Wright 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5793743a77edSAlan Wright 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5794743a77edSAlan Wright 		    &target)) {
5795743a77edSAlan Wright 			VN_RELE(vp);
579689459e17SMark Shellenbaum 			VN_RELE(ZTOV(sharedir));
5797743a77edSAlan Wright 			ZFS_EXIT(zfsvfs);
57981195e687SMark J Musante 			nvlist_free(nvlist);
5799743a77edSAlan Wright 			return (error);
5800743a77edSAlan Wright 		}
5801743a77edSAlan Wright 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5802743a77edSAlan Wright 		    kcred, NULL, 0);
5803743a77edSAlan Wright 		nvlist_free(nvlist);
5804743a77edSAlan Wright 		break;
5805743a77edSAlan Wright 
5806743a77edSAlan Wright 	case ZFS_SMB_ACL_PURGE:
5807743a77edSAlan Wright 		error = zfs_smb_acl_purge(sharedir);
5808743a77edSAlan Wright 		break;
5809743a77edSAlan Wright 
5810743a77edSAlan Wright 	default:
5811be6fd75aSMatthew Ahrens 		error = SET_ERROR(EINVAL);
5812743a77edSAlan Wright 		break;
5813743a77edSAlan Wright 	}
5814743a77edSAlan Wright 
5815743a77edSAlan Wright 	VN_RELE(vp);
5816743a77edSAlan Wright 	VN_RELE(ZTOV(sharedir));
5817743a77edSAlan Wright 
5818743a77edSAlan Wright 	ZFS_EXIT(zfsvfs);
5819743a77edSAlan Wright 
5820743a77edSAlan Wright 	return (error);
5821743a77edSAlan Wright }
5822743a77edSAlan Wright 
5823842727c2SChris Kirby /*
58243b2aab18SMatthew Ahrens  * innvl: {
58253b2aab18SMatthew Ahrens  *     "holds" -> { snapname -> holdname (string), ... }
58263b2aab18SMatthew Ahrens  *     (optional) "cleanup_fd" -> fd (int32)
58273b2aab18SMatthew Ahrens  * }
5828842727c2SChris Kirby  *
58293b2aab18SMatthew Ahrens  * outnvl: {
58303b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
58313b2aab18SMatthew Ahrens  *     ...
58323b2aab18SMatthew Ahrens  * }
5833842727c2SChris Kirby  */
58343b2aab18SMatthew Ahrens /* ARGSUSED */
5835842727c2SChris Kirby static int
58363b2aab18SMatthew Ahrens zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5837842727c2SChris Kirby {
5838752fd8daSJosef 'Jeff' Sipek 	nvpair_t *pair;
58393b2aab18SMatthew Ahrens 	nvlist_t *holds;
58403b2aab18SMatthew Ahrens 	int cleanup_fd = -1;
5841a7f53a56SChris Kirby 	int error;
5842a7f53a56SChris Kirby 	minor_t minor = 0;
5843842727c2SChris Kirby 
58443b2aab18SMatthew Ahrens 	error = nvlist_lookup_nvlist(args, "holds", &holds);
58453b2aab18SMatthew Ahrens 	if (error != 0)
5846be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5847a7f53a56SChris Kirby 
5848752fd8daSJosef 'Jeff' Sipek 	/* make sure the user didn't pass us any invalid (empty) tags */
5849752fd8daSJosef 'Jeff' Sipek 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5850752fd8daSJosef 'Jeff' Sipek 	    pair = nvlist_next_nvpair(holds, pair)) {
5851752fd8daSJosef 'Jeff' Sipek 		char *htag;
5852752fd8daSJosef 'Jeff' Sipek 
5853752fd8daSJosef 'Jeff' Sipek 		error = nvpair_value_string(pair, &htag);
5854752fd8daSJosef 'Jeff' Sipek 		if (error != 0)
5855752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(error));
5856752fd8daSJosef 'Jeff' Sipek 
5857752fd8daSJosef 'Jeff' Sipek 		if (strlen(htag) == 0)
5858752fd8daSJosef 'Jeff' Sipek 			return (SET_ERROR(EINVAL));
5859752fd8daSJosef 'Jeff' Sipek 	}
5860752fd8daSJosef 'Jeff' Sipek 
58613b2aab18SMatthew Ahrens 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
58623b2aab18SMatthew Ahrens 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
58633b2aab18SMatthew Ahrens 		if (error != 0)
5864a7f53a56SChris Kirby 			return (error);
5865a7f53a56SChris Kirby 	}
5866a7f53a56SChris Kirby 
58673b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, minor, errlist);
58683b2aab18SMatthew Ahrens 	if (minor != 0)
58693b2aab18SMatthew Ahrens 		zfs_onexit_fd_rele(cleanup_fd);
5870a7f53a56SChris Kirby 	return (error);
5871842727c2SChris Kirby }
5872842727c2SChris Kirby 
5873842727c2SChris Kirby /*
58743b2aab18SMatthew Ahrens  * innvl is not used.
5875842727c2SChris Kirby  *
58763b2aab18SMatthew Ahrens  * outnvl: {
58773b2aab18SMatthew Ahrens  *    holdname -> time added (uint64 seconds since epoch)
58783b2aab18SMatthew Ahrens  *    ...
58793b2aab18SMatthew Ahrens  * }
5880842727c2SChris Kirby  */
58813b2aab18SMatthew Ahrens /* ARGSUSED */
5882842727c2SChris Kirby static int
58833b2aab18SMatthew Ahrens zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5884842727c2SChris Kirby {
58859c2acf00SAlek Pinchuk 	ASSERT3P(args, ==, NULL);
58863b2aab18SMatthew Ahrens 	return (dsl_dataset_get_holds(snapname, outnvl));
5887842727c2SChris Kirby }
5888842727c2SChris Kirby 
5889842727c2SChris Kirby /*
58903b2aab18SMatthew Ahrens  * innvl: {
58913b2aab18SMatthew Ahrens  *     snapname -> { holdname, ... }
58923b2aab18SMatthew Ahrens  *     ...
58933b2aab18SMatthew Ahrens  * }
5894842727c2SChris Kirby  *
58953b2aab18SMatthew Ahrens  * outnvl: {
58963b2aab18SMatthew Ahrens  *     snapname -> error value (int32)
58973b2aab18SMatthew Ahrens  *     ...
58983b2aab18SMatthew Ahrens  * }
5899842727c2SChris Kirby  */
59003b2aab18SMatthew Ahrens /* ARGSUSED */
5901842727c2SChris Kirby static int
59023b2aab18SMatthew Ahrens zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5903842727c2SChris Kirby {
59043b2aab18SMatthew Ahrens 	return (dsl_dataset_user_release(holds, errlist));
5905842727c2SChris Kirby }
5906842727c2SChris Kirby 
590719b94df9SMatthew Ahrens /*
590819b94df9SMatthew Ahrens  * inputs:
590919b94df9SMatthew Ahrens  * zc_name		name of new filesystem or snapshot
591019b94df9SMatthew Ahrens  * zc_value		full name of old snapshot
591119b94df9SMatthew Ahrens  *
591219b94df9SMatthew Ahrens  * outputs:
591319b94df9SMatthew Ahrens  * zc_cookie		space in bytes
591419b94df9SMatthew Ahrens  * zc_objset_type	compressed space in bytes
591519b94df9SMatthew Ahrens  * zc_perm_action	uncompressed space in bytes
591619b94df9SMatthew Ahrens  */
591719b94df9SMatthew Ahrens static int
591819b94df9SMatthew Ahrens zfs_ioc_space_written(zfs_cmd_t *zc)
591919b94df9SMatthew Ahrens {
592019b94df9SMatthew Ahrens 	int error;
59213b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
592219b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
592319b94df9SMatthew Ahrens 
59243b2aab18SMatthew Ahrens 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
592519b94df9SMatthew Ahrens 	if (error != 0)
592619b94df9SMatthew Ahrens 		return (error);
59273b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
59283b2aab18SMatthew Ahrens 	if (error != 0) {
59293b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
59303b2aab18SMatthew Ahrens 		return (error);
59313b2aab18SMatthew Ahrens 	}
59323b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
593319b94df9SMatthew Ahrens 	if (error != 0) {
593419b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
59353b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
593619b94df9SMatthew Ahrens 		return (error);
593719b94df9SMatthew Ahrens 	}
593819b94df9SMatthew Ahrens 
593919b94df9SMatthew Ahrens 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
594019b94df9SMatthew Ahrens 	    &zc->zc_objset_type, &zc->zc_perm_action);
594119b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
594219b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
59433b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
594419b94df9SMatthew Ahrens 	return (error);
594519b94df9SMatthew Ahrens }
59463b2aab18SMatthew Ahrens 
594719b94df9SMatthew Ahrens /*
59484445fffbSMatthew Ahrens  * innvl: {
59494445fffbSMatthew Ahrens  *     "firstsnap" -> snapshot name
59504445fffbSMatthew Ahrens  * }
595119b94df9SMatthew Ahrens  *
59524445fffbSMatthew Ahrens  * outnvl: {
59534445fffbSMatthew Ahrens  *     "used" -> space in bytes
59544445fffbSMatthew Ahrens  *     "compressed" -> compressed space in bytes
59554445fffbSMatthew Ahrens  *     "uncompressed" -> uncompressed space in bytes
59564445fffbSMatthew Ahrens  * }
595719b94df9SMatthew Ahrens  */
595819b94df9SMatthew Ahrens static int
59594445fffbSMatthew Ahrens zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
596019b94df9SMatthew Ahrens {
596119b94df9SMatthew Ahrens 	int error;
59623b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
596319b94df9SMatthew Ahrens 	dsl_dataset_t *new, *old;
59644445fffbSMatthew Ahrens 	char *firstsnap;
59654445fffbSMatthew Ahrens 	uint64_t used, comp, uncomp;
596619b94df9SMatthew Ahrens 
59674445fffbSMatthew Ahrens 	if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5968be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
59694445fffbSMatthew Ahrens 
59703b2aab18SMatthew Ahrens 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
597119b94df9SMatthew Ahrens 	if (error != 0)
597219b94df9SMatthew Ahrens 		return (error);
59733b2aab18SMatthew Ahrens 
59743b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
597524218bebSAndriy Gapon 	if (error == 0 && !new->ds_is_snapshot) {
597624218bebSAndriy Gapon 		dsl_dataset_rele(new, FTAG);
597724218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
597824218bebSAndriy Gapon 	}
59793b2aab18SMatthew Ahrens 	if (error != 0) {
59803b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
59813b2aab18SMatthew Ahrens 		return (error);
59823b2aab18SMatthew Ahrens 	}
59833b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
598424218bebSAndriy Gapon 	if (error == 0 && !old->ds_is_snapshot) {
598524218bebSAndriy Gapon 		dsl_dataset_rele(old, FTAG);
598624218bebSAndriy Gapon 		error = SET_ERROR(EINVAL);
598724218bebSAndriy Gapon 	}
598819b94df9SMatthew Ahrens 	if (error != 0) {
598919b94df9SMatthew Ahrens 		dsl_dataset_rele(new, FTAG);
59903b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
599119b94df9SMatthew Ahrens 		return (error);
599219b94df9SMatthew Ahrens 	}
599319b94df9SMatthew Ahrens 
59944445fffbSMatthew Ahrens 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
599519b94df9SMatthew Ahrens 	dsl_dataset_rele(old, FTAG);
599619b94df9SMatthew Ahrens 	dsl_dataset_rele(new, FTAG);
59973b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
59984445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "used", used);
59994445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "compressed", comp);
60004445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
600119b94df9SMatthew Ahrens 	return (error);
600219b94df9SMatthew Ahrens }
600319b94df9SMatthew Ahrens 
6004ecd6cf80Smarks /*
60054445fffbSMatthew Ahrens  * innvl: {
60064445fffbSMatthew Ahrens  *     "fd" -> file descriptor to write stream to (int32)
60074445fffbSMatthew Ahrens  *     (optional) "fromsnap" -> full snap name to send an incremental from
6008b5152584SMatthew Ahrens  *     (optional) "largeblockok" -> (value ignored)
6009b5152584SMatthew Ahrens  *         indicates that blocks > 128KB are permitted
60105d7b4d43SMatthew Ahrens  *     (optional) "embedok" -> (value ignored)
60115d7b4d43SMatthew Ahrens  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
60125602294fSDan Kimmel  *     (optional) "compressok" -> (value ignored)
60135602294fSDan Kimmel  *         presence indicates compressed DRR_WRITE records are permitted
6014eb633035STom Caputi  *     (optional) "rawok" -> (value ignored)
6015eb633035STom Caputi  *         presence indicates raw encrypted records should be used.
60169c3fd121SMatthew Ahrens  *     (optional) "resume_object" and "resume_offset" -> (uint64)
60179c3fd121SMatthew Ahrens  *         if present, resume send stream from specified object and offset.
60184445fffbSMatthew Ahrens  * }
60194445fffbSMatthew Ahrens  *
60204445fffbSMatthew Ahrens  * outnvl is unused
6021ecd6cf80Smarks  */
60224445fffbSMatthew Ahrens /* ARGSUSED */
60234445fffbSMatthew Ahrens static int
60244445fffbSMatthew Ahrens zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
60254445fffbSMatthew Ahrens {
60264445fffbSMatthew Ahrens 	int error;
60274445fffbSMatthew Ahrens 	offset_t off;
60283b2aab18SMatthew Ahrens 	char *fromname = NULL;
60294445fffbSMatthew Ahrens 	int fd;
6030b5152584SMatthew Ahrens 	boolean_t largeblockok;
60315d7b4d43SMatthew Ahrens 	boolean_t embedok;
60325602294fSDan Kimmel 	boolean_t compressok;
6033eb633035STom Caputi 	boolean_t rawok;
60349c3fd121SMatthew Ahrens 	uint64_t resumeobj = 0;
60359c3fd121SMatthew Ahrens 	uint64_t resumeoff = 0;
60364445fffbSMatthew Ahrens 
60374445fffbSMatthew Ahrens 	error = nvlist_lookup_int32(innvl, "fd", &fd);
60384445fffbSMatthew Ahrens 	if (error != 0)
6039be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
60404445fffbSMatthew Ahrens 
60413b2aab18SMatthew Ahrens 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
60424445fffbSMatthew Ahrens 
6043b5152584SMatthew Ahrens 	largeblockok = nvlist_exists(innvl, "largeblockok");
60445d7b4d43SMatthew Ahrens 	embedok = nvlist_exists(innvl, "embedok");
60455602294fSDan Kimmel 	compressok = nvlist_exists(innvl, "compressok");
6046eb633035STom Caputi 	rawok = nvlist_exists(innvl, "rawok");
60475d7b4d43SMatthew Ahrens 
60489c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
60499c3fd121SMatthew Ahrens 	(void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
60509c3fd121SMatthew Ahrens 
60514445fffbSMatthew Ahrens 	file_t *fp = getf(fd);
60523b2aab18SMatthew Ahrens 	if (fp == NULL)
6053be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBADF));
60544445fffbSMatthew Ahrens 
60554445fffbSMatthew Ahrens 	off = fp->f_offset;
60565602294fSDan Kimmel 	error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6057eb633035STom Caputi 	    rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
60584445fffbSMatthew Ahrens 
60594445fffbSMatthew Ahrens 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
60604445fffbSMatthew Ahrens 		fp->f_offset = off;
60614445fffbSMatthew Ahrens 	releasef(fd);
60624445fffbSMatthew Ahrens 	return (error);
60634445fffbSMatthew Ahrens }
60644445fffbSMatthew Ahrens 
60654445fffbSMatthew Ahrens /*
60664445fffbSMatthew Ahrens  * Determine approximately how large a zfs send stream will be -- the number
60674445fffbSMatthew Ahrens  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
60684445fffbSMatthew Ahrens  *
60694445fffbSMatthew Ahrens  * innvl: {
6070643da460SMax Grossman  *     (optional) "from" -> full snap or bookmark name to send an incremental
6071643da460SMax Grossman  *                          from
60725602294fSDan Kimmel  *     (optional) "largeblockok" -> (value ignored)
60735602294fSDan Kimmel  *         indicates that blocks > 128KB are permitted
60745602294fSDan Kimmel  *     (optional) "embedok" -> (value ignored)
60755602294fSDan Kimmel  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
60765602294fSDan Kimmel  *     (optional) "compressok" -> (value ignored)
60775602294fSDan Kimmel  *         presence indicates compressed DRR_WRITE records are permitted
60784445fffbSMatthew Ahrens  * }
60794445fffbSMatthew Ahrens  *
60804445fffbSMatthew Ahrens  * outnvl: {
60814445fffbSMatthew Ahrens  *     "space" -> bytes of space (uint64)
60824445fffbSMatthew Ahrens  * }
60834445fffbSMatthew Ahrens  */
60844445fffbSMatthew Ahrens static int
60854445fffbSMatthew Ahrens zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
60864445fffbSMatthew Ahrens {
60873b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
60883b2aab18SMatthew Ahrens 	dsl_dataset_t *tosnap;
60894445fffbSMatthew Ahrens 	int error;
60904445fffbSMatthew Ahrens 	char *fromname;
60915602294fSDan Kimmel 	boolean_t compressok;
6092eb633035STom Caputi 	boolean_t rawok;
60934445fffbSMatthew Ahrens 	uint64_t space;
60944445fffbSMatthew Ahrens 
60953b2aab18SMatthew Ahrens 	error = dsl_pool_hold(snapname, FTAG, &dp);
60963b2aab18SMatthew Ahrens 	if (error != 0)
60973b2aab18SMatthew Ahrens 		return (error);
60983b2aab18SMatthew Ahrens 
60993b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
61003b2aab18SMatthew Ahrens 	if (error != 0) {
61013b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
61024445fffbSMatthew Ahrens 		return (error);
61033b2aab18SMatthew Ahrens 	}
61044445fffbSMatthew Ahrens 
61055602294fSDan Kimmel 	compressok = nvlist_exists(innvl, "compressok");
6106eb633035STom Caputi 	rawok = nvlist_exists(innvl, "rawok");
61075602294fSDan Kimmel 
6108643da460SMax Grossman 	error = nvlist_lookup_string(innvl, "from", &fromname);
61094445fffbSMatthew Ahrens 	if (error == 0) {
6110643da460SMax Grossman 		if (strchr(fromname, '@') != NULL) {
6111643da460SMax Grossman 			/*
6112643da460SMax Grossman 			 * If from is a snapshot, hold it and use the more
6113643da460SMax Grossman 			 * efficient dmu_send_estimate to estimate send space
6114643da460SMax Grossman 			 * size using deadlists.
6115643da460SMax Grossman 			 */
6116643da460SMax Grossman 			dsl_dataset_t *fromsnap;
6117643da460SMax Grossman 			error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6118643da460SMax Grossman 			if (error != 0)
6119643da460SMax Grossman 				goto out;
6120eb633035STom Caputi 			error = dmu_send_estimate(tosnap, fromsnap,
6121eb633035STom Caputi 			    compressok || rawok, &space);
6122643da460SMax Grossman 			dsl_dataset_rele(fromsnap, FTAG);
6123643da460SMax Grossman 		} else if (strchr(fromname, '#') != NULL) {
6124643da460SMax Grossman 			/*
6125643da460SMax Grossman 			 * If from is a bookmark, fetch the creation TXG of the
6126643da460SMax Grossman 			 * snapshot it was created from and use that to find
6127643da460SMax Grossman 			 * blocks that were born after it.
6128643da460SMax Grossman 			 */
6129643da460SMax Grossman 			zfs_bookmark_phys_t frombm;
6130643da460SMax Grossman 
6131643da460SMax Grossman 			error = dsl_bookmark_lookup(dp, fromname, tosnap,
6132643da460SMax Grossman 			    &frombm);
6133643da460SMax Grossman 			if (error != 0)
6134643da460SMax Grossman 				goto out;
6135643da460SMax Grossman 			error = dmu_send_estimate_from_txg(tosnap,
6136eb633035STom Caputi 			    frombm.zbm_creation_txg, compressok || rawok,
6137eb633035STom Caputi 			    &space);
6138643da460SMax Grossman 		} else {
6139643da460SMax Grossman 			/*
6140643da460SMax Grossman 			 * from is not properly formatted as a snapshot or
6141643da460SMax Grossman 			 * bookmark
6142643da460SMax Grossman 			 */
6143643da460SMax Grossman 			error = SET_ERROR(EINVAL);
6144643da460SMax Grossman 			goto out;
61454445fffbSMatthew Ahrens 		}
6146643da460SMax Grossman 	} else {
6147b852c2f5SToomas Soome 		/*
6148b852c2f5SToomas Soome 		 * If estimating the size of a full send, use dmu_send_estimate.
6149b852c2f5SToomas Soome 		 */
6150eb633035STom Caputi 		error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6151eb633035STom Caputi 		    &space);
61524445fffbSMatthew Ahrens 	}
61534445fffbSMatthew Ahrens 
61544445fffbSMatthew Ahrens 	fnvlist_add_uint64(outnvl, "space", space);
61554445fffbSMatthew Ahrens 
6156643da460SMax Grossman out:
61573b2aab18SMatthew Ahrens 	dsl_dataset_rele(tosnap, FTAG);
61583b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
61594445fffbSMatthew Ahrens 	return (error);
61604445fffbSMatthew Ahrens }
61614445fffbSMatthew Ahrens 
61629c2acf00SAlek Pinchuk /*
61639c2acf00SAlek Pinchuk  * Sync the currently open TXG to disk for the specified pool.
61649c2acf00SAlek Pinchuk  * This is somewhat similar to 'zfs_sync()'.
61659c2acf00SAlek Pinchuk  * For cases that do not result in error this ioctl will wait for
61669c2acf00SAlek Pinchuk  * the currently open TXG to commit before returning back to the caller.
61679c2acf00SAlek Pinchuk  *
61689c2acf00SAlek Pinchuk  * innvl: {
61699c2acf00SAlek Pinchuk  *  "force" -> when true, force uberblock update even if there is no dirty data.
61709c2acf00SAlek Pinchuk  *             In addition this will cause the vdev configuration to be written
61719c2acf00SAlek Pinchuk  *             out including updating the zpool cache file. (boolean_t)
61729c2acf00SAlek Pinchuk  * }
61739c2acf00SAlek Pinchuk  *
61749c2acf00SAlek Pinchuk  * onvl is unused
61759c2acf00SAlek Pinchuk  */
61769c2acf00SAlek Pinchuk /* ARGSUSED */
61779c2acf00SAlek Pinchuk static int
61789c2acf00SAlek Pinchuk zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
61799c2acf00SAlek Pinchuk {
61809c2acf00SAlek Pinchuk 	int err;
61819c2acf00SAlek Pinchuk 	boolean_t force;
61829c2acf00SAlek Pinchuk 	spa_t *spa;
61839c2acf00SAlek Pinchuk 
61849c2acf00SAlek Pinchuk 	if ((err = spa_open(pool, &spa, FTAG)) != 0)
61859c2acf00SAlek Pinchuk 		return (err);
61869c2acf00SAlek Pinchuk 
61879c2acf00SAlek Pinchuk 	force = fnvlist_lookup_boolean_value(innvl, "force");
61889c2acf00SAlek Pinchuk 	if (force) {
61899c2acf00SAlek Pinchuk 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
61909c2acf00SAlek Pinchuk 		vdev_config_dirty(spa->spa_root_vdev);
61919c2acf00SAlek Pinchuk 		spa_config_exit(spa, SCL_CONFIG, FTAG);
61929c2acf00SAlek Pinchuk 	}
61939c2acf00SAlek Pinchuk 	txg_wait_synced(spa_get_dsl(spa), 0);
61949c2acf00SAlek Pinchuk 
61959c2acf00SAlek Pinchuk 	spa_close(spa, FTAG);
61969c2acf00SAlek Pinchuk 
61979c2acf00SAlek Pinchuk 	return (err);
61989c2acf00SAlek Pinchuk }
61999c2acf00SAlek Pinchuk 
6200eb633035STom Caputi /*
6201eb633035STom Caputi  * Load a user's wrapping key into the kernel.
6202eb633035STom Caputi  * innvl: {
6203eb633035STom Caputi  *     "hidden_args" -> { "wkeydata" -> value }
6204eb633035STom Caputi  *         raw uint8_t array of encryption wrapping key data (32 bytes)
6205eb633035STom Caputi  *     (optional) "noop" -> (value ignored)
6206eb633035STom Caputi  *         presence indicated key should only be verified, not loaded
6207eb633035STom Caputi  * }
6208eb633035STom Caputi  */
6209eb633035STom Caputi /* ARGSUSED */
6210eb633035STom Caputi static int
6211eb633035STom Caputi zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6212eb633035STom Caputi {
6213eb633035STom Caputi 	int ret = 0;
6214eb633035STom Caputi 	dsl_crypto_params_t *dcp = NULL;
6215eb633035STom Caputi 	nvlist_t *hidden_args;
6216eb633035STom Caputi 	boolean_t noop = nvlist_exists(innvl, "noop");
6217eb633035STom Caputi 
6218eb633035STom Caputi 	if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6219eb633035STom Caputi 		ret = SET_ERROR(EINVAL);
6220eb633035STom Caputi 		goto error;
6221eb633035STom Caputi 	}
6222eb633035STom Caputi 
6223eb633035STom Caputi 	ret = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6224eb633035STom Caputi 	if (ret != 0) {
6225eb633035STom Caputi 		ret = SET_ERROR(EINVAL);
6226eb633035STom Caputi 		goto error;
6227eb633035STom Caputi 	}
6228eb633035STom Caputi 
6229eb633035STom Caputi 	ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6230eb633035STom Caputi 	    hidden_args, &dcp);
6231eb633035STom Caputi 	if (ret != 0)
6232eb633035STom Caputi 		goto error;
6233eb633035STom Caputi 
6234eb633035STom Caputi 	ret = spa_keystore_load_wkey(dsname, dcp, noop);
6235eb633035STom Caputi 	if (ret != 0)
6236eb633035STom Caputi 		goto error;
6237eb633035STom Caputi 
6238eb633035STom Caputi 	dsl_crypto_params_free(dcp, noop);
6239eb633035STom Caputi 
6240eb633035STom Caputi 	return (0);
6241eb633035STom Caputi 
6242eb633035STom Caputi error:
6243eb633035STom Caputi 	dsl_crypto_params_free(dcp, B_TRUE);
6244eb633035STom Caputi 	return (ret);
6245eb633035STom Caputi }
6246eb633035STom Caputi 
6247eb633035STom Caputi /*
6248eb633035STom Caputi  * Unload a user's wrapping key from the kernel.
6249eb633035STom Caputi  * Both innvl and outnvl are unused.
6250eb633035STom Caputi  */
6251eb633035STom Caputi /* ARGSUSED */
6252eb633035STom Caputi static int
6253eb633035STom Caputi zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6254eb633035STom Caputi {
6255eb633035STom Caputi 	int ret = 0;
6256eb633035STom Caputi 
6257eb633035STom Caputi 	if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6258eb633035STom Caputi 		ret = (SET_ERROR(EINVAL));
6259eb633035STom Caputi 		goto out;
6260eb633035STom Caputi 	}
6261eb633035STom Caputi 
6262eb633035STom Caputi 	ret = spa_keystore_unload_wkey(dsname);
6263eb633035STom Caputi 	if (ret != 0)
6264eb633035STom Caputi 		goto out;
6265eb633035STom Caputi 
6266eb633035STom Caputi out:
6267eb633035STom Caputi 	return (ret);
6268eb633035STom Caputi }
6269eb633035STom Caputi 
6270eb633035STom Caputi /*
6271eb633035STom Caputi  * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6272eb633035STom Caputi  * keylocation, pbkdf2salt, and  pbkdf2iters properties can also be specified
6273eb633035STom Caputi  * here to change how the key is derived in userspace.
6274eb633035STom Caputi  *
6275eb633035STom Caputi  * innvl: {
6276eb633035STom Caputi  *    "hidden_args" (optional) -> { "wkeydata" -> value }
6277eb633035STom Caputi  *         raw uint8_t array of new encryption wrapping key data (32 bytes)
6278eb633035STom Caputi  *    "props" (optional) -> { prop -> value }
6279eb633035STom Caputi  * }
6280eb633035STom Caputi  *
6281eb633035STom Caputi  * outnvl is unused
6282eb633035STom Caputi  */
6283eb633035STom Caputi /* ARGSUSED */
6284eb633035STom Caputi static int
6285eb633035STom Caputi zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6286eb633035STom Caputi {
6287eb633035STom Caputi 	int ret;
6288eb633035STom Caputi 	uint64_t cmd = DCP_CMD_NONE;
6289eb633035STom Caputi 	dsl_crypto_params_t *dcp = NULL;
6290eb633035STom Caputi 	nvlist_t *args = NULL, *hidden_args = NULL;
6291eb633035STom Caputi 
6292eb633035STom Caputi 	if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6293eb633035STom Caputi 		ret = (SET_ERROR(EINVAL));
6294eb633035STom Caputi 		goto error;
6295eb633035STom Caputi 	}
6296eb633035STom Caputi 
6297eb633035STom Caputi 	(void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6298eb633035STom Caputi 	(void) nvlist_lookup_nvlist(innvl, "props", &args);
6299eb633035STom Caputi 	(void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6300eb633035STom Caputi 
6301eb633035STom Caputi 	ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6302eb633035STom Caputi 	if (ret != 0)
6303eb633035STom Caputi 		goto error;
6304eb633035STom Caputi 
6305eb633035STom Caputi 	ret = spa_keystore_change_key(dsname, dcp);
6306eb633035STom Caputi 	if (ret != 0)
6307eb633035STom Caputi 		goto error;
6308eb633035STom Caputi 
6309eb633035STom Caputi 	dsl_crypto_params_free(dcp, B_FALSE);
6310eb633035STom Caputi 
6311eb633035STom Caputi 	return (0);
6312eb633035STom Caputi 
6313eb633035STom Caputi error:
6314eb633035STom Caputi 	dsl_crypto_params_free(dcp, B_TRUE);
6315eb633035STom Caputi 	return (ret);
6316eb633035STom Caputi }
6317eb633035STom Caputi 
63184445fffbSMatthew Ahrens static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
63194445fffbSMatthew Ahrens 
63204445fffbSMatthew Ahrens static void
63214445fffbSMatthew Ahrens zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
63224445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
63234445fffbSMatthew Ahrens     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
63244445fffbSMatthew Ahrens {
63254445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
63264445fffbSMatthew Ahrens 
63274445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
63284445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
63294445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
63304445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
63314445fffbSMatthew Ahrens 
63324445fffbSMatthew Ahrens 	vec->zvec_legacy_func = func;
63334445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
63344445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
63354445fffbSMatthew Ahrens 	vec->zvec_allow_log = log_history;
63364445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
63374445fffbSMatthew Ahrens }
63384445fffbSMatthew Ahrens 
63394445fffbSMatthew Ahrens /*
63404445fffbSMatthew Ahrens  * See the block comment at the beginning of this file for details on
63414445fffbSMatthew Ahrens  * each argument to this function.
63424445fffbSMatthew Ahrens  */
63434445fffbSMatthew Ahrens static void
63444445fffbSMatthew Ahrens zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
63454445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
63464445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
63474445fffbSMatthew Ahrens     boolean_t allow_log)
63484445fffbSMatthew Ahrens {
63494445fffbSMatthew Ahrens 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
63504445fffbSMatthew Ahrens 
63514445fffbSMatthew Ahrens 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
63524445fffbSMatthew Ahrens 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
63534445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
63544445fffbSMatthew Ahrens 	ASSERT3P(vec->zvec_func, ==, NULL);
63554445fffbSMatthew Ahrens 
63564445fffbSMatthew Ahrens 	/* if we are logging, the name must be valid */
63574445fffbSMatthew Ahrens 	ASSERT(!allow_log || namecheck != NO_NAME);
63584445fffbSMatthew Ahrens 
63594445fffbSMatthew Ahrens 	vec->zvec_name = name;
63604445fffbSMatthew Ahrens 	vec->zvec_func = func;
63614445fffbSMatthew Ahrens 	vec->zvec_secpolicy = secpolicy;
63624445fffbSMatthew Ahrens 	vec->zvec_namecheck = namecheck;
63634445fffbSMatthew Ahrens 	vec->zvec_pool_check = pool_check;
63644445fffbSMatthew Ahrens 	vec->zvec_smush_outnvlist = smush_outnvlist;
63654445fffbSMatthew Ahrens 	vec->zvec_allow_log = allow_log;
63664445fffbSMatthew Ahrens }
63674445fffbSMatthew Ahrens 
63684445fffbSMatthew Ahrens static void
63694445fffbSMatthew Ahrens zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
63704445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
63714445fffbSMatthew Ahrens     zfs_ioc_poolcheck_t pool_check)
63724445fffbSMatthew Ahrens {
63734445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
63744445fffbSMatthew Ahrens 	    POOL_NAME, log_history, pool_check);
63754445fffbSMatthew Ahrens }
63764445fffbSMatthew Ahrens 
63774445fffbSMatthew Ahrens static void
63784445fffbSMatthew Ahrens zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
63794445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
63804445fffbSMatthew Ahrens {
63814445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
63824445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, pool_check);
63834445fffbSMatthew Ahrens }
63844445fffbSMatthew Ahrens 
63854445fffbSMatthew Ahrens static void
63864445fffbSMatthew Ahrens zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
63874445fffbSMatthew Ahrens {
63884445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
63894445fffbSMatthew Ahrens 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
63904445fffbSMatthew Ahrens }
63914445fffbSMatthew Ahrens 
63924445fffbSMatthew Ahrens static void
63934445fffbSMatthew Ahrens zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
63944445fffbSMatthew Ahrens     zfs_secpolicy_func_t *secpolicy)
63954445fffbSMatthew Ahrens {
63964445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
63974445fffbSMatthew Ahrens 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
63984445fffbSMatthew Ahrens }
63994445fffbSMatthew Ahrens 
64004445fffbSMatthew Ahrens static void
64014445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
64024445fffbSMatthew Ahrens     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
64034445fffbSMatthew Ahrens {
64044445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
64054445fffbSMatthew Ahrens 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
64064445fffbSMatthew Ahrens }
64074445fffbSMatthew Ahrens 
64084445fffbSMatthew Ahrens static void
64094445fffbSMatthew Ahrens zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
64104445fffbSMatthew Ahrens {
64114445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
64124445fffbSMatthew Ahrens 	    zfs_secpolicy_read);
64134445fffbSMatthew Ahrens }
64144445fffbSMatthew Ahrens 
64154445fffbSMatthew Ahrens static void
64164445fffbSMatthew Ahrens zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
64179a686fbcSPaul Dagnelie     zfs_secpolicy_func_t *secpolicy)
64184445fffbSMatthew Ahrens {
64194445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
64204445fffbSMatthew Ahrens 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
64214445fffbSMatthew Ahrens }
64224445fffbSMatthew Ahrens 
64234445fffbSMatthew Ahrens static void
64244445fffbSMatthew Ahrens zfs_ioctl_init(void)
64254445fffbSMatthew Ahrens {
64264445fffbSMatthew Ahrens 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
64274445fffbSMatthew Ahrens 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
64284445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
64294445fffbSMatthew Ahrens 
64304445fffbSMatthew Ahrens 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
64314445fffbSMatthew Ahrens 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
64324445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
64334445fffbSMatthew Ahrens 
64344445fffbSMatthew Ahrens 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
64354445fffbSMatthew Ahrens 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
64364445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
64374445fffbSMatthew Ahrens 
64384445fffbSMatthew Ahrens 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
64394445fffbSMatthew Ahrens 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
64404445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
64414445fffbSMatthew Ahrens 
64424445fffbSMatthew Ahrens 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
64434445fffbSMatthew Ahrens 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
64444445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
64454445fffbSMatthew Ahrens 
64464445fffbSMatthew Ahrens 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
64474445fffbSMatthew Ahrens 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
64484445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
64494445fffbSMatthew Ahrens 
64504445fffbSMatthew Ahrens 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
64514445fffbSMatthew Ahrens 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
64524445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
64534445fffbSMatthew Ahrens 
64545cabbc6bSPrashanth Sreenivasa 	zfs_ioctl_register("remap", ZFS_IOC_REMAP,
64555cabbc6bSPrashanth Sreenivasa 	    zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
64565cabbc6bSPrashanth Sreenivasa 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
64575cabbc6bSPrashanth Sreenivasa 
64584445fffbSMatthew Ahrens 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
64594445fffbSMatthew Ahrens 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
64604445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
64614445fffbSMatthew Ahrens 
64623b2aab18SMatthew Ahrens 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
64633b2aab18SMatthew Ahrens 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
64643b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
64653b2aab18SMatthew Ahrens 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
64663b2aab18SMatthew Ahrens 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
64673b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
64683b2aab18SMatthew Ahrens 
64693b2aab18SMatthew Ahrens 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
64703b2aab18SMatthew Ahrens 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
64713b2aab18SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
64723b2aab18SMatthew Ahrens 
6473a7027df1SMatthew Ahrens 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6474a7027df1SMatthew Ahrens 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6475a7027df1SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6476a7027df1SMatthew Ahrens 
647778f17100SMatthew Ahrens 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
647878f17100SMatthew Ahrens 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
647978f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
648078f17100SMatthew Ahrens 
648178f17100SMatthew Ahrens 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
648278f17100SMatthew Ahrens 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
648378f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
648478f17100SMatthew Ahrens 
648578f17100SMatthew Ahrens 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
648678f17100SMatthew Ahrens 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
648778f17100SMatthew Ahrens 	    POOL_NAME,
648878f17100SMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
648978f17100SMatthew Ahrens 
6490dfc11533SChris Williamson 	zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6491dfc11533SChris Williamson 	    zfs_ioc_channel_program, zfs_secpolicy_config,
6492dfc11533SChris Williamson 	    POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6493dfc11533SChris Williamson 	    B_TRUE);
6494dfc11533SChris Williamson 
649586714001SSerapheim Dimitropoulos 	zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
649686714001SSerapheim Dimitropoulos 	    zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
649786714001SSerapheim Dimitropoulos 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
649886714001SSerapheim Dimitropoulos 
649986714001SSerapheim Dimitropoulos 	zfs_ioctl_register("zpool_discard_checkpoint",
650086714001SSerapheim Dimitropoulos 	    ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
650186714001SSerapheim Dimitropoulos 	    zfs_secpolicy_config, POOL_NAME,
650286714001SSerapheim Dimitropoulos 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
650386714001SSerapheim Dimitropoulos 
6504094e47e9SGeorge Wilson 	zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6505094e47e9SGeorge Wilson 	    zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6506094e47e9SGeorge Wilson 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6507094e47e9SGeorge Wilson 
65089c2acf00SAlek Pinchuk 	zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
65099c2acf00SAlek Pinchuk 	    zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
65109c2acf00SAlek Pinchuk 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
65119c2acf00SAlek Pinchuk 
6512eb633035STom Caputi 	zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6513eb633035STom Caputi 	    zfs_ioc_load_key, zfs_secpolicy_load_key,
6514eb633035STom Caputi 	    DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
6515eb633035STom Caputi 	zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6516eb633035STom Caputi 	    zfs_ioc_unload_key, zfs_secpolicy_load_key,
6517eb633035STom Caputi 	    DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
6518eb633035STom Caputi 	zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6519eb633035STom Caputi 	    zfs_ioc_change_key, zfs_secpolicy_change_key,
6520eb633035STom Caputi 	    DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6521eb633035STom Caputi 	    B_TRUE, B_TRUE);
6522eb633035STom Caputi 
65234445fffbSMatthew Ahrens 	/* IOCTLS that use the legacy function signature */
65244445fffbSMatthew Ahrens 
65254445fffbSMatthew Ahrens 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
65264445fffbSMatthew Ahrens 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
65274445fffbSMatthew Ahrens 
65284445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
65294445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
65304445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
65314445fffbSMatthew Ahrens 	    zfs_ioc_pool_scan);
65324445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
65334445fffbSMatthew Ahrens 	    zfs_ioc_pool_upgrade);
65344445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
65354445fffbSMatthew Ahrens 	    zfs_ioc_vdev_add);
65364445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
65374445fffbSMatthew Ahrens 	    zfs_ioc_vdev_remove);
65384445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
65394445fffbSMatthew Ahrens 	    zfs_ioc_vdev_set_state);
65404445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
65414445fffbSMatthew Ahrens 	    zfs_ioc_vdev_attach);
65424445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
65434445fffbSMatthew Ahrens 	    zfs_ioc_vdev_detach);
65444445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
65454445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setpath);
65464445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
65474445fffbSMatthew Ahrens 	    zfs_ioc_vdev_setfru);
65484445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
65494445fffbSMatthew Ahrens 	    zfs_ioc_pool_set_props);
65504445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
65514445fffbSMatthew Ahrens 	    zfs_ioc_vdev_split);
65524445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
65534445fffbSMatthew Ahrens 	    zfs_ioc_pool_reguid);
65544445fffbSMatthew Ahrens 
65554445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
65564445fffbSMatthew Ahrens 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
65574445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
65584445fffbSMatthew Ahrens 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
65594445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
65604445fffbSMatthew Ahrens 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
65614445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
65624445fffbSMatthew Ahrens 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
65634445fffbSMatthew Ahrens 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
65644445fffbSMatthew Ahrens 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
65654445fffbSMatthew Ahrens 
65664445fffbSMatthew Ahrens 	/*
65674445fffbSMatthew Ahrens 	 * pool destroy, and export don't log the history as part of
65684445fffbSMatthew Ahrens 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
65694445fffbSMatthew Ahrens 	 * does the logging of those commands.
65704445fffbSMatthew Ahrens 	 */
65714445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
65724445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
65734445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
65744445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
65754445fffbSMatthew Ahrens 
65764445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
65774445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
65784445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
65794445fffbSMatthew Ahrens 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
65804445fffbSMatthew Ahrens 
65814445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
65824445fffbSMatthew Ahrens 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
65834445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
65844445fffbSMatthew Ahrens 	    zfs_ioc_dsobj_to_dsname,
65854445fffbSMatthew Ahrens 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
65864445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
65874445fffbSMatthew Ahrens 	    zfs_ioc_pool_get_history,
65884445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
65894445fffbSMatthew Ahrens 
65904445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
65914445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
65924445fffbSMatthew Ahrens 
65934445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6594f4c1745bSloli 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
65954445fffbSMatthew Ahrens 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
65964445fffbSMatthew Ahrens 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
65974445fffbSMatthew Ahrens 
65984445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
65994445fffbSMatthew Ahrens 	    zfs_ioc_space_written);
66004445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
66014445fffbSMatthew Ahrens 	    zfs_ioc_objset_recvd_props);
66024445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
66034445fffbSMatthew Ahrens 	    zfs_ioc_next_obj);
66044445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
66054445fffbSMatthew Ahrens 	    zfs_ioc_get_fsacl);
66064445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
66074445fffbSMatthew Ahrens 	    zfs_ioc_objset_stats);
66084445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
66094445fffbSMatthew Ahrens 	    zfs_ioc_objset_zplprops);
66104445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
66114445fffbSMatthew Ahrens 	    zfs_ioc_dataset_list_next);
66124445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
66134445fffbSMatthew Ahrens 	    zfs_ioc_snapshot_list_next);
66144445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
66154445fffbSMatthew Ahrens 	    zfs_ioc_send_progress);
66164445fffbSMatthew Ahrens 
66174445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
66184445fffbSMatthew Ahrens 	    zfs_ioc_diff, zfs_secpolicy_diff);
66194445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
66204445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
66214445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
66224445fffbSMatthew Ahrens 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
66234445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
66244445fffbSMatthew Ahrens 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
66254445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
66264445fffbSMatthew Ahrens 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
66274445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
66284445fffbSMatthew Ahrens 	    zfs_ioc_send, zfs_secpolicy_send);
66294445fffbSMatthew Ahrens 
66304445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
66314445fffbSMatthew Ahrens 	    zfs_secpolicy_none);
66324445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
66334445fffbSMatthew Ahrens 	    zfs_secpolicy_destroy);
66344445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
66354445fffbSMatthew Ahrens 	    zfs_secpolicy_rename);
66364445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
66374445fffbSMatthew Ahrens 	    zfs_secpolicy_recv);
66384445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
66394445fffbSMatthew Ahrens 	    zfs_secpolicy_promote);
66404445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
66414445fffbSMatthew Ahrens 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
66424445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
66434445fffbSMatthew Ahrens 	    zfs_secpolicy_set_fsacl);
66444445fffbSMatthew Ahrens 
66454445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
66464445fffbSMatthew Ahrens 	    zfs_secpolicy_share, POOL_CHECK_NONE);
66474445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
66484445fffbSMatthew Ahrens 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
66494445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
66504445fffbSMatthew Ahrens 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
66514445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
66524445fffbSMatthew Ahrens 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
66534445fffbSMatthew Ahrens 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
66544445fffbSMatthew Ahrens 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
66554445fffbSMatthew Ahrens }
6656fa9e4066Sahrens 
665754d692b7SGeorge Wilson int
6658f9af39baSGeorge Wilson pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6659f9af39baSGeorge Wilson     zfs_ioc_poolcheck_t check)
666054d692b7SGeorge Wilson {
666154d692b7SGeorge Wilson 	spa_t *spa;
666254d692b7SGeorge Wilson 	int error;
666354d692b7SGeorge Wilson 
666454d692b7SGeorge Wilson 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
666554d692b7SGeorge Wilson 
6666f9af39baSGeorge Wilson 	if (check & POOL_CHECK_NONE)
6667f9af39baSGeorge Wilson 		return (0);
6668f9af39baSGeorge Wilson 
666914843421SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
667054d692b7SGeorge Wilson 	if (error == 0) {
6671f9af39baSGeorge Wilson 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6672be6fd75aSMatthew Ahrens 			error = SET_ERROR(EAGAIN);
6673f9af39baSGeorge Wilson 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6674be6fd75aSMatthew Ahrens 			error = SET_ERROR(EROFS);
667554d692b7SGeorge Wilson 		spa_close(spa, FTAG);
667654d692b7SGeorge Wilson 	}
667754d692b7SGeorge Wilson 	return (error);
667854d692b7SGeorge Wilson }
667954d692b7SGeorge Wilson 
6680c99e4bdcSChris Kirby /*
6681c99e4bdcSChris Kirby  * Find a free minor number.
6682c99e4bdcSChris Kirby  */
6683c99e4bdcSChris Kirby minor_t
6684c99e4bdcSChris Kirby zfsdev_minor_alloc(void)
6685c99e4bdcSChris Kirby {
6686c99e4bdcSChris Kirby 	static minor_t last_minor;
6687c99e4bdcSChris Kirby 	minor_t m;
6688c99e4bdcSChris Kirby 
6689c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6690c99e4bdcSChris Kirby 
6691c99e4bdcSChris Kirby 	for (m = last_minor + 1; m != last_minor; m++) {
6692c99e4bdcSChris Kirby 		if (m > ZFSDEV_MAX_MINOR)
6693c99e4bdcSChris Kirby 			m = 1;
6694c99e4bdcSChris Kirby 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6695c99e4bdcSChris Kirby 			last_minor = m;
6696c99e4bdcSChris Kirby 			return (m);
6697c99e4bdcSChris Kirby 		}
6698c99e4bdcSChris Kirby 	}
6699c99e4bdcSChris Kirby 
6700c99e4bdcSChris Kirby 	return (0);
6701c99e4bdcSChris Kirby }
6702c99e4bdcSChris Kirby 
6703c99e4bdcSChris Kirby static int
6704c99e4bdcSChris Kirby zfs_ctldev_init(dev_t *devp)
6705c99e4bdcSChris Kirby {
6706c99e4bdcSChris Kirby 	minor_t minor;
6707c99e4bdcSChris Kirby 	zfs_soft_state_t *zs;
6708c99e4bdcSChris Kirby 
6709c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6710c99e4bdcSChris Kirby 	ASSERT(getminor(*devp) == 0);
6711c99e4bdcSChris Kirby 
6712c99e4bdcSChris Kirby 	minor = zfsdev_minor_alloc();
6713c99e4bdcSChris Kirby 	if (minor == 0)
6714be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
6715c99e4bdcSChris Kirby 
6716c99e4bdcSChris Kirby 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6717be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
6718c99e4bdcSChris Kirby 
6719c99e4bdcSChris Kirby 	*devp = makedevice(getemajor(*devp), minor);
6720c99e4bdcSChris Kirby 
6721c99e4bdcSChris Kirby 	zs = ddi_get_soft_state(zfsdev_state, minor);
6722c99e4bdcSChris Kirby 	zs->zss_type = ZSST_CTLDEV;
6723c99e4bdcSChris Kirby 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6724c99e4bdcSChris Kirby 
6725c99e4bdcSChris Kirby 	return (0);
6726c99e4bdcSChris Kirby }
6727c99e4bdcSChris Kirby 
6728c99e4bdcSChris Kirby static void
6729c99e4bdcSChris Kirby zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6730c99e4bdcSChris Kirby {
6731c99e4bdcSChris Kirby 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6732c99e4bdcSChris Kirby 
6733c99e4bdcSChris Kirby 	zfs_onexit_destroy(zo);
6734c99e4bdcSChris Kirby 	ddi_soft_state_free(zfsdev_state, minor);
6735c99e4bdcSChris Kirby }
6736c99e4bdcSChris Kirby 
6737c99e4bdcSChris Kirby void *
6738c99e4bdcSChris Kirby zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6739c99e4bdcSChris Kirby {
6740c99e4bdcSChris Kirby 	zfs_soft_state_t *zp;
6741c99e4bdcSChris Kirby 
6742c99e4bdcSChris Kirby 	zp = ddi_get_soft_state(zfsdev_state, minor);
6743c99e4bdcSChris Kirby 	if (zp == NULL || zp->zss_type != which)
6744c99e4bdcSChris Kirby 		return (NULL);
6745c99e4bdcSChris Kirby 
6746c99e4bdcSChris Kirby 	return (zp->zss_data);
6747c99e4bdcSChris Kirby }
6748c99e4bdcSChris Kirby 
6749c99e4bdcSChris Kirby static int
6750c99e4bdcSChris Kirby zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6751c99e4bdcSChris Kirby {
6752c99e4bdcSChris Kirby 	int error = 0;
6753c99e4bdcSChris Kirby 
6754c99e4bdcSChris Kirby 	if (getminor(*devp) != 0)
6755c99e4bdcSChris Kirby 		return (zvol_open(devp, flag, otyp, cr));
6756c99e4bdcSChris Kirby 
6757c99e4bdcSChris Kirby 	/* This is the control device. Allocate a new minor if requested. */
6758c99e4bdcSChris Kirby 	if (flag & FEXCL) {
6759c99e4bdcSChris Kirby 		mutex_enter(&zfsdev_state_lock);
6760c99e4bdcSChris Kirby 		error = zfs_ctldev_init(devp);
6761c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
6762c99e4bdcSChris Kirby 	}
6763c99e4bdcSChris Kirby 
6764c99e4bdcSChris Kirby 	return (error);
6765c99e4bdcSChris Kirby }
6766c99e4bdcSChris Kirby 
6767c99e4bdcSChris Kirby static int
6768c99e4bdcSChris Kirby zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6769c99e4bdcSChris Kirby {
6770c99e4bdcSChris Kirby 	zfs_onexit_t *zo;
6771c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
6772c99e4bdcSChris Kirby 
6773c99e4bdcSChris Kirby 	if (minor == 0)
6774c99e4bdcSChris Kirby 		return (0);
6775c99e4bdcSChris Kirby 
6776c99e4bdcSChris Kirby 	mutex_enter(&zfsdev_state_lock);
6777c99e4bdcSChris Kirby 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6778c99e4bdcSChris Kirby 	if (zo == NULL) {
6779c99e4bdcSChris Kirby 		mutex_exit(&zfsdev_state_lock);
6780c99e4bdcSChris Kirby 		return (zvol_close(dev, flag, otyp, cr));
6781c99e4bdcSChris Kirby 	}
6782c99e4bdcSChris Kirby 	zfs_ctldev_destroy(zo, minor);
6783c99e4bdcSChris Kirby 	mutex_exit(&zfsdev_state_lock);
6784c99e4bdcSChris Kirby 
6785c99e4bdcSChris Kirby 	return (0);
6786c99e4bdcSChris Kirby }
6787c99e4bdcSChris Kirby 
6788fa9e4066Sahrens static int
6789fa9e4066Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6790fa9e4066Sahrens {
6791fa9e4066Sahrens 	zfs_cmd_t *zc;
67924445fffbSMatthew Ahrens 	uint_t vecnum;
67934445fffbSMatthew Ahrens 	int error, rc, len;
6794c99e4bdcSChris Kirby 	minor_t minor = getminor(dev);
67954445fffbSMatthew Ahrens 	const zfs_ioc_vec_t *vec;
67964445fffbSMatthew Ahrens 	char *saved_poolname = NULL;
67974445fffbSMatthew Ahrens 	nvlist_t *innvl = NULL;
6798fa9e4066Sahrens 
6799c99e4bdcSChris Kirby 	if (minor != 0 &&
6800c99e4bdcSChris Kirby 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6801fa9e4066Sahrens 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6802fa9e4066Sahrens 
68034445fffbSMatthew Ahrens 	vecnum = cmd - ZFS_IOC_FIRST;
680491ebeef5Sahrens 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6805fa9e4066Sahrens 
68064445fffbSMatthew Ahrens 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6807be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
68084445fffbSMatthew Ahrens 	vec = &zfs_ioc_vec[vecnum];
6809fa9e4066Sahrens 
6810fa9e4066Sahrens 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6811fa9e4066Sahrens 
6812478ed9adSEric Taylor 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
68134445fffbSMatthew Ahrens 	if (error != 0) {
6814be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
68154445fffbSMatthew Ahrens 		goto out;
68164445fffbSMatthew Ahrens 	}
6817fa9e4066Sahrens 
68184445fffbSMatthew Ahrens 	zc->zc_iflags = flag & FKIOCTL;
68194445fffbSMatthew Ahrens 	if (zc->zc_nvlist_src_size != 0) {
68204445fffbSMatthew Ahrens 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
68214445fffbSMatthew Ahrens 		    zc->zc_iflags, &innvl);
68224445fffbSMatthew Ahrens 		if (error != 0)
68234445fffbSMatthew Ahrens 			goto out;
68244445fffbSMatthew Ahrens 	}
6825fa9e4066Sahrens 
6826fa9e4066Sahrens 	/*
6827fa9e4066Sahrens 	 * Ensure that all pool/dataset names are valid before we pass down to
6828fa9e4066Sahrens 	 * the lower layers.
6829fa9e4066Sahrens 	 */
68304445fffbSMatthew Ahrens 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
68314445fffbSMatthew Ahrens 	switch (vec->zvec_namecheck) {
68324445fffbSMatthew Ahrens 	case POOL_NAME:
68334445fffbSMatthew Ahrens 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6834be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
68354445fffbSMatthew Ahrens 		else
6836f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
68374445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
68384445fffbSMatthew Ahrens 		break;
6839fa9e4066Sahrens 
68404445fffbSMatthew Ahrens 	case DATASET_NAME:
68414445fffbSMatthew Ahrens 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6842be6fd75aSMatthew Ahrens 			error = SET_ERROR(EINVAL);
68434445fffbSMatthew Ahrens 		else
6844f9af39baSGeorge Wilson 			error = pool_status_check(zc->zc_name,
68454445fffbSMatthew Ahrens 			    vec->zvec_namecheck, vec->zvec_pool_check);
68464445fffbSMatthew Ahrens 		break;
68475ad82045Snd 
68484445fffbSMatthew Ahrens 	case NO_NAME:
68494445fffbSMatthew Ahrens 		break;
6850fa9e4066Sahrens 	}
6851fa9e4066Sahrens 
6852fa9e4066Sahrens 
685345b17475SAlex Wilson 	if (error == 0)
68544445fffbSMatthew Ahrens 		error = vec->zvec_secpolicy(zc, innvl, cr);
68554445fffbSMatthew Ahrens 
68564445fffbSMatthew Ahrens 	if (error != 0)
68574445fffbSMatthew Ahrens 		goto out;
68584445fffbSMatthew Ahrens 
68594445fffbSMatthew Ahrens 	/* legacy ioctls can modify zc_name */
686078f17100SMatthew Ahrens 	len = strcspn(zc->zc_name, "/@#") + 1;
68614445fffbSMatthew Ahrens 	saved_poolname = kmem_alloc(len, KM_SLEEP);
68624445fffbSMatthew Ahrens 	(void) strlcpy(saved_poolname, zc->zc_name, len);
68634445fffbSMatthew Ahrens 
68644445fffbSMatthew Ahrens 	if (vec->zvec_func != NULL) {
68654445fffbSMatthew Ahrens 		nvlist_t *outnvl;
68664445fffbSMatthew Ahrens 		int puterror = 0;
68674445fffbSMatthew Ahrens 		spa_t *spa;
68684445fffbSMatthew Ahrens 		nvlist_t *lognv = NULL;
68694445fffbSMatthew Ahrens 
68704445fffbSMatthew Ahrens 		ASSERT(vec->zvec_legacy_func == NULL);
68714445fffbSMatthew Ahrens 
68724445fffbSMatthew Ahrens 		/*
68734445fffbSMatthew Ahrens 		 * Add the innvl to the lognv before calling the func,
68744445fffbSMatthew Ahrens 		 * in case the func changes the innvl.
68754445fffbSMatthew Ahrens 		 */
68764445fffbSMatthew Ahrens 		if (vec->zvec_allow_log) {
68774445fffbSMatthew Ahrens 			lognv = fnvlist_alloc();
68784445fffbSMatthew Ahrens 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
68794445fffbSMatthew Ahrens 			    vec->zvec_name);
68804445fffbSMatthew Ahrens 			if (!nvlist_empty(innvl)) {
68814445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
68824445fffbSMatthew Ahrens 				    innvl);
68834445fffbSMatthew Ahrens 			}
68844445fffbSMatthew Ahrens 		}
68854445fffbSMatthew Ahrens 
68864445fffbSMatthew Ahrens 		outnvl = fnvlist_alloc();
68874445fffbSMatthew Ahrens 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
68884445fffbSMatthew Ahrens 
6889dfc11533SChris Williamson 		/*
6890d0cb1fb9SDon Brady 		 * Some commands can partially execute, modify state, and still
6891dfc11533SChris Williamson 		 * return an error.  In these cases, attempt to record what
6892dfc11533SChris Williamson 		 * was modified.
6893dfc11533SChris Williamson 		 */
6894dfc11533SChris Williamson 		if ((error == 0 ||
6895dfc11533SChris Williamson 		    (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6896dfc11533SChris Williamson 		    vec->zvec_allow_log &&
68974445fffbSMatthew Ahrens 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
68984445fffbSMatthew Ahrens 			if (!nvlist_empty(outnvl)) {
68994445fffbSMatthew Ahrens 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
69004445fffbSMatthew Ahrens 				    outnvl);
69014445fffbSMatthew Ahrens 			}
6902dfc11533SChris Williamson 			if (error != 0) {
6903dfc11533SChris Williamson 				fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6904dfc11533SChris Williamson 				    error);
6905dfc11533SChris Williamson 			}
69064445fffbSMatthew Ahrens 			(void) spa_history_log_nvl(spa, lognv);
69074445fffbSMatthew Ahrens 			spa_close(spa, FTAG);
69084445fffbSMatthew Ahrens 		}
69094445fffbSMatthew Ahrens 		fnvlist_free(lognv);
69104445fffbSMatthew Ahrens 
69114445fffbSMatthew Ahrens 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
69124445fffbSMatthew Ahrens 			int smusherror = 0;
69134445fffbSMatthew Ahrens 			if (vec->zvec_smush_outnvlist) {
69144445fffbSMatthew Ahrens 				smusherror = nvlist_smush(outnvl,
69154445fffbSMatthew Ahrens 				    zc->zc_nvlist_dst_size);
69164445fffbSMatthew Ahrens 			}
69174445fffbSMatthew Ahrens 			if (smusherror == 0)
69184445fffbSMatthew Ahrens 				puterror = put_nvlist(zc, outnvl);
69194445fffbSMatthew Ahrens 		}
69204445fffbSMatthew Ahrens 
69214445fffbSMatthew Ahrens 		if (puterror != 0)
69224445fffbSMatthew Ahrens 			error = puterror;
69234445fffbSMatthew Ahrens 
69244445fffbSMatthew Ahrens 		nvlist_free(outnvl);
69254445fffbSMatthew Ahrens 	} else {
69264445fffbSMatthew Ahrens 		error = vec->zvec_legacy_func(zc);
69274445fffbSMatthew Ahrens 	}
69284445fffbSMatthew Ahrens 
69294445fffbSMatthew Ahrens out:
69304445fffbSMatthew Ahrens 	nvlist_free(innvl);
6931478ed9adSEric Taylor 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
69324445fffbSMatthew Ahrens 	if (error == 0 && rc != 0)
6933be6fd75aSMatthew Ahrens 		error = SET_ERROR(EFAULT);
69344445fffbSMatthew Ahrens 	if (error == 0 && vec->zvec_allow_log) {
69354445fffbSMatthew Ahrens 		char *s = tsd_get(zfs_allow_log_key);
69364445fffbSMatthew Ahrens 		if (s != NULL)
69374445fffbSMatthew Ahrens 			strfree(s);
69384445fffbSMatthew Ahrens 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
69394445fffbSMatthew Ahrens 	} else {
69404445fffbSMatthew Ahrens 		if (saved_poolname != NULL)
69414445fffbSMatthew Ahrens 			strfree(saved_poolname);
6942ecd6cf80Smarks 	}
6943fa9e4066Sahrens 
6944fa9e4066Sahrens 	kmem_free(zc, sizeof (zfs_cmd_t));
6945fa9e4066Sahrens 	return (error);
6946fa9e4066Sahrens }
6947fa9e4066Sahrens 
6948fa9e4066Sahrens static int
6949fa9e4066Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6950fa9e4066Sahrens {
6951fa9e4066Sahrens 	if (cmd != DDI_ATTACH)
6952fa9e4066Sahrens 		return (DDI_FAILURE);
6953fa9e4066Sahrens 
6954fa9e4066Sahrens 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6955fa9e4066Sahrens 	    DDI_PSEUDO, 0) == DDI_FAILURE)
6956fa9e4066Sahrens 		return (DDI_FAILURE);
6957fa9e4066Sahrens 
6958fa9e4066Sahrens 	zfs_dip = dip;
6959fa9e4066Sahrens 
6960fa9e4066Sahrens 	ddi_report_dev(dip);
6961fa9e4066Sahrens 
6962fa9e4066Sahrens 	return (DDI_SUCCESS);
6963fa9e4066Sahrens }
6964fa9e4066Sahrens 
6965fa9e4066Sahrens static int
6966fa9e4066Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6967fa9e4066Sahrens {
6968fa9e4066Sahrens 	if (spa_busy() || zfs_busy() || zvol_busy())
6969fa9e4066Sahrens 		return (DDI_FAILURE);
6970fa9e4066Sahrens 
6971fa9e4066Sahrens 	if (cmd != DDI_DETACH)
6972fa9e4066Sahrens 		return (DDI_FAILURE);
6973fa9e4066Sahrens 
6974fa9e4066Sahrens 	zfs_dip = NULL;
6975fa9e4066Sahrens 
6976fa9e4066Sahrens 	ddi_prop_remove_all(dip);
6977fa9e4066Sahrens 	ddi_remove_minor_node(dip, NULL);
6978fa9e4066Sahrens 
6979fa9e4066Sahrens 	return (DDI_SUCCESS);
6980fa9e4066Sahrens }
6981fa9e4066Sahrens 
6982fa9e4066Sahrens /*ARGSUSED*/
6983fa9e4066Sahrens static int
6984fa9e4066Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6985fa9e4066Sahrens {
6986fa9e4066Sahrens 	switch (infocmd) {
6987fa9e4066Sahrens 	case DDI_INFO_DEVT2DEVINFO:
6988fa9e4066Sahrens 		*result = zfs_dip;
6989fa9e4066Sahrens 		return (DDI_SUCCESS);
6990fa9e4066Sahrens 
6991fa9e4066Sahrens 	case DDI_INFO_DEVT2INSTANCE:
6992a0965f35Sbonwick 		*result = (void *)0;
6993fa9e4066Sahrens 		return (DDI_SUCCESS);
6994fa9e4066Sahrens 	}
6995fa9e4066Sahrens 
6996fa9e4066Sahrens 	return (DDI_FAILURE);
6997fa9e4066Sahrens }
6998fa9e4066Sahrens 
6999fa9e4066Sahrens /*
7000fa9e4066Sahrens  * OK, so this is a little weird.
7001fa9e4066Sahrens  *
7002fa9e4066Sahrens  * /dev/zfs is the control node, i.e. minor 0.
7003fa9e4066Sahrens  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
7004fa9e4066Sahrens  *
7005fa9e4066Sahrens  * /dev/zfs has basically nothing to do except serve up ioctls,
7006fa9e4066Sahrens  * so most of the standard driver entry points are in zvol.c.
7007fa9e4066Sahrens  */
7008fa9e4066Sahrens static struct cb_ops zfs_cb_ops = {
7009c99e4bdcSChris Kirby 	zfsdev_open,	/* open */
7010c99e4bdcSChris Kirby 	zfsdev_close,	/* close */
7011fa9e4066Sahrens 	zvol_strategy,	/* strategy */
7012fa9e4066Sahrens 	nodev,		/* print */
7013e7cbe64fSgw 	zvol_dump,	/* dump */
7014fa9e4066Sahrens 	zvol_read,	/* read */
7015fa9e4066Sahrens 	zvol_write,	/* write */
7016fa9e4066Sahrens 	zfsdev_ioctl,	/* ioctl */
7017fa9e4066Sahrens 	nodev,		/* devmap */
7018fa9e4066Sahrens 	nodev,		/* mmap */
7019fa9e4066Sahrens 	nodev,		/* segmap */
7020fa9e4066Sahrens 	nochpoll,	/* poll */
7021fa9e4066Sahrens 	ddi_prop_op,	/* prop_op */
7022fa9e4066Sahrens 	NULL,		/* streamtab */
7023fa9e4066Sahrens 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
7024fa9e4066Sahrens 	CB_REV,		/* version */
7025feb08c6bSbillm 	nodev,		/* async read */
7026feb08c6bSbillm 	nodev,		/* async write */
7027fa9e4066Sahrens };
7028fa9e4066Sahrens 
7029fa9e4066Sahrens static struct dev_ops zfs_dev_ops = {
7030fa9e4066Sahrens 	DEVO_REV,	/* version */
7031fa9e4066Sahrens 	0,		/* refcnt */
7032fa9e4066Sahrens 	zfs_info,	/* info */
7033fa9e4066Sahrens 	nulldev,	/* identify */
7034fa9e4066Sahrens 	nulldev,	/* probe */
7035fa9e4066Sahrens 	zfs_attach,	/* attach */
7036fa9e4066Sahrens 	zfs_detach,	/* detach */
7037fa9e4066Sahrens 	nodev,		/* reset */
7038fa9e4066Sahrens 	&zfs_cb_ops,	/* driver operations */
703919397407SSherry Moore 	NULL,		/* no bus operations */
704019397407SSherry Moore 	NULL,		/* power */
704119397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
7042fa9e4066Sahrens };
7043fa9e4066Sahrens 
7044fa9e4066Sahrens static struct modldrv zfs_modldrv = {
704519397407SSherry Moore 	&mod_driverops,
704619397407SSherry Moore 	"ZFS storage pool",
704719397407SSherry Moore 	&zfs_dev_ops
7048fa9e4066Sahrens };
7049fa9e4066Sahrens 
7050fa9e4066Sahrens static struct modlinkage modlinkage = {
7051fa9e4066Sahrens 	MODREV_1,
7052fa9e4066Sahrens 	(void *)&zfs_modlfs,
7053fa9e4066Sahrens 	(void *)&zfs_modldrv,
7054fa9e4066Sahrens 	NULL
7055fa9e4066Sahrens };
7056fa9e4066Sahrens 
70574445fffbSMatthew Ahrens static void
70584445fffbSMatthew Ahrens zfs_allow_log_destroy(void *arg)
70594445fffbSMatthew Ahrens {
70604445fffbSMatthew Ahrens 	char *poolname = arg;
70614445fffbSMatthew Ahrens 	strfree(poolname);
70624445fffbSMatthew Ahrens }
7063ec533521Sfr 
7064fa9e4066Sahrens int
7065fa9e4066Sahrens _init(void)
7066fa9e4066Sahrens {
7067fa9e4066Sahrens 	int error;
7068fa9e4066Sahrens 
7069a0965f35Sbonwick 	spa_init(FREAD | FWRITE);
7070a0965f35Sbonwick 	zfs_init();
7071a0965f35Sbonwick 	zvol_init();
70724445fffbSMatthew Ahrens 	zfs_ioctl_init();
7073a0965f35Sbonwick 
7074a0965f35Sbonwick 	if ((error = mod_install(&modlinkage)) != 0) {
7075a0965f35Sbonwick 		zvol_fini();
7076a0965f35Sbonwick 		zfs_fini();
7077a0965f35Sbonwick 		spa_fini();
7078fa9e4066Sahrens 		return (error);
7079a0965f35Sbonwick 	}
7080fa9e4066Sahrens 
7081ec533521Sfr 	tsd_create(&zfs_fsyncer_key, NULL);
70824445fffbSMatthew Ahrens 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
70834445fffbSMatthew Ahrens 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7084ec533521Sfr 
7085fa9e4066Sahrens 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
7086fa9e4066Sahrens 	ASSERT(error == 0);
7087ecd6cf80Smarks 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7088fa9e4066Sahrens 
7089fa9e4066Sahrens 	return (0);
7090fa9e4066Sahrens }
7091fa9e4066Sahrens 
7092fa9e4066Sahrens int
7093fa9e4066Sahrens _fini(void)
7094fa9e4066Sahrens {
7095fa9e4066Sahrens 	int error;
7096fa9e4066Sahrens 
7097ea8dc4b6Seschrock 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
7098be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
7099fa9e4066Sahrens 
7100fa9e4066Sahrens 	if ((error = mod_remove(&modlinkage)) != 0)
7101fa9e4066Sahrens 		return (error);
7102fa9e4066Sahrens 
7103fa9e4066Sahrens 	zvol_fini();
7104fa9e4066Sahrens 	zfs_fini();
7105fa9e4066Sahrens 	spa_fini();
7106da6c28aaSamw 	if (zfs_nfsshare_inited)
7107ecd6cf80Smarks 		(void) ddi_modclose(nfs_mod);
7108da6c28aaSamw 	if (zfs_smbshare_inited)
7109da6c28aaSamw 		(void) ddi_modclose(smbsrv_mod);
7110da6c28aaSamw 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
7111ecd6cf80Smarks 		(void) ddi_modclose(sharefs_mod);
7112fa9e4066Sahrens 
7113ec533521Sfr 	tsd_destroy(&zfs_fsyncer_key);
7114fa9e4066Sahrens 	ldi_ident_release(zfs_li);
7115fa9e4066Sahrens 	zfs_li = NULL;
7116ecd6cf80Smarks 	mutex_destroy(&zfs_share_lock);
7117fa9e4066Sahrens 
7118fa9e4066Sahrens 	return (error);
7119fa9e4066Sahrens }
7120fa9e4066Sahrens 
7121fa9e4066Sahrens int
7122fa9e4066Sahrens _info(struct modinfo *modinfop)
7123fa9e4066Sahrens {
7124fa9e4066Sahrens 	return (mod_info(&modlinkage, modinfop));
7125fa9e4066Sahrens }
7126