xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision c21bd51d7acbaf77116c4cc3a23dfc6d16c637c2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28  * Portions Copyright 2011 Martin Matuska
29  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
30  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
31  * Copyright 2019 Joyent, Inc.
32  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
33  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
34  * Copyright (c) 2013 Steven Hartland. All rights reserved.
35  * Copyright (c) 2014 Integros [integros.com]
36  * Copyright 2016 Toomas Soome <tsoome@me.com>
37  * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
38  * Copyright 2017 RackTop Systems.
39  * Copyright (c) 2017, Datto, Inc. All rights reserved.
40  */
41 
42 /*
43  * ZFS ioctls.
44  *
45  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
46  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
47  *
48  * There are two ways that we handle ioctls: the legacy way where almost
49  * all of the logic is in the ioctl callback, and the new way where most
50  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
51  *
52  * Non-legacy ioctls should be registered by calling
53  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
54  * from userland by lzc_ioctl().
55  *
56  * The registration arguments are as follows:
57  *
58  * const char *name
59  *   The name of the ioctl.  This is used for history logging.  If the
60  *   ioctl returns successfully (the callback returns 0), and allow_log
61  *   is true, then a history log entry will be recorded with the input &
62  *   output nvlists.  The log entry can be printed with "zpool history -i".
63  *
64  * zfs_ioc_t ioc
65  *   The ioctl request number, which userland will pass to ioctl(2).
66  *   We want newer versions of libzfs and libzfs_core to run against
67  *   existing zfs kernel modules (i.e. a deferred reboot after an update).
68  *   Therefore the ioctl numbers cannot change from release to release.
69  *
70  * zfs_secpolicy_func_t *secpolicy
71  *   This function will be called before the zfs_ioc_func_t, to
72  *   determine if this operation is permitted.  It should return EPERM
73  *   on failure, and 0 on success.  Checks include determining if the
74  *   dataset is visible in this zone, and if the user has either all
75  *   zfs privileges in the zone (SYS_MOUNT), or has been granted permission
76  *   to do this operation on this dataset with "zfs allow".
77  *
78  * zfs_ioc_namecheck_t namecheck
79  *   This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
80  *   name, a dataset name, or nothing.  If the name is not well-formed,
81  *   the ioctl will fail and the callback will not be called.
82  *   Therefore, the callback can assume that the name is well-formed
83  *   (e.g. is null-terminated, doesn't have more than one '@' character,
84  *   doesn't have invalid characters).
85  *
86  * zfs_ioc_poolcheck_t pool_check
87  *   This specifies requirements on the pool state.  If the pool does
88  *   not meet them (is suspended or is readonly), the ioctl will fail
89  *   and the callback will not be called.  If any checks are specified
90  *   (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
91  *   Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
92  *   POOL_CHECK_READONLY).
93  *
94  * zfs_ioc_key_t *nvl_keys
95  *  The list of expected/allowable innvl input keys. This list is used
96  *  to validate the nvlist input to the ioctl.
97  *
98  * boolean_t smush_outnvlist
99  *   If smush_outnvlist is true, then the output is presumed to be a
100  *   list of errors, and it will be "smushed" down to fit into the
101  *   caller's buffer, by removing some entries and replacing them with a
102  *   single "N_MORE_ERRORS" entry indicating how many were removed.  See
103  *   nvlist_smush() for details.  If smush_outnvlist is false, and the
104  *   outnvlist does not fit into the userland-provided buffer, then the
105  *   ioctl will fail with ENOMEM.
106  *
107  * zfs_ioc_func_t *func
108  *   The callback function that will perform the operation.
109  *
110  *   The callback should return 0 on success, or an error number on
111  *   failure.  If the function fails, the userland ioctl will return -1,
112  *   and errno will be set to the callback's return value.  The callback
113  *   will be called with the following arguments:
114  *
115  *   const char *name
116  *     The name of the pool or dataset to operate on, from
117  *     zfs_cmd_t:zc_name.  The 'namecheck' argument specifies the
118  *     expected type (pool, dataset, or none).
119  *
120  *   nvlist_t *innvl
121  *     The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src.  Or
122  *     NULL if no input nvlist was provided.  Changes to this nvlist are
123  *     ignored.  If the input nvlist could not be deserialized, the
124  *     ioctl will fail and the callback will not be called.
125  *
126  *   nvlist_t *outnvl
127  *     The output nvlist, initially empty.  The callback can fill it in,
128  *     and it will be returned to userland by serializing it into
129  *     zfs_cmd_t:zc_nvlist_dst.  If it is non-empty, and serialization
130  *     fails (e.g. because the caller didn't supply a large enough
131  *     buffer), then the overall ioctl will fail.  See the
132  *     'smush_nvlist' argument above for additional behaviors.
133  *
134  *     There are two typical uses of the output nvlist:
135  *       - To return state, e.g. property values.  In this case,
136  *         smush_outnvlist should be false.  If the buffer was not large
137  *         enough, the caller will reallocate a larger buffer and try
138  *         the ioctl again.
139  *
140  *       - To return multiple errors from an ioctl which makes on-disk
141  *         changes.  In this case, smush_outnvlist should be true.
142  *         Ioctls which make on-disk modifications should generally not
143  *         use the outnvl if they succeed, because the caller can not
144  *         distinguish between the operation failing, and
145  *         deserialization failing.
146  *
147  * IOCTL Interface Errors
148  *
149  * The following ioctl input errors can be returned:
150  *   ZFS_ERR_IOC_CMD_UNAVAIL	the ioctl number is not supported by kernel
151  *   ZFS_ERR_IOC_ARG_UNAVAIL	an input argument is not supported by kernel
152  *   ZFS_ERR_IOC_ARG_REQUIRED	a required input argument is missing
153  *   ZFS_ERR_IOC_ARG_BADTYPE	an input argument has an invalid type
154  */
155 
156 #include <sys/types.h>
157 #include <sys/param.h>
158 #include <sys/errno.h>
159 #include <sys/uio.h>
160 #include <sys/buf.h>
161 #include <sys/modctl.h>
162 #include <sys/open.h>
163 #include <sys/file.h>
164 #include <sys/kmem.h>
165 #include <sys/conf.h>
166 #include <sys/cmn_err.h>
167 #include <sys/stat.h>
168 #include <sys/zfs_ioctl.h>
169 #include <sys/zfs_vfsops.h>
170 #include <sys/zfs_znode.h>
171 #include <sys/zap.h>
172 #include <sys/spa.h>
173 #include <sys/spa_impl.h>
174 #include <sys/vdev.h>
175 #include <sys/priv_impl.h>
176 #include <sys/dmu.h>
177 #include <sys/dsl_dir.h>
178 #include <sys/dsl_dataset.h>
179 #include <sys/dsl_prop.h>
180 #include <sys/dsl_deleg.h>
181 #include <sys/dmu_objset.h>
182 #include <sys/dmu_impl.h>
183 #include <sys/dmu_tx.h>
184 #include <sys/ddi.h>
185 #include <sys/sunddi.h>
186 #include <sys/sunldi.h>
187 #include <sys/policy.h>
188 #include <sys/zone.h>
189 #include <sys/nvpair.h>
190 #include <sys/pathname.h>
191 #include <sys/mount.h>
192 #include <sys/sdt.h>
193 #include <sys/fs/zfs.h>
194 #include <sys/zfs_ctldir.h>
195 #include <sys/zfs_dir.h>
196 #include <sys/zfs_onexit.h>
197 #include <sys/zvol.h>
198 #include <sys/dsl_scan.h>
199 #include <sharefs/share.h>
200 #include <sys/dmu_objset.h>
201 #include <sys/dmu_recv.h>
202 #include <sys/dmu_send.h>
203 #include <sys/dsl_destroy.h>
204 #include <sys/dsl_bookmark.h>
205 #include <sys/dsl_userhold.h>
206 #include <sys/zfeature.h>
207 #include <sys/zcp.h>
208 #include <sys/zio_checksum.h>
209 #include <sys/vdev_removal.h>
210 #include <sys/vdev_impl.h>
211 #include <sys/vdev_initialize.h>
212 #include <sys/vdev_trim.h>
213 #include <sys/dsl_crypt.h>
214 
215 #include "zfs_namecheck.h"
216 #include "zfs_prop.h"
217 #include "zfs_deleg.h"
218 #include "zfs_comutil.h"
219 
220 #include "lua.h"
221 #include "lauxlib.h"
222 
223 extern struct modlfs zfs_modlfs;
224 
225 extern void zfs_init(void);
226 extern void zfs_fini(void);
227 
228 ldi_ident_t zfs_li = NULL;
229 dev_info_t *zfs_dip;
230 
231 uint_t zfs_fsyncer_key;
232 extern uint_t rrw_tsd_key;
233 static uint_t zfs_allow_log_key;
234 
235 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
236 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
237 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
238 
239 /*
240  * IOC Keys are used to document and validate user->kernel interface inputs.
241  * See zfs_keys_recv_new for an example declaration. Any key name that is not
242  * listed will be rejected as input.
243  *
244  * The keyname 'optional' is always allowed, and must be an nvlist if present.
245  * Arguments which older kernels can safely ignore can be placed under the
246  * "optional" key.
247  *
248  * When adding new keys to an existing ioc for new functionality, consider:
249  *	- adding an entry into zfs_sysfs.c zfs_features[] list
250  *	- updating the libzfs_input_check.c test utility
251  *
252  * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
253  * for the expected name (bookmark, snapshot, property, etc) but there
254  * is no validation in the preflight zfs_check_input_nvpairs() check.
255  */
256 typedef enum {
257 	ZK_OPTIONAL = 1 << 0,		/* pair is optional */
258 	ZK_WILDCARDLIST = 1 << 1,	/* one or more unspecified key names */
259 } ioc_key_flag_t;
260 
261 /* DATA_TYPE_ANY is used when zkey_type can vary. */
262 #define	DATA_TYPE_ANY	DATA_TYPE_UNKNOWN
263 
264 typedef struct zfs_ioc_key {
265 	const char	*zkey_name;
266 	data_type_t	zkey_type;
267 	ioc_key_flag_t	zkey_flags;
268 } zfs_ioc_key_t;
269 
270 typedef enum {
271 	NO_NAME,
272 	POOL_NAME,
273 	DATASET_NAME
274 } zfs_ioc_namecheck_t;
275 
276 typedef enum {
277 	POOL_CHECK_NONE		= 1 << 0,
278 	POOL_CHECK_SUSPENDED	= 1 << 1,
279 	POOL_CHECK_READONLY	= 1 << 2,
280 } zfs_ioc_poolcheck_t;
281 
282 typedef struct zfs_ioc_vec {
283 	zfs_ioc_legacy_func_t	*zvec_legacy_func;
284 	zfs_ioc_func_t		*zvec_func;
285 	zfs_secpolicy_func_t	*zvec_secpolicy;
286 	zfs_ioc_namecheck_t	zvec_namecheck;
287 	boolean_t		zvec_allow_log;
288 	zfs_ioc_poolcheck_t	zvec_pool_check;
289 	boolean_t		zvec_smush_outnvlist;
290 	const char		*zvec_name;
291 	const zfs_ioc_key_t	*zvec_nvl_keys;
292 	size_t			zvec_nvl_key_count;
293 } zfs_ioc_vec_t;
294 
295 /* This array is indexed by zfs_userquota_prop_t */
296 static const char *userquota_perms[] = {
297 	ZFS_DELEG_PERM_USERUSED,
298 	ZFS_DELEG_PERM_USERQUOTA,
299 	ZFS_DELEG_PERM_GROUPUSED,
300 	ZFS_DELEG_PERM_GROUPQUOTA,
301 	ZFS_DELEG_PERM_USEROBJUSED,
302 	ZFS_DELEG_PERM_USEROBJQUOTA,
303 	ZFS_DELEG_PERM_GROUPOBJUSED,
304 	ZFS_DELEG_PERM_GROUPOBJQUOTA,
305 	ZFS_DELEG_PERM_PROJECTUSED,
306 	ZFS_DELEG_PERM_PROJECTQUOTA,
307 	ZFS_DELEG_PERM_PROJECTOBJUSED,
308 	ZFS_DELEG_PERM_PROJECTOBJQUOTA,
309 };
310 
311 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
312 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
313 static int zfs_check_settable(const char *name, nvpair_t *property,
314     cred_t *cr);
315 static int zfs_check_clearable(char *dataset, nvlist_t *props,
316     nvlist_t **errors);
317 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
318     boolean_t *);
319 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
320 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
321 
322 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
323 
324 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
325 void
326 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
327 {
328 	const char *newfile;
329 	char buf[512];
330 	va_list adx;
331 
332 	/*
333 	 * Get rid of annoying "../common/" prefix to filename.
334 	 */
335 	newfile = strrchr(file, '/');
336 	if (newfile != NULL) {
337 		newfile = newfile + 1; /* Get rid of leading / */
338 	} else {
339 		newfile = file;
340 	}
341 
342 	va_start(adx, fmt);
343 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
344 	va_end(adx);
345 
346 	/*
347 	 * To get this data, use the zfs-dprintf probe as so:
348 	 * dtrace -q -n 'zfs-dprintf \
349 	 *	/stringof(arg0) == "dbuf.c"/ \
350 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
351 	 * arg0 = file name
352 	 * arg1 = function name
353 	 * arg2 = line number
354 	 * arg3 = message
355 	 */
356 	DTRACE_PROBE4(zfs__dprintf,
357 	    char *, newfile, char *, func, int, line, char *, buf);
358 }
359 
360 static void
361 history_str_free(char *buf)
362 {
363 	kmem_free(buf, HIS_MAX_RECORD_LEN);
364 }
365 
366 static char *
367 history_str_get(zfs_cmd_t *zc)
368 {
369 	char *buf;
370 
371 	if (zc->zc_history == 0)
372 		return (NULL);
373 
374 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
375 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
376 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
377 		history_str_free(buf);
378 		return (NULL);
379 	}
380 
381 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
382 
383 	return (buf);
384 }
385 
386 /*
387  * Check to see if the named dataset is currently defined as bootable
388  */
389 static boolean_t
390 zfs_is_bootfs(const char *name)
391 {
392 	objset_t *os;
393 
394 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
395 		boolean_t ret;
396 		ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
397 		dmu_objset_rele(os, FTAG);
398 		return (ret);
399 	}
400 	return (B_FALSE);
401 }
402 
403 /*
404  * Return non-zero if the spa version is less than requested version.
405  */
406 static int
407 zfs_earlier_version(const char *name, int version)
408 {
409 	spa_t *spa;
410 
411 	if (spa_open(name, &spa, FTAG) == 0) {
412 		if (spa_version(spa) < version) {
413 			spa_close(spa, FTAG);
414 			return (1);
415 		}
416 		spa_close(spa, FTAG);
417 	}
418 	return (0);
419 }
420 
421 /*
422  * Return TRUE if the ZPL version is less than requested version.
423  */
424 static boolean_t
425 zpl_earlier_version(const char *name, int version)
426 {
427 	objset_t *os;
428 	boolean_t rc = B_TRUE;
429 
430 	if (dmu_objset_hold(name, FTAG, &os) == 0) {
431 		uint64_t zplversion;
432 
433 		if (dmu_objset_type(os) != DMU_OST_ZFS) {
434 			dmu_objset_rele(os, FTAG);
435 			return (B_TRUE);
436 		}
437 		/* XXX reading from non-owned objset */
438 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
439 			rc = zplversion < version;
440 		dmu_objset_rele(os, FTAG);
441 	}
442 	return (rc);
443 }
444 
445 static void
446 zfs_log_history(zfs_cmd_t *zc)
447 {
448 	spa_t *spa;
449 	char *buf;
450 
451 	if ((buf = history_str_get(zc)) == NULL)
452 		return;
453 
454 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
455 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
456 			(void) spa_history_log(spa, buf);
457 		spa_close(spa, FTAG);
458 	}
459 	history_str_free(buf);
460 }
461 
462 /*
463  * Policy for top-level read operations (list pools).  Requires no privileges,
464  * and can be used in the local zone, as there is no associated dataset.
465  */
466 /* ARGSUSED */
467 static int
468 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
469 {
470 	return (0);
471 }
472 
473 /*
474  * Policy for dataset read operations (list children, get statistics).  Requires
475  * no privileges, but must be visible in the local zone.
476  */
477 /* ARGSUSED */
478 static int
479 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
480 {
481 	if (INGLOBALZONE(curproc) ||
482 	    zone_dataset_visible(zc->zc_name, NULL))
483 		return (0);
484 
485 	return (SET_ERROR(ENOENT));
486 }
487 
488 static int
489 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
490 {
491 	int writable = 1;
492 
493 	/*
494 	 * The dataset must be visible by this zone -- check this first
495 	 * so they don't see EPERM on something they shouldn't know about.
496 	 */
497 	if (!INGLOBALZONE(curproc) &&
498 	    !zone_dataset_visible(dataset, &writable))
499 		return (SET_ERROR(ENOENT));
500 
501 	if (INGLOBALZONE(curproc)) {
502 		/*
503 		 * If the fs is zoned, only root can access it from the
504 		 * global zone.
505 		 */
506 		if (secpolicy_zfs(cr) && zoned)
507 			return (SET_ERROR(EPERM));
508 	} else {
509 		/*
510 		 * If we are in a local zone, the 'zoned' property must be set.
511 		 */
512 		if (!zoned)
513 			return (SET_ERROR(EPERM));
514 
515 		/* must be writable by this zone */
516 		if (!writable)
517 			return (SET_ERROR(EPERM));
518 	}
519 	return (0);
520 }
521 
522 static int
523 zfs_dozonecheck(const char *dataset, cred_t *cr)
524 {
525 	uint64_t zoned;
526 
527 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
528 		return (SET_ERROR(ENOENT));
529 
530 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
531 }
532 
533 static int
534 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
535 {
536 	uint64_t zoned;
537 
538 	if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
539 		return (SET_ERROR(ENOENT));
540 
541 	return (zfs_dozonecheck_impl(dataset, zoned, cr));
542 }
543 
544 static int
545 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
546     const char *perm, cred_t *cr)
547 {
548 	int error;
549 
550 	error = zfs_dozonecheck_ds(name, ds, cr);
551 	if (error == 0) {
552 		error = secpolicy_zfs(cr);
553 		if (error != 0)
554 			error = dsl_deleg_access_impl(ds, perm, cr);
555 	}
556 	return (error);
557 }
558 
559 static int
560 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
561 {
562 	int error;
563 	dsl_dataset_t *ds;
564 	dsl_pool_t *dp;
565 
566 	/*
567 	 * First do a quick check for root in the global zone, which
568 	 * is allowed to do all write_perms.  This ensures that zfs_ioc_*
569 	 * will get to handle nonexistent datasets.
570 	 */
571 	if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
572 		return (0);
573 
574 	error = dsl_pool_hold(name, FTAG, &dp);
575 	if (error != 0)
576 		return (error);
577 
578 	error = dsl_dataset_hold(dp, name, FTAG, &ds);
579 	if (error != 0) {
580 		dsl_pool_rele(dp, FTAG);
581 		return (error);
582 	}
583 
584 	error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
585 
586 	dsl_dataset_rele(ds, FTAG);
587 	dsl_pool_rele(dp, FTAG);
588 	return (error);
589 }
590 
591 /*
592  * Policy for setting the security label property.
593  *
594  * Returns 0 for success, non-zero for access and other errors.
595  */
596 static int
597 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
598 {
599 	char		ds_hexsl[MAXNAMELEN];
600 	bslabel_t	ds_sl, new_sl;
601 	boolean_t	new_default = FALSE;
602 	uint64_t	zoned;
603 	int		needed_priv = -1;
604 	int		error;
605 
606 	/* First get the existing dataset label. */
607 	error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
608 	    1, sizeof (ds_hexsl), &ds_hexsl, NULL);
609 	if (error != 0)
610 		return (SET_ERROR(EPERM));
611 
612 	if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
613 		new_default = TRUE;
614 
615 	/* The label must be translatable */
616 	if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
617 		return (SET_ERROR(EINVAL));
618 
619 	/*
620 	 * In a non-global zone, disallow attempts to set a label that
621 	 * doesn't match that of the zone; otherwise no other checks
622 	 * are needed.
623 	 */
624 	if (!INGLOBALZONE(curproc)) {
625 		if (new_default || !blequal(&new_sl, CR_SL(CRED())))
626 			return (SET_ERROR(EPERM));
627 		return (0);
628 	}
629 
630 	/*
631 	 * For global-zone datasets (i.e., those whose zoned property is
632 	 * "off", verify that the specified new label is valid for the
633 	 * global zone.
634 	 */
635 	if (dsl_prop_get_integer(name,
636 	    zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
637 		return (SET_ERROR(EPERM));
638 	if (!zoned) {
639 		if (zfs_check_global_label(name, strval) != 0)
640 			return (SET_ERROR(EPERM));
641 	}
642 
643 	/*
644 	 * If the existing dataset label is nondefault, check if the
645 	 * dataset is mounted (label cannot be changed while mounted).
646 	 * Get the zfsvfs; if there isn't one, then the dataset isn't
647 	 * mounted (or isn't a dataset, doesn't exist, ...).
648 	 */
649 	if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
650 		objset_t *os;
651 		static char *setsl_tag = "setsl_tag";
652 
653 		/*
654 		 * Try to own the dataset; abort if there is any error,
655 		 * (e.g., already mounted, in use, or other error).
656 		 */
657 		error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
658 		    setsl_tag, &os);
659 		if (error != 0)
660 			return (SET_ERROR(EPERM));
661 
662 		dmu_objset_disown(os, B_TRUE, setsl_tag);
663 
664 		if (new_default) {
665 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
666 			goto out_check;
667 		}
668 
669 		if (hexstr_to_label(strval, &new_sl) != 0)
670 			return (SET_ERROR(EPERM));
671 
672 		if (blstrictdom(&ds_sl, &new_sl))
673 			needed_priv = PRIV_FILE_DOWNGRADE_SL;
674 		else if (blstrictdom(&new_sl, &ds_sl))
675 			needed_priv = PRIV_FILE_UPGRADE_SL;
676 	} else {
677 		/* dataset currently has a default label */
678 		if (!new_default)
679 			needed_priv = PRIV_FILE_UPGRADE_SL;
680 	}
681 
682 out_check:
683 	if (needed_priv != -1)
684 		return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
685 	return (0);
686 }
687 
688 static int
689 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
690     cred_t *cr)
691 {
692 	char *strval;
693 
694 	/*
695 	 * Check permissions for special properties.
696 	 */
697 	switch (prop) {
698 	case ZFS_PROP_ZONED:
699 		/*
700 		 * Disallow setting of 'zoned' from within a local zone.
701 		 */
702 		if (!INGLOBALZONE(curproc))
703 			return (SET_ERROR(EPERM));
704 		break;
705 
706 	case ZFS_PROP_QUOTA:
707 	case ZFS_PROP_FILESYSTEM_LIMIT:
708 	case ZFS_PROP_SNAPSHOT_LIMIT:
709 		if (!INGLOBALZONE(curproc)) {
710 			uint64_t zoned;
711 			char setpoint[ZFS_MAX_DATASET_NAME_LEN];
712 			/*
713 			 * Unprivileged users are allowed to modify the
714 			 * limit on things *under* (ie. contained by)
715 			 * the thing they own.
716 			 */
717 			if (dsl_prop_get_integer(dsname, "zoned", &zoned,
718 			    setpoint))
719 				return (SET_ERROR(EPERM));
720 			if (!zoned || strlen(dsname) <= strlen(setpoint))
721 				return (SET_ERROR(EPERM));
722 		}
723 		break;
724 
725 	case ZFS_PROP_MLSLABEL:
726 		if (!is_system_labeled())
727 			return (SET_ERROR(EPERM));
728 
729 		if (nvpair_value_string(propval, &strval) == 0) {
730 			int err;
731 
732 			err = zfs_set_slabel_policy(dsname, strval, CRED());
733 			if (err != 0)
734 				return (err);
735 		}
736 		break;
737 	}
738 
739 	return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
740 }
741 
742 /* ARGSUSED */
743 static int
744 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
745 {
746 	int error;
747 
748 	error = zfs_dozonecheck(zc->zc_name, cr);
749 	if (error != 0)
750 		return (error);
751 
752 	/*
753 	 * permission to set permissions will be evaluated later in
754 	 * dsl_deleg_can_allow()
755 	 */
756 	return (0);
757 }
758 
759 /* ARGSUSED */
760 static int
761 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
762 {
763 	return (zfs_secpolicy_write_perms(zc->zc_name,
764 	    ZFS_DELEG_PERM_ROLLBACK, cr));
765 }
766 
767 /* ARGSUSED */
768 static int
769 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
770 {
771 	dsl_pool_t *dp;
772 	dsl_dataset_t *ds;
773 	char *cp;
774 	int error;
775 
776 	/*
777 	 * Generate the current snapshot name from the given objsetid, then
778 	 * use that name for the secpolicy/zone checks.
779 	 */
780 	cp = strchr(zc->zc_name, '@');
781 	if (cp == NULL)
782 		return (SET_ERROR(EINVAL));
783 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
784 	if (error != 0)
785 		return (error);
786 
787 	error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
788 	if (error != 0) {
789 		dsl_pool_rele(dp, FTAG);
790 		return (error);
791 	}
792 
793 	dsl_dataset_name(ds, zc->zc_name);
794 
795 	error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
796 	    ZFS_DELEG_PERM_SEND, cr);
797 	dsl_dataset_rele(ds, FTAG);
798 	dsl_pool_rele(dp, FTAG);
799 
800 	return (error);
801 }
802 
803 /* ARGSUSED */
804 static int
805 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
806 {
807 	return (zfs_secpolicy_write_perms(zc->zc_name,
808 	    ZFS_DELEG_PERM_SEND, cr));
809 }
810 
811 /* ARGSUSED */
812 static int
813 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
814 {
815 	vnode_t *vp;
816 	int error;
817 
818 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
819 	    NO_FOLLOW, NULL, &vp)) != 0)
820 		return (error);
821 
822 	/* Now make sure mntpnt and dataset are ZFS */
823 
824 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
825 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
826 	    zc->zc_name) != 0)) {
827 		VN_RELE(vp);
828 		return (SET_ERROR(EPERM));
829 	}
830 
831 	VN_RELE(vp);
832 	return (dsl_deleg_access(zc->zc_name,
833 	    ZFS_DELEG_PERM_SHARE, cr));
834 }
835 
836 int
837 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
838 {
839 	if (secpolicy_nfs(cr) == 0) {
840 		return (0);
841 	} else {
842 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
843 	}
844 }
845 
846 int
847 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
848 {
849 	if (secpolicy_smb(cr) == 0) {
850 		return (0);
851 	} else {
852 		return (zfs_secpolicy_deleg_share(zc, innvl, cr));
853 	}
854 }
855 
856 static int
857 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
858 {
859 	char *cp;
860 
861 	/*
862 	 * Remove the @bla or /bla from the end of the name to get the parent.
863 	 */
864 	(void) strncpy(parent, datasetname, parentsize);
865 	cp = strrchr(parent, '@');
866 	if (cp != NULL) {
867 		cp[0] = '\0';
868 	} else {
869 		cp = strrchr(parent, '/');
870 		if (cp == NULL)
871 			return (SET_ERROR(ENOENT));
872 		cp[0] = '\0';
873 	}
874 
875 	return (0);
876 }
877 
878 int
879 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
880 {
881 	int error;
882 
883 	if ((error = zfs_secpolicy_write_perms(name,
884 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
885 		return (error);
886 
887 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
888 }
889 
890 /* ARGSUSED */
891 static int
892 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
893 {
894 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
895 }
896 
897 /*
898  * Destroying snapshots with delegated permissions requires
899  * descendant mount and destroy permissions.
900  */
901 /* ARGSUSED */
902 static int
903 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
904 {
905 	nvlist_t *snaps;
906 	nvpair_t *pair, *nextpair;
907 	int error = 0;
908 
909 	snaps = fnvlist_lookup_nvlist(innvl, "snaps");
910 
911 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
912 	    pair = nextpair) {
913 		nextpair = nvlist_next_nvpair(snaps, pair);
914 		error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
915 		if (error == ENOENT) {
916 			/*
917 			 * Ignore any snapshots that don't exist (we consider
918 			 * them "already destroyed").  Remove the name from the
919 			 * nvl here in case the snapshot is created between
920 			 * now and when we try to destroy it (in which case
921 			 * we don't want to destroy it since we haven't
922 			 * checked for permission).
923 			 */
924 			fnvlist_remove_nvpair(snaps, pair);
925 			error = 0;
926 		}
927 		if (error != 0)
928 			break;
929 	}
930 
931 	return (error);
932 }
933 
934 int
935 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
936 {
937 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
938 	int	error;
939 
940 	if ((error = zfs_secpolicy_write_perms(from,
941 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
942 		return (error);
943 
944 	if ((error = zfs_secpolicy_write_perms(from,
945 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
946 		return (error);
947 
948 	if ((error = zfs_get_parent(to, parentname,
949 	    sizeof (parentname))) != 0)
950 		return (error);
951 
952 	if ((error = zfs_secpolicy_write_perms(parentname,
953 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
954 		return (error);
955 
956 	if ((error = zfs_secpolicy_write_perms(parentname,
957 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
958 		return (error);
959 
960 	return (error);
961 }
962 
963 /* ARGSUSED */
964 static int
965 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
966 {
967 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
968 }
969 
970 /* ARGSUSED */
971 static int
972 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
973 {
974 	dsl_pool_t *dp;
975 	dsl_dataset_t *clone;
976 	int error;
977 
978 	error = zfs_secpolicy_write_perms(zc->zc_name,
979 	    ZFS_DELEG_PERM_PROMOTE, cr);
980 	if (error != 0)
981 		return (error);
982 
983 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
984 	if (error != 0)
985 		return (error);
986 
987 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
988 
989 	if (error == 0) {
990 		char parentname[ZFS_MAX_DATASET_NAME_LEN];
991 		dsl_dataset_t *origin = NULL;
992 		dsl_dir_t *dd;
993 		dd = clone->ds_dir;
994 
995 		error = dsl_dataset_hold_obj(dd->dd_pool,
996 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
997 		if (error != 0) {
998 			dsl_dataset_rele(clone, FTAG);
999 			dsl_pool_rele(dp, FTAG);
1000 			return (error);
1001 		}
1002 
1003 		error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
1004 		    ZFS_DELEG_PERM_MOUNT, cr);
1005 
1006 		dsl_dataset_name(origin, parentname);
1007 		if (error == 0) {
1008 			error = zfs_secpolicy_write_perms_ds(parentname, origin,
1009 			    ZFS_DELEG_PERM_PROMOTE, cr);
1010 		}
1011 		dsl_dataset_rele(clone, FTAG);
1012 		dsl_dataset_rele(origin, FTAG);
1013 	}
1014 	dsl_pool_rele(dp, FTAG);
1015 	return (error);
1016 }
1017 
1018 /* ARGSUSED */
1019 static int
1020 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1021 {
1022 	int error;
1023 
1024 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1025 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
1026 		return (error);
1027 
1028 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1029 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1030 		return (error);
1031 
1032 	return (zfs_secpolicy_write_perms(zc->zc_name,
1033 	    ZFS_DELEG_PERM_CREATE, cr));
1034 }
1035 
1036 int
1037 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1038 {
1039 	return (zfs_secpolicy_write_perms(name,
1040 	    ZFS_DELEG_PERM_SNAPSHOT, cr));
1041 }
1042 
1043 /*
1044  * Check for permission to create each snapshot in the nvlist.
1045  */
1046 /* ARGSUSED */
1047 static int
1048 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1049 {
1050 	nvlist_t *snaps;
1051 	int error = 0;
1052 	nvpair_t *pair;
1053 
1054 	snaps = fnvlist_lookup_nvlist(innvl, "snaps");
1055 
1056 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1057 	    pair = nvlist_next_nvpair(snaps, pair)) {
1058 		char *name = nvpair_name(pair);
1059 		char *atp = strchr(name, '@');
1060 
1061 		if (atp == NULL) {
1062 			error = SET_ERROR(EINVAL);
1063 			break;
1064 		}
1065 		*atp = '\0';
1066 		error = zfs_secpolicy_snapshot_perms(name, cr);
1067 		*atp = '@';
1068 		if (error != 0)
1069 			break;
1070 	}
1071 	return (error);
1072 }
1073 
1074 /*
1075  * Check for permission to create each bookmark in the nvlist.
1076  */
1077 /* ARGSUSED */
1078 static int
1079 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1080 {
1081 	int error = 0;
1082 
1083 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1084 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1085 		char *name = nvpair_name(pair);
1086 		char *hashp = strchr(name, '#');
1087 
1088 		if (hashp == NULL) {
1089 			error = SET_ERROR(EINVAL);
1090 			break;
1091 		}
1092 		*hashp = '\0';
1093 		error = zfs_secpolicy_write_perms(name,
1094 		    ZFS_DELEG_PERM_BOOKMARK, cr);
1095 		*hashp = '#';
1096 		if (error != 0)
1097 			break;
1098 	}
1099 	return (error);
1100 }
1101 
1102 /* ARGSUSED */
1103 static int
1104 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1105 {
1106 	return (zfs_secpolicy_write_perms(zc->zc_name,
1107 	    ZFS_DELEG_PERM_REMAP, cr));
1108 }
1109 
1110 /* ARGSUSED */
1111 static int
1112 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1113 {
1114 	nvpair_t *pair, *nextpair;
1115 	int error = 0;
1116 
1117 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1118 	    pair = nextpair) {
1119 		char *name = nvpair_name(pair);
1120 		char *hashp = strchr(name, '#');
1121 		nextpair = nvlist_next_nvpair(innvl, pair);
1122 
1123 		if (hashp == NULL) {
1124 			error = SET_ERROR(EINVAL);
1125 			break;
1126 		}
1127 
1128 		*hashp = '\0';
1129 		error = zfs_secpolicy_write_perms(name,
1130 		    ZFS_DELEG_PERM_DESTROY, cr);
1131 		*hashp = '#';
1132 		if (error == ENOENT) {
1133 			/*
1134 			 * Ignore any filesystems that don't exist (we consider
1135 			 * their bookmarks "already destroyed").  Remove
1136 			 * the name from the nvl here in case the filesystem
1137 			 * is created between now and when we try to destroy
1138 			 * the bookmark (in which case we don't want to
1139 			 * destroy it since we haven't checked for permission).
1140 			 */
1141 			fnvlist_remove_nvpair(innvl, pair);
1142 			error = 0;
1143 		}
1144 		if (error != 0)
1145 			break;
1146 	}
1147 
1148 	return (error);
1149 }
1150 
1151 /* ARGSUSED */
1152 static int
1153 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1154 {
1155 	/*
1156 	 * Even root must have a proper TSD so that we know what pool
1157 	 * to log to.
1158 	 */
1159 	if (tsd_get(zfs_allow_log_key) == NULL)
1160 		return (SET_ERROR(EPERM));
1161 	return (0);
1162 }
1163 
1164 static int
1165 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1166 {
1167 	char	parentname[ZFS_MAX_DATASET_NAME_LEN];
1168 	int	error;
1169 	char	*origin;
1170 
1171 	if ((error = zfs_get_parent(zc->zc_name, parentname,
1172 	    sizeof (parentname))) != 0)
1173 		return (error);
1174 
1175 	if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1176 	    (error = zfs_secpolicy_write_perms(origin,
1177 	    ZFS_DELEG_PERM_CLONE, cr)) != 0)
1178 		return (error);
1179 
1180 	if ((error = zfs_secpolicy_write_perms(parentname,
1181 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
1182 		return (error);
1183 
1184 	return (zfs_secpolicy_write_perms(parentname,
1185 	    ZFS_DELEG_PERM_MOUNT, cr));
1186 }
1187 
1188 /*
1189  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
1190  * SYS_CONFIG privilege, which is not available in a local zone.
1191  */
1192 /* ARGSUSED */
1193 static int
1194 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1195 {
1196 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
1197 		return (SET_ERROR(EPERM));
1198 
1199 	return (0);
1200 }
1201 
1202 /*
1203  * Policy for object to name lookups.
1204  */
1205 /* ARGSUSED */
1206 static int
1207 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1208 {
1209 	int error;
1210 
1211 	if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1212 		return (0);
1213 
1214 	error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1215 	return (error);
1216 }
1217 
1218 /*
1219  * Policy for fault injection.  Requires all privileges.
1220  */
1221 /* ARGSUSED */
1222 static int
1223 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1224 {
1225 	return (secpolicy_zinject(cr));
1226 }
1227 
1228 /* ARGSUSED */
1229 static int
1230 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1231 {
1232 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1233 
1234 	if (prop == ZPROP_INVAL) {
1235 		if (!zfs_prop_user(zc->zc_value))
1236 			return (SET_ERROR(EINVAL));
1237 		return (zfs_secpolicy_write_perms(zc->zc_name,
1238 		    ZFS_DELEG_PERM_USERPROP, cr));
1239 	} else {
1240 		return (zfs_secpolicy_setprop(zc->zc_name, prop,
1241 		    NULL, cr));
1242 	}
1243 }
1244 
1245 static int
1246 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1247 {
1248 	int err = zfs_secpolicy_read(zc, innvl, cr);
1249 	if (err)
1250 		return (err);
1251 
1252 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1253 		return (SET_ERROR(EINVAL));
1254 
1255 	if (zc->zc_value[0] == 0) {
1256 		/*
1257 		 * They are asking about a posix uid/gid.  If it's
1258 		 * themself, allow it.
1259 		 */
1260 		if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1261 		    zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1262 		    zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1263 		    zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1264 			if (zc->zc_guid == crgetuid(cr))
1265 				return (0);
1266 		} else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1267 		    zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1268 		    zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1269 		    zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1270 			if (groupmember(zc->zc_guid, cr))
1271 				return (0);
1272 		}
1273 		/* else is for project quota/used */
1274 	}
1275 
1276 	return (zfs_secpolicy_write_perms(zc->zc_name,
1277 	    userquota_perms[zc->zc_objset_type], cr));
1278 }
1279 
1280 static int
1281 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1282 {
1283 	int err = zfs_secpolicy_read(zc, innvl, cr);
1284 	if (err)
1285 		return (err);
1286 
1287 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1288 		return (SET_ERROR(EINVAL));
1289 
1290 	return (zfs_secpolicy_write_perms(zc->zc_name,
1291 	    userquota_perms[zc->zc_objset_type], cr));
1292 }
1293 
1294 /* ARGSUSED */
1295 static int
1296 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1297 {
1298 	return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1299 	    NULL, cr));
1300 }
1301 
1302 /* ARGSUSED */
1303 static int
1304 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1305 {
1306 	nvpair_t *pair;
1307 	nvlist_t *holds;
1308 	int error;
1309 
1310 	holds = fnvlist_lookup_nvlist(innvl, "holds");
1311 
1312 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1313 	    pair = nvlist_next_nvpair(holds, pair)) {
1314 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
1315 		error = dmu_fsname(nvpair_name(pair), fsname);
1316 		if (error != 0)
1317 			return (error);
1318 		error = zfs_secpolicy_write_perms(fsname,
1319 		    ZFS_DELEG_PERM_HOLD, cr);
1320 		if (error != 0)
1321 			return (error);
1322 	}
1323 	return (0);
1324 }
1325 
1326 /* ARGSUSED */
1327 static int
1328 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1329 {
1330 	nvpair_t *pair;
1331 	int error;
1332 
1333 	for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1334 	    pair = nvlist_next_nvpair(innvl, pair)) {
1335 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
1336 		error = dmu_fsname(nvpair_name(pair), fsname);
1337 		if (error != 0)
1338 			return (error);
1339 		error = zfs_secpolicy_write_perms(fsname,
1340 		    ZFS_DELEG_PERM_RELEASE, cr);
1341 		if (error != 0)
1342 			return (error);
1343 	}
1344 	return (0);
1345 }
1346 
1347 /* ARGSUSED */
1348 static int
1349 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1350 {
1351 	return (zfs_secpolicy_write_perms(zc->zc_name,
1352 	    ZFS_DELEG_PERM_LOAD_KEY, cr));
1353 }
1354 
1355 /* ARGSUSED */
1356 static int
1357 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1358 {
1359 	return (zfs_secpolicy_write_perms(zc->zc_name,
1360 	    ZFS_DELEG_PERM_CHANGE_KEY, cr));
1361 }
1362 
1363 /*
1364  * Policy for allowing temporary snapshots to be taken or released
1365  */
1366 static int
1367 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1368 {
1369 	/*
1370 	 * A temporary snapshot is the same as a snapshot,
1371 	 * hold, destroy and release all rolled into one.
1372 	 * Delegated diff alone is sufficient that we allow this.
1373 	 */
1374 	int error;
1375 
1376 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1377 	    ZFS_DELEG_PERM_DIFF, cr)) == 0)
1378 		return (0);
1379 
1380 	error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1381 
1382 	if (innvl != NULL) {
1383 		if (error == 0)
1384 			error = zfs_secpolicy_hold(zc, innvl, cr);
1385 		if (error == 0)
1386 			error = zfs_secpolicy_release(zc, innvl, cr);
1387 		if (error == 0)
1388 			error = zfs_secpolicy_destroy(zc, innvl, cr);
1389 	}
1390 	return (error);
1391 }
1392 
1393 /*
1394  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1395  */
1396 static int
1397 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1398 {
1399 	char *packed;
1400 	int error;
1401 	nvlist_t *list = NULL;
1402 
1403 	/*
1404 	 * Read in and unpack the user-supplied nvlist.
1405 	 */
1406 	if (size == 0)
1407 		return (SET_ERROR(EINVAL));
1408 
1409 	packed = kmem_alloc(size, KM_SLEEP);
1410 
1411 	if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1412 	    iflag)) != 0) {
1413 		kmem_free(packed, size);
1414 		return (SET_ERROR(EFAULT));
1415 	}
1416 
1417 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1418 		kmem_free(packed, size);
1419 		return (error);
1420 	}
1421 
1422 	kmem_free(packed, size);
1423 
1424 	*nvp = list;
1425 	return (0);
1426 }
1427 
1428 /*
1429  * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1430  * Entries will be removed from the end of the nvlist, and one int32 entry
1431  * named "N_MORE_ERRORS" will be added indicating how many entries were
1432  * removed.
1433  */
1434 static int
1435 nvlist_smush(nvlist_t *errors, size_t max)
1436 {
1437 	size_t size;
1438 
1439 	size = fnvlist_size(errors);
1440 
1441 	if (size > max) {
1442 		nvpair_t *more_errors;
1443 		int n = 0;
1444 
1445 		if (max < 1024)
1446 			return (SET_ERROR(ENOMEM));
1447 
1448 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1449 		more_errors = nvlist_prev_nvpair(errors, NULL);
1450 
1451 		do {
1452 			nvpair_t *pair = nvlist_prev_nvpair(errors,
1453 			    more_errors);
1454 			fnvlist_remove_nvpair(errors, pair);
1455 			n++;
1456 			size = fnvlist_size(errors);
1457 		} while (size > max);
1458 
1459 		fnvlist_remove_nvpair(errors, more_errors);
1460 		fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1461 		ASSERT3U(fnvlist_size(errors), <=, max);
1462 	}
1463 
1464 	return (0);
1465 }
1466 
1467 static int
1468 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1469 {
1470 	char *packed = NULL;
1471 	int error = 0;
1472 	size_t size;
1473 
1474 	size = fnvlist_size(nvl);
1475 
1476 	if (size > zc->zc_nvlist_dst_size) {
1477 		error = SET_ERROR(ENOMEM);
1478 	} else {
1479 		packed = fnvlist_pack(nvl, &size);
1480 		if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1481 		    size, zc->zc_iflags) != 0)
1482 			error = SET_ERROR(EFAULT);
1483 		fnvlist_pack_free(packed, size);
1484 	}
1485 
1486 	zc->zc_nvlist_dst_size = size;
1487 	zc->zc_nvlist_dst_filled = B_TRUE;
1488 	return (error);
1489 }
1490 
1491 int
1492 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1493 {
1494 	int error = 0;
1495 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1496 		return (SET_ERROR(EINVAL));
1497 	}
1498 
1499 	mutex_enter(&os->os_user_ptr_lock);
1500 	*zfvp = dmu_objset_get_user(os);
1501 	if (*zfvp) {
1502 		VFS_HOLD((*zfvp)->z_vfs);
1503 	} else {
1504 		error = SET_ERROR(ESRCH);
1505 	}
1506 	mutex_exit(&os->os_user_ptr_lock);
1507 	return (error);
1508 }
1509 
1510 int
1511 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1512 {
1513 	objset_t *os;
1514 	int error;
1515 
1516 	error = dmu_objset_hold(dsname, FTAG, &os);
1517 	if (error != 0)
1518 		return (error);
1519 
1520 	error = getzfsvfs_impl(os, zfvp);
1521 	dmu_objset_rele(os, FTAG);
1522 	return (error);
1523 }
1524 
1525 /*
1526  * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1527  * case its z_vfs will be NULL, and it will be opened as the owner.
1528  * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1529  * which prevents all vnode ops from running.
1530  */
1531 static int
1532 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1533 {
1534 	int error = 0;
1535 
1536 	if (getzfsvfs(name, zfvp) != 0)
1537 		error = zfsvfs_create(name, B_FALSE, zfvp);
1538 	if (error == 0) {
1539 		rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1540 		    RW_READER, tag);
1541 		if ((*zfvp)->z_unmounted) {
1542 			/*
1543 			 * XXX we could probably try again, since the unmounting
1544 			 * thread should be just about to disassociate the
1545 			 * objset from the zfsvfs.
1546 			 */
1547 			rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1548 			return (SET_ERROR(EBUSY));
1549 		}
1550 	}
1551 	return (error);
1552 }
1553 
1554 static void
1555 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1556 {
1557 	rrm_exit(&zfsvfs->z_teardown_lock, tag);
1558 
1559 	if (zfsvfs->z_vfs) {
1560 		VFS_RELE(zfsvfs->z_vfs);
1561 	} else {
1562 		dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1563 		zfsvfs_free(zfsvfs);
1564 	}
1565 }
1566 
1567 static int
1568 zfs_ioc_pool_create(zfs_cmd_t *zc)
1569 {
1570 	int error;
1571 	nvlist_t *config, *props = NULL;
1572 	nvlist_t *rootprops = NULL;
1573 	nvlist_t *zplprops = NULL;
1574 	char *spa_name = zc->zc_name;
1575 	dsl_crypto_params_t *dcp = NULL;
1576 
1577 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1578 	    zc->zc_iflags, &config))
1579 		return (error);
1580 
1581 	if (zc->zc_nvlist_src_size != 0 && (error =
1582 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1583 	    zc->zc_iflags, &props))) {
1584 		nvlist_free(config);
1585 		return (error);
1586 	}
1587 
1588 	if (props) {
1589 		nvlist_t *nvl = NULL;
1590 		nvlist_t *hidden_args = NULL;
1591 		uint64_t version = SPA_VERSION;
1592 		char *tname;
1593 
1594 		(void) nvlist_lookup_uint64(props,
1595 		    zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1596 		if (!SPA_VERSION_IS_SUPPORTED(version)) {
1597 			error = SET_ERROR(EINVAL);
1598 			goto pool_props_bad;
1599 		}
1600 		(void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1601 		if (nvl) {
1602 			error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1603 			if (error != 0) {
1604 				nvlist_free(config);
1605 				nvlist_free(props);
1606 				return (error);
1607 			}
1608 			(void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1609 		}
1610 
1611 		(void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1612 		    &hidden_args);
1613 		error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1614 		    rootprops, hidden_args, &dcp);
1615 		if (error != 0) {
1616 			nvlist_free(config);
1617 			nvlist_free(props);
1618 			return (error);
1619 		}
1620 		(void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1621 
1622 		VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1623 		error = zfs_fill_zplprops_root(version, rootprops,
1624 		    zplprops, NULL);
1625 		if (error != 0)
1626 			goto pool_props_bad;
1627 
1628 		if (nvlist_lookup_string(props,
1629 		    zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1630 			spa_name = tname;
1631 	}
1632 
1633 	error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1634 
1635 	/*
1636 	 * Set the remaining root properties
1637 	 */
1638 	if (!error && (error = zfs_set_prop_nvlist(spa_name,
1639 	    ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1640 		(void) spa_destroy(spa_name);
1641 
1642 pool_props_bad:
1643 	nvlist_free(rootprops);
1644 	nvlist_free(zplprops);
1645 	nvlist_free(config);
1646 	nvlist_free(props);
1647 	dsl_crypto_params_free(dcp, !!error);
1648 
1649 	return (error);
1650 }
1651 
1652 static int
1653 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1654 {
1655 	int error;
1656 	zfs_log_history(zc);
1657 	error = spa_destroy(zc->zc_name);
1658 	if (error == 0)
1659 		zvol_remove_minors(zc->zc_name);
1660 	return (error);
1661 }
1662 
1663 static int
1664 zfs_ioc_pool_import(zfs_cmd_t *zc)
1665 {
1666 	nvlist_t *config, *props = NULL;
1667 	uint64_t guid;
1668 	int error;
1669 
1670 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1671 	    zc->zc_iflags, &config)) != 0)
1672 		return (error);
1673 
1674 	if (zc->zc_nvlist_src_size != 0 && (error =
1675 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1676 	    zc->zc_iflags, &props))) {
1677 		nvlist_free(config);
1678 		return (error);
1679 	}
1680 
1681 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1682 	    guid != zc->zc_guid)
1683 		error = SET_ERROR(EINVAL);
1684 	else
1685 		error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1686 
1687 	if (zc->zc_nvlist_dst != 0) {
1688 		int err;
1689 
1690 		if ((err = put_nvlist(zc, config)) != 0)
1691 			error = err;
1692 	}
1693 
1694 	nvlist_free(config);
1695 
1696 	nvlist_free(props);
1697 
1698 	return (error);
1699 }
1700 
1701 static int
1702 zfs_ioc_pool_export(zfs_cmd_t *zc)
1703 {
1704 	int error;
1705 	boolean_t force = (boolean_t)zc->zc_cookie;
1706 	boolean_t hardforce = (boolean_t)zc->zc_guid;
1707 
1708 	zfs_log_history(zc);
1709 	error = spa_export(zc->zc_name, NULL, force, hardforce);
1710 	if (error == 0)
1711 		zvol_remove_minors(zc->zc_name);
1712 	return (error);
1713 }
1714 
1715 static int
1716 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1717 {
1718 	nvlist_t *configs;
1719 	int error;
1720 
1721 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1722 		return (SET_ERROR(EEXIST));
1723 
1724 	error = put_nvlist(zc, configs);
1725 
1726 	nvlist_free(configs);
1727 
1728 	return (error);
1729 }
1730 
1731 /*
1732  * inputs:
1733  * zc_name		name of the pool
1734  *
1735  * outputs:
1736  * zc_cookie		real errno
1737  * zc_nvlist_dst	config nvlist
1738  * zc_nvlist_dst_size	size of config nvlist
1739  */
1740 static int
1741 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1742 {
1743 	nvlist_t *config;
1744 	int error;
1745 	int ret = 0;
1746 
1747 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1748 	    sizeof (zc->zc_value));
1749 
1750 	if (config != NULL) {
1751 		ret = put_nvlist(zc, config);
1752 		nvlist_free(config);
1753 
1754 		/*
1755 		 * The config may be present even if 'error' is non-zero.
1756 		 * In this case we return success, and preserve the real errno
1757 		 * in 'zc_cookie'.
1758 		 */
1759 		zc->zc_cookie = error;
1760 	} else {
1761 		ret = error;
1762 	}
1763 
1764 	return (ret);
1765 }
1766 
1767 /*
1768  * Try to import the given pool, returning pool stats as appropriate so that
1769  * user land knows which devices are available and overall pool health.
1770  */
1771 static int
1772 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1773 {
1774 	nvlist_t *tryconfig, *config;
1775 	int error;
1776 
1777 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1778 	    zc->zc_iflags, &tryconfig)) != 0)
1779 		return (error);
1780 
1781 	config = spa_tryimport(tryconfig);
1782 
1783 	nvlist_free(tryconfig);
1784 
1785 	if (config == NULL)
1786 		return (SET_ERROR(EINVAL));
1787 
1788 	error = put_nvlist(zc, config);
1789 	nvlist_free(config);
1790 
1791 	return (error);
1792 }
1793 
1794 /*
1795  * inputs:
1796  * zc_name              name of the pool
1797  * zc_cookie            scan func (pool_scan_func_t)
1798  * zc_flags             scrub pause/resume flag (pool_scrub_cmd_t)
1799  */
1800 static int
1801 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1802 {
1803 	spa_t *spa;
1804 	int error;
1805 
1806 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1807 		return (error);
1808 
1809 	if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1810 		return (SET_ERROR(EINVAL));
1811 
1812 	if (zc->zc_flags == POOL_SCRUB_PAUSE)
1813 		error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1814 	else if (zc->zc_cookie == POOL_SCAN_NONE)
1815 		error = spa_scan_stop(spa);
1816 	else
1817 		error = spa_scan(spa, zc->zc_cookie);
1818 
1819 	spa_close(spa, FTAG);
1820 
1821 	return (error);
1822 }
1823 
1824 static int
1825 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1826 {
1827 	spa_t *spa;
1828 	int error;
1829 
1830 	error = spa_open(zc->zc_name, &spa, FTAG);
1831 	if (error == 0) {
1832 		spa_freeze(spa);
1833 		spa_close(spa, FTAG);
1834 	}
1835 	return (error);
1836 }
1837 
1838 static int
1839 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1840 {
1841 	spa_t *spa;
1842 	int error;
1843 
1844 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1845 		return (error);
1846 
1847 	if (zc->zc_cookie < spa_version(spa) ||
1848 	    !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1849 		spa_close(spa, FTAG);
1850 		return (SET_ERROR(EINVAL));
1851 	}
1852 
1853 	spa_upgrade(spa, zc->zc_cookie);
1854 	spa_close(spa, FTAG);
1855 
1856 	return (error);
1857 }
1858 
1859 static int
1860 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1861 {
1862 	spa_t *spa;
1863 	char *hist_buf;
1864 	uint64_t size;
1865 	int error;
1866 
1867 	if ((size = zc->zc_history_len) == 0)
1868 		return (SET_ERROR(EINVAL));
1869 
1870 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1871 		return (error);
1872 
1873 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1874 		spa_close(spa, FTAG);
1875 		return (SET_ERROR(ENOTSUP));
1876 	}
1877 
1878 	hist_buf = kmem_alloc(size, KM_SLEEP);
1879 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
1880 	    &zc->zc_history_len, hist_buf)) == 0) {
1881 		error = ddi_copyout(hist_buf,
1882 		    (void *)(uintptr_t)zc->zc_history,
1883 		    zc->zc_history_len, zc->zc_iflags);
1884 	}
1885 
1886 	spa_close(spa, FTAG);
1887 	kmem_free(hist_buf, size);
1888 	return (error);
1889 }
1890 
1891 static int
1892 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1893 {
1894 	spa_t *spa;
1895 	int error;
1896 
1897 	error = spa_open(zc->zc_name, &spa, FTAG);
1898 	if (error == 0) {
1899 		error = spa_change_guid(spa);
1900 		spa_close(spa, FTAG);
1901 	}
1902 	return (error);
1903 }
1904 
1905 static int
1906 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1907 {
1908 	return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1909 }
1910 
1911 /*
1912  * inputs:
1913  * zc_name		name of filesystem
1914  * zc_obj		object to find
1915  *
1916  * outputs:
1917  * zc_value		name of object
1918  */
1919 static int
1920 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1921 {
1922 	objset_t *os;
1923 	int error;
1924 
1925 	/* XXX reading from objset not owned */
1926 	if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1927 	    FTAG, &os)) != 0)
1928 		return (error);
1929 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1930 		dmu_objset_rele_flags(os, B_TRUE, FTAG);
1931 		return (SET_ERROR(EINVAL));
1932 	}
1933 	error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1934 	    sizeof (zc->zc_value));
1935 	dmu_objset_rele_flags(os, B_TRUE, FTAG);
1936 
1937 	return (error);
1938 }
1939 
1940 /*
1941  * inputs:
1942  * zc_name		name of filesystem
1943  * zc_obj		object to find
1944  *
1945  * outputs:
1946  * zc_stat		stats on object
1947  * zc_value		path to object
1948  */
1949 static int
1950 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1951 {
1952 	objset_t *os;
1953 	int error;
1954 
1955 	/* XXX reading from objset not owned */
1956 	if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1957 	    FTAG, &os)) != 0)
1958 		return (error);
1959 	if (dmu_objset_type(os) != DMU_OST_ZFS) {
1960 		dmu_objset_rele_flags(os, B_TRUE, FTAG);
1961 		return (SET_ERROR(EINVAL));
1962 	}
1963 	error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1964 	    sizeof (zc->zc_value));
1965 	dmu_objset_rele_flags(os, B_TRUE, FTAG);
1966 
1967 	return (error);
1968 }
1969 
1970 static int
1971 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1972 {
1973 	spa_t *spa;
1974 	int error;
1975 	nvlist_t *config;
1976 
1977 	error = spa_open(zc->zc_name, &spa, FTAG);
1978 	if (error != 0)
1979 		return (error);
1980 
1981 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1982 	    zc->zc_iflags, &config);
1983 
1984 	if (error == 0) {
1985 		error = spa_vdev_add(spa, config);
1986 		nvlist_free(config);
1987 	}
1988 	spa_close(spa, FTAG);
1989 	return (error);
1990 }
1991 
1992 /*
1993  * inputs:
1994  * zc_name		name of the pool
1995  * zc_guid		guid of vdev to remove
1996  * zc_cookie		cancel removal
1997  */
1998 static int
1999 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
2000 {
2001 	spa_t *spa;
2002 	int error;
2003 
2004 	error = spa_open(zc->zc_name, &spa, FTAG);
2005 	if (error != 0)
2006 		return (error);
2007 	if (zc->zc_cookie != 0) {
2008 		error = spa_vdev_remove_cancel(spa);
2009 	} else {
2010 		error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
2011 	}
2012 	spa_close(spa, FTAG);
2013 	return (error);
2014 }
2015 
2016 static int
2017 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
2018 {
2019 	spa_t *spa;
2020 	int error;
2021 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2022 
2023 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2024 		return (error);
2025 	switch (zc->zc_cookie) {
2026 	case VDEV_STATE_ONLINE:
2027 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
2028 		break;
2029 
2030 	case VDEV_STATE_OFFLINE:
2031 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
2032 		break;
2033 
2034 	case VDEV_STATE_FAULTED:
2035 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2036 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
2037 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2038 
2039 		error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
2040 		break;
2041 
2042 	case VDEV_STATE_DEGRADED:
2043 		if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2044 		    zc->zc_obj != VDEV_AUX_EXTERNAL)
2045 			zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2046 
2047 		error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
2048 		break;
2049 
2050 	default:
2051 		error = SET_ERROR(EINVAL);
2052 	}
2053 	zc->zc_cookie = newstate;
2054 	spa_close(spa, FTAG);
2055 	return (error);
2056 }
2057 
2058 static int
2059 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2060 {
2061 	spa_t *spa;
2062 	int replacing = zc->zc_cookie;
2063 	nvlist_t *config;
2064 	int error;
2065 
2066 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2067 		return (error);
2068 
2069 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2070 	    zc->zc_iflags, &config)) == 0) {
2071 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2072 		nvlist_free(config);
2073 	}
2074 
2075 	spa_close(spa, FTAG);
2076 	return (error);
2077 }
2078 
2079 static int
2080 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2081 {
2082 	spa_t *spa;
2083 	int error;
2084 
2085 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2086 		return (error);
2087 
2088 	error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2089 
2090 	spa_close(spa, FTAG);
2091 	return (error);
2092 }
2093 
2094 static int
2095 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2096 {
2097 	spa_t *spa;
2098 	nvlist_t *config, *props = NULL;
2099 	int error;
2100 	boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2101 
2102 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2103 		return (error);
2104 
2105 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2106 	    zc->zc_iflags, &config)) {
2107 		spa_close(spa, FTAG);
2108 		return (error);
2109 	}
2110 
2111 	if (zc->zc_nvlist_src_size != 0 && (error =
2112 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2113 	    zc->zc_iflags, &props))) {
2114 		spa_close(spa, FTAG);
2115 		nvlist_free(config);
2116 		return (error);
2117 	}
2118 
2119 	error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2120 
2121 	spa_close(spa, FTAG);
2122 
2123 	nvlist_free(config);
2124 	nvlist_free(props);
2125 
2126 	return (error);
2127 }
2128 
2129 static int
2130 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2131 {
2132 	spa_t *spa;
2133 	char *path = zc->zc_value;
2134 	uint64_t guid = zc->zc_guid;
2135 	int error;
2136 
2137 	error = spa_open(zc->zc_name, &spa, FTAG);
2138 	if (error != 0)
2139 		return (error);
2140 
2141 	error = spa_vdev_setpath(spa, guid, path);
2142 	spa_close(spa, FTAG);
2143 	return (error);
2144 }
2145 
2146 static int
2147 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2148 {
2149 	spa_t *spa;
2150 	char *fru = zc->zc_value;
2151 	uint64_t guid = zc->zc_guid;
2152 	int error;
2153 
2154 	error = spa_open(zc->zc_name, &spa, FTAG);
2155 	if (error != 0)
2156 		return (error);
2157 
2158 	error = spa_vdev_setfru(spa, guid, fru);
2159 	spa_close(spa, FTAG);
2160 	return (error);
2161 }
2162 
2163 static int
2164 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2165 {
2166 	int error = 0;
2167 	nvlist_t *nv;
2168 
2169 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2170 
2171 	if (zc->zc_nvlist_dst != 0 &&
2172 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
2173 		dmu_objset_stats(os, nv);
2174 		/*
2175 		 * NB: zvol_get_stats() will read the objset contents,
2176 		 * which we aren't supposed to do with a
2177 		 * DS_MODE_USER hold, because it could be
2178 		 * inconsistent.  So this is a bit of a workaround...
2179 		 * XXX reading with out owning
2180 		 */
2181 		if (!zc->zc_objset_stats.dds_inconsistent &&
2182 		    dmu_objset_type(os) == DMU_OST_ZVOL) {
2183 			error = zvol_get_stats(os, nv);
2184 			if (error == EIO)
2185 				return (error);
2186 			VERIFY0(error);
2187 		}
2188 		error = put_nvlist(zc, nv);
2189 		nvlist_free(nv);
2190 	}
2191 
2192 	return (error);
2193 }
2194 
2195 /*
2196  * inputs:
2197  * zc_name		name of filesystem
2198  * zc_nvlist_dst_size	size of buffer for property nvlist
2199  *
2200  * outputs:
2201  * zc_objset_stats	stats
2202  * zc_nvlist_dst	property nvlist
2203  * zc_nvlist_dst_size	size of property nvlist
2204  */
2205 static int
2206 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2207 {
2208 	objset_t *os;
2209 	int error;
2210 
2211 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2212 	if (error == 0) {
2213 		error = zfs_ioc_objset_stats_impl(zc, os);
2214 		dmu_objset_rele(os, FTAG);
2215 	}
2216 
2217 	return (error);
2218 }
2219 
2220 /*
2221  * inputs:
2222  * zc_name		name of filesystem
2223  * zc_nvlist_dst_size	size of buffer for property nvlist
2224  *
2225  * outputs:
2226  * zc_nvlist_dst	received property nvlist
2227  * zc_nvlist_dst_size	size of received property nvlist
2228  *
2229  * Gets received properties (distinct from local properties on or after
2230  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2231  * local property values.
2232  */
2233 static int
2234 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2235 {
2236 	int error = 0;
2237 	nvlist_t *nv;
2238 
2239 	/*
2240 	 * Without this check, we would return local property values if the
2241 	 * caller has not already received properties on or after
2242 	 * SPA_VERSION_RECVD_PROPS.
2243 	 */
2244 	if (!dsl_prop_get_hasrecvd(zc->zc_name))
2245 		return (SET_ERROR(ENOTSUP));
2246 
2247 	if (zc->zc_nvlist_dst != 0 &&
2248 	    (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2249 		error = put_nvlist(zc, nv);
2250 		nvlist_free(nv);
2251 	}
2252 
2253 	return (error);
2254 }
2255 
2256 static int
2257 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2258 {
2259 	uint64_t value;
2260 	int error;
2261 
2262 	/*
2263 	 * zfs_get_zplprop() will either find a value or give us
2264 	 * the default value (if there is one).
2265 	 */
2266 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2267 		return (error);
2268 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2269 	return (0);
2270 }
2271 
2272 /*
2273  * inputs:
2274  * zc_name		name of filesystem
2275  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
2276  *
2277  * outputs:
2278  * zc_nvlist_dst	zpl property nvlist
2279  * zc_nvlist_dst_size	size of zpl property nvlist
2280  */
2281 static int
2282 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2283 {
2284 	objset_t *os;
2285 	int err;
2286 
2287 	/* XXX reading without owning */
2288 	if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2289 		return (err);
2290 
2291 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2292 
2293 	/*
2294 	 * NB: nvl_add_zplprop() will read the objset contents,
2295 	 * which we aren't supposed to do with a DS_MODE_USER
2296 	 * hold, because it could be inconsistent.
2297 	 */
2298 	if (zc->zc_nvlist_dst != 0 &&
2299 	    !zc->zc_objset_stats.dds_inconsistent &&
2300 	    dmu_objset_type(os) == DMU_OST_ZFS) {
2301 		nvlist_t *nv;
2302 
2303 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2304 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2305 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2306 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2307 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2308 			err = put_nvlist(zc, nv);
2309 		nvlist_free(nv);
2310 	} else {
2311 		err = SET_ERROR(ENOENT);
2312 	}
2313 	dmu_objset_rele(os, FTAG);
2314 	return (err);
2315 }
2316 
2317 static boolean_t
2318 dataset_name_hidden(const char *name)
2319 {
2320 	/*
2321 	 * Skip over datasets that are not visible in this zone,
2322 	 * internal datasets (which have a $ in their name), and
2323 	 * temporary datasets (which have a % in their name).
2324 	 */
2325 	if (strchr(name, '$') != NULL)
2326 		return (B_TRUE);
2327 	if (strchr(name, '%') != NULL)
2328 		return (B_TRUE);
2329 	if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2330 		return (B_TRUE);
2331 	return (B_FALSE);
2332 }
2333 
2334 /*
2335  * inputs:
2336  * zc_name		name of filesystem
2337  * zc_cookie		zap cursor
2338  * zc_nvlist_dst_size	size of buffer for property nvlist
2339  *
2340  * outputs:
2341  * zc_name		name of next filesystem
2342  * zc_cookie		zap cursor
2343  * zc_objset_stats	stats
2344  * zc_nvlist_dst	property nvlist
2345  * zc_nvlist_dst_size	size of property nvlist
2346  */
2347 static int
2348 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2349 {
2350 	objset_t *os;
2351 	int error;
2352 	char *p;
2353 	size_t orig_len = strlen(zc->zc_name);
2354 
2355 top:
2356 	if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2357 		if (error == ENOENT)
2358 			error = SET_ERROR(ESRCH);
2359 		return (error);
2360 	}
2361 
2362 	p = strrchr(zc->zc_name, '/');
2363 	if (p == NULL || p[1] != '\0')
2364 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2365 	p = zc->zc_name + strlen(zc->zc_name);
2366 
2367 	do {
2368 		error = dmu_dir_list_next(os,
2369 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
2370 		    NULL, &zc->zc_cookie);
2371 		if (error == ENOENT)
2372 			error = SET_ERROR(ESRCH);
2373 	} while (error == 0 && dataset_name_hidden(zc->zc_name));
2374 	dmu_objset_rele(os, FTAG);
2375 
2376 	/*
2377 	 * If it's an internal dataset (ie. with a '$' in its name),
2378 	 * don't try to get stats for it, otherwise we'll return ENOENT.
2379 	 */
2380 	if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2381 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2382 		if (error == ENOENT) {
2383 			/* We lost a race with destroy, get the next one. */
2384 			zc->zc_name[orig_len] = '\0';
2385 			goto top;
2386 		}
2387 	}
2388 	return (error);
2389 }
2390 
2391 /*
2392  * inputs:
2393  * zc_name		name of filesystem
2394  * zc_cookie		zap cursor
2395  * zc_nvlist_dst_size	size of buffer for property nvlist
2396  * zc_simple		when set, only name is requested
2397  *
2398  * outputs:
2399  * zc_name		name of next snapshot
2400  * zc_objset_stats	stats
2401  * zc_nvlist_dst	property nvlist
2402  * zc_nvlist_dst_size	size of property nvlist
2403  */
2404 static int
2405 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2406 {
2407 	objset_t *os;
2408 	int error;
2409 
2410 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2411 	if (error != 0) {
2412 		return (error == ENOENT ? ESRCH : error);
2413 	}
2414 
2415 	/*
2416 	 * A dataset name of maximum length cannot have any snapshots,
2417 	 * so exit immediately.
2418 	 */
2419 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2420 	    ZFS_MAX_DATASET_NAME_LEN) {
2421 		dmu_objset_rele(os, FTAG);
2422 		return (SET_ERROR(ESRCH));
2423 	}
2424 
2425 	error = dmu_snapshot_list_next(os,
2426 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
2427 	    zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2428 	    NULL);
2429 
2430 	if (error == 0 && !zc->zc_simple) {
2431 		dsl_dataset_t *ds;
2432 		dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2433 
2434 		error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2435 		if (error == 0) {
2436 			objset_t *ossnap;
2437 
2438 			error = dmu_objset_from_ds(ds, &ossnap);
2439 			if (error == 0)
2440 				error = zfs_ioc_objset_stats_impl(zc, ossnap);
2441 			dsl_dataset_rele(ds, FTAG);
2442 		}
2443 	} else if (error == ENOENT) {
2444 		error = SET_ERROR(ESRCH);
2445 	}
2446 
2447 	dmu_objset_rele(os, FTAG);
2448 	/* if we failed, undo the @ that we tacked on to zc_name */
2449 	if (error != 0)
2450 		*strchr(zc->zc_name, '@') = '\0';
2451 	return (error);
2452 }
2453 
2454 static int
2455 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2456 {
2457 	const char *propname = nvpair_name(pair);
2458 	uint64_t *valary;
2459 	unsigned int vallen;
2460 	const char *domain;
2461 	char *dash;
2462 	zfs_userquota_prop_t type;
2463 	uint64_t rid;
2464 	uint64_t quota;
2465 	zfsvfs_t *zfsvfs;
2466 	int err;
2467 
2468 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2469 		nvlist_t *attrs;
2470 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2471 		if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2472 		    &pair) != 0)
2473 			return (SET_ERROR(EINVAL));
2474 	}
2475 
2476 	/*
2477 	 * A correctly constructed propname is encoded as
2478 	 * userquota@<rid>-<domain>.
2479 	 */
2480 	if ((dash = strchr(propname, '-')) == NULL ||
2481 	    nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2482 	    vallen != 3)
2483 		return (SET_ERROR(EINVAL));
2484 
2485 	domain = dash + 1;
2486 	type = valary[0];
2487 	rid = valary[1];
2488 	quota = valary[2];
2489 
2490 	err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2491 	if (err == 0) {
2492 		err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2493 		zfsvfs_rele(zfsvfs, FTAG);
2494 	}
2495 
2496 	return (err);
2497 }
2498 
2499 /*
2500  * If the named property is one that has a special function to set its value,
2501  * return 0 on success and a positive error code on failure; otherwise if it is
2502  * not one of the special properties handled by this function, return -1.
2503  *
2504  * XXX: It would be better for callers of the property interface if we handled
2505  * these special cases in dsl_prop.c (in the dsl layer).
2506  */
2507 static int
2508 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2509     nvpair_t *pair)
2510 {
2511 	const char *propname = nvpair_name(pair);
2512 	zfs_prop_t prop = zfs_name_to_prop(propname);
2513 	uint64_t intval = 0;
2514 	char *strval = NULL;
2515 	int err = -1;
2516 
2517 	if (prop == ZPROP_INVAL) {
2518 		if (zfs_prop_userquota(propname))
2519 			return (zfs_prop_set_userquota(dsname, pair));
2520 		return (-1);
2521 	}
2522 
2523 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2524 		nvlist_t *attrs;
2525 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2526 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2527 		    &pair) == 0);
2528 	}
2529 
2530 	/* all special properties are numeric except for keylocation */
2531 	if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2532 		strval = fnvpair_value_string(pair);
2533 	} else {
2534 		intval = fnvpair_value_uint64(pair);
2535 	}
2536 
2537 	switch (prop) {
2538 	case ZFS_PROP_QUOTA:
2539 		err = dsl_dir_set_quota(dsname, source, intval);
2540 		break;
2541 	case ZFS_PROP_REFQUOTA:
2542 		err = dsl_dataset_set_refquota(dsname, source, intval);
2543 		break;
2544 	case ZFS_PROP_FILESYSTEM_LIMIT:
2545 	case ZFS_PROP_SNAPSHOT_LIMIT:
2546 		if (intval == UINT64_MAX) {
2547 			/* clearing the limit, just do it */
2548 			err = 0;
2549 		} else {
2550 			err = dsl_dir_activate_fs_ss_limit(dsname);
2551 		}
2552 		/*
2553 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2554 		 * default path to set the value in the nvlist.
2555 		 */
2556 		if (err == 0)
2557 			err = -1;
2558 		break;
2559 	case ZFS_PROP_KEYLOCATION:
2560 		err = dsl_crypto_can_set_keylocation(dsname, strval);
2561 
2562 		/*
2563 		 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2564 		 * default path to set the value in the nvlist.
2565 		 */
2566 		if (err == 0)
2567 			err = -1;
2568 		break;
2569 	case ZFS_PROP_RESERVATION:
2570 		err = dsl_dir_set_reservation(dsname, source, intval);
2571 		break;
2572 	case ZFS_PROP_REFRESERVATION:
2573 		err = dsl_dataset_set_refreservation(dsname, source, intval);
2574 		break;
2575 	case ZFS_PROP_VOLSIZE:
2576 		err = zvol_set_volsize(dsname, intval);
2577 		break;
2578 	case ZFS_PROP_VERSION:
2579 	{
2580 		zfsvfs_t *zfsvfs;
2581 
2582 		if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2583 			break;
2584 
2585 		err = zfs_set_version(zfsvfs, intval);
2586 		zfsvfs_rele(zfsvfs, FTAG);
2587 
2588 		if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2589 			zfs_cmd_t *zc;
2590 
2591 			zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2592 			(void) strcpy(zc->zc_name, dsname);
2593 			(void) zfs_ioc_userspace_upgrade(zc);
2594 			(void) zfs_ioc_id_quota_upgrade(zc);
2595 			kmem_free(zc, sizeof (zfs_cmd_t));
2596 		}
2597 		break;
2598 	}
2599 	default:
2600 		err = -1;
2601 	}
2602 
2603 	return (err);
2604 }
2605 
2606 /*
2607  * This function is best effort. If it fails to set any of the given properties,
2608  * it continues to set as many as it can and returns the last error
2609  * encountered. If the caller provides a non-NULL errlist, it will be filled in
2610  * with the list of names of all the properties that failed along with the
2611  * corresponding error numbers.
2612  *
2613  * If every property is set successfully, zero is returned and errlist is not
2614  * modified.
2615  */
2616 int
2617 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2618     nvlist_t *errlist)
2619 {
2620 	nvpair_t *pair;
2621 	nvpair_t *propval;
2622 	int rv = 0;
2623 	uint64_t intval;
2624 	char *strval;
2625 	nvlist_t *genericnvl = fnvlist_alloc();
2626 	nvlist_t *retrynvl = fnvlist_alloc();
2627 
2628 retry:
2629 	pair = NULL;
2630 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2631 		const char *propname = nvpair_name(pair);
2632 		zfs_prop_t prop = zfs_name_to_prop(propname);
2633 		int err = 0;
2634 
2635 		/* decode the property value */
2636 		propval = pair;
2637 		if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2638 			nvlist_t *attrs;
2639 			attrs = fnvpair_value_nvlist(pair);
2640 			if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2641 			    &propval) != 0)
2642 				err = SET_ERROR(EINVAL);
2643 		}
2644 
2645 		/* Validate value type */
2646 		if (err == 0 && source == ZPROP_SRC_INHERITED) {
2647 			/* inherited properties are expected to be booleans */
2648 			if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2649 				err = SET_ERROR(EINVAL);
2650 		} else if (err == 0 && prop == ZPROP_INVAL) {
2651 			if (zfs_prop_user(propname)) {
2652 				if (nvpair_type(propval) != DATA_TYPE_STRING)
2653 					err = SET_ERROR(EINVAL);
2654 			} else if (zfs_prop_userquota(propname)) {
2655 				if (nvpair_type(propval) !=
2656 				    DATA_TYPE_UINT64_ARRAY)
2657 					err = SET_ERROR(EINVAL);
2658 			} else {
2659 				err = SET_ERROR(EINVAL);
2660 			}
2661 		} else if (err == 0) {
2662 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
2663 				if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2664 					err = SET_ERROR(EINVAL);
2665 			} else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2666 				const char *unused;
2667 
2668 				intval = fnvpair_value_uint64(propval);
2669 
2670 				switch (zfs_prop_get_type(prop)) {
2671 				case PROP_TYPE_NUMBER:
2672 					break;
2673 				case PROP_TYPE_STRING:
2674 					err = SET_ERROR(EINVAL);
2675 					break;
2676 				case PROP_TYPE_INDEX:
2677 					if (zfs_prop_index_to_string(prop,
2678 					    intval, &unused) != 0)
2679 						err = SET_ERROR(EINVAL);
2680 					break;
2681 				default:
2682 					cmn_err(CE_PANIC,
2683 					    "unknown property type");
2684 				}
2685 			} else {
2686 				err = SET_ERROR(EINVAL);
2687 			}
2688 		}
2689 
2690 		/* Validate permissions */
2691 		if (err == 0)
2692 			err = zfs_check_settable(dsname, pair, CRED());
2693 
2694 		if (err == 0) {
2695 			if (source == ZPROP_SRC_INHERITED)
2696 				err = -1; /* does not need special handling */
2697 			else
2698 				err = zfs_prop_set_special(dsname, source,
2699 				    pair);
2700 			if (err == -1) {
2701 				/*
2702 				 * For better performance we build up a list of
2703 				 * properties to set in a single transaction.
2704 				 */
2705 				err = nvlist_add_nvpair(genericnvl, pair);
2706 			} else if (err != 0 && nvl != retrynvl) {
2707 				/*
2708 				 * This may be a spurious error caused by
2709 				 * receiving quota and reservation out of order.
2710 				 * Try again in a second pass.
2711 				 */
2712 				err = nvlist_add_nvpair(retrynvl, pair);
2713 			}
2714 		}
2715 
2716 		if (err != 0) {
2717 			if (errlist != NULL)
2718 				fnvlist_add_int32(errlist, propname, err);
2719 			rv = err;
2720 		}
2721 	}
2722 
2723 	if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2724 		nvl = retrynvl;
2725 		goto retry;
2726 	}
2727 
2728 	if (!nvlist_empty(genericnvl) &&
2729 	    dsl_props_set(dsname, source, genericnvl) != 0) {
2730 		/*
2731 		 * If this fails, we still want to set as many properties as we
2732 		 * can, so try setting them individually.
2733 		 */
2734 		pair = NULL;
2735 		while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2736 			const char *propname = nvpair_name(pair);
2737 			int err = 0;
2738 
2739 			propval = pair;
2740 			if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2741 				nvlist_t *attrs;
2742 				attrs = fnvpair_value_nvlist(pair);
2743 				propval = fnvlist_lookup_nvpair(attrs,
2744 				    ZPROP_VALUE);
2745 			}
2746 
2747 			if (nvpair_type(propval) == DATA_TYPE_STRING) {
2748 				strval = fnvpair_value_string(propval);
2749 				err = dsl_prop_set_string(dsname, propname,
2750 				    source, strval);
2751 			} else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2752 				err = dsl_prop_inherit(dsname, propname,
2753 				    source);
2754 			} else {
2755 				intval = fnvpair_value_uint64(propval);
2756 				err = dsl_prop_set_int(dsname, propname, source,
2757 				    intval);
2758 			}
2759 
2760 			if (err != 0) {
2761 				if (errlist != NULL) {
2762 					fnvlist_add_int32(errlist, propname,
2763 					    err);
2764 				}
2765 				rv = err;
2766 			}
2767 		}
2768 	}
2769 	nvlist_free(genericnvl);
2770 	nvlist_free(retrynvl);
2771 
2772 	return (rv);
2773 }
2774 
2775 /*
2776  * Check that all the properties are valid user properties.
2777  */
2778 static int
2779 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2780 {
2781 	nvpair_t *pair = NULL;
2782 	int error = 0;
2783 
2784 	while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2785 		const char *propname = nvpair_name(pair);
2786 
2787 		if (!zfs_prop_user(propname) ||
2788 		    nvpair_type(pair) != DATA_TYPE_STRING)
2789 			return (SET_ERROR(EINVAL));
2790 
2791 		if (error = zfs_secpolicy_write_perms(fsname,
2792 		    ZFS_DELEG_PERM_USERPROP, CRED()))
2793 			return (error);
2794 
2795 		if (strlen(propname) >= ZAP_MAXNAMELEN)
2796 			return (SET_ERROR(ENAMETOOLONG));
2797 
2798 		if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2799 			return (E2BIG);
2800 	}
2801 	return (0);
2802 }
2803 
2804 static void
2805 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2806 {
2807 	nvpair_t *pair;
2808 
2809 	VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2810 
2811 	pair = NULL;
2812 	while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2813 		if (nvlist_exists(skipped, nvpair_name(pair)))
2814 			continue;
2815 
2816 		VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2817 	}
2818 }
2819 
2820 static int
2821 clear_received_props(const char *dsname, nvlist_t *props,
2822     nvlist_t *skipped)
2823 {
2824 	int err = 0;
2825 	nvlist_t *cleared_props = NULL;
2826 	props_skip(props, skipped, &cleared_props);
2827 	if (!nvlist_empty(cleared_props)) {
2828 		/*
2829 		 * Acts on local properties until the dataset has received
2830 		 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2831 		 */
2832 		zprop_source_t flags = (ZPROP_SRC_NONE |
2833 		    (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2834 		err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2835 	}
2836 	nvlist_free(cleared_props);
2837 	return (err);
2838 }
2839 
2840 /*
2841  * inputs:
2842  * zc_name		name of filesystem
2843  * zc_value		name of property to set
2844  * zc_nvlist_src{_size}	nvlist of properties to apply
2845  * zc_cookie		received properties flag
2846  *
2847  * outputs:
2848  * zc_nvlist_dst{_size} error for each unapplied received property
2849  */
2850 static int
2851 zfs_ioc_set_prop(zfs_cmd_t *zc)
2852 {
2853 	nvlist_t *nvl;
2854 	boolean_t received = zc->zc_cookie;
2855 	zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2856 	    ZPROP_SRC_LOCAL);
2857 	nvlist_t *errors;
2858 	int error;
2859 
2860 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2861 	    zc->zc_iflags, &nvl)) != 0)
2862 		return (error);
2863 
2864 	if (received) {
2865 		nvlist_t *origprops;
2866 
2867 		if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2868 			(void) clear_received_props(zc->zc_name,
2869 			    origprops, nvl);
2870 			nvlist_free(origprops);
2871 		}
2872 
2873 		error = dsl_prop_set_hasrecvd(zc->zc_name);
2874 	}
2875 
2876 	errors = fnvlist_alloc();
2877 	if (error == 0)
2878 		error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2879 
2880 	if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2881 		(void) put_nvlist(zc, errors);
2882 	}
2883 
2884 	nvlist_free(errors);
2885 	nvlist_free(nvl);
2886 	return (error);
2887 }
2888 
2889 /*
2890  * inputs:
2891  * zc_name		name of filesystem
2892  * zc_value		name of property to inherit
2893  * zc_cookie		revert to received value if TRUE
2894  *
2895  * outputs:		none
2896  */
2897 static int
2898 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2899 {
2900 	const char *propname = zc->zc_value;
2901 	zfs_prop_t prop = zfs_name_to_prop(propname);
2902 	boolean_t received = zc->zc_cookie;
2903 	zprop_source_t source = (received
2904 	    ? ZPROP_SRC_NONE		/* revert to received value, if any */
2905 	    : ZPROP_SRC_INHERITED);	/* explicitly inherit */
2906 
2907 	if (received) {
2908 		nvlist_t *dummy;
2909 		nvpair_t *pair;
2910 		zprop_type_t type;
2911 		int err;
2912 
2913 		/*
2914 		 * zfs_prop_set_special() expects properties in the form of an
2915 		 * nvpair with type info.
2916 		 */
2917 		if (prop == ZPROP_INVAL) {
2918 			if (!zfs_prop_user(propname))
2919 				return (SET_ERROR(EINVAL));
2920 
2921 			type = PROP_TYPE_STRING;
2922 		} else if (prop == ZFS_PROP_VOLSIZE ||
2923 		    prop == ZFS_PROP_VERSION) {
2924 			return (SET_ERROR(EINVAL));
2925 		} else {
2926 			type = zfs_prop_get_type(prop);
2927 		}
2928 
2929 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2930 
2931 		switch (type) {
2932 		case PROP_TYPE_STRING:
2933 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2934 			break;
2935 		case PROP_TYPE_NUMBER:
2936 		case PROP_TYPE_INDEX:
2937 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2938 			break;
2939 		default:
2940 			nvlist_free(dummy);
2941 			return (SET_ERROR(EINVAL));
2942 		}
2943 
2944 		pair = nvlist_next_nvpair(dummy, NULL);
2945 		err = zfs_prop_set_special(zc->zc_name, source, pair);
2946 		nvlist_free(dummy);
2947 		if (err != -1)
2948 			return (err); /* special property already handled */
2949 	} else {
2950 		/*
2951 		 * Only check this in the non-received case. We want to allow
2952 		 * 'inherit -S' to revert non-inheritable properties like quota
2953 		 * and reservation to the received or default values even though
2954 		 * they are not considered inheritable.
2955 		 */
2956 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2957 			return (SET_ERROR(EINVAL));
2958 	}
2959 
2960 	/* property name has been validated by zfs_secpolicy_inherit_prop() */
2961 	return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2962 }
2963 
2964 static int
2965 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2966 {
2967 	nvlist_t *props;
2968 	spa_t *spa;
2969 	int error;
2970 	nvpair_t *pair;
2971 
2972 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2973 	    zc->zc_iflags, &props))
2974 		return (error);
2975 
2976 	/*
2977 	 * If the only property is the configfile, then just do a spa_lookup()
2978 	 * to handle the faulted case.
2979 	 */
2980 	pair = nvlist_next_nvpair(props, NULL);
2981 	if (pair != NULL && strcmp(nvpair_name(pair),
2982 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2983 	    nvlist_next_nvpair(props, pair) == NULL) {
2984 		mutex_enter(&spa_namespace_lock);
2985 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2986 			spa_configfile_set(spa, props, B_FALSE);
2987 			spa_write_cachefile(spa, B_FALSE, B_TRUE);
2988 		}
2989 		mutex_exit(&spa_namespace_lock);
2990 		if (spa != NULL) {
2991 			nvlist_free(props);
2992 			return (0);
2993 		}
2994 	}
2995 
2996 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2997 		nvlist_free(props);
2998 		return (error);
2999 	}
3000 
3001 	error = spa_prop_set(spa, props);
3002 
3003 	nvlist_free(props);
3004 	spa_close(spa, FTAG);
3005 
3006 	return (error);
3007 }
3008 
3009 static int
3010 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
3011 {
3012 	spa_t *spa;
3013 	int error;
3014 	nvlist_t *nvp = NULL;
3015 
3016 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3017 		/*
3018 		 * If the pool is faulted, there may be properties we can still
3019 		 * get (such as altroot and cachefile), so attempt to get them
3020 		 * anyway.
3021 		 */
3022 		mutex_enter(&spa_namespace_lock);
3023 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
3024 			error = spa_prop_get(spa, &nvp);
3025 		mutex_exit(&spa_namespace_lock);
3026 	} else {
3027 		error = spa_prop_get(spa, &nvp);
3028 		spa_close(spa, FTAG);
3029 	}
3030 
3031 	if (error == 0 && zc->zc_nvlist_dst != 0)
3032 		error = put_nvlist(zc, nvp);
3033 	else
3034 		error = SET_ERROR(EFAULT);
3035 
3036 	nvlist_free(nvp);
3037 	return (error);
3038 }
3039 
3040 /*
3041  * inputs:
3042  * zc_name		name of filesystem
3043  * zc_nvlist_src{_size}	nvlist of delegated permissions
3044  * zc_perm_action	allow/unallow flag
3045  *
3046  * outputs:		none
3047  */
3048 static int
3049 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3050 {
3051 	int error;
3052 	nvlist_t *fsaclnv = NULL;
3053 
3054 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3055 	    zc->zc_iflags, &fsaclnv)) != 0)
3056 		return (error);
3057 
3058 	/*
3059 	 * Verify nvlist is constructed correctly
3060 	 */
3061 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3062 		nvlist_free(fsaclnv);
3063 		return (SET_ERROR(EINVAL));
3064 	}
3065 
3066 	/*
3067 	 * If we don't have PRIV_SYS_MOUNT, then validate
3068 	 * that user is allowed to hand out each permission in
3069 	 * the nvlist(s)
3070 	 */
3071 
3072 	error = secpolicy_zfs(CRED());
3073 	if (error != 0) {
3074 		if (zc->zc_perm_action == B_FALSE) {
3075 			error = dsl_deleg_can_allow(zc->zc_name,
3076 			    fsaclnv, CRED());
3077 		} else {
3078 			error = dsl_deleg_can_unallow(zc->zc_name,
3079 			    fsaclnv, CRED());
3080 		}
3081 	}
3082 
3083 	if (error == 0)
3084 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3085 
3086 	nvlist_free(fsaclnv);
3087 	return (error);
3088 }
3089 
3090 /*
3091  * inputs:
3092  * zc_name		name of filesystem
3093  *
3094  * outputs:
3095  * zc_nvlist_src{_size}	nvlist of delegated permissions
3096  */
3097 static int
3098 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3099 {
3100 	nvlist_t *nvp;
3101 	int error;
3102 
3103 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3104 		error = put_nvlist(zc, nvp);
3105 		nvlist_free(nvp);
3106 	}
3107 
3108 	return (error);
3109 }
3110 
3111 /* ARGSUSED */
3112 static void
3113 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3114 {
3115 	zfs_creat_t *zct = arg;
3116 
3117 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3118 }
3119 
3120 #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
3121 
3122 /*
3123  * inputs:
3124  * os			parent objset pointer (NULL if root fs)
3125  * fuids_ok		fuids allowed in this version of the spa?
3126  * sa_ok		SAs allowed in this version of the spa?
3127  * createprops		list of properties requested by creator
3128  *
3129  * outputs:
3130  * zplprops	values for the zplprops we attach to the master node object
3131  * is_ci	true if requested file system will be purely case-insensitive
3132  *
3133  * Determine the settings for utf8only, normalization and
3134  * casesensitivity.  Specific values may have been requested by the
3135  * creator and/or we can inherit values from the parent dataset.  If
3136  * the file system is of too early a vintage, a creator can not
3137  * request settings for these properties, even if the requested
3138  * setting is the default value.  We don't actually want to create dsl
3139  * properties for these, so remove them from the source nvlist after
3140  * processing.
3141  */
3142 static int
3143 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3144     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3145     nvlist_t *zplprops, boolean_t *is_ci)
3146 {
3147 	uint64_t sense = ZFS_PROP_UNDEFINED;
3148 	uint64_t norm = ZFS_PROP_UNDEFINED;
3149 	uint64_t u8 = ZFS_PROP_UNDEFINED;
3150 
3151 	ASSERT(zplprops != NULL);
3152 
3153 	if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3154 		return (SET_ERROR(EINVAL));
3155 
3156 	/*
3157 	 * Pull out creator prop choices, if any.
3158 	 */
3159 	if (createprops) {
3160 		(void) nvlist_lookup_uint64(createprops,
3161 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3162 		(void) nvlist_lookup_uint64(createprops,
3163 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3164 		(void) nvlist_remove_all(createprops,
3165 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3166 		(void) nvlist_lookup_uint64(createprops,
3167 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3168 		(void) nvlist_remove_all(createprops,
3169 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3170 		(void) nvlist_lookup_uint64(createprops,
3171 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3172 		(void) nvlist_remove_all(createprops,
3173 		    zfs_prop_to_name(ZFS_PROP_CASE));
3174 	}
3175 
3176 	/*
3177 	 * If the zpl version requested is whacky or the file system
3178 	 * or pool is version is too "young" to support normalization
3179 	 * and the creator tried to set a value for one of the props,
3180 	 * error out.
3181 	 */
3182 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3183 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3184 	    (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3185 	    (zplver < ZPL_VERSION_NORMALIZATION &&
3186 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3187 	    sense != ZFS_PROP_UNDEFINED)))
3188 		return (SET_ERROR(ENOTSUP));
3189 
3190 	/*
3191 	 * Put the version in the zplprops
3192 	 */
3193 	VERIFY(nvlist_add_uint64(zplprops,
3194 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3195 
3196 	if (norm == ZFS_PROP_UNDEFINED)
3197 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3198 	VERIFY(nvlist_add_uint64(zplprops,
3199 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3200 
3201 	/*
3202 	 * If we're normalizing, names must always be valid UTF-8 strings.
3203 	 */
3204 	if (norm)
3205 		u8 = 1;
3206 	if (u8 == ZFS_PROP_UNDEFINED)
3207 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3208 	VERIFY(nvlist_add_uint64(zplprops,
3209 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3210 
3211 	if (sense == ZFS_PROP_UNDEFINED)
3212 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3213 	VERIFY(nvlist_add_uint64(zplprops,
3214 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3215 
3216 	if (is_ci)
3217 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
3218 
3219 	return (0);
3220 }
3221 
3222 static int
3223 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3224     nvlist_t *zplprops, boolean_t *is_ci)
3225 {
3226 	boolean_t fuids_ok, sa_ok;
3227 	uint64_t zplver = ZPL_VERSION;
3228 	objset_t *os = NULL;
3229 	char parentname[ZFS_MAX_DATASET_NAME_LEN];
3230 	char *cp;
3231 	spa_t *spa;
3232 	uint64_t spa_vers;
3233 	int error;
3234 
3235 	(void) strlcpy(parentname, dataset, sizeof (parentname));
3236 	cp = strrchr(parentname, '/');
3237 	ASSERT(cp != NULL);
3238 	cp[0] = '\0';
3239 
3240 	if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3241 		return (error);
3242 
3243 	spa_vers = spa_version(spa);
3244 	spa_close(spa, FTAG);
3245 
3246 	zplver = zfs_zpl_version_map(spa_vers);
3247 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
3248 	sa_ok = (zplver >= ZPL_VERSION_SA);
3249 
3250 	/*
3251 	 * Open parent object set so we can inherit zplprop values.
3252 	 */
3253 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3254 		return (error);
3255 
3256 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3257 	    zplprops, is_ci);
3258 	dmu_objset_rele(os, FTAG);
3259 	return (error);
3260 }
3261 
3262 static int
3263 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3264     nvlist_t *zplprops, boolean_t *is_ci)
3265 {
3266 	boolean_t fuids_ok;
3267 	boolean_t sa_ok;
3268 	uint64_t zplver = ZPL_VERSION;
3269 	int error;
3270 
3271 	zplver = zfs_zpl_version_map(spa_vers);
3272 	fuids_ok = (zplver >= ZPL_VERSION_FUID);
3273 	sa_ok = (zplver >= ZPL_VERSION_SA);
3274 
3275 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3276 	    createprops, zplprops, is_ci);
3277 	return (error);
3278 }
3279 
3280 /*
3281  * innvl: {
3282  *     "type" -> dmu_objset_type_t (int32)
3283  *     (optional) "props" -> { prop -> value }
3284  *     (optional) "hidden_args" -> { "wkeydata" -> value }
3285  *         raw uint8_t array of encryption wrapping key data (32 bytes)
3286  * }
3287  *
3288  * outnvl: propname -> error code (int32)
3289  */
3290 
3291 static const zfs_ioc_key_t zfs_keys_create[] = {
3292 	{"type",	DATA_TYPE_INT32,	0},
3293 	{"props",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
3294 	{"hidden_args",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
3295 };
3296 
3297 static int
3298 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3299 {
3300 	int error = 0;
3301 	zfs_creat_t zct = { 0 };
3302 	nvlist_t *nvprops = NULL;
3303 	nvlist_t *hidden_args = NULL;
3304 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3305 	dmu_objset_type_t type;
3306 	boolean_t is_insensitive = B_FALSE;
3307 	dsl_crypto_params_t *dcp = NULL;
3308 
3309 	type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3310 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3311 	(void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3312 
3313 	switch (type) {
3314 	case DMU_OST_ZFS:
3315 		cbfunc = zfs_create_cb;
3316 		break;
3317 
3318 	case DMU_OST_ZVOL:
3319 		cbfunc = zvol_create_cb;
3320 		break;
3321 
3322 	default:
3323 		cbfunc = NULL;
3324 		break;
3325 	}
3326 	if (strchr(fsname, '@') ||
3327 	    strchr(fsname, '%'))
3328 		return (SET_ERROR(EINVAL));
3329 
3330 	zct.zct_props = nvprops;
3331 
3332 	if (cbfunc == NULL)
3333 		return (SET_ERROR(EINVAL));
3334 
3335 	if (type == DMU_OST_ZVOL) {
3336 		uint64_t volsize, volblocksize;
3337 
3338 		if (nvprops == NULL)
3339 			return (SET_ERROR(EINVAL));
3340 		if (nvlist_lookup_uint64(nvprops,
3341 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3342 			return (SET_ERROR(EINVAL));
3343 
3344 		if ((error = nvlist_lookup_uint64(nvprops,
3345 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3346 		    &volblocksize)) != 0 && error != ENOENT)
3347 			return (SET_ERROR(EINVAL));
3348 
3349 		if (error != 0)
3350 			volblocksize = zfs_prop_default_numeric(
3351 			    ZFS_PROP_VOLBLOCKSIZE);
3352 
3353 		if ((error = zvol_check_volblocksize(
3354 		    volblocksize)) != 0 ||
3355 		    (error = zvol_check_volsize(volsize,
3356 		    volblocksize)) != 0)
3357 			return (error);
3358 	} else if (type == DMU_OST_ZFS) {
3359 		int error;
3360 
3361 		/*
3362 		 * We have to have normalization and
3363 		 * case-folding flags correct when we do the
3364 		 * file system creation, so go figure them out
3365 		 * now.
3366 		 */
3367 		VERIFY(nvlist_alloc(&zct.zct_zplprops,
3368 		    NV_UNIQUE_NAME, KM_SLEEP) == 0);
3369 		error = zfs_fill_zplprops(fsname, nvprops,
3370 		    zct.zct_zplprops, &is_insensitive);
3371 		if (error != 0) {
3372 			nvlist_free(zct.zct_zplprops);
3373 			return (error);
3374 		}
3375 	}
3376 
3377 	error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3378 	    hidden_args, &dcp);
3379 	if (error != 0) {
3380 		nvlist_free(zct.zct_zplprops);
3381 		return (error);
3382 	}
3383 
3384 	error = dmu_objset_create(fsname, type,
3385 	    is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3386 
3387 	nvlist_free(zct.zct_zplprops);
3388 	dsl_crypto_params_free(dcp, !!error);
3389 
3390 	/*
3391 	 * It would be nice to do this atomically.
3392 	 */
3393 	if (error == 0) {
3394 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3395 		    nvprops, outnvl);
3396 		if (error != 0)
3397 			(void) dsl_destroy_head(fsname);
3398 	}
3399 	return (error);
3400 }
3401 
3402 /*
3403  * innvl: {
3404  *     "origin" -> name of origin snapshot
3405  *     (optional) "props" -> { prop -> value }
3406  *     (optional) "hidden_args" -> { "wkeydata" -> value }
3407  *         raw uint8_t array of encryption wrapping key data (32 bytes)
3408  * }
3409  *
3410  * outnvl: propname -> error code (int32)
3411  */
3412 static const zfs_ioc_key_t zfs_keys_clone[] = {
3413 	{"origin",	DATA_TYPE_STRING,	0},
3414 	{"props",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
3415 	{"hidden_args",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
3416 };
3417 
3418 static int
3419 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3420 {
3421 	int error = 0;
3422 	nvlist_t *nvprops = NULL;
3423 	char *origin_name;
3424 
3425 	if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3426 		return (SET_ERROR(EINVAL));
3427 	(void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3428 
3429 	if (strchr(fsname, '@') ||
3430 	    strchr(fsname, '%'))
3431 		return (SET_ERROR(EINVAL));
3432 
3433 	if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3434 		return (SET_ERROR(EINVAL));
3435 
3436 	error = dmu_objset_clone(fsname, origin_name);
3437 
3438 	/*
3439 	 * It would be nice to do this atomically.
3440 	 */
3441 	if (error == 0) {
3442 		error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3443 		    nvprops, outnvl);
3444 		if (error != 0)
3445 			(void) dsl_destroy_head(fsname);
3446 	}
3447 	return (error);
3448 }
3449 
3450 static const zfs_ioc_key_t zfs_keys_remap[] = {
3451 	/* no nvl keys */
3452 };
3453 
3454 /* ARGSUSED */
3455 static int
3456 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3457 {
3458 	if (strchr(fsname, '@') ||
3459 	    strchr(fsname, '%'))
3460 		return (SET_ERROR(EINVAL));
3461 
3462 	return (dmu_objset_remap_indirects(fsname));
3463 }
3464 
3465 /*
3466  * innvl: {
3467  *     "snaps" -> { snapshot1, snapshot2 }
3468  *     (optional) "props" -> { prop -> value (string) }
3469  * }
3470  *
3471  * outnvl: snapshot -> error code (int32)
3472  */
3473 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3474 	{"snaps",	DATA_TYPE_NVLIST,	0},
3475 	{"props",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
3476 };
3477 
3478 static int
3479 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3480 {
3481 	nvlist_t *snaps;
3482 	nvlist_t *props = NULL;
3483 	int error, poollen;
3484 	nvpair_t *pair;
3485 
3486 	(void) nvlist_lookup_nvlist(innvl, "props", &props);
3487 	if ((error = zfs_check_userprops(poolname, props)) != 0)
3488 		return (error);
3489 
3490 	if (!nvlist_empty(props) &&
3491 	    zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3492 		return (SET_ERROR(ENOTSUP));
3493 
3494 	snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3495 	poollen = strlen(poolname);
3496 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3497 	    pair = nvlist_next_nvpair(snaps, pair)) {
3498 		const char *name = nvpair_name(pair);
3499 		const char *cp = strchr(name, '@');
3500 
3501 		/*
3502 		 * The snap name must contain an @, and the part after it must
3503 		 * contain only valid characters.
3504 		 */
3505 		if (cp == NULL ||
3506 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3507 			return (SET_ERROR(EINVAL));
3508 
3509 		/*
3510 		 * The snap must be in the specified pool.
3511 		 */
3512 		if (strncmp(name, poolname, poollen) != 0 ||
3513 		    (name[poollen] != '/' && name[poollen] != '@'))
3514 			return (SET_ERROR(EXDEV));
3515 
3516 		/* This must be the only snap of this fs. */
3517 		for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3518 		    pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3519 			if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3520 			    == 0) {
3521 				return (SET_ERROR(EXDEV));
3522 			}
3523 		}
3524 	}
3525 
3526 	error = dsl_dataset_snapshot(snaps, props, outnvl);
3527 	return (error);
3528 }
3529 
3530 /*
3531  * innvl: "message" -> string
3532  */
3533 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3534 	{"message",	DATA_TYPE_STRING,	0},
3535 };
3536 
3537 /* ARGSUSED */
3538 static int
3539 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3540 {
3541 	char *message;
3542 	spa_t *spa;
3543 	int error;
3544 	char *poolname;
3545 
3546 	/*
3547 	 * The poolname in the ioctl is not set, we get it from the TSD,
3548 	 * which was set at the end of the last successful ioctl that allows
3549 	 * logging.  The secpolicy func already checked that it is set.
3550 	 * Only one log ioctl is allowed after each successful ioctl, so
3551 	 * we clear the TSD here.
3552 	 */
3553 	poolname = tsd_get(zfs_allow_log_key);
3554 	(void) tsd_set(zfs_allow_log_key, NULL);
3555 	error = spa_open(poolname, &spa, FTAG);
3556 	strfree(poolname);
3557 	if (error != 0)
3558 		return (error);
3559 
3560 	message = fnvlist_lookup_string(innvl, "message");
3561 
3562 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3563 		spa_close(spa, FTAG);
3564 		return (SET_ERROR(ENOTSUP));
3565 	}
3566 
3567 	error = spa_history_log(spa, message);
3568 	spa_close(spa, FTAG);
3569 	return (error);
3570 }
3571 
3572 /*
3573  * This ioctl is used to set the bootenv configuration on the current
3574  * pool. This configuration is stored in the second padding area of the label,
3575  * and it is used by the bootloader(s) to store bootloader and/or system
3576  * specific data.
3577  * The data is stored as nvlist data stream, and is protected by
3578  * an embedded checksum.
3579  * The version can have two possible values:
3580  * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3581  * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3582  */
3583 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3584 	{"version",	DATA_TYPE_UINT64, 0},
3585 	{"<keys>",	DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3586 };
3587 
3588 static int
3589 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl,
3590     nvlist_t *outnvl __unused)
3591 {
3592 	int error;
3593 	spa_t *spa;
3594 
3595 	if ((error = spa_open(name, &spa, FTAG)) != 0)
3596 		return (error);
3597 	spa_vdev_state_enter(spa, SCL_ALL);
3598 	error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3599 	(void) spa_vdev_state_exit(spa, NULL, 0);
3600 	spa_close(spa, FTAG);
3601 	return (error);
3602 }
3603 
3604 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3605 	/* no nvl keys */
3606 };
3607 
3608 static int
3609 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl __unused,
3610     nvlist_t *outnvl)
3611 {
3612 	spa_t *spa;
3613 	int error;
3614 
3615 	if ((error = spa_open(name, &spa, FTAG)) != 0)
3616 		return (error);
3617 	spa_vdev_state_enter(spa, SCL_ALL);
3618 	error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3619 	(void) spa_vdev_state_exit(spa, NULL, 0);
3620 	spa_close(spa, FTAG);
3621 	return (error);
3622 }
3623 
3624 /*
3625  * The dp_config_rwlock must not be held when calling this, because the
3626  * unmount may need to write out data.
3627  *
3628  * This function is best-effort.  Callers must deal gracefully if it
3629  * remains mounted (or is remounted after this call).
3630  *
3631  * Returns 0 if the argument is not a snapshot, or it is not currently a
3632  * filesystem, or we were able to unmount it.  Returns error code otherwise.
3633  */
3634 void
3635 zfs_unmount_snap(const char *snapname)
3636 {
3637 	vfs_t *vfsp = NULL;
3638 	zfsvfs_t *zfsvfs = NULL;
3639 
3640 	if (strchr(snapname, '@') == NULL)
3641 		return;
3642 
3643 	int err = getzfsvfs(snapname, &zfsvfs);
3644 	if (err != 0) {
3645 		ASSERT3P(zfsvfs, ==, NULL);
3646 		return;
3647 	}
3648 	vfsp = zfsvfs->z_vfs;
3649 
3650 	ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3651 
3652 	err = vn_vfswlock(vfsp->vfs_vnodecovered);
3653 	VFS_RELE(vfsp);
3654 	if (err != 0)
3655 		return;
3656 
3657 	/*
3658 	 * Always force the unmount for snapshots.
3659 	 */
3660 	(void) dounmount(vfsp, MS_FORCE, kcred);
3661 }
3662 
3663 /* ARGSUSED */
3664 static int
3665 zfs_unmount_snap_cb(const char *snapname, void *arg)
3666 {
3667 	zfs_unmount_snap(snapname);
3668 	return (0);
3669 }
3670 
3671 /*
3672  * When a clone is destroyed, its origin may also need to be destroyed,
3673  * in which case it must be unmounted.  This routine will do that unmount
3674  * if necessary.
3675  */
3676 void
3677 zfs_destroy_unmount_origin(const char *fsname)
3678 {
3679 	int error;
3680 	objset_t *os;
3681 	dsl_dataset_t *ds;
3682 
3683 	error = dmu_objset_hold(fsname, FTAG, &os);
3684 	if (error != 0)
3685 		return;
3686 	ds = dmu_objset_ds(os);
3687 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3688 		char originname[ZFS_MAX_DATASET_NAME_LEN];
3689 		dsl_dataset_name(ds->ds_prev, originname);
3690 		dmu_objset_rele(os, FTAG);
3691 		zfs_unmount_snap(originname);
3692 	} else {
3693 		dmu_objset_rele(os, FTAG);
3694 	}
3695 }
3696 
3697 /*
3698  * innvl: {
3699  *     "snaps" -> { snapshot1, snapshot2 }
3700  *     (optional boolean) "defer"
3701  * }
3702  *
3703  * outnvl: snapshot -> error code (int32)
3704  *
3705  */
3706 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3707 	{"snaps",	DATA_TYPE_NVLIST,	0},
3708 	{"defer",	DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
3709 };
3710 
3711 /* ARGSUSED */
3712 static int
3713 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3714 {
3715 	nvlist_t *snaps;
3716 	nvpair_t *pair;
3717 	boolean_t defer;
3718 
3719 	if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3720 		return (SET_ERROR(EINVAL));
3721 	defer = nvlist_exists(innvl, "defer");
3722 
3723 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3724 	    pair = nvlist_next_nvpair(snaps, pair)) {
3725 		zfs_unmount_snap(nvpair_name(pair));
3726 	}
3727 
3728 	return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3729 }
3730 
3731 /*
3732  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
3733  * All bookmarks must be in the same pool.
3734  *
3735  * innvl: {
3736  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
3737  * }
3738  *
3739  * outnvl: bookmark -> error code (int32)
3740  *
3741  */
3742 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3743 	{"<bookmark>...",	DATA_TYPE_STRING,	ZK_WILDCARDLIST},
3744 };
3745 
3746 /* ARGSUSED */
3747 static int
3748 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3749 {
3750 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3751 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3752 		char *snap_name;
3753 
3754 		/*
3755 		 * Verify the snapshot argument.
3756 		 */
3757 		if (nvpair_value_string(pair, &snap_name) != 0)
3758 			return (SET_ERROR(EINVAL));
3759 
3760 
3761 		/* Verify that the keys (bookmarks) are unique */
3762 		for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3763 		    pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3764 			if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3765 				return (SET_ERROR(EINVAL));
3766 		}
3767 	}
3768 
3769 	return (dsl_bookmark_create(innvl, outnvl));
3770 }
3771 
3772 /*
3773  * innvl: {
3774  *     property 1, property 2, ...
3775  * }
3776  *
3777  * outnvl: {
3778  *     bookmark name 1 -> { property 1, property 2, ... },
3779  *     bookmark name 2 -> { property 1, property 2, ... }
3780  * }
3781  *
3782  */
3783 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3784 	{"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3785 };
3786 
3787 static int
3788 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3789 {
3790 	return (dsl_get_bookmarks(fsname, innvl, outnvl));
3791 }
3792 
3793 /*
3794  * innvl: {
3795  *     bookmark name 1, bookmark name 2
3796  * }
3797  *
3798  * outnvl: bookmark -> error code (int32)
3799  *
3800  */
3801 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3802 	{"<bookmark>...",	DATA_TYPE_BOOLEAN,	ZK_WILDCARDLIST},
3803 };
3804 
3805 static int
3806 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3807     nvlist_t *outnvl)
3808 {
3809 	int error, poollen;
3810 
3811 	poollen = strlen(poolname);
3812 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3813 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3814 		const char *name = nvpair_name(pair);
3815 		const char *cp = strchr(name, '#');
3816 
3817 		/*
3818 		 * The bookmark name must contain an #, and the part after it
3819 		 * must contain only valid characters.
3820 		 */
3821 		if (cp == NULL ||
3822 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3823 			return (SET_ERROR(EINVAL));
3824 
3825 		/*
3826 		 * The bookmark must be in the specified pool.
3827 		 */
3828 		if (strncmp(name, poolname, poollen) != 0 ||
3829 		    (name[poollen] != '/' && name[poollen] != '#'))
3830 			return (SET_ERROR(EXDEV));
3831 	}
3832 
3833 	error = dsl_bookmark_destroy(innvl, outnvl);
3834 	return (error);
3835 }
3836 
3837 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3838 	{"program",	DATA_TYPE_STRING,		0},
3839 	{"arg",		DATA_TYPE_ANY,			0},
3840 	{"sync",	DATA_TYPE_BOOLEAN_VALUE,	ZK_OPTIONAL},
3841 	{"instrlimit",	DATA_TYPE_UINT64,		ZK_OPTIONAL},
3842 	{"memlimit",	DATA_TYPE_UINT64,		ZK_OPTIONAL},
3843 };
3844 
3845 static int
3846 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3847     nvlist_t *outnvl)
3848 {
3849 	char *program;
3850 	uint64_t instrlimit, memlimit;
3851 	boolean_t sync_flag;
3852 	nvpair_t *nvarg = NULL;
3853 
3854 	program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3855 	if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3856 		sync_flag = B_TRUE;
3857 	}
3858 	if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3859 		instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3860 	}
3861 	if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3862 		memlimit = ZCP_DEFAULT_MEMLIMIT;
3863 	}
3864 	nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3865 
3866 	if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3867 		return (EINVAL);
3868 	if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3869 		return (EINVAL);
3870 
3871 	return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3872 	    nvarg, outnvl));
3873 }
3874 
3875 /*
3876  * innvl: unused
3877  * outnvl: empty
3878  */
3879 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3880 	/* no nvl keys */
3881 };
3882 
3883 /* ARGSUSED */
3884 static int
3885 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3886 {
3887 	return (spa_checkpoint(poolname));
3888 }
3889 
3890 /*
3891  * innvl: unused
3892  * outnvl: empty
3893  */
3894 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3895 	/* no nvl keys */
3896 };
3897 
3898 /* ARGSUSED */
3899 static int
3900 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3901     nvlist_t *outnvl)
3902 {
3903 	return (spa_checkpoint_discard(poolname));
3904 }
3905 
3906 /*
3907  * inputs:
3908  * zc_name		name of dataset to destroy
3909  * zc_defer_destroy	mark for deferred destroy
3910  *
3911  * outputs:		none
3912  */
3913 static int
3914 zfs_ioc_destroy(zfs_cmd_t *zc)
3915 {
3916 	objset_t *os;
3917 	dmu_objset_type_t ost;
3918 	int err;
3919 
3920 	err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3921 	if (err != 0)
3922 		return (err);
3923 	ost = dmu_objset_type(os);
3924 	dmu_objset_rele(os, FTAG);
3925 
3926 	if (ost == DMU_OST_ZFS)
3927 		zfs_unmount_snap(zc->zc_name);
3928 
3929 	if (strchr(zc->zc_name, '@')) {
3930 		err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3931 	} else {
3932 		err = dsl_destroy_head(zc->zc_name);
3933 		if (err == EEXIST) {
3934 			/*
3935 			 * It is possible that the given DS may have
3936 			 * hidden child (%recv) datasets - "leftovers"
3937 			 * resulting from the previously interrupted
3938 			 * 'zfs receive'.
3939 			 *
3940 			 * 6 extra bytes for /%recv
3941 			 */
3942 			char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3943 
3944 			if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3945 			    zc->zc_name, recv_clone_name) >=
3946 			    sizeof (namebuf))
3947 				return (SET_ERROR(EINVAL));
3948 
3949 			/*
3950 			 * Try to remove the hidden child (%recv) and after
3951 			 * that try to remove the target dataset.
3952 			 * If the hidden child (%recv) does not exist
3953 			 * the original error (EEXIST) will be returned
3954 			 */
3955 			err = dsl_destroy_head(namebuf);
3956 			if (err == 0)
3957 				err = dsl_destroy_head(zc->zc_name);
3958 			else if (err == ENOENT)
3959 				err = SET_ERROR(EEXIST);
3960 		}
3961 	}
3962 	if (ost == DMU_OST_ZVOL && err == 0)
3963 		(void) zvol_remove_minor(zc->zc_name);
3964 	return (err);
3965 }
3966 
3967 /*
3968  * innvl: {
3969  *     "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
3970  *     "initialize_vdevs": { -> guids to initialize (nvlist)
3971  *         "vdev_path_1": vdev_guid_1, (uint64),
3972  *         "vdev_path_2": vdev_guid_2, (uint64),
3973  *         ...
3974  *     },
3975  * }
3976  *
3977  * outnvl: {
3978  *     "initialize_vdevs": { -> initialization errors (nvlist)
3979  *         "vdev_path_1": errno, see function body for possible errnos (uint64)
3980  *         "vdev_path_2": errno, ... (uint64)
3981  *         ...
3982  *     }
3983  * }
3984  *
3985  * EINVAL is returned for an unknown command or if any of the provided vdev
3986  * guids have be specified with a type other than uint64.
3987  */
3988 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
3989 	{ZPOOL_INITIALIZE_COMMAND,	DATA_TYPE_UINT64,	0},
3990 	{ZPOOL_INITIALIZE_VDEVS,	DATA_TYPE_NVLIST,	0}
3991 };
3992 
3993 static int
3994 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3995 {
3996 	uint64_t cmd_type;
3997 	if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3998 	    &cmd_type) != 0) {
3999 		return (SET_ERROR(EINVAL));
4000 	}
4001 
4002 	if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4003 	    cmd_type == POOL_INITIALIZE_START ||
4004 	    cmd_type == POOL_INITIALIZE_SUSPEND)) {
4005 		return (SET_ERROR(EINVAL));
4006 	}
4007 
4008 	nvlist_t *vdev_guids;
4009 	if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4010 	    &vdev_guids) != 0) {
4011 		return (SET_ERROR(EINVAL));
4012 	}
4013 
4014 	for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4015 	    pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4016 		uint64_t vdev_guid;
4017 		if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4018 			return (SET_ERROR(EINVAL));
4019 		}
4020 	}
4021 
4022 	spa_t *spa;
4023 	int error = spa_open(poolname, &spa, FTAG);
4024 	if (error != 0)
4025 		return (error);
4026 
4027 	nvlist_t *vdev_errlist = fnvlist_alloc();
4028 	int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4029 	    vdev_errlist);
4030 
4031 	if (fnvlist_size(vdev_errlist) > 0) {
4032 		fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4033 		    vdev_errlist);
4034 	}
4035 	fnvlist_free(vdev_errlist);
4036 
4037 	spa_close(spa, FTAG);
4038 	return (total_errors > 0 ? EINVAL : 0);
4039 }
4040 
4041 /*
4042  * innvl: {
4043  *     "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4044  *     "trim_vdevs": { -> guids to TRIM (nvlist)
4045  *         "vdev_path_1": vdev_guid_1, (uint64),
4046  *         "vdev_path_2": vdev_guid_2, (uint64),
4047  *         ...
4048  *     },
4049  *     "trim_rate" -> Target TRIM rate in bytes/sec.
4050  *     "trim_secure" -> Set to request a secure TRIM.
4051  * }
4052  *
4053  * outnvl: {
4054  *     "trim_vdevs": { -> TRIM errors (nvlist)
4055  *         "vdev_path_1": errno, see function body for possible errnos (uint64)
4056  *         "vdev_path_2": errno, ... (uint64)
4057  *         ...
4058  *     }
4059  * }
4060  *
4061  * EINVAL is returned for an unknown command or if any of the provided vdev
4062  * guids have be specified with a type other than uint64.
4063  */
4064 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4065 	{ZPOOL_TRIM_COMMAND,	DATA_TYPE_UINT64,		0},
4066 	{ZPOOL_TRIM_VDEVS,	DATA_TYPE_NVLIST,		0},
4067 	{ZPOOL_TRIM_RATE,	DATA_TYPE_UINT64,		ZK_OPTIONAL},
4068 	{ZPOOL_TRIM_SECURE,	DATA_TYPE_BOOLEAN_VALUE,	ZK_OPTIONAL},
4069 };
4070 
4071 static int
4072 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4073 {
4074 	uint64_t cmd_type;
4075 	if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4076 		return (SET_ERROR(EINVAL));
4077 
4078 	if (!(cmd_type == POOL_TRIM_CANCEL ||
4079 	    cmd_type == POOL_TRIM_START ||
4080 	    cmd_type == POOL_TRIM_SUSPEND)) {
4081 		return (SET_ERROR(EINVAL));
4082 	}
4083 
4084 	nvlist_t *vdev_guids;
4085 	if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4086 		return (SET_ERROR(EINVAL));
4087 
4088 	for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4089 	    pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4090 		uint64_t vdev_guid;
4091 		if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4092 			return (SET_ERROR(EINVAL));
4093 		}
4094 	}
4095 
4096 	/* Optional, defaults to maximum rate when not provided */
4097 	uint64_t rate;
4098 	if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4099 		rate = 0;
4100 
4101 	/* Optional, defaults to standard TRIM when not provided */
4102 	boolean_t secure;
4103 	if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4104 	    &secure) != 0) {
4105 		secure = B_FALSE;
4106 	}
4107 
4108 	spa_t *spa;
4109 	int error = spa_open(poolname, &spa, FTAG);
4110 	if (error != 0)
4111 		return (error);
4112 
4113 	nvlist_t *vdev_errlist = fnvlist_alloc();
4114 	int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4115 	    rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4116 
4117 	if (fnvlist_size(vdev_errlist) > 0)
4118 		fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4119 
4120 	fnvlist_free(vdev_errlist);
4121 
4122 	spa_close(spa, FTAG);
4123 	return (total_errors > 0 ? EINVAL : 0);
4124 }
4125 
4126 /*
4127  * fsname is name of dataset to rollback (to most recent snapshot)
4128  *
4129  * innvl may contain name of expected target snapshot
4130  *
4131  * outnvl: "target" -> name of most recent snapshot
4132  * }
4133  */
4134 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4135 	{"target",	DATA_TYPE_STRING,	ZK_OPTIONAL},
4136 };
4137 
4138 /* ARGSUSED */
4139 static int
4140 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4141 {
4142 	zfsvfs_t *zfsvfs;
4143 	char *target = NULL;
4144 	int error;
4145 
4146 	(void) nvlist_lookup_string(innvl, "target", &target);
4147 	if (target != NULL) {
4148 		const char *cp = strchr(target, '@');
4149 
4150 		/*
4151 		 * The snap name must contain an @, and the part after it must
4152 		 * contain only valid characters.
4153 		 */
4154 		if (cp == NULL ||
4155 		    zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4156 			return (SET_ERROR(EINVAL));
4157 	}
4158 
4159 	if (getzfsvfs(fsname, &zfsvfs) == 0) {
4160 		dsl_dataset_t *ds;
4161 
4162 		ds = dmu_objset_ds(zfsvfs->z_os);
4163 		error = zfs_suspend_fs(zfsvfs);
4164 		if (error == 0) {
4165 			int resume_err;
4166 
4167 			error = dsl_dataset_rollback(fsname, target, zfsvfs,
4168 			    outnvl);
4169 			resume_err = zfs_resume_fs(zfsvfs, ds);
4170 			error = error ? error : resume_err;
4171 		}
4172 		VFS_RELE(zfsvfs->z_vfs);
4173 	} else {
4174 		error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4175 	}
4176 	return (error);
4177 }
4178 
4179 static int
4180 recursive_unmount(const char *fsname, void *arg)
4181 {
4182 	const char *snapname = arg;
4183 	char fullname[ZFS_MAX_DATASET_NAME_LEN];
4184 
4185 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
4186 	zfs_unmount_snap(fullname);
4187 
4188 	return (0);
4189 }
4190 
4191 /*
4192  * inputs:
4193  * zc_name	old name of dataset
4194  * zc_value	new name of dataset
4195  * zc_cookie	recursive flag (only valid for snapshots)
4196  *
4197  * outputs:	none
4198  */
4199 static int
4200 zfs_ioc_rename(zfs_cmd_t *zc)
4201 {
4202 	objset_t *os;
4203 	dmu_objset_type_t ost;
4204 	boolean_t recursive = zc->zc_cookie & 1;
4205 	char *at;
4206 	int err;
4207 
4208 	/* "zfs rename" from and to ...%recv datasets should both fail */
4209 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4210 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4211 	if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4212 	    dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4213 	    strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4214 		return (SET_ERROR(EINVAL));
4215 
4216 	err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4217 	if (err != 0)
4218 		return (err);
4219 	ost = dmu_objset_type(os);
4220 	dmu_objset_rele(os, FTAG);
4221 
4222 	at = strchr(zc->zc_name, '@');
4223 	if (at != NULL) {
4224 		/* snaps must be in same fs */
4225 		int error;
4226 
4227 		if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4228 			return (SET_ERROR(EXDEV));
4229 		*at = '\0';
4230 		if (ost == DMU_OST_ZFS) {
4231 			error = dmu_objset_find(zc->zc_name,
4232 			    recursive_unmount, at + 1,
4233 			    recursive ? DS_FIND_CHILDREN : 0);
4234 			if (error != 0) {
4235 				*at = '@';
4236 				return (error);
4237 			}
4238 		}
4239 		error = dsl_dataset_rename_snapshot(zc->zc_name,
4240 		    at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4241 		*at = '@';
4242 
4243 		return (error);
4244 	} else {
4245 		if (ost == DMU_OST_ZVOL)
4246 			(void) zvol_remove_minor(zc->zc_name);
4247 		return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4248 	}
4249 }
4250 
4251 static int
4252 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4253 {
4254 	const char *propname = nvpair_name(pair);
4255 	boolean_t issnap = (strchr(dsname, '@') != NULL);
4256 	zfs_prop_t prop = zfs_name_to_prop(propname);
4257 	uint64_t intval;
4258 	int err;
4259 
4260 	if (prop == ZPROP_INVAL) {
4261 		if (zfs_prop_user(propname)) {
4262 			if (err = zfs_secpolicy_write_perms(dsname,
4263 			    ZFS_DELEG_PERM_USERPROP, cr))
4264 				return (err);
4265 			return (0);
4266 		}
4267 
4268 		if (!issnap && zfs_prop_userquota(propname)) {
4269 			const char *perm = NULL;
4270 			const char *uq_prefix =
4271 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4272 			const char *gq_prefix =
4273 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4274 			const char *uiq_prefix =
4275 			    zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4276 			const char *giq_prefix =
4277 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4278 			const char *pq_prefix =
4279 			    zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4280 			const char *piq_prefix = zfs_userquota_prop_prefixes[\
4281 			    ZFS_PROP_PROJECTOBJQUOTA];
4282 
4283 			if (strncmp(propname, uq_prefix,
4284 			    strlen(uq_prefix)) == 0) {
4285 				perm = ZFS_DELEG_PERM_USERQUOTA;
4286 			} else if (strncmp(propname, uiq_prefix,
4287 			    strlen(uiq_prefix)) == 0) {
4288 				perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4289 			} else if (strncmp(propname, gq_prefix,
4290 			    strlen(gq_prefix)) == 0) {
4291 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
4292 			} else if (strncmp(propname, giq_prefix,
4293 			    strlen(giq_prefix)) == 0) {
4294 				perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4295 			} else if (strncmp(propname, pq_prefix,
4296 			    strlen(pq_prefix)) == 0) {
4297 				perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4298 			} else if (strncmp(propname, piq_prefix,
4299 			    strlen(piq_prefix)) == 0) {
4300 				perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4301 			} else {
4302 				/* {USER|GROUP|PROJECT}USED are read-only */
4303 				return (SET_ERROR(EINVAL));
4304 			}
4305 
4306 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
4307 				return (err);
4308 			return (0);
4309 		}
4310 
4311 		return (SET_ERROR(EINVAL));
4312 	}
4313 
4314 	if (issnap)
4315 		return (SET_ERROR(EINVAL));
4316 
4317 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4318 		/*
4319 		 * dsl_prop_get_all_impl() returns properties in this
4320 		 * format.
4321 		 */
4322 		nvlist_t *attrs;
4323 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4324 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4325 		    &pair) == 0);
4326 	}
4327 
4328 	/*
4329 	 * Check that this value is valid for this pool version
4330 	 */
4331 	switch (prop) {
4332 	case ZFS_PROP_COMPRESSION:
4333 		/*
4334 		 * If the user specified gzip compression, make sure
4335 		 * the SPA supports it. We ignore any errors here since
4336 		 * we'll catch them later.
4337 		 */
4338 		if (nvpair_value_uint64(pair, &intval) == 0) {
4339 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
4340 			    intval <= ZIO_COMPRESS_GZIP_9 &&
4341 			    zfs_earlier_version(dsname,
4342 			    SPA_VERSION_GZIP_COMPRESSION)) {
4343 				return (SET_ERROR(ENOTSUP));
4344 			}
4345 
4346 			if (intval == ZIO_COMPRESS_ZLE &&
4347 			    zfs_earlier_version(dsname,
4348 			    SPA_VERSION_ZLE_COMPRESSION))
4349 				return (SET_ERROR(ENOTSUP));
4350 
4351 			if (intval == ZIO_COMPRESS_LZ4) {
4352 				spa_t *spa;
4353 
4354 				if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4355 					return (err);
4356 
4357 				if (!spa_feature_is_enabled(spa,
4358 				    SPA_FEATURE_LZ4_COMPRESS)) {
4359 					spa_close(spa, FTAG);
4360 					return (SET_ERROR(ENOTSUP));
4361 				}
4362 				spa_close(spa, FTAG);
4363 			}
4364 
4365 			/*
4366 			 * If this is a bootable dataset then
4367 			 * verify that the compression algorithm
4368 			 * is supported for booting. We must return
4369 			 * something other than ENOTSUP since it
4370 			 * implies a downrev pool version.
4371 			 */
4372 			if (zfs_is_bootfs(dsname) &&
4373 			    !BOOTFS_COMPRESS_VALID(intval)) {
4374 				return (SET_ERROR(ERANGE));
4375 			}
4376 		}
4377 		break;
4378 
4379 	case ZFS_PROP_COPIES:
4380 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4381 			return (SET_ERROR(ENOTSUP));
4382 		break;
4383 
4384 	case ZFS_PROP_RECORDSIZE:
4385 		/* Record sizes above 128k need the feature to be enabled */
4386 		if (nvpair_value_uint64(pair, &intval) == 0 &&
4387 		    intval > SPA_OLD_MAXBLOCKSIZE) {
4388 			spa_t *spa;
4389 
4390 			/*
4391 			 * We don't allow setting the property above 1MB,
4392 			 * unless the tunable has been changed.
4393 			 */
4394 			if (intval > zfs_max_recordsize ||
4395 			    intval > SPA_MAXBLOCKSIZE)
4396 				return (SET_ERROR(ERANGE));
4397 
4398 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4399 				return (err);
4400 
4401 			if (!spa_feature_is_enabled(spa,
4402 			    SPA_FEATURE_LARGE_BLOCKS)) {
4403 				spa_close(spa, FTAG);
4404 				return (SET_ERROR(ENOTSUP));
4405 			}
4406 			spa_close(spa, FTAG);
4407 		}
4408 		break;
4409 
4410 	case ZFS_PROP_DNODESIZE:
4411 		/* Dnode sizes above 512 need the feature to be enabled */
4412 		if (nvpair_value_uint64(pair, &intval) == 0 &&
4413 		    intval != ZFS_DNSIZE_LEGACY) {
4414 			spa_t *spa;
4415 
4416 			if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4417 				return (err);
4418 
4419 			if (!spa_feature_is_enabled(spa,
4420 			    SPA_FEATURE_LARGE_DNODE)) {
4421 				spa_close(spa, FTAG);
4422 				return (SET_ERROR(ENOTSUP));
4423 			}
4424 			spa_close(spa, FTAG);
4425 		}
4426 		break;
4427 
4428 	case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4429 		/*
4430 		 * This property could require the allocation classes
4431 		 * feature to be active for setting, however we allow
4432 		 * it so that tests of settable properties succeed.
4433 		 * The CLI will issue a warning in this case.
4434 		 */
4435 		break;
4436 
4437 	case ZFS_PROP_SHARESMB:
4438 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4439 			return (SET_ERROR(ENOTSUP));
4440 		break;
4441 
4442 	case ZFS_PROP_ACLINHERIT:
4443 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4444 		    nvpair_value_uint64(pair, &intval) == 0) {
4445 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
4446 			    zfs_earlier_version(dsname,
4447 			    SPA_VERSION_PASSTHROUGH_X))
4448 				return (SET_ERROR(ENOTSUP));
4449 		}
4450 		break;
4451 
4452 	case ZFS_PROP_CHECKSUM:
4453 	case ZFS_PROP_DEDUP:
4454 	{
4455 		spa_feature_t feature;
4456 		spa_t *spa;
4457 
4458 		/* dedup feature version checks */
4459 		if (prop == ZFS_PROP_DEDUP &&
4460 		    zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4461 			return (SET_ERROR(ENOTSUP));
4462 
4463 		if (nvpair_value_uint64(pair, &intval) != 0)
4464 			return (SET_ERROR(EINVAL));
4465 
4466 		/* check prop value is enabled in features */
4467 		feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4468 		if (feature == SPA_FEATURE_NONE)
4469 			break;
4470 
4471 		if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4472 			return (err);
4473 
4474 		if (!spa_feature_is_enabled(spa, feature)) {
4475 			spa_close(spa, FTAG);
4476 			return (SET_ERROR(ENOTSUP));
4477 		}
4478 		spa_close(spa, FTAG);
4479 		break;
4480 	}
4481 	}
4482 
4483 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4484 }
4485 
4486 /*
4487  * Checks for a race condition to make sure we don't increment a feature flag
4488  * multiple times.
4489  */
4490 static int
4491 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4492 {
4493 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4494 	spa_feature_t *featurep = arg;
4495 
4496 	if (!spa_feature_is_active(spa, *featurep))
4497 		return (0);
4498 	else
4499 		return (SET_ERROR(EBUSY));
4500 }
4501 
4502 /*
4503  * The callback invoked on feature activation in the sync task caused by
4504  * zfs_prop_activate_feature.
4505  */
4506 static void
4507 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4508 {
4509 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4510 	spa_feature_t *featurep = arg;
4511 
4512 	spa_feature_incr(spa, *featurep, tx);
4513 }
4514 
4515 /*
4516  * Activates a feature on a pool in response to a property setting. This
4517  * creates a new sync task which modifies the pool to reflect the feature
4518  * as being active.
4519  */
4520 static int
4521 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4522 {
4523 	int err;
4524 
4525 	/* EBUSY here indicates that the feature is already active */
4526 	err = dsl_sync_task(spa_name(spa),
4527 	    zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4528 	    &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4529 
4530 	if (err != 0 && err != EBUSY)
4531 		return (err);
4532 	else
4533 		return (0);
4534 }
4535 
4536 /*
4537  * Removes properties from the given props list that fail permission checks
4538  * needed to clear them and to restore them in case of a receive error. For each
4539  * property, make sure we have both set and inherit permissions.
4540  *
4541  * Returns the first error encountered if any permission checks fail. If the
4542  * caller provides a non-NULL errlist, it also gives the complete list of names
4543  * of all the properties that failed a permission check along with the
4544  * corresponding error numbers. The caller is responsible for freeing the
4545  * returned errlist.
4546  *
4547  * If every property checks out successfully, zero is returned and the list
4548  * pointed at by errlist is NULL.
4549  */
4550 static int
4551 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4552 {
4553 	zfs_cmd_t *zc;
4554 	nvpair_t *pair, *next_pair;
4555 	nvlist_t *errors;
4556 	int err, rv = 0;
4557 
4558 	if (props == NULL)
4559 		return (0);
4560 
4561 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4562 
4563 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4564 	(void) strcpy(zc->zc_name, dataset);
4565 	pair = nvlist_next_nvpair(props, NULL);
4566 	while (pair != NULL) {
4567 		next_pair = nvlist_next_nvpair(props, pair);
4568 
4569 		(void) strcpy(zc->zc_value, nvpair_name(pair));
4570 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4571 		    (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4572 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4573 			VERIFY(nvlist_add_int32(errors,
4574 			    zc->zc_value, err) == 0);
4575 		}
4576 		pair = next_pair;
4577 	}
4578 	kmem_free(zc, sizeof (zfs_cmd_t));
4579 
4580 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4581 		nvlist_free(errors);
4582 		errors = NULL;
4583 	} else {
4584 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
4585 	}
4586 
4587 	if (errlist == NULL)
4588 		nvlist_free(errors);
4589 	else
4590 		*errlist = errors;
4591 
4592 	return (rv);
4593 }
4594 
4595 static boolean_t
4596 propval_equals(nvpair_t *p1, nvpair_t *p2)
4597 {
4598 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4599 		/* dsl_prop_get_all_impl() format */
4600 		nvlist_t *attrs;
4601 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4602 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4603 		    &p1) == 0);
4604 	}
4605 
4606 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4607 		nvlist_t *attrs;
4608 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4609 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4610 		    &p2) == 0);
4611 	}
4612 
4613 	if (nvpair_type(p1) != nvpair_type(p2))
4614 		return (B_FALSE);
4615 
4616 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
4617 		char *valstr1, *valstr2;
4618 
4619 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4620 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4621 		return (strcmp(valstr1, valstr2) == 0);
4622 	} else {
4623 		uint64_t intval1, intval2;
4624 
4625 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4626 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4627 		return (intval1 == intval2);
4628 	}
4629 }
4630 
4631 /*
4632  * Remove properties from props if they are not going to change (as determined
4633  * by comparison with origprops). Remove them from origprops as well, since we
4634  * do not need to clear or restore properties that won't change.
4635  */
4636 static void
4637 props_reduce(nvlist_t *props, nvlist_t *origprops)
4638 {
4639 	nvpair_t *pair, *next_pair;
4640 
4641 	if (origprops == NULL)
4642 		return; /* all props need to be received */
4643 
4644 	pair = nvlist_next_nvpair(props, NULL);
4645 	while (pair != NULL) {
4646 		const char *propname = nvpair_name(pair);
4647 		nvpair_t *match;
4648 
4649 		next_pair = nvlist_next_nvpair(props, pair);
4650 
4651 		if ((nvlist_lookup_nvpair(origprops, propname,
4652 		    &match) != 0) || !propval_equals(pair, match))
4653 			goto next; /* need to set received value */
4654 
4655 		/* don't clear the existing received value */
4656 		(void) nvlist_remove_nvpair(origprops, match);
4657 		/* don't bother receiving the property */
4658 		(void) nvlist_remove_nvpair(props, pair);
4659 next:
4660 		pair = next_pair;
4661 	}
4662 }
4663 
4664 /*
4665  * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4666  * For example, refquota cannot be set until after the receipt of a dataset,
4667  * because in replication streams, an older/earlier snapshot may exceed the
4668  * refquota.  We want to receive the older/earlier snapshot, but setting
4669  * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4670  * the older/earlier snapshot from being received (with EDQUOT).
4671  *
4672  * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4673  *
4674  * libzfs will need to be judicious handling errors encountered by props
4675  * extracted by this function.
4676  */
4677 static nvlist_t *
4678 extract_delay_props(nvlist_t *props)
4679 {
4680 	nvlist_t *delayprops;
4681 	nvpair_t *nvp, *tmp;
4682 	static const zfs_prop_t delayable[] = {
4683 		ZFS_PROP_REFQUOTA,
4684 		ZFS_PROP_KEYLOCATION,
4685 		0
4686 	};
4687 	int i;
4688 
4689 	VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4690 
4691 	for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4692 	    nvp = nvlist_next_nvpair(props, nvp)) {
4693 		/*
4694 		 * strcmp() is safe because zfs_prop_to_name() always returns
4695 		 * a bounded string.
4696 		 */
4697 		for (i = 0; delayable[i] != 0; i++) {
4698 			if (strcmp(zfs_prop_to_name(delayable[i]),
4699 			    nvpair_name(nvp)) == 0) {
4700 				break;
4701 			}
4702 		}
4703 		if (delayable[i] != 0) {
4704 			tmp = nvlist_prev_nvpair(props, nvp);
4705 			VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4706 			VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4707 			nvp = tmp;
4708 		}
4709 	}
4710 
4711 	if (nvlist_empty(delayprops)) {
4712 		nvlist_free(delayprops);
4713 		delayprops = NULL;
4714 	}
4715 	return (delayprops);
4716 }
4717 
4718 #ifdef	DEBUG
4719 static boolean_t zfs_ioc_recv_inject_err;
4720 #endif
4721 
4722 /*
4723  * nvlist 'errors' is always allocated. It will contain descriptions of
4724  * encountered errors, if any. It's the callers responsibility to free.
4725  */
4726 static int
4727 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4728     nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4729     boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4730     int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4731     uint64_t *action_handle, nvlist_t **errors)
4732 {
4733 	dmu_recv_cookie_t drc;
4734 	int error = 0;
4735 	int props_error = 0;
4736 	offset_t off;
4737 	nvlist_t *local_delayprops = NULL;
4738 	nvlist_t *recv_delayprops = NULL;
4739 	nvlist_t *origprops = NULL; /* existing properties */
4740 	nvlist_t *origrecvd = NULL; /* existing received properties */
4741 	boolean_t first_recvd_props = B_FALSE;
4742 	file_t *input_fp;
4743 
4744 	*read_bytes = 0;
4745 	*errflags = 0;
4746 	*errors = fnvlist_alloc();
4747 
4748 	input_fp = getf(input_fd);
4749 	if (input_fp == NULL)
4750 		return (SET_ERROR(EBADF));
4751 
4752 	error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4753 	    resumable, localprops, hidden_args, origin, &drc);
4754 	if (error != 0)
4755 		goto out;
4756 
4757 	/*
4758 	 * Set properties before we receive the stream so that they are applied
4759 	 * to the new data. Note that we must call dmu_recv_stream() if
4760 	 * dmu_recv_begin() succeeds.
4761 	 */
4762 	if (recvprops != NULL && !drc.drc_newfs) {
4763 		if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4764 		    SPA_VERSION_RECVD_PROPS &&
4765 		    !dsl_prop_get_hasrecvd(tofs))
4766 			first_recvd_props = B_TRUE;
4767 
4768 		/*
4769 		 * If new received properties are supplied, they are to
4770 		 * completely replace the existing received properties,
4771 		 * so stash away the existing ones.
4772 		 */
4773 		if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4774 			nvlist_t *errlist = NULL;
4775 			/*
4776 			 * Don't bother writing a property if its value won't
4777 			 * change (and avoid the unnecessary security checks).
4778 			 *
4779 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
4780 			 * special case where we blow away all local properties
4781 			 * regardless.
4782 			 */
4783 			if (!first_recvd_props)
4784 				props_reduce(recvprops, origrecvd);
4785 			if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4786 				(void) nvlist_merge(*errors, errlist, 0);
4787 			nvlist_free(errlist);
4788 
4789 			if (clear_received_props(tofs, origrecvd,
4790 			    first_recvd_props ? NULL : recvprops) != 0)
4791 				*errflags |= ZPROP_ERR_NOCLEAR;
4792 		} else {
4793 			*errflags |= ZPROP_ERR_NOCLEAR;
4794 		}
4795 	}
4796 
4797 	/*
4798 	 * Stash away existing properties so we can restore them on error unless
4799 	 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4800 	 * case "origrecvd" will take care of that.
4801 	 */
4802 	if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4803 		objset_t *os;
4804 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4805 			if (dsl_prop_get_all(os, &origprops) != 0) {
4806 				*errflags |= ZPROP_ERR_NOCLEAR;
4807 			}
4808 			dmu_objset_rele(os, FTAG);
4809 		} else {
4810 			*errflags |= ZPROP_ERR_NOCLEAR;
4811 		}
4812 	}
4813 
4814 	if (recvprops != NULL) {
4815 		props_error = dsl_prop_set_hasrecvd(tofs);
4816 
4817 		if (props_error == 0) {
4818 			recv_delayprops = extract_delay_props(recvprops);
4819 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4820 			    recvprops, *errors);
4821 		}
4822 	}
4823 
4824 	if (localprops != NULL) {
4825 		nvlist_t *oprops = fnvlist_alloc();
4826 		nvlist_t *xprops = fnvlist_alloc();
4827 		nvpair_t *nvp = NULL;
4828 
4829 		while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4830 			if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4831 				/* -x property */
4832 				const char *name = nvpair_name(nvp);
4833 				zfs_prop_t prop = zfs_name_to_prop(name);
4834 				if (prop != ZPROP_INVAL) {
4835 					if (!zfs_prop_inheritable(prop))
4836 						continue;
4837 				} else if (!zfs_prop_user(name))
4838 					continue;
4839 				fnvlist_add_boolean(xprops, name);
4840 			} else {
4841 				/* -o property=value */
4842 				fnvlist_add_nvpair(oprops, nvp);
4843 			}
4844 		}
4845 
4846 		local_delayprops = extract_delay_props(oprops);
4847 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4848 		    oprops, *errors);
4849 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4850 		    xprops, *errors);
4851 
4852 		nvlist_free(oprops);
4853 		nvlist_free(xprops);
4854 	}
4855 
4856 	off = input_fp->f_offset;
4857 	error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4858 	    action_handle);
4859 
4860 	if (error == 0) {
4861 		zfsvfs_t *zfsvfs = NULL;
4862 
4863 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
4864 			/* online recv */
4865 			dsl_dataset_t *ds;
4866 			int end_err;
4867 
4868 			ds = dmu_objset_ds(zfsvfs->z_os);
4869 			error = zfs_suspend_fs(zfsvfs);
4870 			/*
4871 			 * If the suspend fails, then the recv_end will
4872 			 * likely also fail, and clean up after itself.
4873 			 */
4874 			end_err = dmu_recv_end(&drc, zfsvfs);
4875 			if (error == 0)
4876 				error = zfs_resume_fs(zfsvfs, ds);
4877 			error = error ? error : end_err;
4878 			VFS_RELE(zfsvfs->z_vfs);
4879 		} else {
4880 			error = dmu_recv_end(&drc, NULL);
4881 		}
4882 
4883 		/* Set delayed properties now, after we're done receiving. */
4884 		if (recv_delayprops != NULL && error == 0) {
4885 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4886 			    recv_delayprops, *errors);
4887 		}
4888 		if (local_delayprops != NULL && error == 0) {
4889 			(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4890 			    local_delayprops, *errors);
4891 		}
4892 	}
4893 
4894 	/*
4895 	 * Merge delayed props back in with initial props, in case
4896 	 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4897 	 * we have to make sure clear_received_props() includes
4898 	 * the delayed properties).
4899 	 *
4900 	 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4901 	 * using ASSERT() will be just like a VERIFY.
4902 	 */
4903 	if (recv_delayprops != NULL) {
4904 		ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4905 		nvlist_free(recv_delayprops);
4906 	}
4907 	if (local_delayprops != NULL) {
4908 		ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4909 		nvlist_free(local_delayprops);
4910 	}
4911 
4912 	*read_bytes = off - input_fp->f_offset;
4913 	if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4914 		input_fp->f_offset = off;
4915 
4916 #ifdef	DEBUG
4917 	if (zfs_ioc_recv_inject_err) {
4918 		zfs_ioc_recv_inject_err = B_FALSE;
4919 		error = 1;
4920 	}
4921 #endif
4922 
4923 	/*
4924 	 * On error, restore the original props.
4925 	 */
4926 	if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4927 		if (clear_received_props(tofs, recvprops, NULL) != 0) {
4928 			/*
4929 			 * We failed to clear the received properties.
4930 			 * Since we may have left a $recvd value on the
4931 			 * system, we can't clear the $hasrecvd flag.
4932 			 */
4933 			*errflags |= ZPROP_ERR_NORESTORE;
4934 		} else if (first_recvd_props) {
4935 			dsl_prop_unset_hasrecvd(tofs);
4936 		}
4937 
4938 		if (origrecvd == NULL && !drc.drc_newfs) {
4939 			/* We failed to stash the original properties. */
4940 			*errflags |= ZPROP_ERR_NORESTORE;
4941 		}
4942 
4943 		/*
4944 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4945 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4946 		 * explicitly if we're restoring local properties cleared in the
4947 		 * first new-style receive.
4948 		 */
4949 		if (origrecvd != NULL &&
4950 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4951 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4952 		    origrecvd, NULL) != 0) {
4953 			/*
4954 			 * We stashed the original properties but failed to
4955 			 * restore them.
4956 			 */
4957 			*errflags |= ZPROP_ERR_NORESTORE;
4958 		}
4959 	}
4960 	if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4961 	    !first_recvd_props) {
4962 		nvlist_t *setprops;
4963 		nvlist_t *inheritprops;
4964 		nvpair_t *nvp;
4965 
4966 		if (origprops == NULL) {
4967 			/* We failed to stash the original properties. */
4968 			*errflags |= ZPROP_ERR_NORESTORE;
4969 			goto out;
4970 		}
4971 
4972 		/* Restore original props */
4973 		setprops = fnvlist_alloc();
4974 		inheritprops = fnvlist_alloc();
4975 		nvp = NULL;
4976 		while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4977 			const char *name = nvpair_name(nvp);
4978 			const char *source;
4979 			nvlist_t *attrs;
4980 
4981 			if (!nvlist_exists(origprops, name)) {
4982 				/*
4983 				 * Property was not present or was explicitly
4984 				 * inherited before the receive, restore this.
4985 				 */
4986 				fnvlist_add_boolean(inheritprops, name);
4987 				continue;
4988 			}
4989 			attrs = fnvlist_lookup_nvlist(origprops, name);
4990 			source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4991 
4992 			/* Skip received properties */
4993 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4994 				continue;
4995 
4996 			if (strcmp(source, tofs) == 0) {
4997 				/* Property was locally set */
4998 				fnvlist_add_nvlist(setprops, name, attrs);
4999 			} else {
5000 				/* Property was implicitly inherited */
5001 				fnvlist_add_boolean(inheritprops, name);
5002 			}
5003 		}
5004 
5005 		if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5006 		    NULL) != 0)
5007 			*errflags |= ZPROP_ERR_NORESTORE;
5008 		if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5009 		    NULL) != 0)
5010 			*errflags |= ZPROP_ERR_NORESTORE;
5011 
5012 		nvlist_free(setprops);
5013 		nvlist_free(inheritprops);
5014 	}
5015 out:
5016 	releasef(input_fd);
5017 	nvlist_free(origrecvd);
5018 	nvlist_free(origprops);
5019 
5020 	if (error == 0)
5021 		error = props_error;
5022 
5023 	return (error);
5024 }
5025 
5026 /*
5027  * inputs:
5028  * zc_name		name of containing filesystem
5029  * zc_nvlist_src{_size}	nvlist of received properties to apply
5030  * zc_nvlist_conf{_size} nvlist of local properties to apply
5031  * zc_history_offset{_len} nvlist of hidden args { "wkeydata" -> value }
5032  * zc_value		name of snapshot to create
5033  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
5034  * zc_cookie		file descriptor to recv from
5035  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
5036  * zc_guid		force flag
5037  * zc_cleanup_fd	cleanup-on-exit file descriptor
5038  * zc_action_handle	handle for this guid/ds mapping (or zero on first call)
5039  * zc_resumable		if data is incomplete assume sender will resume
5040  *
5041  * outputs:
5042  * zc_cookie		number of bytes read
5043  * zc_nvlist_dst{_size} error for each unapplied received property
5044  * zc_obj		zprop_errflags_t
5045  * zc_action_handle	handle for this guid/ds mapping
5046  */
5047 static int
5048 zfs_ioc_recv(zfs_cmd_t *zc)
5049 {
5050 	dmu_replay_record_t begin_record;
5051 	nvlist_t *errors = NULL;
5052 	nvlist_t *recvdprops = NULL;
5053 	nvlist_t *localprops = NULL;
5054 	nvlist_t *hidden_args = NULL;
5055 	char *origin = NULL;
5056 	char *tosnap;
5057 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
5058 	int error = 0;
5059 
5060 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5061 	    strchr(zc->zc_value, '@') == NULL ||
5062 	    strchr(zc->zc_value, '%'))
5063 		return (SET_ERROR(EINVAL));
5064 
5065 	(void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5066 	tosnap = strchr(tofs, '@');
5067 	*tosnap++ = '\0';
5068 
5069 	if (zc->zc_nvlist_src != 0 &&
5070 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5071 	    zc->zc_iflags, &recvdprops)) != 0)
5072 		return (error);
5073 
5074 	if (zc->zc_nvlist_conf != 0 &&
5075 	    (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5076 	    zc->zc_iflags, &localprops)) != 0)
5077 		return (error);
5078 
5079 	if (zc->zc_history_offset != 0 &&
5080 	    (error = get_nvlist(zc->zc_history_offset, zc->zc_history_len,
5081 	    zc->zc_iflags, &hidden_args)) != 0)
5082 		return (error);
5083 
5084 	if (zc->zc_string[0])
5085 		origin = zc->zc_string;
5086 
5087 	begin_record.drr_type = DRR_BEGIN;
5088 	begin_record.drr_payloadlen = zc->zc_begin_record.drr_payloadlen;
5089 	begin_record.drr_u.drr_begin = zc->zc_begin_record.drr_u.drr_begin;
5090 
5091 	error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5092 	    hidden_args, zc->zc_guid, zc->zc_resumable, zc->zc_cookie,
5093 	    &begin_record, zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
5094 	    &zc->zc_action_handle, &errors);
5095 	nvlist_free(recvdprops);
5096 	nvlist_free(localprops);
5097 
5098 	/*
5099 	 * Now that all props, initial and delayed, are set, report the prop
5100 	 * errors to the caller.
5101 	 */
5102 	if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5103 	    (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5104 	    put_nvlist(zc, errors) != 0)) {
5105 		/*
5106 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
5107 		 * size or supplied an invalid address.
5108 		 */
5109 		error = SET_ERROR(EINVAL);
5110 	}
5111 
5112 	nvlist_free(errors);
5113 
5114 	return (error);
5115 }
5116 
5117 /*
5118  * inputs:
5119  * zc_name	name of snapshot to send
5120  * zc_cookie	file descriptor to send stream to
5121  * zc_obj	fromorigin flag (mutually exclusive with zc_fromobj)
5122  * zc_sendobj	objsetid of snapshot to send
5123  * zc_fromobj	objsetid of incremental fromsnap (may be zero)
5124  * zc_guid	if set, estimate size of stream only.  zc_cookie is ignored.
5125  *		output size in zc_objset_type.
5126  * zc_flags	lzc_send_flags
5127  *
5128  * outputs:
5129  * zc_objset_type	estimated size, if zc_guid is set
5130  */
5131 static int
5132 zfs_ioc_send(zfs_cmd_t *zc)
5133 {
5134 	int error;
5135 	offset_t off;
5136 	boolean_t estimate = (zc->zc_guid != 0);
5137 	boolean_t embedok = (zc->zc_flags & 0x1);
5138 	boolean_t large_block_ok = (zc->zc_flags & 0x2);
5139 	boolean_t compressok = (zc->zc_flags & 0x4);
5140 	boolean_t rawok = (zc->zc_flags & 0x8);
5141 
5142 	if (zc->zc_obj != 0) {
5143 		dsl_pool_t *dp;
5144 		dsl_dataset_t *tosnap;
5145 
5146 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5147 		if (error != 0)
5148 			return (error);
5149 
5150 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5151 		if (error != 0) {
5152 			dsl_pool_rele(dp, FTAG);
5153 			return (error);
5154 		}
5155 
5156 		if (dsl_dir_is_clone(tosnap->ds_dir))
5157 			zc->zc_fromobj =
5158 			    dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5159 		dsl_dataset_rele(tosnap, FTAG);
5160 		dsl_pool_rele(dp, FTAG);
5161 	}
5162 
5163 	if (estimate) {
5164 		dsl_pool_t *dp;
5165 		dsl_dataset_t *tosnap;
5166 		dsl_dataset_t *fromsnap = NULL;
5167 
5168 		error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5169 		if (error != 0)
5170 			return (error);
5171 
5172 		error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5173 		    FTAG, &tosnap);
5174 		if (error != 0) {
5175 			dsl_pool_rele(dp, FTAG);
5176 			return (error);
5177 		}
5178 
5179 		if (zc->zc_fromobj != 0) {
5180 			error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5181 			    FTAG, &fromsnap);
5182 			if (error != 0) {
5183 				dsl_dataset_rele(tosnap, FTAG);
5184 				dsl_pool_rele(dp, FTAG);
5185 				return (error);
5186 			}
5187 		}
5188 
5189 		error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
5190 		    &zc->zc_objset_type);
5191 
5192 		if (fromsnap != NULL)
5193 			dsl_dataset_rele(fromsnap, FTAG);
5194 		dsl_dataset_rele(tosnap, FTAG);
5195 		dsl_pool_rele(dp, FTAG);
5196 	} else {
5197 		file_t *fp = getf(zc->zc_cookie);
5198 		if (fp == NULL)
5199 			return (SET_ERROR(EBADF));
5200 
5201 		off = fp->f_offset;
5202 		error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5203 		    zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
5204 		    zc->zc_cookie, fp->f_vnode, &off);
5205 
5206 		if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5207 			fp->f_offset = off;
5208 		releasef(zc->zc_cookie);
5209 	}
5210 	return (error);
5211 }
5212 
5213 /*
5214  * inputs:
5215  * zc_name	name of snapshot on which to report progress
5216  * zc_cookie	file descriptor of send stream
5217  *
5218  * outputs:
5219  * zc_cookie	number of bytes written in send stream thus far
5220  */
5221 static int
5222 zfs_ioc_send_progress(zfs_cmd_t *zc)
5223 {
5224 	dsl_pool_t *dp;
5225 	dsl_dataset_t *ds;
5226 	dmu_sendarg_t *dsp = NULL;
5227 	int error;
5228 
5229 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5230 	if (error != 0)
5231 		return (error);
5232 
5233 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5234 	if (error != 0) {
5235 		dsl_pool_rele(dp, FTAG);
5236 		return (error);
5237 	}
5238 
5239 	mutex_enter(&ds->ds_sendstream_lock);
5240 
5241 	/*
5242 	 * Iterate over all the send streams currently active on this dataset.
5243 	 * If there's one which matches the specified file descriptor _and_ the
5244 	 * stream was started by the current process, return the progress of
5245 	 * that stream.
5246 	 */
5247 	for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5248 	    dsp = list_next(&ds->ds_sendstreams, dsp)) {
5249 		if (dsp->dsa_outfd == zc->zc_cookie &&
5250 		    dsp->dsa_proc == curproc)
5251 			break;
5252 	}
5253 
5254 	if (dsp != NULL)
5255 		zc->zc_cookie = *(dsp->dsa_off);
5256 	else
5257 		error = SET_ERROR(ENOENT);
5258 
5259 	mutex_exit(&ds->ds_sendstream_lock);
5260 	dsl_dataset_rele(ds, FTAG);
5261 	dsl_pool_rele(dp, FTAG);
5262 	return (error);
5263 }
5264 
5265 static int
5266 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5267 {
5268 	int id, error;
5269 
5270 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5271 	    &zc->zc_inject_record);
5272 
5273 	if (error == 0)
5274 		zc->zc_guid = (uint64_t)id;
5275 
5276 	return (error);
5277 }
5278 
5279 static int
5280 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5281 {
5282 	return (zio_clear_fault((int)zc->zc_guid));
5283 }
5284 
5285 static int
5286 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5287 {
5288 	int id = (int)zc->zc_guid;
5289 	int error;
5290 
5291 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5292 	    &zc->zc_inject_record);
5293 
5294 	zc->zc_guid = id;
5295 
5296 	return (error);
5297 }
5298 
5299 static int
5300 zfs_ioc_error_log(zfs_cmd_t *zc)
5301 {
5302 	spa_t *spa;
5303 	int error;
5304 	size_t count = (size_t)zc->zc_nvlist_dst_size;
5305 
5306 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5307 		return (error);
5308 
5309 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5310 	    &count);
5311 	if (error == 0)
5312 		zc->zc_nvlist_dst_size = count;
5313 	else
5314 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5315 
5316 	spa_close(spa, FTAG);
5317 
5318 	return (error);
5319 }
5320 
5321 static int
5322 zfs_ioc_clear(zfs_cmd_t *zc)
5323 {
5324 	spa_t *spa;
5325 	vdev_t *vd;
5326 	int error;
5327 
5328 	/*
5329 	 * On zpool clear we also fix up missing slogs
5330 	 */
5331 	mutex_enter(&spa_namespace_lock);
5332 	spa = spa_lookup(zc->zc_name);
5333 	if (spa == NULL) {
5334 		mutex_exit(&spa_namespace_lock);
5335 		return (SET_ERROR(EIO));
5336 	}
5337 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5338 		/* we need to let spa_open/spa_load clear the chains */
5339 		spa_set_log_state(spa, SPA_LOG_CLEAR);
5340 	}
5341 	spa->spa_last_open_failed = 0;
5342 	mutex_exit(&spa_namespace_lock);
5343 
5344 	if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5345 		error = spa_open(zc->zc_name, &spa, FTAG);
5346 	} else {
5347 		nvlist_t *policy;
5348 		nvlist_t *config = NULL;
5349 
5350 		if (zc->zc_nvlist_src == 0)
5351 			return (SET_ERROR(EINVAL));
5352 
5353 		if ((error = get_nvlist(zc->zc_nvlist_src,
5354 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5355 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5356 			    policy, &config);
5357 			if (config != NULL) {
5358 				int err;
5359 
5360 				if ((err = put_nvlist(zc, config)) != 0)
5361 					error = err;
5362 				nvlist_free(config);
5363 			}
5364 			nvlist_free(policy);
5365 		}
5366 	}
5367 
5368 	if (error != 0)
5369 		return (error);
5370 
5371 	/*
5372 	 * If multihost is enabled, resuming I/O is unsafe as another
5373 	 * host may have imported the pool.
5374 	 */
5375 	if (spa_multihost(spa) && spa_suspended(spa))
5376 		return (SET_ERROR(EINVAL));
5377 
5378 	spa_vdev_state_enter(spa, SCL_NONE);
5379 
5380 	if (zc->zc_guid == 0) {
5381 		vd = NULL;
5382 	} else {
5383 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5384 		if (vd == NULL) {
5385 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
5386 			spa_close(spa, FTAG);
5387 			return (SET_ERROR(ENODEV));
5388 		}
5389 	}
5390 
5391 	vdev_clear(spa, vd);
5392 
5393 	(void) spa_vdev_state_exit(spa, NULL, 0);
5394 
5395 	/*
5396 	 * Resume any suspended I/Os.
5397 	 */
5398 	if (zio_resume(spa) != 0)
5399 		error = SET_ERROR(EIO);
5400 
5401 	spa_close(spa, FTAG);
5402 
5403 	return (error);
5404 }
5405 
5406 static int
5407 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
5408 {
5409 	spa_t *spa;
5410 	int error;
5411 
5412 	error = spa_open(zc->zc_name, &spa, FTAG);
5413 	if (error != 0)
5414 		return (error);
5415 
5416 	spa_vdev_state_enter(spa, SCL_NONE);
5417 
5418 	/*
5419 	 * If a resilver is already in progress then set the
5420 	 * spa_scrub_reopen flag to B_TRUE so that we don't restart
5421 	 * the scan as a side effect of the reopen. Otherwise, let
5422 	 * vdev_open() decided if a resilver is required.
5423 	 */
5424 	spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
5425 	vdev_reopen(spa->spa_root_vdev);
5426 	spa->spa_scrub_reopen = B_FALSE;
5427 
5428 	(void) spa_vdev_state_exit(spa, NULL, 0);
5429 	spa_close(spa, FTAG);
5430 	return (0);
5431 }
5432 /*
5433  * inputs:
5434  * zc_name	name of filesystem
5435  *
5436  * outputs:
5437  * zc_string	name of conflicting snapshot, if there is one
5438  */
5439 static int
5440 zfs_ioc_promote(zfs_cmd_t *zc)
5441 {
5442 	dsl_pool_t *dp;
5443 	dsl_dataset_t *ds, *ods;
5444 	char origin[ZFS_MAX_DATASET_NAME_LEN];
5445 	char *cp;
5446 	int error;
5447 
5448 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5449 	if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5450 	    strchr(zc->zc_name, '%'))
5451 		return (SET_ERROR(EINVAL));
5452 
5453 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5454 	if (error != 0)
5455 		return (error);
5456 
5457 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5458 	if (error != 0) {
5459 		dsl_pool_rele(dp, FTAG);
5460 		return (error);
5461 	}
5462 
5463 	if (!dsl_dir_is_clone(ds->ds_dir)) {
5464 		dsl_dataset_rele(ds, FTAG);
5465 		dsl_pool_rele(dp, FTAG);
5466 		return (SET_ERROR(EINVAL));
5467 	}
5468 
5469 	error = dsl_dataset_hold_obj(dp,
5470 	    dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5471 	if (error != 0) {
5472 		dsl_dataset_rele(ds, FTAG);
5473 		dsl_pool_rele(dp, FTAG);
5474 		return (error);
5475 	}
5476 
5477 	dsl_dataset_name(ods, origin);
5478 	dsl_dataset_rele(ods, FTAG);
5479 	dsl_dataset_rele(ds, FTAG);
5480 	dsl_pool_rele(dp, FTAG);
5481 
5482 	/*
5483 	 * We don't need to unmount *all* the origin fs's snapshots, but
5484 	 * it's easier.
5485 	 */
5486 	cp = strchr(origin, '@');
5487 	if (cp)
5488 		*cp = '\0';
5489 	(void) dmu_objset_find(origin,
5490 	    zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5491 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5492 }
5493 
5494 /*
5495  * Retrieve a single {user|group|project}{used|quota}@... property.
5496  *
5497  * inputs:
5498  * zc_name	name of filesystem
5499  * zc_objset_type zfs_userquota_prop_t
5500  * zc_value	domain name (eg. "S-1-234-567-89")
5501  * zc_guid	RID/UID/GID
5502  *
5503  * outputs:
5504  * zc_cookie	property value
5505  */
5506 static int
5507 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5508 {
5509 	zfsvfs_t *zfsvfs;
5510 	int error;
5511 
5512 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5513 		return (SET_ERROR(EINVAL));
5514 
5515 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5516 	if (error != 0)
5517 		return (error);
5518 
5519 	error = zfs_userspace_one(zfsvfs,
5520 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5521 	zfsvfs_rele(zfsvfs, FTAG);
5522 
5523 	return (error);
5524 }
5525 
5526 /*
5527  * inputs:
5528  * zc_name		name of filesystem
5529  * zc_cookie		zap cursor
5530  * zc_objset_type	zfs_userquota_prop_t
5531  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5532  *
5533  * outputs:
5534  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
5535  * zc_cookie	zap cursor
5536  */
5537 static int
5538 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5539 {
5540 	zfsvfs_t *zfsvfs;
5541 	int bufsize = zc->zc_nvlist_dst_size;
5542 
5543 	if (bufsize <= 0)
5544 		return (SET_ERROR(ENOMEM));
5545 
5546 	int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5547 	if (error != 0)
5548 		return (error);
5549 
5550 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
5551 
5552 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5553 	    buf, &zc->zc_nvlist_dst_size);
5554 
5555 	if (error == 0) {
5556 		error = xcopyout(buf,
5557 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
5558 		    zc->zc_nvlist_dst_size);
5559 	}
5560 	kmem_free(buf, bufsize);
5561 	zfsvfs_rele(zfsvfs, FTAG);
5562 
5563 	return (error);
5564 }
5565 
5566 /*
5567  * inputs:
5568  * zc_name		name of filesystem
5569  *
5570  * outputs:
5571  * none
5572  */
5573 static int
5574 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5575 {
5576 	objset_t *os;
5577 	int error = 0;
5578 	zfsvfs_t *zfsvfs;
5579 
5580 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5581 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5582 			/*
5583 			 * If userused is not enabled, it may be because the
5584 			 * objset needs to be closed & reopened (to grow the
5585 			 * objset_phys_t).  Suspend/resume the fs will do that.
5586 			 */
5587 			dsl_dataset_t *ds, *newds;
5588 
5589 			ds = dmu_objset_ds(zfsvfs->z_os);
5590 			error = zfs_suspend_fs(zfsvfs);
5591 			if (error == 0) {
5592 				dmu_objset_refresh_ownership(ds, &newds,
5593 				    B_TRUE, zfsvfs);
5594 				error = zfs_resume_fs(zfsvfs, newds);
5595 			}
5596 		}
5597 		if (error == 0)
5598 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5599 		VFS_RELE(zfsvfs->z_vfs);
5600 	} else {
5601 		/* XXX kind of reading contents without owning */
5602 		error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5603 		if (error != 0)
5604 			return (error);
5605 
5606 		error = dmu_objset_userspace_upgrade(os);
5607 		dmu_objset_rele_flags(os, B_TRUE, FTAG);
5608 	}
5609 
5610 	return (error);
5611 }
5612 
5613 /*
5614  * inputs:
5615  * zc_name		name of filesystem
5616  *
5617  * outputs:
5618  * none
5619  */
5620 static int
5621 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5622 {
5623 	objset_t *os;
5624 	int error;
5625 
5626 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5627 	if (error != 0)
5628 		return (error);
5629 
5630 	if (dmu_objset_userobjspace_upgradable(os) ||
5631 	    dmu_objset_projectquota_upgradable(os)) {
5632 		mutex_enter(&os->os_upgrade_lock);
5633 		if (os->os_upgrade_id == 0) {
5634 			/* clear potential error code and retry */
5635 			os->os_upgrade_status = 0;
5636 			mutex_exit(&os->os_upgrade_lock);
5637 
5638 			dmu_objset_id_quota_upgrade(os);
5639 		} else {
5640 			mutex_exit(&os->os_upgrade_lock);
5641 		}
5642 
5643 		dsl_pool_rele(dmu_objset_pool(os), FTAG);
5644 
5645 		taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5646 		error = os->os_upgrade_status;
5647 	} else {
5648 		dsl_pool_rele(dmu_objset_pool(os), FTAG);
5649 	}
5650 
5651 	dsl_dataset_rele(dmu_objset_ds(os), FTAG);
5652 
5653 	return (error);
5654 }
5655 
5656 /*
5657  * We don't want to have a hard dependency
5658  * against some special symbols in sharefs
5659  * nfs, and smbsrv.  Determine them if needed when
5660  * the first file system is shared.
5661  * Neither sharefs, nfs or smbsrv are unloadable modules.
5662  */
5663 int (*znfsexport_fs)(void *arg);
5664 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5665 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5666 
5667 int zfs_nfsshare_inited;
5668 int zfs_smbshare_inited;
5669 
5670 ddi_modhandle_t nfs_mod;
5671 ddi_modhandle_t sharefs_mod;
5672 ddi_modhandle_t smbsrv_mod;
5673 kmutex_t zfs_share_lock;
5674 
5675 static int
5676 zfs_init_sharefs()
5677 {
5678 	int error;
5679 
5680 	ASSERT(MUTEX_HELD(&zfs_share_lock));
5681 	/* Both NFS and SMB shares also require sharetab support. */
5682 	if (sharefs_mod == NULL && ((sharefs_mod =
5683 	    ddi_modopen("fs/sharefs",
5684 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
5685 		return (SET_ERROR(ENOSYS));
5686 	}
5687 	if (zshare_fs == NULL && ((zshare_fs =
5688 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5689 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5690 		return (SET_ERROR(ENOSYS));
5691 	}
5692 	return (0);
5693 }
5694 
5695 static int
5696 zfs_ioc_share(zfs_cmd_t *zc)
5697 {
5698 	int error;
5699 	int opcode;
5700 
5701 	switch (zc->zc_share.z_sharetype) {
5702 	case ZFS_SHARE_NFS:
5703 	case ZFS_UNSHARE_NFS:
5704 		if (zfs_nfsshare_inited == 0) {
5705 			mutex_enter(&zfs_share_lock);
5706 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5707 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
5708 				mutex_exit(&zfs_share_lock);
5709 				return (SET_ERROR(ENOSYS));
5710 			}
5711 			if (znfsexport_fs == NULL &&
5712 			    ((znfsexport_fs = (int (*)(void *))
5713 			    ddi_modsym(nfs_mod,
5714 			    "nfs_export", &error)) == NULL)) {
5715 				mutex_exit(&zfs_share_lock);
5716 				return (SET_ERROR(ENOSYS));
5717 			}
5718 			error = zfs_init_sharefs();
5719 			if (error != 0) {
5720 				mutex_exit(&zfs_share_lock);
5721 				return (SET_ERROR(ENOSYS));
5722 			}
5723 			zfs_nfsshare_inited = 1;
5724 			mutex_exit(&zfs_share_lock);
5725 		}
5726 		break;
5727 	case ZFS_SHARE_SMB:
5728 	case ZFS_UNSHARE_SMB:
5729 		if (zfs_smbshare_inited == 0) {
5730 			mutex_enter(&zfs_share_lock);
5731 			if (smbsrv_mod == NULL && ((smbsrv_mod =
5732 			    ddi_modopen("drv/smbsrv",
5733 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
5734 				mutex_exit(&zfs_share_lock);
5735 				return (SET_ERROR(ENOSYS));
5736 			}
5737 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5738 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5739 			    "smb_server_share", &error)) == NULL)) {
5740 				mutex_exit(&zfs_share_lock);
5741 				return (SET_ERROR(ENOSYS));
5742 			}
5743 			error = zfs_init_sharefs();
5744 			if (error != 0) {
5745 				mutex_exit(&zfs_share_lock);
5746 				return (SET_ERROR(ENOSYS));
5747 			}
5748 			zfs_smbshare_inited = 1;
5749 			mutex_exit(&zfs_share_lock);
5750 		}
5751 		break;
5752 	default:
5753 		return (SET_ERROR(EINVAL));
5754 	}
5755 
5756 	switch (zc->zc_share.z_sharetype) {
5757 	case ZFS_SHARE_NFS:
5758 	case ZFS_UNSHARE_NFS:
5759 		if (error =
5760 		    znfsexport_fs((void *)
5761 		    (uintptr_t)zc->zc_share.z_exportdata))
5762 			return (error);
5763 		break;
5764 	case ZFS_SHARE_SMB:
5765 	case ZFS_UNSHARE_SMB:
5766 		if (error = zsmbexport_fs((void *)
5767 		    (uintptr_t)zc->zc_share.z_exportdata,
5768 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5769 		    B_TRUE: B_FALSE)) {
5770 			return (error);
5771 		}
5772 		break;
5773 	}
5774 
5775 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5776 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5777 	    SHAREFS_ADD : SHAREFS_REMOVE;
5778 
5779 	/*
5780 	 * Add or remove share from sharetab
5781 	 */
5782 	error = zshare_fs(opcode,
5783 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
5784 	    zc->zc_share.z_sharemax);
5785 
5786 	return (error);
5787 
5788 }
5789 
5790 ace_t full_access[] = {
5791 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5792 };
5793 
5794 /*
5795  * inputs:
5796  * zc_name		name of containing filesystem
5797  * zc_obj		object # beyond which we want next in-use object #
5798  *
5799  * outputs:
5800  * zc_obj		next in-use object #
5801  */
5802 static int
5803 zfs_ioc_next_obj(zfs_cmd_t *zc)
5804 {
5805 	objset_t *os = NULL;
5806 	int error;
5807 
5808 	error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5809 	if (error != 0)
5810 		return (error);
5811 
5812 	error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5813 	    dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5814 
5815 	dmu_objset_rele(os, FTAG);
5816 	return (error);
5817 }
5818 
5819 /*
5820  * inputs:
5821  * zc_name		name of filesystem
5822  * zc_value		prefix name for snapshot
5823  * zc_cleanup_fd	cleanup-on-exit file descriptor for calling process
5824  *
5825  * outputs:
5826  * zc_value		short name of new snapshot
5827  */
5828 static int
5829 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5830 {
5831 	char *snap_name;
5832 	char *hold_name;
5833 	int error;
5834 	minor_t minor;
5835 
5836 	error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5837 	if (error != 0)
5838 		return (error);
5839 
5840 	snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5841 	    (u_longlong_t)ddi_get_lbolt64());
5842 	hold_name = kmem_asprintf("%%%s", zc->zc_value);
5843 
5844 	error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5845 	    hold_name);
5846 	if (error == 0)
5847 		(void) strcpy(zc->zc_value, snap_name);
5848 	strfree(snap_name);
5849 	strfree(hold_name);
5850 	zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5851 	return (error);
5852 }
5853 
5854 /*
5855  * inputs:
5856  * zc_name		name of "to" snapshot
5857  * zc_value		name of "from" snapshot
5858  * zc_cookie		file descriptor to write diff data on
5859  *
5860  * outputs:
5861  * dmu_diff_record_t's to the file descriptor
5862  */
5863 static int
5864 zfs_ioc_diff(zfs_cmd_t *zc)
5865 {
5866 	file_t *fp;
5867 	offset_t off;
5868 	int error;
5869 
5870 	fp = getf(zc->zc_cookie);
5871 	if (fp == NULL)
5872 		return (SET_ERROR(EBADF));
5873 
5874 	off = fp->f_offset;
5875 
5876 	error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5877 
5878 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5879 		fp->f_offset = off;
5880 	releasef(zc->zc_cookie);
5881 
5882 	return (error);
5883 }
5884 
5885 /*
5886  * Remove all ACL files in shares dir
5887  */
5888 static int
5889 zfs_smb_acl_purge(znode_t *dzp)
5890 {
5891 	zap_cursor_t	zc;
5892 	zap_attribute_t	zap;
5893 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5894 	int error;
5895 
5896 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5897 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5898 	    zap_cursor_advance(&zc)) {
5899 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5900 		    NULL, 0)) != 0)
5901 			break;
5902 	}
5903 	zap_cursor_fini(&zc);
5904 	return (error);
5905 }
5906 
5907 static int
5908 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5909 {
5910 	vnode_t *vp;
5911 	znode_t *dzp;
5912 	vnode_t *resourcevp = NULL;
5913 	znode_t *sharedir;
5914 	zfsvfs_t *zfsvfs;
5915 	nvlist_t *nvlist;
5916 	char *src, *target;
5917 	vattr_t vattr;
5918 	vsecattr_t vsec;
5919 	int error = 0;
5920 
5921 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5922 	    NO_FOLLOW, NULL, &vp)) != 0)
5923 		return (error);
5924 
5925 	/* Now make sure mntpnt and dataset are ZFS */
5926 
5927 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5928 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5929 	    zc->zc_name) != 0)) {
5930 		VN_RELE(vp);
5931 		return (SET_ERROR(EINVAL));
5932 	}
5933 
5934 	dzp = VTOZ(vp);
5935 	zfsvfs = dzp->z_zfsvfs;
5936 	ZFS_ENTER(zfsvfs);
5937 
5938 	/*
5939 	 * Create share dir if its missing.
5940 	 */
5941 	mutex_enter(&zfsvfs->z_lock);
5942 	if (zfsvfs->z_shares_dir == 0) {
5943 		dmu_tx_t *tx;
5944 
5945 		tx = dmu_tx_create(zfsvfs->z_os);
5946 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5947 		    ZFS_SHARES_DIR);
5948 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5949 		error = dmu_tx_assign(tx, TXG_WAIT);
5950 		if (error != 0) {
5951 			dmu_tx_abort(tx);
5952 		} else {
5953 			error = zfs_create_share_dir(zfsvfs, tx);
5954 			dmu_tx_commit(tx);
5955 		}
5956 		if (error != 0) {
5957 			mutex_exit(&zfsvfs->z_lock);
5958 			VN_RELE(vp);
5959 			ZFS_EXIT(zfsvfs);
5960 			return (error);
5961 		}
5962 	}
5963 	mutex_exit(&zfsvfs->z_lock);
5964 
5965 	ASSERT(zfsvfs->z_shares_dir);
5966 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5967 		VN_RELE(vp);
5968 		ZFS_EXIT(zfsvfs);
5969 		return (error);
5970 	}
5971 
5972 	switch (zc->zc_cookie) {
5973 	case ZFS_SMB_ACL_ADD:
5974 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5975 		vattr.va_type = VREG;
5976 		vattr.va_mode = S_IFREG|0777;
5977 		vattr.va_uid = 0;
5978 		vattr.va_gid = 0;
5979 
5980 		vsec.vsa_mask = VSA_ACE;
5981 		vsec.vsa_aclentp = &full_access;
5982 		vsec.vsa_aclentsz = sizeof (full_access);
5983 		vsec.vsa_aclcnt = 1;
5984 
5985 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5986 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5987 		if (resourcevp)
5988 			VN_RELE(resourcevp);
5989 		break;
5990 
5991 	case ZFS_SMB_ACL_REMOVE:
5992 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5993 		    NULL, 0);
5994 		break;
5995 
5996 	case ZFS_SMB_ACL_RENAME:
5997 		if ((error = get_nvlist(zc->zc_nvlist_src,
5998 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5999 			VN_RELE(vp);
6000 			VN_RELE(ZTOV(sharedir));
6001 			ZFS_EXIT(zfsvfs);
6002 			return (error);
6003 		}
6004 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
6005 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
6006 		    &target)) {
6007 			VN_RELE(vp);
6008 			VN_RELE(ZTOV(sharedir));
6009 			ZFS_EXIT(zfsvfs);
6010 			nvlist_free(nvlist);
6011 			return (error);
6012 		}
6013 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
6014 		    kcred, NULL, 0);
6015 		nvlist_free(nvlist);
6016 		break;
6017 
6018 	case ZFS_SMB_ACL_PURGE:
6019 		error = zfs_smb_acl_purge(sharedir);
6020 		break;
6021 
6022 	default:
6023 		error = SET_ERROR(EINVAL);
6024 		break;
6025 	}
6026 
6027 	VN_RELE(vp);
6028 	VN_RELE(ZTOV(sharedir));
6029 
6030 	ZFS_EXIT(zfsvfs);
6031 
6032 	return (error);
6033 }
6034 
6035 /*
6036  * innvl: {
6037  *     "holds" -> { snapname -> holdname (string), ... }
6038  *     (optional) "cleanup_fd" -> fd (int32)
6039  * }
6040  *
6041  * outnvl: {
6042  *     snapname -> error value (int32)
6043  *     ...
6044  * }
6045  */
6046 static const zfs_ioc_key_t zfs_keys_hold[] = {
6047 	{"holds",		DATA_TYPE_NVLIST,	0},
6048 	{"cleanup_fd",		DATA_TYPE_INT32,	ZK_OPTIONAL},
6049 };
6050 
6051 /* ARGSUSED */
6052 static int
6053 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6054 {
6055 	nvpair_t *pair;
6056 	nvlist_t *holds;
6057 	int cleanup_fd = -1;
6058 	int error;
6059 	minor_t minor = 0;
6060 
6061 	holds = fnvlist_lookup_nvlist(args, "holds");
6062 
6063 	/* make sure the user didn't pass us any invalid (empty) tags */
6064 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6065 	    pair = nvlist_next_nvpair(holds, pair)) {
6066 		char *htag;
6067 
6068 		error = nvpair_value_string(pair, &htag);
6069 		if (error != 0)
6070 			return (SET_ERROR(error));
6071 
6072 		if (strlen(htag) == 0)
6073 			return (SET_ERROR(EINVAL));
6074 	}
6075 
6076 	if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6077 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
6078 		if (error != 0)
6079 			return (error);
6080 	}
6081 
6082 	error = dsl_dataset_user_hold(holds, minor, errlist);
6083 	if (minor != 0)
6084 		zfs_onexit_fd_rele(cleanup_fd);
6085 	return (error);
6086 }
6087 
6088 /*
6089  * innvl is not used.
6090  *
6091  * outnvl: {
6092  *    holdname -> time added (uint64 seconds since epoch)
6093  *    ...
6094  * }
6095  */
6096 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6097 	/* no nvl keys */
6098 };
6099 
6100 /* ARGSUSED */
6101 static int
6102 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6103 {
6104 	return (dsl_dataset_get_holds(snapname, outnvl));
6105 }
6106 
6107 /*
6108  * innvl: {
6109  *     snapname -> { holdname, ... }
6110  *     ...
6111  * }
6112  *
6113  * outnvl: {
6114  *     snapname -> error value (int32)
6115  *     ...
6116  * }
6117  */
6118 static const zfs_ioc_key_t zfs_keys_release[] = {
6119 	{"<snapname>...",	DATA_TYPE_NVLIST,	ZK_WILDCARDLIST},
6120 };
6121 
6122 /* ARGSUSED */
6123 static int
6124 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6125 {
6126 	return (dsl_dataset_user_release(holds, errlist));
6127 }
6128 
6129 /*
6130  * inputs:
6131  * zc_name		name of new filesystem or snapshot
6132  * zc_value		full name of old snapshot
6133  *
6134  * outputs:
6135  * zc_cookie		space in bytes
6136  * zc_objset_type	compressed space in bytes
6137  * zc_perm_action	uncompressed space in bytes
6138  */
6139 static int
6140 zfs_ioc_space_written(zfs_cmd_t *zc)
6141 {
6142 	int error;
6143 	dsl_pool_t *dp;
6144 	dsl_dataset_t *new, *old;
6145 
6146 	error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6147 	if (error != 0)
6148 		return (error);
6149 	error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6150 	if (error != 0) {
6151 		dsl_pool_rele(dp, FTAG);
6152 		return (error);
6153 	}
6154 	error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6155 	if (error != 0) {
6156 		dsl_dataset_rele(new, FTAG);
6157 		dsl_pool_rele(dp, FTAG);
6158 		return (error);
6159 	}
6160 
6161 	error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
6162 	    &zc->zc_objset_type, &zc->zc_perm_action);
6163 	dsl_dataset_rele(old, FTAG);
6164 	dsl_dataset_rele(new, FTAG);
6165 	dsl_pool_rele(dp, FTAG);
6166 	return (error);
6167 }
6168 
6169 /*
6170  * innvl: {
6171  *     "firstsnap" -> snapshot name
6172  * }
6173  *
6174  * outnvl: {
6175  *     "used" -> space in bytes
6176  *     "compressed" -> compressed space in bytes
6177  *     "uncompressed" -> uncompressed space in bytes
6178  * }
6179  */
6180 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6181 	{"firstsnap",	DATA_TYPE_STRING,	0},
6182 };
6183 
6184 static int
6185 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6186 {
6187 	int error;
6188 	dsl_pool_t *dp;
6189 	dsl_dataset_t *new, *old;
6190 	char *firstsnap;
6191 	uint64_t used, comp, uncomp;
6192 
6193 	firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6194 
6195 	error = dsl_pool_hold(lastsnap, FTAG, &dp);
6196 	if (error != 0)
6197 		return (error);
6198 
6199 	error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6200 	if (error == 0 && !new->ds_is_snapshot) {
6201 		dsl_dataset_rele(new, FTAG);
6202 		error = SET_ERROR(EINVAL);
6203 	}
6204 	if (error != 0) {
6205 		dsl_pool_rele(dp, FTAG);
6206 		return (error);
6207 	}
6208 	error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6209 	if (error == 0 && !old->ds_is_snapshot) {
6210 		dsl_dataset_rele(old, FTAG);
6211 		error = SET_ERROR(EINVAL);
6212 	}
6213 	if (error != 0) {
6214 		dsl_dataset_rele(new, FTAG);
6215 		dsl_pool_rele(dp, FTAG);
6216 		return (error);
6217 	}
6218 
6219 	error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6220 	dsl_dataset_rele(old, FTAG);
6221 	dsl_dataset_rele(new, FTAG);
6222 	dsl_pool_rele(dp, FTAG);
6223 	fnvlist_add_uint64(outnvl, "used", used);
6224 	fnvlist_add_uint64(outnvl, "compressed", comp);
6225 	fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6226 	return (error);
6227 }
6228 
6229 /*
6230  * innvl: {
6231  *     "fd" -> file descriptor to write stream to (int32)
6232  *     (optional) "fromsnap" -> full snap name to send an incremental from
6233  *     (optional) "largeblockok" -> (value ignored)
6234  *         indicates that blocks > 128KB are permitted
6235  *     (optional) "embedok" -> (value ignored)
6236  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
6237  *     (optional) "compressok" -> (value ignored)
6238  *         presence indicates compressed DRR_WRITE records are permitted
6239  *     (optional) "rawok" -> (value ignored)
6240  *         presence indicates raw encrypted records should be used.
6241  *     (optional) "resume_object" and "resume_offset" -> (uint64)
6242  *         if present, resume send stream from specified object and offset.
6243  * }
6244  *
6245  * outnvl is unused
6246  */
6247 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6248 	{"fd",			DATA_TYPE_INT32,	0},
6249 	{"fromsnap",		DATA_TYPE_STRING,	ZK_OPTIONAL},
6250 	{"largeblockok",	DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6251 	{"embedok",		DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6252 	{"compressok",		DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6253 	{"rawok",		DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6254 	{"resume_object",	DATA_TYPE_UINT64,	ZK_OPTIONAL},
6255 	{"resume_offset",	DATA_TYPE_UINT64,	ZK_OPTIONAL},
6256 };
6257 
6258 /* ARGSUSED */
6259 static int
6260 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6261 {
6262 	int error;
6263 	offset_t off;
6264 	char *fromname = NULL;
6265 	int fd;
6266 	boolean_t largeblockok;
6267 	boolean_t embedok;
6268 	boolean_t compressok;
6269 	boolean_t rawok;
6270 	uint64_t resumeobj = 0;
6271 	uint64_t resumeoff = 0;
6272 
6273 	fd = fnvlist_lookup_int32(innvl, "fd");
6274 
6275 	(void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6276 
6277 	largeblockok = nvlist_exists(innvl, "largeblockok");
6278 	embedok = nvlist_exists(innvl, "embedok");
6279 	compressok = nvlist_exists(innvl, "compressok");
6280 	rawok = nvlist_exists(innvl, "rawok");
6281 
6282 	(void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6283 	(void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6284 
6285 	file_t *fp = getf(fd);
6286 	if (fp == NULL)
6287 		return (SET_ERROR(EBADF));
6288 
6289 	off = fp->f_offset;
6290 	error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6291 	    rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
6292 
6293 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
6294 		fp->f_offset = off;
6295 	releasef(fd);
6296 	return (error);
6297 }
6298 
6299 /*
6300  * Determine approximately how large a zfs send stream will be -- the number
6301  * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6302  *
6303  * innvl: {
6304  *     (optional) "from" -> full snap or bookmark name to send an incremental
6305  *                          from
6306  *     (optional) "largeblockok" -> (value ignored)
6307  *         indicates that blocks > 128KB are permitted
6308  *     (optional) "embedok" -> (value ignored)
6309  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
6310  *     (optional) "compressok" -> (value ignored)
6311  *         presence indicates compressed DRR_WRITE records are permitted
6312  * }
6313  *
6314  * outnvl: {
6315  *     "space" -> bytes of space (uint64)
6316  * }
6317  */
6318 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6319 	{"from",		DATA_TYPE_STRING,	ZK_OPTIONAL},
6320 	{"fromsnap",		DATA_TYPE_STRING,	ZK_OPTIONAL},
6321 	{"largeblockok",	DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6322 	{"embedok",		DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6323 	{"compressok",		DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6324 	{"rawok",		DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6325 };
6326 
6327 static int
6328 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6329 {
6330 	dsl_pool_t *dp;
6331 	dsl_dataset_t *tosnap;
6332 	int error;
6333 	char *fromname;
6334 	boolean_t compressok;
6335 	boolean_t rawok;
6336 	uint64_t space;
6337 
6338 	error = dsl_pool_hold(snapname, FTAG, &dp);
6339 	if (error != 0)
6340 		return (error);
6341 
6342 	error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6343 	if (error != 0) {
6344 		dsl_pool_rele(dp, FTAG);
6345 		return (error);
6346 	}
6347 
6348 	compressok = nvlist_exists(innvl, "compressok");
6349 	rawok = nvlist_exists(innvl, "rawok");
6350 
6351 	error = nvlist_lookup_string(innvl, "from", &fromname);
6352 	if (error == 0) {
6353 		if (strchr(fromname, '@') != NULL) {
6354 			/*
6355 			 * If from is a snapshot, hold it and use the more
6356 			 * efficient dmu_send_estimate to estimate send space
6357 			 * size using deadlists.
6358 			 */
6359 			dsl_dataset_t *fromsnap;
6360 			error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6361 			if (error != 0)
6362 				goto out;
6363 			error = dmu_send_estimate(tosnap, fromsnap,
6364 			    compressok || rawok, &space);
6365 			dsl_dataset_rele(fromsnap, FTAG);
6366 		} else if (strchr(fromname, '#') != NULL) {
6367 			/*
6368 			 * If from is a bookmark, fetch the creation TXG of the
6369 			 * snapshot it was created from and use that to find
6370 			 * blocks that were born after it.
6371 			 */
6372 			zfs_bookmark_phys_t frombm;
6373 
6374 			error = dsl_bookmark_lookup(dp, fromname, tosnap,
6375 			    &frombm);
6376 			if (error != 0)
6377 				goto out;
6378 			error = dmu_send_estimate_from_txg(tosnap,
6379 			    frombm.zbm_creation_txg, compressok || rawok,
6380 			    &space);
6381 		} else {
6382 			/*
6383 			 * from is not properly formatted as a snapshot or
6384 			 * bookmark
6385 			 */
6386 			error = SET_ERROR(EINVAL);
6387 			goto out;
6388 		}
6389 	} else {
6390 		/*
6391 		 * If estimating the size of a full send, use dmu_send_estimate.
6392 		 */
6393 		error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6394 		    &space);
6395 	}
6396 
6397 	fnvlist_add_uint64(outnvl, "space", space);
6398 
6399 out:
6400 	dsl_dataset_rele(tosnap, FTAG);
6401 	dsl_pool_rele(dp, FTAG);
6402 	return (error);
6403 }
6404 
6405 /*
6406  * Sync the currently open TXG to disk for the specified pool.
6407  * This is somewhat similar to 'zfs_sync()'.
6408  * For cases that do not result in error this ioctl will wait for
6409  * the currently open TXG to commit before returning back to the caller.
6410  *
6411  * innvl: {
6412  *  "force" -> when true, force uberblock update even if there is no dirty data.
6413  *             In addition this will cause the vdev configuration to be written
6414  *             out including updating the zpool cache file. (boolean_t)
6415  * }
6416  *
6417  * onvl is unused
6418  */
6419 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6420 	{"force",	DATA_TYPE_BOOLEAN_VALUE,	0},
6421 };
6422 
6423 /* ARGSUSED */
6424 static int
6425 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6426 {
6427 	int err;
6428 	boolean_t force = B_FALSE;
6429 	spa_t *spa;
6430 
6431 	if ((err = spa_open(pool, &spa, FTAG)) != 0)
6432 		return (err);
6433 
6434 	if (innvl)
6435 		force = fnvlist_lookup_boolean_value(innvl, "force");
6436 
6437 	if (force) {
6438 		spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6439 		vdev_config_dirty(spa->spa_root_vdev);
6440 		spa_config_exit(spa, SCL_CONFIG, FTAG);
6441 	}
6442 	txg_wait_synced(spa_get_dsl(spa), 0);
6443 
6444 	spa_close(spa, FTAG);
6445 
6446 	return (err);
6447 }
6448 
6449 /*
6450  * Load a user's wrapping key into the kernel.
6451  * innvl: {
6452  *     "hidden_args" -> { "wkeydata" -> value }
6453  *         raw uint8_t array of encryption wrapping key data (32 bytes)
6454  *     (optional) "noop" -> (value ignored)
6455  *         presence indicated key should only be verified, not loaded
6456  * }
6457  */
6458 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6459 	{"hidden_args",	DATA_TYPE_NVLIST,	0},
6460 	{"noop",	DATA_TYPE_BOOLEAN,	ZK_OPTIONAL},
6461 };
6462 
6463 /* ARGSUSED */
6464 static int
6465 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6466 {
6467 	int ret = 0;
6468 	dsl_crypto_params_t *dcp = NULL;
6469 	nvlist_t *hidden_args;
6470 	boolean_t noop = nvlist_exists(innvl, "noop");
6471 
6472 	if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6473 		ret = SET_ERROR(EINVAL);
6474 		goto error;
6475 	}
6476 
6477 	hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6478 
6479 	ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6480 	    hidden_args, &dcp);
6481 	if (ret != 0)
6482 		goto error;
6483 
6484 	ret = spa_keystore_load_wkey(dsname, dcp, noop);
6485 	if (ret != 0)
6486 		goto error;
6487 
6488 	dsl_crypto_params_free(dcp, noop);
6489 
6490 	return (0);
6491 
6492 error:
6493 	dsl_crypto_params_free(dcp, B_TRUE);
6494 	return (ret);
6495 }
6496 
6497 /*
6498  * Unload a user's wrapping key from the kernel.
6499  * Both innvl and outnvl are unused.
6500  */
6501 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6502 	/* no nvl keys */
6503 };
6504 
6505 /* ARGSUSED */
6506 static int
6507 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6508 {
6509 	int ret = 0;
6510 
6511 	if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6512 		ret = (SET_ERROR(EINVAL));
6513 		goto out;
6514 	}
6515 
6516 	ret = spa_keystore_unload_wkey(dsname);
6517 	if (ret != 0)
6518 		goto out;
6519 
6520 out:
6521 	return (ret);
6522 }
6523 
6524 /*
6525  * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6526  * keylocation, pbkdf2salt, and  pbkdf2iters properties can also be specified
6527  * here to change how the key is derived in userspace.
6528  *
6529  * innvl: {
6530  *    "hidden_args" (optional) -> { "wkeydata" -> value }
6531  *         raw uint8_t array of new encryption wrapping key data (32 bytes)
6532  *    "props" (optional) -> { prop -> value }
6533  * }
6534  *
6535  * outnvl is unused
6536  */
6537 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6538 	{"crypt_cmd",	DATA_TYPE_UINT64,	ZK_OPTIONAL},
6539 	{"hidden_args",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
6540 	{"props",	DATA_TYPE_NVLIST,	ZK_OPTIONAL},
6541 };
6542 
6543 /* ARGSUSED */
6544 static int
6545 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6546 {
6547 	int ret;
6548 	uint64_t cmd = DCP_CMD_NONE;
6549 	dsl_crypto_params_t *dcp = NULL;
6550 	nvlist_t *args = NULL, *hidden_args = NULL;
6551 
6552 	if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6553 		ret = (SET_ERROR(EINVAL));
6554 		goto error;
6555 	}
6556 
6557 	(void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6558 	(void) nvlist_lookup_nvlist(innvl, "props", &args);
6559 	(void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6560 
6561 	ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6562 	if (ret != 0)
6563 		goto error;
6564 
6565 	ret = spa_keystore_change_key(dsname, dcp);
6566 	if (ret != 0)
6567 		goto error;
6568 
6569 	dsl_crypto_params_free(dcp, B_FALSE);
6570 
6571 	return (0);
6572 
6573 error:
6574 	dsl_crypto_params_free(dcp, B_TRUE);
6575 	return (ret);
6576 }
6577 
6578 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6579 
6580 static void
6581 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6582     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6583     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6584 {
6585 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6586 
6587 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6588 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
6589 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6590 	ASSERT3P(vec->zvec_func, ==, NULL);
6591 
6592 	vec->zvec_legacy_func = func;
6593 	vec->zvec_secpolicy = secpolicy;
6594 	vec->zvec_namecheck = namecheck;
6595 	vec->zvec_allow_log = log_history;
6596 	vec->zvec_pool_check = pool_check;
6597 }
6598 
6599 /*
6600  * See the block comment at the beginning of this file for details on
6601  * each argument to this function.
6602  */
6603 static void
6604 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6605     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6606     zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6607     boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6608 {
6609 	zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6610 
6611 	ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6612 	ASSERT3U(ioc, <, ZFS_IOC_LAST);
6613 	ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6614 	ASSERT3P(vec->zvec_func, ==, NULL);
6615 
6616 	/* if we are logging, the name must be valid */
6617 	ASSERT(!allow_log || namecheck != NO_NAME);
6618 
6619 	vec->zvec_name = name;
6620 	vec->zvec_func = func;
6621 	vec->zvec_secpolicy = secpolicy;
6622 	vec->zvec_namecheck = namecheck;
6623 	vec->zvec_pool_check = pool_check;
6624 	vec->zvec_smush_outnvlist = smush_outnvlist;
6625 	vec->zvec_allow_log = allow_log;
6626 	vec->zvec_nvl_keys = nvl_keys;
6627 	vec->zvec_nvl_key_count = num_keys;
6628 }
6629 
6630 static void
6631 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6632     zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6633     zfs_ioc_poolcheck_t pool_check)
6634 {
6635 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
6636 	    POOL_NAME, log_history, pool_check);
6637 }
6638 
6639 static void
6640 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6641     zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6642 {
6643 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
6644 	    DATASET_NAME, B_FALSE, pool_check);
6645 }
6646 
6647 static void
6648 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6649 {
6650 	zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6651 	    POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6652 }
6653 
6654 static void
6655 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6656     zfs_secpolicy_func_t *secpolicy)
6657 {
6658 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
6659 	    NO_NAME, B_FALSE, POOL_CHECK_NONE);
6660 }
6661 
6662 static void
6663 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6664     zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6665 {
6666 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
6667 	    DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6668 }
6669 
6670 static void
6671 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6672 {
6673 	zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6674 	    zfs_secpolicy_read);
6675 }
6676 
6677 static void
6678 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6679     zfs_secpolicy_func_t *secpolicy)
6680 {
6681 	zfs_ioctl_register_legacy(ioc, func, secpolicy,
6682 	    DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6683 }
6684 
6685 static void
6686 zfs_ioctl_init(void)
6687 {
6688 	zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6689 	    zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6690 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6691 	    zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6692 
6693 	zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6694 	    zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6695 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6696 	    zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6697 
6698 	zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6699 	    zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6700 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6701 	    zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6702 
6703 	zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6704 	    zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6705 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6706 	    zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6707 
6708 	zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6709 	    zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6710 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6711 	    zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6712 
6713 	zfs_ioctl_register("create", ZFS_IOC_CREATE,
6714 	    zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6715 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6716 	    zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6717 
6718 	zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6719 	    zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6720 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6721 	    zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6722 
6723 	zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6724 	    zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6725 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6726 	    zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6727 
6728 	zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6729 	    zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6730 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6731 	    zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6732 
6733 	zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6734 	    zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6735 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6736 	    zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6737 	zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6738 	    zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6739 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6740 	    zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6741 
6742 	zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6743 	    zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6744 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6745 	    zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6746 
6747 	zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6748 	    zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6749 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6750 	    zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6751 
6752 	zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6753 	    zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6754 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6755 	    zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6756 
6757 	zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6758 	    zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6759 	    POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6760 	    zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6761 
6762 	zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6763 	    zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6764 	    POOL_NAME,
6765 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6766 	    zfs_keys_destroy_bookmarks,
6767 	    ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6768 
6769 	zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6770 	    zfs_ioc_load_key, zfs_secpolicy_load_key,
6771 	    DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6772 	    zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
6773 	zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6774 	    zfs_ioc_unload_key, zfs_secpolicy_load_key,
6775 	    DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6776 	    zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
6777 	zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6778 	    zfs_ioc_change_key, zfs_secpolicy_change_key,
6779 	    DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6780 	    B_TRUE, B_TRUE, zfs_keys_change_key,
6781 	    ARRAY_SIZE(zfs_keys_change_key));
6782 
6783 	zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6784 	    zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6785 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6786 	    zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6787 
6788 	zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6789 	    zfs_ioc_channel_program, zfs_secpolicy_config,
6790 	    POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6791 	    B_TRUE, zfs_keys_channel_program,
6792 	    ARRAY_SIZE(zfs_keys_channel_program));
6793 
6794 	zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6795 	    zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6796 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6797 	    zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6798 
6799 	zfs_ioctl_register("zpool_discard_checkpoint",
6800 	    ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6801 	    zfs_secpolicy_config, POOL_NAME,
6802 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6803 	    zfs_keys_pool_discard_checkpoint,
6804 	    ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
6805 
6806 	zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6807 	    zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6808 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6809 	    zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
6810 
6811 	zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
6812 	    zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
6813 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6814 	    zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
6815 
6816 	zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
6817 	    zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
6818 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6819 	    zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
6820 
6821 	zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
6822 	    zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
6823 	    POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
6824 	    zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
6825 
6826 	/* IOCTLS that use the legacy function signature */
6827 
6828 	zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6829 	    zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6830 
6831 	zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6832 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6833 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6834 	    zfs_ioc_pool_scan);
6835 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6836 	    zfs_ioc_pool_upgrade);
6837 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6838 	    zfs_ioc_vdev_add);
6839 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6840 	    zfs_ioc_vdev_remove);
6841 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6842 	    zfs_ioc_vdev_set_state);
6843 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6844 	    zfs_ioc_vdev_attach);
6845 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6846 	    zfs_ioc_vdev_detach);
6847 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6848 	    zfs_ioc_vdev_setpath);
6849 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6850 	    zfs_ioc_vdev_setfru);
6851 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6852 	    zfs_ioc_pool_set_props);
6853 	zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6854 	    zfs_ioc_vdev_split);
6855 	zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6856 	    zfs_ioc_pool_reguid);
6857 
6858 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6859 	    zfs_ioc_pool_configs, zfs_secpolicy_none);
6860 	zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6861 	    zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6862 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6863 	    zfs_ioc_inject_fault, zfs_secpolicy_inject);
6864 	zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6865 	    zfs_ioc_clear_fault, zfs_secpolicy_inject);
6866 	zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6867 	    zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6868 
6869 	/*
6870 	 * pool destroy, and export don't log the history as part of
6871 	 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6872 	 * does the logging of those commands.
6873 	 */
6874 	zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6875 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6876 	zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6877 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6878 
6879 	zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6880 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6881 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6882 	    zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6883 
6884 	zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6885 	    zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6886 	zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6887 	    zfs_ioc_dsobj_to_dsname,
6888 	    zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6889 	zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6890 	    zfs_ioc_pool_get_history,
6891 	    zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6892 
6893 	zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6894 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6895 
6896 	zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6897 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6898 	zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6899 	    zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6900 
6901 	zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6902 	    zfs_ioc_space_written);
6903 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6904 	    zfs_ioc_objset_recvd_props);
6905 	zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6906 	    zfs_ioc_next_obj);
6907 	zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6908 	    zfs_ioc_get_fsacl);
6909 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6910 	    zfs_ioc_objset_stats);
6911 	zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6912 	    zfs_ioc_objset_zplprops);
6913 	zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6914 	    zfs_ioc_dataset_list_next);
6915 	zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6916 	    zfs_ioc_snapshot_list_next);
6917 	zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6918 	    zfs_ioc_send_progress);
6919 
6920 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6921 	    zfs_ioc_diff, zfs_secpolicy_diff);
6922 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6923 	    zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6924 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6925 	    zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6926 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6927 	    zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6928 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6929 	    zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6930 	zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6931 	    zfs_ioc_send, zfs_secpolicy_send);
6932 
6933 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6934 	    zfs_secpolicy_none);
6935 	zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6936 	    zfs_secpolicy_destroy);
6937 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6938 	    zfs_secpolicy_rename);
6939 	zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6940 	    zfs_secpolicy_recv);
6941 	zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6942 	    zfs_secpolicy_promote);
6943 	zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6944 	    zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6945 	zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6946 	    zfs_secpolicy_set_fsacl);
6947 
6948 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6949 	    zfs_secpolicy_share, POOL_CHECK_NONE);
6950 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6951 	    zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6952 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6953 	    zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6954 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6955 	zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6956 	    zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6957 	    POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6958 }
6959 
6960 /*
6961  * Verify that for non-legacy ioctls the input nvlist
6962  * pairs match against the expected input.
6963  *
6964  * Possible errors are:
6965  * ZFS_ERR_IOC_ARG_UNAVAIL	An unrecognized nvpair was encountered
6966  * ZFS_ERR_IOC_ARG_REQUIRED	A required nvpair is missing
6967  * ZFS_ERR_IOC_ARG_BADTYPE	Invalid type for nvpair
6968  */
6969 static int
6970 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
6971 {
6972 	const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
6973 	boolean_t required_keys_found = B_FALSE;
6974 
6975 	/*
6976 	 * examine each input pair
6977 	 */
6978 	for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
6979 	    pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
6980 		char *name = nvpair_name(pair);
6981 		data_type_t type = nvpair_type(pair);
6982 		boolean_t identified = B_FALSE;
6983 
6984 		/*
6985 		 * check pair against the documented names and type
6986 		 */
6987 		for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6988 			/* if not a wild card name, check for an exact match */
6989 			if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
6990 			    strcmp(nvl_keys[k].zkey_name, name) != 0)
6991 				continue;
6992 
6993 			identified = B_TRUE;
6994 
6995 			if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
6996 			    nvl_keys[k].zkey_type != type) {
6997 				return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
6998 			}
6999 
7000 			if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7001 				continue;
7002 
7003 			required_keys_found = B_TRUE;
7004 			break;
7005 		}
7006 
7007 		/* allow an 'optional' key, everything else is invalid */
7008 		if (!identified &&
7009 		    (strcmp(name, "optional") != 0 ||
7010 		    type != DATA_TYPE_NVLIST)) {
7011 			return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7012 		}
7013 	}
7014 
7015 	/* verify that all required keys were found */
7016 	for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7017 		if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7018 			continue;
7019 
7020 		if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7021 			/* at least one non-optionial key is expected here */
7022 			if (!required_keys_found)
7023 				return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7024 			continue;
7025 		}
7026 
7027 		if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7028 			return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7029 	}
7030 
7031 	return (0);
7032 }
7033 
7034 int
7035 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7036     zfs_ioc_poolcheck_t check)
7037 {
7038 	spa_t *spa;
7039 	int error;
7040 
7041 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
7042 
7043 	if (check & POOL_CHECK_NONE)
7044 		return (0);
7045 
7046 	error = spa_open(name, &spa, FTAG);
7047 	if (error == 0) {
7048 		if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7049 			error = SET_ERROR(EAGAIN);
7050 		else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7051 			error = SET_ERROR(EROFS);
7052 		spa_close(spa, FTAG);
7053 	}
7054 	return (error);
7055 }
7056 
7057 /*
7058  * Find a free minor number.
7059  */
7060 minor_t
7061 zfsdev_minor_alloc(void)
7062 {
7063 	static minor_t last_minor;
7064 	minor_t m;
7065 
7066 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7067 
7068 	for (m = last_minor + 1; m != last_minor; m++) {
7069 		if (m > ZFSDEV_MAX_MINOR)
7070 			m = 1;
7071 		if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
7072 			last_minor = m;
7073 			return (m);
7074 		}
7075 	}
7076 
7077 	return (0);
7078 }
7079 
7080 static int
7081 zfs_ctldev_init(dev_t *devp)
7082 {
7083 	minor_t minor;
7084 	zfs_soft_state_t *zs;
7085 
7086 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7087 	ASSERT(getminor(*devp) == 0);
7088 
7089 	minor = zfsdev_minor_alloc();
7090 	if (minor == 0)
7091 		return (SET_ERROR(ENXIO));
7092 
7093 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
7094 		return (SET_ERROR(EAGAIN));
7095 
7096 	*devp = makedevice(getemajor(*devp), minor);
7097 
7098 	zs = ddi_get_soft_state(zfsdev_state, minor);
7099 	zs->zss_type = ZSST_CTLDEV;
7100 	zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
7101 
7102 	return (0);
7103 }
7104 
7105 static void
7106 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
7107 {
7108 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7109 
7110 	zfs_onexit_destroy(zo);
7111 	ddi_soft_state_free(zfsdev_state, minor);
7112 }
7113 
7114 void *
7115 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
7116 {
7117 	zfs_soft_state_t *zp;
7118 
7119 	zp = ddi_get_soft_state(zfsdev_state, minor);
7120 	if (zp == NULL || zp->zss_type != which)
7121 		return (NULL);
7122 
7123 	return (zp->zss_data);
7124 }
7125 
7126 static int
7127 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
7128 {
7129 	int error = 0;
7130 
7131 	if (getminor(*devp) != 0)
7132 		return (zvol_open(devp, flag, otyp, cr));
7133 
7134 	/* This is the control device. Allocate a new minor if requested. */
7135 	if (flag & FEXCL) {
7136 		mutex_enter(&zfsdev_state_lock);
7137 		error = zfs_ctldev_init(devp);
7138 		mutex_exit(&zfsdev_state_lock);
7139 	}
7140 
7141 	return (error);
7142 }
7143 
7144 static int
7145 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
7146 {
7147 	zfs_onexit_t *zo;
7148 	minor_t minor = getminor(dev);
7149 
7150 	if (minor == 0)
7151 		return (0);
7152 
7153 	mutex_enter(&zfsdev_state_lock);
7154 	zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
7155 	if (zo == NULL) {
7156 		mutex_exit(&zfsdev_state_lock);
7157 		return (zvol_close(dev, flag, otyp, cr));
7158 	}
7159 	zfs_ctldev_destroy(zo, minor);
7160 	mutex_exit(&zfsdev_state_lock);
7161 
7162 	return (0);
7163 }
7164 
7165 static int
7166 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
7167 {
7168 	zfs_cmd_t *zc;
7169 	uint_t vecnum;
7170 	int error, rc, len;
7171 	minor_t minor = getminor(dev);
7172 	const zfs_ioc_vec_t *vec;
7173 	char *saved_poolname = NULL;
7174 	nvlist_t *innvl = NULL;
7175 
7176 	if (minor != 0 &&
7177 	    zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
7178 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
7179 
7180 	vecnum = cmd - ZFS_IOC_FIRST;
7181 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
7182 
7183 	if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7184 		return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7185 	vec = &zfs_ioc_vec[vecnum];
7186 
7187 	/*
7188 	 * The registered ioctl list may be sparse, verify that either
7189 	 * a normal or legacy handler are registered.
7190 	 */
7191 	if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7192 		return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7193 
7194 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
7195 
7196 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
7197 	if (error != 0) {
7198 		error = SET_ERROR(EFAULT);
7199 		goto out;
7200 	}
7201 
7202 	zc->zc_iflags = flag & FKIOCTL;
7203 	if (zc->zc_nvlist_src_size != 0) {
7204 		error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7205 		    zc->zc_iflags, &innvl);
7206 		if (error != 0)
7207 			goto out;
7208 	}
7209 
7210 	/*
7211 	 * Ensure that all pool/dataset names are valid before we pass down to
7212 	 * the lower layers.
7213 	 */
7214 	zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7215 	switch (vec->zvec_namecheck) {
7216 	case POOL_NAME:
7217 		if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7218 			error = SET_ERROR(EINVAL);
7219 		else
7220 			error = pool_status_check(zc->zc_name,
7221 			    vec->zvec_namecheck, vec->zvec_pool_check);
7222 		break;
7223 
7224 	case DATASET_NAME:
7225 		if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7226 			error = SET_ERROR(EINVAL);
7227 		else
7228 			error = pool_status_check(zc->zc_name,
7229 			    vec->zvec_namecheck, vec->zvec_pool_check);
7230 		break;
7231 
7232 	case NO_NAME:
7233 		break;
7234 	}
7235 
7236 	/*
7237 	 * Ensure that all input pairs are valid before we pass them down
7238 	 * to the lower layers.
7239 	 *
7240 	 * The vectored functions can use fnvlist_lookup_{type} for any
7241 	 * required pairs since zfs_check_input_nvpairs() confirmed that
7242 	 * they exist and are of the correct type.
7243 	 */
7244 	if (error == 0 && vec->zvec_func != NULL) {
7245 		error = zfs_check_input_nvpairs(innvl, vec);
7246 		if (error != 0)
7247 			goto out;
7248 	}
7249 
7250 	if (error == 0)
7251 		error = vec->zvec_secpolicy(zc, innvl, cr);
7252 
7253 	if (error != 0)
7254 		goto out;
7255 
7256 	/* legacy ioctls can modify zc_name */
7257 	len = strcspn(zc->zc_name, "/@#") + 1;
7258 	saved_poolname = kmem_alloc(len, KM_SLEEP);
7259 	(void) strlcpy(saved_poolname, zc->zc_name, len);
7260 
7261 	if (vec->zvec_func != NULL) {
7262 		nvlist_t *outnvl;
7263 		int puterror = 0;
7264 		spa_t *spa;
7265 		nvlist_t *lognv = NULL;
7266 
7267 		ASSERT(vec->zvec_legacy_func == NULL);
7268 
7269 		/*
7270 		 * Add the innvl to the lognv before calling the func,
7271 		 * in case the func changes the innvl.
7272 		 */
7273 		if (vec->zvec_allow_log) {
7274 			lognv = fnvlist_alloc();
7275 			fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7276 			    vec->zvec_name);
7277 			if (!nvlist_empty(innvl)) {
7278 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7279 				    innvl);
7280 			}
7281 		}
7282 
7283 		outnvl = fnvlist_alloc();
7284 		error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7285 
7286 		/*
7287 		 * Some commands can partially execute, modify state, and still
7288 		 * return an error.  In these cases, attempt to record what
7289 		 * was modified.
7290 		 */
7291 		if ((error == 0 ||
7292 		    (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7293 		    vec->zvec_allow_log &&
7294 		    spa_open(zc->zc_name, &spa, FTAG) == 0) {
7295 			if (!nvlist_empty(outnvl)) {
7296 				fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7297 				    outnvl);
7298 			}
7299 			if (error != 0) {
7300 				fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7301 				    error);
7302 			}
7303 			(void) spa_history_log_nvl(spa, lognv);
7304 			spa_close(spa, FTAG);
7305 		}
7306 		fnvlist_free(lognv);
7307 
7308 		if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7309 			int smusherror = 0;
7310 			if (vec->zvec_smush_outnvlist) {
7311 				smusherror = nvlist_smush(outnvl,
7312 				    zc->zc_nvlist_dst_size);
7313 			}
7314 			if (smusherror == 0)
7315 				puterror = put_nvlist(zc, outnvl);
7316 		}
7317 
7318 		if (puterror != 0)
7319 			error = puterror;
7320 
7321 		nvlist_free(outnvl);
7322 	} else {
7323 		error = vec->zvec_legacy_func(zc);
7324 	}
7325 
7326 out:
7327 	nvlist_free(innvl);
7328 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
7329 	if (error == 0 && rc != 0)
7330 		error = SET_ERROR(EFAULT);
7331 	if (error == 0 && vec->zvec_allow_log) {
7332 		char *s = tsd_get(zfs_allow_log_key);
7333 		if (s != NULL)
7334 			strfree(s);
7335 		(void) tsd_set(zfs_allow_log_key, saved_poolname);
7336 	} else {
7337 		if (saved_poolname != NULL)
7338 			strfree(saved_poolname);
7339 	}
7340 
7341 	kmem_free(zc, sizeof (zfs_cmd_t));
7342 	return (error);
7343 }
7344 
7345 static int
7346 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7347 {
7348 	if (cmd != DDI_ATTACH)
7349 		return (DDI_FAILURE);
7350 
7351 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
7352 	    DDI_PSEUDO, 0) == DDI_FAILURE)
7353 		return (DDI_FAILURE);
7354 
7355 	zfs_dip = dip;
7356 
7357 	ddi_report_dev(dip);
7358 
7359 	return (DDI_SUCCESS);
7360 }
7361 
7362 static int
7363 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7364 {
7365 	if (spa_busy() || zfs_busy() || zvol_busy())
7366 		return (DDI_FAILURE);
7367 
7368 	if (cmd != DDI_DETACH)
7369 		return (DDI_FAILURE);
7370 
7371 	zfs_dip = NULL;
7372 
7373 	ddi_prop_remove_all(dip);
7374 	ddi_remove_minor_node(dip, NULL);
7375 
7376 	return (DDI_SUCCESS);
7377 }
7378 
7379 /*ARGSUSED*/
7380 static int
7381 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7382 {
7383 	switch (infocmd) {
7384 	case DDI_INFO_DEVT2DEVINFO:
7385 		*result = zfs_dip;
7386 		return (DDI_SUCCESS);
7387 
7388 	case DDI_INFO_DEVT2INSTANCE:
7389 		*result = (void *)0;
7390 		return (DDI_SUCCESS);
7391 	}
7392 
7393 	return (DDI_FAILURE);
7394 }
7395 
7396 /*
7397  * OK, so this is a little weird.
7398  *
7399  * /dev/zfs is the control node, i.e. minor 0.
7400  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
7401  *
7402  * /dev/zfs has basically nothing to do except serve up ioctls,
7403  * so most of the standard driver entry points are in zvol.c.
7404  */
7405 static struct cb_ops zfs_cb_ops = {
7406 	zfsdev_open,	/* open */
7407 	zfsdev_close,	/* close */
7408 	zvol_strategy,	/* strategy */
7409 	nodev,		/* print */
7410 	zvol_dump,	/* dump */
7411 	zvol_read,	/* read */
7412 	zvol_write,	/* write */
7413 	zfsdev_ioctl,	/* ioctl */
7414 	nodev,		/* devmap */
7415 	nodev,		/* mmap */
7416 	nodev,		/* segmap */
7417 	nochpoll,	/* poll */
7418 	ddi_prop_op,	/* prop_op */
7419 	NULL,		/* streamtab */
7420 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
7421 	CB_REV,		/* version */
7422 	nodev,		/* async read */
7423 	nodev,		/* async write */
7424 };
7425 
7426 static struct dev_ops zfs_dev_ops = {
7427 	DEVO_REV,	/* version */
7428 	0,		/* refcnt */
7429 	zfs_info,	/* info */
7430 	nulldev,	/* identify */
7431 	nulldev,	/* probe */
7432 	zfs_attach,	/* attach */
7433 	zfs_detach,	/* detach */
7434 	nodev,		/* reset */
7435 	&zfs_cb_ops,	/* driver operations */
7436 	NULL,		/* no bus operations */
7437 	NULL,		/* power */
7438 	ddi_quiesce_not_needed,	/* quiesce */
7439 };
7440 
7441 static struct modldrv zfs_modldrv = {
7442 	&mod_driverops,
7443 	"ZFS storage pool",
7444 	&zfs_dev_ops
7445 };
7446 
7447 static struct modlinkage modlinkage = {
7448 	MODREV_1,
7449 	(void *)&zfs_modlfs,
7450 	(void *)&zfs_modldrv,
7451 	NULL
7452 };
7453 
7454 static void
7455 zfs_allow_log_destroy(void *arg)
7456 {
7457 	char *poolname = arg;
7458 	strfree(poolname);
7459 }
7460 
7461 int
7462 _init(void)
7463 {
7464 	int error;
7465 
7466 	spa_init(FREAD | FWRITE);
7467 	zfs_init();
7468 	zvol_init();
7469 	zfs_ioctl_init();
7470 
7471 	if ((error = mod_install(&modlinkage)) != 0) {
7472 		zvol_fini();
7473 		zfs_fini();
7474 		spa_fini();
7475 		return (error);
7476 	}
7477 
7478 	tsd_create(&zfs_fsyncer_key, NULL);
7479 	tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7480 	tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7481 
7482 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
7483 	ASSERT(error == 0);
7484 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7485 
7486 	return (0);
7487 }
7488 
7489 int
7490 _fini(void)
7491 {
7492 	int error;
7493 
7494 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
7495 		return (SET_ERROR(EBUSY));
7496 
7497 	if ((error = mod_remove(&modlinkage)) != 0)
7498 		return (error);
7499 
7500 	zvol_fini();
7501 	zfs_fini();
7502 	spa_fini();
7503 	if (zfs_nfsshare_inited)
7504 		(void) ddi_modclose(nfs_mod);
7505 	if (zfs_smbshare_inited)
7506 		(void) ddi_modclose(smbsrv_mod);
7507 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
7508 		(void) ddi_modclose(sharefs_mod);
7509 
7510 	tsd_destroy(&zfs_fsyncer_key);
7511 	ldi_ident_release(zfs_li);
7512 	zfs_li = NULL;
7513 	mutex_destroy(&zfs_share_lock);
7514 
7515 	return (error);
7516 }
7517 
7518 int
7519 _info(struct modinfo *modinfop)
7520 {
7521 	return (mod_info(&modlinkage, modinfop));
7522 }
7523