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