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