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