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