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