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