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