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