xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 3cb34c601f3ef3016f638574f5982e80c3735c71)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/uio.h>
32 #include <sys/buf.h>
33 #include <sys/modctl.h>
34 #include <sys/open.h>
35 #include <sys/file.h>
36 #include <sys/kmem.h>
37 #include <sys/conf.h>
38 #include <sys/cmn_err.h>
39 #include <sys/stat.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zfs_i18n.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/zap.h>
44 #include <sys/spa.h>
45 #include <sys/spa_impl.h>
46 #include <sys/vdev.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/dmu.h>
49 #include <sys/dsl_dir.h>
50 #include <sys/dsl_dataset.h>
51 #include <sys/dsl_prop.h>
52 #include <sys/dsl_deleg.h>
53 #include <sys/dmu_objset.h>
54 #include <sys/ddi.h>
55 #include <sys/sunddi.h>
56 #include <sys/sunldi.h>
57 #include <sys/policy.h>
58 #include <sys/zone.h>
59 #include <sys/nvpair.h>
60 #include <sys/pathname.h>
61 #include <sys/mount.h>
62 #include <sys/sdt.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zfs_dir.h>
66 #include <sys/zvol.h>
67 #include <sharefs/share.h>
68 #include <sys/zfs_znode.h>
69 #include <sys/zfs_vfsops.h>
70 #include <sys/dmu_objset.h>
71 
72 #include "zfs_namecheck.h"
73 #include "zfs_prop.h"
74 #include "zfs_deleg.h"
75 
76 extern struct modlfs zfs_modlfs;
77 
78 extern void zfs_init(void);
79 extern void zfs_fini(void);
80 
81 ldi_ident_t zfs_li = NULL;
82 dev_info_t *zfs_dip;
83 
84 typedef int zfs_ioc_func_t(zfs_cmd_t *);
85 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
86 
87 typedef struct zfs_ioc_vec {
88 	zfs_ioc_func_t		*zvec_func;
89 	zfs_secpolicy_func_t	*zvec_secpolicy;
90 	enum {
91 		NO_NAME,
92 		POOL_NAME,
93 		DATASET_NAME
94 	} zvec_namecheck;
95 	boolean_t		zvec_his_log;
96 } zfs_ioc_vec_t;
97 
98 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
99 void
100 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
101 {
102 	const char *newfile;
103 	char buf[256];
104 	va_list adx;
105 
106 	/*
107 	 * Get rid of annoying "../common/" prefix to filename.
108 	 */
109 	newfile = strrchr(file, '/');
110 	if (newfile != NULL) {
111 		newfile = newfile + 1; /* Get rid of leading / */
112 	} else {
113 		newfile = file;
114 	}
115 
116 	va_start(adx, fmt);
117 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
118 	va_end(adx);
119 
120 	/*
121 	 * To get this data, use the zfs-dprintf probe as so:
122 	 * dtrace -q -n 'zfs-dprintf \
123 	 *	/stringof(arg0) == "dbuf.c"/ \
124 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
125 	 * arg0 = file name
126 	 * arg1 = function name
127 	 * arg2 = line number
128 	 * arg3 = message
129 	 */
130 	DTRACE_PROBE4(zfs__dprintf,
131 	    char *, newfile, char *, func, int, line, char *, buf);
132 }
133 
134 static void
135 history_str_free(char *buf)
136 {
137 	kmem_free(buf, HIS_MAX_RECORD_LEN);
138 }
139 
140 static char *
141 history_str_get(zfs_cmd_t *zc)
142 {
143 	char *buf;
144 
145 	if (zc->zc_history == NULL)
146 		return (NULL);
147 
148 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
149 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
150 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
151 		history_str_free(buf);
152 		return (NULL);
153 	}
154 
155 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
156 
157 	return (buf);
158 }
159 
160 static int
161 zfs_check_version(const char *name, int version)
162 {
163 
164 	spa_t *spa;
165 
166 	if (spa_open(name, &spa, FTAG) == 0) {
167 		if (spa_version(spa) < version) {
168 			spa_close(spa, FTAG);
169 			return (1);
170 		}
171 		spa_close(spa, FTAG);
172 	}
173 	return (0);
174 }
175 
176 static void
177 zfs_log_history(zfs_cmd_t *zc)
178 {
179 	spa_t *spa;
180 	char *buf;
181 
182 	if ((buf = history_str_get(zc)) == NULL)
183 		return;
184 
185 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
186 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
187 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
188 		spa_close(spa, FTAG);
189 	}
190 	history_str_free(buf);
191 }
192 
193 /*
194  * Policy for top-level read operations (list pools).  Requires no privileges,
195  * and can be used in the local zone, as there is no associated dataset.
196  */
197 /* ARGSUSED */
198 static int
199 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
200 {
201 	return (0);
202 }
203 
204 /*
205  * Policy for dataset read operations (list children, get statistics).  Requires
206  * no privileges, but must be visible in the local zone.
207  */
208 /* ARGSUSED */
209 static int
210 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
211 {
212 	if (INGLOBALZONE(curproc) ||
213 	    zone_dataset_visible(zc->zc_name, NULL))
214 		return (0);
215 
216 	return (ENOENT);
217 }
218 
219 static int
220 zfs_dozonecheck(const char *dataset, cred_t *cr)
221 {
222 	uint64_t zoned;
223 	int writable = 1;
224 
225 	/*
226 	 * The dataset must be visible by this zone -- check this first
227 	 * so they don't see EPERM on something they shouldn't know about.
228 	 */
229 	if (!INGLOBALZONE(curproc) &&
230 	    !zone_dataset_visible(dataset, &writable))
231 		return (ENOENT);
232 
233 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
234 		return (ENOENT);
235 
236 	if (INGLOBALZONE(curproc)) {
237 		/*
238 		 * If the fs is zoned, only root can access it from the
239 		 * global zone.
240 		 */
241 		if (secpolicy_zfs(cr) && zoned)
242 			return (EPERM);
243 	} else {
244 		/*
245 		 * If we are in a local zone, the 'zoned' property must be set.
246 		 */
247 		if (!zoned)
248 			return (EPERM);
249 
250 		/* must be writable by this zone */
251 		if (!writable)
252 			return (EPERM);
253 	}
254 	return (0);
255 }
256 
257 int
258 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
259 {
260 	int error;
261 
262 	error = zfs_dozonecheck(name, cr);
263 	if (error == 0) {
264 		error = secpolicy_zfs(cr);
265 		if (error)
266 			error = dsl_deleg_access(name, perm, cr);
267 	}
268 	return (error);
269 }
270 
271 static int
272 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
273 {
274 	/*
275 	 * Check permissions for special properties.
276 	 */
277 	switch (prop) {
278 	case ZFS_PROP_ZONED:
279 		/*
280 		 * Disallow setting of 'zoned' from within a local zone.
281 		 */
282 		if (!INGLOBALZONE(curproc))
283 			return (EPERM);
284 		break;
285 
286 	case ZFS_PROP_QUOTA:
287 		if (!INGLOBALZONE(curproc)) {
288 			uint64_t zoned;
289 			char setpoint[MAXNAMELEN];
290 			/*
291 			 * Unprivileged users are allowed to modify the
292 			 * quota on things *under* (ie. contained by)
293 			 * the thing they own.
294 			 */
295 			if (dsl_prop_get_integer(name, "zoned", &zoned,
296 			    setpoint))
297 				return (EPERM);
298 			if (!zoned || strlen(name) <= strlen(setpoint))
299 				return (EPERM);
300 		}
301 		break;
302 	}
303 
304 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
305 }
306 
307 int
308 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
309 {
310 	int error;
311 
312 	error = zfs_dozonecheck(zc->zc_name, cr);
313 	if (error)
314 		return (error);
315 
316 	/*
317 	 * permission to set permissions will be evaluated later in
318 	 * dsl_deleg_can_allow()
319 	 */
320 	return (0);
321 }
322 
323 int
324 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
325 {
326 	int error;
327 	error = zfs_secpolicy_write_perms(zc->zc_name,
328 	    ZFS_DELEG_PERM_ROLLBACK, cr);
329 	if (error == 0)
330 		error = zfs_secpolicy_write_perms(zc->zc_name,
331 		    ZFS_DELEG_PERM_MOUNT, cr);
332 	return (error);
333 }
334 
335 int
336 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
337 {
338 	return (zfs_secpolicy_write_perms(zc->zc_name,
339 	    ZFS_DELEG_PERM_SEND, cr));
340 }
341 
342 int
343 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
344 {
345 	if (!INGLOBALZONE(curproc))
346 		return (EPERM);
347 
348 	if (secpolicy_nfs(cr) == 0) {
349 		return (0);
350 	} else {
351 		vnode_t *vp;
352 		int error;
353 
354 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
355 		    NO_FOLLOW, NULL, &vp)) != 0)
356 			return (error);
357 
358 		/* Now make sure mntpnt and dataset are ZFS */
359 
360 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
361 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
362 		    zc->zc_name) != 0)) {
363 			VN_RELE(vp);
364 			return (EPERM);
365 		}
366 
367 		VN_RELE(vp);
368 		return (dsl_deleg_access(zc->zc_name,
369 		    ZFS_DELEG_PERM_SHARE, cr));
370 	}
371 }
372 
373 static int
374 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
375 {
376 	char *cp;
377 
378 	/*
379 	 * Remove the @bla or /bla from the end of the name to get the parent.
380 	 */
381 	(void) strncpy(parent, datasetname, parentsize);
382 	cp = strrchr(parent, '@');
383 	if (cp != NULL) {
384 		cp[0] = '\0';
385 	} else {
386 		cp = strrchr(parent, '/');
387 		if (cp == NULL)
388 			return (ENOENT);
389 		cp[0] = '\0';
390 	}
391 
392 	return (0);
393 }
394 
395 int
396 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
397 {
398 	int error;
399 
400 	if ((error = zfs_secpolicy_write_perms(name,
401 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
402 		return (error);
403 
404 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
405 }
406 
407 static int
408 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
409 {
410 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
411 }
412 
413 /*
414  * Must have sys_config privilege to check the iscsi permission
415  */
416 /* ARGSUSED */
417 static int
418 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
419 {
420 	return (secpolicy_zfs(cr));
421 }
422 
423 int
424 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
425 {
426 	char 	parentname[MAXNAMELEN];
427 	int	error;
428 
429 	if ((error = zfs_secpolicy_write_perms(from,
430 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
431 		return (error);
432 
433 	if ((error = zfs_secpolicy_write_perms(from,
434 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
435 		return (error);
436 
437 	if ((error = zfs_get_parent(to, parentname,
438 	    sizeof (parentname))) != 0)
439 		return (error);
440 
441 	if ((error = zfs_secpolicy_write_perms(parentname,
442 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
443 		return (error);
444 
445 	if ((error = zfs_secpolicy_write_perms(parentname,
446 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
447 		return (error);
448 
449 	return (error);
450 }
451 
452 static int
453 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
454 {
455 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
456 }
457 
458 static int
459 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
460 {
461 	char 	parentname[MAXNAMELEN];
462 	objset_t *clone;
463 	int error;
464 
465 	error = zfs_secpolicy_write_perms(zc->zc_name,
466 	    ZFS_DELEG_PERM_PROMOTE, cr);
467 	if (error)
468 		return (error);
469 
470 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
471 	    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
472 
473 	if (error == 0) {
474 		dsl_dataset_t *pclone = NULL;
475 		dsl_dir_t *dd;
476 		dd = clone->os->os_dsl_dataset->ds_dir;
477 
478 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
479 		error = dsl_dataset_open_obj(dd->dd_pool,
480 		    dd->dd_phys->dd_origin_obj, NULL,
481 		    DS_MODE_NONE, FTAG, &pclone);
482 		rw_exit(&dd->dd_pool->dp_config_rwlock);
483 		if (error) {
484 			dmu_objset_close(clone);
485 			return (error);
486 		}
487 
488 		error = zfs_secpolicy_write_perms(zc->zc_name,
489 		    ZFS_DELEG_PERM_MOUNT, cr);
490 
491 		dsl_dataset_name(pclone, parentname);
492 		dmu_objset_close(clone);
493 		dsl_dataset_close(pclone, DS_MODE_NONE, FTAG);
494 		if (error == 0)
495 			error = zfs_secpolicy_write_perms(parentname,
496 			    ZFS_DELEG_PERM_PROMOTE, cr);
497 	}
498 	return (error);
499 }
500 
501 static int
502 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
503 {
504 	int error;
505 
506 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
507 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
508 		return (error);
509 
510 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
511 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
512 		return (error);
513 
514 	return (zfs_secpolicy_write_perms(zc->zc_name,
515 	    ZFS_DELEG_PERM_CREATE, cr));
516 }
517 
518 int
519 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
520 {
521 	int error;
522 
523 	if ((error = zfs_secpolicy_write_perms(name,
524 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
525 		return (error);
526 
527 	error = zfs_secpolicy_write_perms(name,
528 	    ZFS_DELEG_PERM_MOUNT, cr);
529 
530 	return (error);
531 }
532 
533 static int
534 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
535 {
536 
537 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
538 }
539 
540 static int
541 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
542 {
543 	char 	parentname[MAXNAMELEN];
544 	int 	error;
545 
546 	if ((error = zfs_get_parent(zc->zc_name, parentname,
547 	    sizeof (parentname))) != 0)
548 		return (error);
549 
550 	if (zc->zc_value[0] != '\0') {
551 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
552 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
553 			return (error);
554 	}
555 
556 	if ((error = zfs_secpolicy_write_perms(parentname,
557 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
558 		return (error);
559 
560 	error = zfs_secpolicy_write_perms(parentname,
561 	    ZFS_DELEG_PERM_MOUNT, cr);
562 
563 	return (error);
564 }
565 
566 static int
567 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
568 {
569 	int error;
570 
571 	error = secpolicy_fs_unmount(cr, NULL);
572 	if (error) {
573 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
574 	}
575 	return (error);
576 }
577 
578 /*
579  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
580  * SYS_CONFIG privilege, which is not available in a local zone.
581  */
582 /* ARGSUSED */
583 static int
584 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
585 {
586 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
587 		return (EPERM);
588 
589 	return (0);
590 }
591 
592 /*
593  * Just like zfs_secpolicy_config, except that we will check for
594  * mount permission on the dataset for permission to create/remove
595  * the minor nodes.
596  */
597 static int
598 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
599 {
600 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
601 		return (dsl_deleg_access(zc->zc_name,
602 		    ZFS_DELEG_PERM_MOUNT, cr));
603 	}
604 
605 	return (0);
606 }
607 
608 /*
609  * Policy for fault injection.  Requires all privileges.
610  */
611 /* ARGSUSED */
612 static int
613 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
614 {
615 	return (secpolicy_zinject(cr));
616 }
617 
618 static int
619 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
620 {
621 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
622 
623 	if (prop == ZPROP_INVAL) {
624 		if (!zfs_prop_user(zc->zc_value))
625 			return (EINVAL);
626 		return (zfs_secpolicy_write_perms(zc->zc_name,
627 		    ZFS_DELEG_PERM_USERPROP, cr));
628 	} else {
629 		if (!zfs_prop_inheritable(prop))
630 			return (EINVAL);
631 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
632 	}
633 }
634 
635 /*
636  * Returns the nvlist as specified by the user in the zfs_cmd_t.
637  */
638 static int
639 get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
640 {
641 	char *packed;
642 	int error;
643 	nvlist_t *list = NULL;
644 
645 	/*
646 	 * Read in and unpack the user-supplied nvlist.
647 	 */
648 	if (size == 0)
649 		return (EINVAL);
650 
651 	packed = kmem_alloc(size, KM_SLEEP);
652 
653 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
654 		kmem_free(packed, size);
655 		return (error);
656 	}
657 
658 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
659 		kmem_free(packed, size);
660 		return (error);
661 	}
662 
663 	kmem_free(packed, size);
664 
665 	*nvp = list;
666 	return (0);
667 }
668 
669 static int
670 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
671 {
672 	char *packed = NULL;
673 	size_t size;
674 	int error;
675 
676 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
677 
678 	if (size > zc->zc_nvlist_dst_size) {
679 		error = ENOMEM;
680 	} else {
681 		packed = kmem_alloc(size, KM_SLEEP);
682 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
683 		    KM_SLEEP) == 0);
684 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
685 		    size);
686 		kmem_free(packed, size);
687 	}
688 
689 	zc->zc_nvlist_dst_size = size;
690 	return (error);
691 }
692 
693 static int
694 zfs_ioc_pool_create(zfs_cmd_t *zc)
695 {
696 	int error;
697 	nvlist_t *config, *props = NULL;
698 	char *buf;
699 
700 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
701 	    &config))
702 		return (error);
703 
704 	if (zc->zc_nvlist_src_size != 0 && (error =
705 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
706 		nvlist_free(config);
707 		return (error);
708 	}
709 
710 	buf = history_str_get(zc);
711 
712 	error = spa_create(zc->zc_name, config, props, buf);
713 
714 	if (buf != NULL)
715 		history_str_free(buf);
716 
717 	nvlist_free(config);
718 
719 	if (props)
720 		nvlist_free(props);
721 
722 	return (error);
723 }
724 
725 static int
726 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
727 {
728 	int error;
729 	zfs_log_history(zc);
730 	error = spa_destroy(zc->zc_name);
731 	return (error);
732 }
733 
734 static int
735 zfs_ioc_pool_import(zfs_cmd_t *zc)
736 {
737 	int error;
738 	nvlist_t *config, *props = NULL;
739 	uint64_t guid;
740 
741 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
742 	    &config)) != 0)
743 		return (error);
744 
745 	if (zc->zc_nvlist_src_size != 0 && (error =
746 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
747 		nvlist_free(config);
748 		return (error);
749 	}
750 
751 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
752 	    guid != zc->zc_guid)
753 		error = EINVAL;
754 	else
755 		error = spa_import(zc->zc_name, config, props);
756 
757 	nvlist_free(config);
758 
759 	if (props)
760 		nvlist_free(props);
761 
762 	return (error);
763 }
764 
765 static int
766 zfs_ioc_pool_export(zfs_cmd_t *zc)
767 {
768 	int error;
769 	zfs_log_history(zc);
770 	error = spa_export(zc->zc_name, NULL);
771 	return (error);
772 }
773 
774 static int
775 zfs_ioc_pool_configs(zfs_cmd_t *zc)
776 {
777 	nvlist_t *configs;
778 	int error;
779 
780 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
781 		return (EEXIST);
782 
783 	error = put_nvlist(zc, configs);
784 
785 	nvlist_free(configs);
786 
787 	return (error);
788 }
789 
790 static int
791 zfs_ioc_pool_stats(zfs_cmd_t *zc)
792 {
793 	nvlist_t *config;
794 	int error;
795 	int ret = 0;
796 
797 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
798 	    sizeof (zc->zc_value));
799 
800 	if (config != NULL) {
801 		ret = put_nvlist(zc, config);
802 		nvlist_free(config);
803 
804 		/*
805 		 * The config may be present even if 'error' is non-zero.
806 		 * In this case we return success, and preserve the real errno
807 		 * in 'zc_cookie'.
808 		 */
809 		zc->zc_cookie = error;
810 	} else {
811 		ret = error;
812 	}
813 
814 	return (ret);
815 }
816 
817 /*
818  * Try to import the given pool, returning pool stats as appropriate so that
819  * user land knows which devices are available and overall pool health.
820  */
821 static int
822 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
823 {
824 	nvlist_t *tryconfig, *config;
825 	int error;
826 
827 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
828 	    &tryconfig)) != 0)
829 		return (error);
830 
831 	config = spa_tryimport(tryconfig);
832 
833 	nvlist_free(tryconfig);
834 
835 	if (config == NULL)
836 		return (EINVAL);
837 
838 	error = put_nvlist(zc, config);
839 	nvlist_free(config);
840 
841 	return (error);
842 }
843 
844 static int
845 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
846 {
847 	spa_t *spa;
848 	int error;
849 
850 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
851 		return (error);
852 
853 	mutex_enter(&spa_namespace_lock);
854 	error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
855 	mutex_exit(&spa_namespace_lock);
856 
857 	spa_close(spa, FTAG);
858 
859 	return (error);
860 }
861 
862 static int
863 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
864 {
865 	spa_t *spa;
866 	int error;
867 
868 	error = spa_open(zc->zc_name, &spa, FTAG);
869 	if (error == 0) {
870 		spa_freeze(spa);
871 		spa_close(spa, FTAG);
872 	}
873 	return (error);
874 }
875 
876 static int
877 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
878 {
879 	spa_t *spa;
880 	int error;
881 
882 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
883 		return (error);
884 
885 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
886 		spa_close(spa, FTAG);
887 		return (EINVAL);
888 	}
889 
890 	spa_upgrade(spa, zc->zc_cookie);
891 	spa_close(spa, FTAG);
892 
893 	return (error);
894 }
895 
896 static int
897 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
898 {
899 	spa_t *spa;
900 	char *hist_buf;
901 	uint64_t size;
902 	int error;
903 
904 	if ((size = zc->zc_history_len) == 0)
905 		return (EINVAL);
906 
907 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
908 		return (error);
909 
910 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
911 		spa_close(spa, FTAG);
912 		return (ENOTSUP);
913 	}
914 
915 	hist_buf = kmem_alloc(size, KM_SLEEP);
916 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
917 	    &zc->zc_history_len, hist_buf)) == 0) {
918 		error = xcopyout(hist_buf,
919 		    (char *)(uintptr_t)zc->zc_history,
920 		    zc->zc_history_len);
921 	}
922 
923 	spa_close(spa, FTAG);
924 	kmem_free(hist_buf, size);
925 	return (error);
926 }
927 
928 static int
929 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
930 {
931 	int error;
932 
933 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
934 		return (error);
935 
936 	return (0);
937 }
938 
939 static int
940 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
941 {
942 	objset_t *osp;
943 	int error;
944 
945 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
946 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
947 		return (error);
948 
949 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
950 	    sizeof (zc->zc_value));
951 	dmu_objset_close(osp);
952 
953 	return (error);
954 }
955 
956 static int
957 zfs_ioc_vdev_add(zfs_cmd_t *zc)
958 {
959 	spa_t *spa;
960 	int error;
961 	nvlist_t *config;
962 
963 	error = spa_open(zc->zc_name, &spa, FTAG);
964 	if (error != 0)
965 		return (error);
966 
967 	/*
968 	 * A root pool with concatenated devices is not supported.
969 	 * Thus, can not add a device to a root pool with one device.
970 	 */
971 	if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0) {
972 		spa_close(spa, FTAG);
973 		return (EDOM);
974 	}
975 
976 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
977 	    &config)) == 0) {
978 		error = spa_vdev_add(spa, config);
979 		nvlist_free(config);
980 	}
981 	spa_close(spa, FTAG);
982 	return (error);
983 }
984 
985 static int
986 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
987 {
988 	spa_t *spa;
989 	int error;
990 
991 	error = spa_open(zc->zc_name, &spa, FTAG);
992 	if (error != 0)
993 		return (error);
994 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
995 	spa_close(spa, FTAG);
996 	return (error);
997 }
998 
999 static int
1000 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1001 {
1002 	spa_t *spa;
1003 	int error;
1004 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1005 
1006 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1007 		return (error);
1008 	switch (zc->zc_cookie) {
1009 	case VDEV_STATE_ONLINE:
1010 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1011 		break;
1012 
1013 	case VDEV_STATE_OFFLINE:
1014 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1015 		break;
1016 
1017 	case VDEV_STATE_FAULTED:
1018 		error = vdev_fault(spa, zc->zc_guid);
1019 		break;
1020 
1021 	case VDEV_STATE_DEGRADED:
1022 		error = vdev_degrade(spa, zc->zc_guid);
1023 		break;
1024 
1025 	default:
1026 		error = EINVAL;
1027 	}
1028 	zc->zc_cookie = newstate;
1029 	spa_close(spa, FTAG);
1030 	return (error);
1031 }
1032 
1033 static int
1034 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1035 {
1036 	spa_t *spa;
1037 	int replacing = zc->zc_cookie;
1038 	nvlist_t *config;
1039 	int error;
1040 
1041 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1042 		return (error);
1043 
1044 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1045 	    &config)) == 0) {
1046 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1047 		nvlist_free(config);
1048 	}
1049 
1050 	spa_close(spa, FTAG);
1051 	return (error);
1052 }
1053 
1054 static int
1055 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1056 {
1057 	spa_t *spa;
1058 	int error;
1059 
1060 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1061 		return (error);
1062 
1063 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1064 
1065 	spa_close(spa, FTAG);
1066 	return (error);
1067 }
1068 
1069 static int
1070 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1071 {
1072 	spa_t *spa;
1073 	char *path = zc->zc_value;
1074 	uint64_t guid = zc->zc_guid;
1075 	int error;
1076 
1077 	error = spa_open(zc->zc_name, &spa, FTAG);
1078 	if (error != 0)
1079 		return (error);
1080 
1081 	error = spa_vdev_setpath(spa, guid, path);
1082 	spa_close(spa, FTAG);
1083 	return (error);
1084 }
1085 
1086 /*
1087  * inputs:
1088  * zc_name		name of filesystem
1089  * zc_nvlist_dst_size	size of buffer for property nvlist
1090  *
1091  * outputs:
1092  * zc_objset_stats	stats
1093  * zc_nvlist_dst	property nvlist
1094  * zc_nvlist_dst_size	size of property nvlist
1095  * zc_value		alternate root
1096  */
1097 static int
1098 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1099 {
1100 	objset_t *os = NULL;
1101 	int error;
1102 	nvlist_t *nv;
1103 
1104 retry:
1105 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1106 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1107 	if (error != 0) {
1108 		/*
1109 		 * This is ugly: dmu_objset_open() can return EBUSY if
1110 		 * the objset is held exclusively. Fortunately this hold is
1111 		 * only for a short while, so we retry here.
1112 		 * This avoids user code having to handle EBUSY,
1113 		 * for example for a "zfs list".
1114 		 */
1115 		if (error == EBUSY) {
1116 			delay(1);
1117 			goto retry;
1118 		}
1119 		return (error);
1120 	}
1121 
1122 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1123 
1124 	if (zc->zc_nvlist_dst != 0 &&
1125 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1126 		dmu_objset_stats(os, nv);
1127 		/*
1128 		 * NB: zvol_get_stats() will read the objset contents,
1129 		 * which we aren't supposed to do with a
1130 		 * DS_MODE_STANDARD open, because it could be
1131 		 * inconsistent.  So this is a bit of a workaround...
1132 		 */
1133 		if (!zc->zc_objset_stats.dds_inconsistent) {
1134 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1135 				VERIFY(zvol_get_stats(os, nv) == 0);
1136 		}
1137 		error = put_nvlist(zc, nv);
1138 		nvlist_free(nv);
1139 	}
1140 
1141 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
1142 
1143 	dmu_objset_close(os);
1144 	return (error);
1145 }
1146 
1147 /*
1148  * inputs:
1149  * zc_name		name of filesystem
1150  * zc_cookie		zap cursor
1151  * zc_nvlist_dst_size	size of buffer for property nvlist
1152  *
1153  * outputs:
1154  * zc_name		name of next filesystem
1155  * zc_objset_stats	stats
1156  * zc_nvlist_dst	property nvlist
1157  * zc_nvlist_dst_size	size of property nvlist
1158  * zc_value		alternate root
1159  */
1160 static int
1161 zfs_ioc_objset_version(zfs_cmd_t *zc)
1162 {
1163 	objset_t *os = NULL;
1164 	int error;
1165 
1166 retry:
1167 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1168 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1169 	if (error != 0) {
1170 		/*
1171 		 * This is ugly: dmu_objset_open() can return EBUSY if
1172 		 * the objset is held exclusively. Fortunately this hold is
1173 		 * only for a short while, so we retry here.
1174 		 * This avoids user code having to handle EBUSY,
1175 		 * for example for a "zfs list".
1176 		 */
1177 		if (error == EBUSY) {
1178 			delay(1);
1179 			goto retry;
1180 		}
1181 		return (error);
1182 	}
1183 
1184 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1185 
1186 	/*
1187 	 * NB: zfs_get_version() will read the objset contents,
1188 	 * which we aren't supposed to do with a
1189 	 * DS_MODE_STANDARD open, because it could be
1190 	 * inconsistent.  So this is a bit of a workaround...
1191 	 */
1192 	zc->zc_cookie = 0;
1193 	if (!zc->zc_objset_stats.dds_inconsistent)
1194 		if (dmu_objset_type(os) == DMU_OST_ZFS)
1195 			(void) zfs_get_version(os, &zc->zc_cookie);
1196 
1197 	dmu_objset_close(os);
1198 	return (0);
1199 }
1200 
1201 static int
1202 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1203 {
1204 	objset_t *os;
1205 	int error;
1206 	char *p;
1207 
1208 retry:
1209 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1210 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1211 	if (error != 0) {
1212 		/*
1213 		 * This is ugly: dmu_objset_open() can return EBUSY if
1214 		 * the objset is held exclusively. Fortunately this hold is
1215 		 * only for a short while, so we retry here.
1216 		 * This avoids user code having to handle EBUSY,
1217 		 * for example for a "zfs list".
1218 		 */
1219 		if (error == EBUSY) {
1220 			delay(1);
1221 			goto retry;
1222 		}
1223 		if (error == ENOENT)
1224 			error = ESRCH;
1225 		return (error);
1226 	}
1227 
1228 	p = strrchr(zc->zc_name, '/');
1229 	if (p == NULL || p[1] != '\0')
1230 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1231 	p = zc->zc_name + strlen(zc->zc_name);
1232 
1233 	do {
1234 		error = dmu_dir_list_next(os,
1235 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1236 		    NULL, &zc->zc_cookie);
1237 		if (error == ENOENT)
1238 			error = ESRCH;
1239 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1240 	    !zone_dataset_visible(zc->zc_name, NULL));
1241 
1242 	/*
1243 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1244 	 * try to get stats for it.  Userland will skip over it.
1245 	 */
1246 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1247 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1248 
1249 	dmu_objset_close(os);
1250 	return (error);
1251 }
1252 
1253 /*
1254  * inputs:
1255  * zc_name		name of filesystem
1256  * zc_cookie		zap cursor
1257  * zc_nvlist_dst_size	size of buffer for property nvlist
1258  *
1259  * outputs:
1260  * zc_name		name of next snapshot
1261  * zc_objset_stats	stats
1262  * zc_nvlist_dst	property nvlist
1263  * zc_nvlist_dst_size	size of property nvlist
1264  * zc_value		alternate root
1265  */
1266 static int
1267 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1268 {
1269 	objset_t *os;
1270 	int error;
1271 
1272 retry:
1273 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1274 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1275 	if (error != 0) {
1276 		/*
1277 		 * This is ugly: dmu_objset_open() can return EBUSY if
1278 		 * the objset is held exclusively. Fortunately this hold is
1279 		 * only for a short while, so we retry here.
1280 		 * This avoids user code having to handle EBUSY,
1281 		 * for example for a "zfs list".
1282 		 */
1283 		if (error == EBUSY) {
1284 			delay(1);
1285 			goto retry;
1286 		}
1287 		if (error == ENOENT)
1288 			error = ESRCH;
1289 		return (error);
1290 	}
1291 
1292 	/*
1293 	 * A dataset name of maximum length cannot have any snapshots,
1294 	 * so exit immediately.
1295 	 */
1296 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1297 		dmu_objset_close(os);
1298 		return (ESRCH);
1299 	}
1300 
1301 	error = dmu_snapshot_list_next(os,
1302 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1303 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
1304 	if (error == ENOENT)
1305 		error = ESRCH;
1306 
1307 	if (error == 0)
1308 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1309 
1310 	/* if we failed, undo the @ that we tacked on to zc_name */
1311 	if (error != 0)
1312 		*strchr(zc->zc_name, '@') = '\0';
1313 
1314 	dmu_objset_close(os);
1315 	return (error);
1316 }
1317 
1318 static int
1319 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1320 {
1321 	nvpair_t *elem;
1322 	int error;
1323 	uint64_t intval;
1324 	char *strval;
1325 
1326 	/*
1327 	 * First validate permission to set all of the properties
1328 	 */
1329 	elem = NULL;
1330 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1331 		const char *propname = nvpair_name(elem);
1332 		zfs_prop_t prop = zfs_name_to_prop(propname);
1333 
1334 		if (prop == ZPROP_INVAL) {
1335 			/*
1336 			 * If this is a user-defined property, it must be a
1337 			 * string, and there is no further validation to do.
1338 			 */
1339 			if (!zfs_prop_user(propname) ||
1340 			    nvpair_type(elem) != DATA_TYPE_STRING)
1341 				return (EINVAL);
1342 
1343 			if (error = zfs_secpolicy_write_perms(name,
1344 			    ZFS_DELEG_PERM_USERPROP, CRED()))
1345 				return (error);
1346 			continue;
1347 		}
1348 
1349 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1350 			return (error);
1351 
1352 		/*
1353 		 * Check that this value is valid for this pool version
1354 		 */
1355 		switch (prop) {
1356 		case ZFS_PROP_COMPRESSION:
1357 			/*
1358 			 * If the user specified gzip compression, make sure
1359 			 * the SPA supports it. We ignore any errors here since
1360 			 * we'll catch them later.
1361 			 */
1362 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1363 			    nvpair_value_uint64(elem, &intval) == 0 &&
1364 			    intval >= ZIO_COMPRESS_GZIP_1 &&
1365 			    intval <= ZIO_COMPRESS_GZIP_9) {
1366 				if (zfs_check_version(name,
1367 				    SPA_VERSION_GZIP_COMPRESSION))
1368 					return (ENOTSUP);
1369 			}
1370 			break;
1371 
1372 		case ZFS_PROP_COPIES:
1373 			if (zfs_check_version(name, SPA_VERSION_DITTO_BLOCKS))
1374 				return (ENOTSUP);
1375 			break;
1376 		case ZFS_PROP_NORMALIZE:
1377 		case ZFS_PROP_UTF8ONLY:
1378 		case ZFS_PROP_CASE:
1379 			if (zfs_check_version(name, SPA_VERSION_NORMALIZATION))
1380 				return (ENOTSUP);
1381 
1382 		}
1383 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1384 			return (error);
1385 	}
1386 
1387 	elem = NULL;
1388 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1389 		const char *propname = nvpair_name(elem);
1390 		zfs_prop_t prop = zfs_name_to_prop(propname);
1391 
1392 		if (prop == ZPROP_INVAL) {
1393 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1394 			error = dsl_prop_set(name, propname, 1,
1395 			    strlen(strval) + 1, strval);
1396 			if (error == 0)
1397 				continue;
1398 			else
1399 				return (error);
1400 		}
1401 
1402 		switch (prop) {
1403 		case ZFS_PROP_QUOTA:
1404 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1405 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1406 				return (error);
1407 			break;
1408 
1409 		case ZFS_PROP_RESERVATION:
1410 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1411 			    (error = dsl_dir_set_reservation(name,
1412 			    intval)) != 0)
1413 				return (error);
1414 			break;
1415 
1416 		case ZFS_PROP_VOLSIZE:
1417 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1418 			    (error = zvol_set_volsize(name,
1419 			    ddi_driver_major(zfs_dip), intval)) != 0)
1420 				return (error);
1421 			break;
1422 
1423 		case ZFS_PROP_VOLBLOCKSIZE:
1424 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1425 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1426 				return (error);
1427 			break;
1428 
1429 		case ZFS_PROP_VERSION:
1430 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1431 			    (error = zfs_set_version(name, intval)) != 0)
1432 				return (error);
1433 			break;
1434 
1435 		default:
1436 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1437 				if (zfs_prop_get_type(prop) !=
1438 				    PROP_TYPE_STRING)
1439 					return (EINVAL);
1440 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1441 				if ((error = dsl_prop_set(name,
1442 				    nvpair_name(elem), 1, strlen(strval) + 1,
1443 				    strval)) != 0)
1444 					return (error);
1445 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1446 				const char *unused;
1447 
1448 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1449 
1450 				switch (zfs_prop_get_type(prop)) {
1451 				case PROP_TYPE_NUMBER:
1452 					break;
1453 				case PROP_TYPE_STRING:
1454 					return (EINVAL);
1455 				case PROP_TYPE_INDEX:
1456 					if (zfs_prop_index_to_string(prop,
1457 					    intval, &unused) != 0)
1458 						return (EINVAL);
1459 					break;
1460 				default:
1461 					cmn_err(CE_PANIC,
1462 					    "unknown property type");
1463 					break;
1464 				}
1465 
1466 				if ((error = dsl_prop_set(name, propname,
1467 				    8, 1, &intval)) != 0)
1468 					return (error);
1469 			} else {
1470 				return (EINVAL);
1471 			}
1472 			break;
1473 		}
1474 	}
1475 
1476 	return (0);
1477 }
1478 
1479 /*
1480  * inputs:
1481  * zc_name		name of filesystem
1482  * zc_value		name of property to inherit
1483  * zc_nvlist_src{_size}	nvlist of properties to apply
1484  *
1485  * outputs:		none
1486  */
1487 static int
1488 zfs_ioc_set_prop(zfs_cmd_t *zc)
1489 {
1490 	nvlist_t *nvl;
1491 	int error;
1492 
1493 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1494 	    &nvl)) != 0)
1495 		return (error);
1496 
1497 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1498 
1499 	nvlist_free(nvl);
1500 	return (error);
1501 }
1502 
1503 /*
1504  * inputs:
1505  * zc_name		name of filesystem
1506  * zc_value		name of property to inherit
1507  *
1508  * outputs:		none
1509  */
1510 static int
1511 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1512 {
1513 	/* the property name has been validated by zfs_secpolicy_inherit() */
1514 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1515 }
1516 
1517 static int
1518 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1519 {
1520 	nvlist_t *props;
1521 	spa_t *spa;
1522 	int error;
1523 
1524 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1525 	    &props)))
1526 		return (error);
1527 
1528 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1529 		nvlist_free(props);
1530 		return (error);
1531 	}
1532 
1533 	error = spa_prop_set(spa, props);
1534 
1535 	nvlist_free(props);
1536 	spa_close(spa, FTAG);
1537 
1538 	return (error);
1539 }
1540 
1541 static int
1542 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1543 {
1544 	spa_t *spa;
1545 	int error;
1546 	nvlist_t *nvp = NULL;
1547 
1548 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1549 		return (error);
1550 
1551 	error = spa_prop_get(spa, &nvp);
1552 
1553 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1554 		error = put_nvlist(zc, nvp);
1555 	else
1556 		error = EFAULT;
1557 
1558 	spa_close(spa, FTAG);
1559 
1560 	if (nvp)
1561 		nvlist_free(nvp);
1562 	return (error);
1563 }
1564 
1565 static int
1566 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1567 {
1568 	nvlist_t *nvp;
1569 	int error;
1570 	uint32_t uid;
1571 	uint32_t gid;
1572 	uint32_t *groups;
1573 	uint_t group_cnt;
1574 	cred_t	*usercred;
1575 
1576 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1577 	    &nvp)) != 0) {
1578 		return (error);
1579 	}
1580 
1581 	if ((error = nvlist_lookup_uint32(nvp,
1582 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1583 		nvlist_free(nvp);
1584 		return (EPERM);
1585 	}
1586 
1587 	if ((error = nvlist_lookup_uint32(nvp,
1588 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1589 		nvlist_free(nvp);
1590 		return (EPERM);
1591 	}
1592 
1593 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1594 	    &groups, &group_cnt)) != 0) {
1595 		nvlist_free(nvp);
1596 		return (EPERM);
1597 	}
1598 	usercred = cralloc();
1599 	if ((crsetugid(usercred, uid, gid) != 0) ||
1600 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1601 		nvlist_free(nvp);
1602 		crfree(usercred);
1603 		return (EPERM);
1604 	}
1605 	nvlist_free(nvp);
1606 	error = dsl_deleg_access(zc->zc_name,
1607 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1608 	crfree(usercred);
1609 	return (error);
1610 }
1611 
1612 /*
1613  * inputs:
1614  * zc_name		name of filesystem
1615  * zc_nvlist_src{_size}	nvlist of delegated permissions
1616  * zc_perm_action	allow/unallow flag
1617  *
1618  * outputs:		none
1619  */
1620 static int
1621 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1622 {
1623 	int error;
1624 	nvlist_t *fsaclnv = NULL;
1625 
1626 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1627 	    &fsaclnv)) != 0)
1628 		return (error);
1629 
1630 	/*
1631 	 * Verify nvlist is constructed correctly
1632 	 */
1633 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1634 		nvlist_free(fsaclnv);
1635 		return (EINVAL);
1636 	}
1637 
1638 	/*
1639 	 * If we don't have PRIV_SYS_MOUNT, then validate
1640 	 * that user is allowed to hand out each permission in
1641 	 * the nvlist(s)
1642 	 */
1643 
1644 	error = secpolicy_zfs(CRED());
1645 	if (error) {
1646 		if (zc->zc_perm_action == B_FALSE) {
1647 			error = dsl_deleg_can_allow(zc->zc_name,
1648 			    fsaclnv, CRED());
1649 		} else {
1650 			error = dsl_deleg_can_unallow(zc->zc_name,
1651 			    fsaclnv, CRED());
1652 		}
1653 	}
1654 
1655 	if (error == 0)
1656 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1657 
1658 	nvlist_free(fsaclnv);
1659 	return (error);
1660 }
1661 
1662 /*
1663  * inputs:
1664  * zc_name		name of filesystem
1665  *
1666  * outputs:
1667  * zc_nvlist_src{_size}	nvlist of delegated permissions
1668  */
1669 static int
1670 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1671 {
1672 	nvlist_t *nvp;
1673 	int error;
1674 
1675 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1676 		error = put_nvlist(zc, nvp);
1677 		nvlist_free(nvp);
1678 	}
1679 
1680 	return (error);
1681 }
1682 
1683 /*
1684  * inputs:
1685  * zc_name		name of volume
1686  *
1687  * outputs:		none
1688  */
1689 static int
1690 zfs_ioc_create_minor(zfs_cmd_t *zc)
1691 {
1692 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1693 }
1694 
1695 /*
1696  * inputs:
1697  * zc_name		name of volume
1698  *
1699  * outputs:		none
1700  */
1701 static int
1702 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1703 {
1704 	return (zvol_remove_minor(zc->zc_name));
1705 }
1706 
1707 /*
1708  * Search the vfs list for a specified resource.  Returns a pointer to it
1709  * or NULL if no suitable entry is found. The caller of this routine
1710  * is responsible for releasing the returned vfs pointer.
1711  */
1712 static vfs_t *
1713 zfs_get_vfs(const char *resource)
1714 {
1715 	struct vfs *vfsp;
1716 	struct vfs *vfs_found = NULL;
1717 
1718 	vfs_list_read_lock();
1719 	vfsp = rootvfs;
1720 	do {
1721 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1722 			VFS_HOLD(vfsp);
1723 			vfs_found = vfsp;
1724 			break;
1725 		}
1726 		vfsp = vfsp->vfs_next;
1727 	} while (vfsp != rootvfs);
1728 	vfs_list_unlock();
1729 	return (vfs_found);
1730 }
1731 
1732 /* ARGSUSED */
1733 static void
1734 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1735 {
1736 	zfs_creat_t *zct = arg;
1737 	uint64_t version;
1738 
1739 	if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
1740 		version = ZPL_VERSION;
1741 	else
1742 		version = ZPL_VERSION_FUID - 1;
1743 
1744 	(void) nvlist_lookup_uint64(zct->zct_props,
1745 	    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
1746 
1747 	zfs_create_fs(os, cr, version, zct->zct_norm, tx);
1748 }
1749 
1750 /*
1751  * zfs_prop_lookup()
1752  *
1753  * Look for the property first in the existing property nvlist.  If
1754  * it's already present, you're done.  If it's not there, attempt to
1755  * find the property value from a parent dataset.  If that fails, fall
1756  * back to the property's default value.  In either of these two
1757  * cases, if update is TRUE, add a value for the property to the
1758  * property nvlist.
1759  *
1760  * If the rval pointer is non-NULL, copy the discovered value to rval.
1761  *
1762  * If we get any unexpected errors, bail and return the error number
1763  * to the caller.
1764  *
1765  * If we succeed, return 0.
1766  */
1767 static int
1768 zfs_prop_lookup(const char *parentname, zfs_prop_t propnum,
1769     nvlist_t *proplist, uint64_t *rval, boolean_t update)
1770 {
1771 	const char *propname;
1772 	uint64_t value;
1773 	int error = ENOENT;
1774 
1775 	propname = zfs_prop_to_name(propnum);
1776 	if (proplist != NULL)
1777 		error = nvlist_lookup_uint64(proplist, propname, &value);
1778 	if (error == ENOENT) {
1779 		error = dsl_prop_get_integer(parentname, propname,
1780 		    &value, NULL);
1781 		if (error == ENOENT)
1782 			value = zfs_prop_default_numeric(propnum);
1783 		else if (error != 0)
1784 			return (error);
1785 		if (update) {
1786 			ASSERT(proplist != NULL);
1787 			error = nvlist_add_uint64(proplist, propname, value);
1788 		}
1789 	}
1790 	if (error == 0 && rval)
1791 		*rval = value;
1792 	return (error);
1793 }
1794 
1795 /*
1796  * zfs_normalization_get
1797  *
1798  * Get the normalization flag value.  If the properties have
1799  * non-default values, make sure the pool version is recent enough to
1800  * support these choices.
1801  */
1802 static int
1803 zfs_normalization_get(const char *dataset, nvlist_t *proplist, int *norm,
1804     boolean_t update)
1805 {
1806 	char parentname[MAXNAMELEN];
1807 	char poolname[MAXNAMELEN];
1808 	char *cp;
1809 	uint64_t value;
1810 	int check = 0;
1811 	int error;
1812 
1813 	ASSERT(norm != NULL);
1814 	*norm = 0;
1815 
1816 	(void) strncpy(parentname, dataset, sizeof (parentname));
1817 	cp = strrchr(parentname, '@');
1818 	if (cp != NULL) {
1819 		cp[0] = '\0';
1820 	} else {
1821 		cp = strrchr(parentname, '/');
1822 		if (cp == NULL)
1823 			return (ENOENT);
1824 		cp[0] = '\0';
1825 	}
1826 
1827 	(void) strncpy(poolname, dataset, sizeof (poolname));
1828 	cp = strchr(poolname, '/');
1829 	if (cp != NULL)
1830 		cp[0] = '\0';
1831 
1832 	error = zfs_prop_lookup(parentname, ZFS_PROP_UTF8ONLY,
1833 	    proplist, &value, update);
1834 	if (error != 0)
1835 		return (error);
1836 	if (value != zfs_prop_default_numeric(ZFS_PROP_UTF8ONLY))
1837 		check = 1;
1838 
1839 	error = zfs_prop_lookup(parentname, ZFS_PROP_NORMALIZE,
1840 	    proplist, &value, update);
1841 	if (error != 0)
1842 		return (error);
1843 	if (value != zfs_prop_default_numeric(ZFS_PROP_NORMALIZE)) {
1844 		check = 1;
1845 		switch ((int)value) {
1846 		case ZFS_NORMALIZE_NONE:
1847 			break;
1848 		case ZFS_NORMALIZE_C:
1849 			*norm |= U8_TEXTPREP_NFC;
1850 			break;
1851 		case ZFS_NORMALIZE_D:
1852 			*norm |= U8_TEXTPREP_NFD;
1853 			break;
1854 		case ZFS_NORMALIZE_KC:
1855 			*norm |= U8_TEXTPREP_NFKC;
1856 			break;
1857 		case ZFS_NORMALIZE_KD:
1858 			*norm |= U8_TEXTPREP_NFKD;
1859 			break;
1860 		default:
1861 			ASSERT((int)value >= ZFS_NORMALIZE_NONE);
1862 			ASSERT((int)value <= ZFS_NORMALIZE_KD);
1863 			break;
1864 		}
1865 	}
1866 
1867 	error = zfs_prop_lookup(parentname, ZFS_PROP_CASE,
1868 	    proplist, &value, update);
1869 	if (error != 0)
1870 		return (error);
1871 	if (value != zfs_prop_default_numeric(ZFS_PROP_CASE)) {
1872 		check = 1;
1873 		switch ((int)value) {
1874 		case ZFS_CASE_SENSITIVE:
1875 			break;
1876 		case ZFS_CASE_INSENSITIVE:
1877 			*norm |= U8_TEXTPREP_TOUPPER;
1878 			break;
1879 		case ZFS_CASE_MIXED:
1880 			*norm |= U8_TEXTPREP_TOUPPER;
1881 			break;
1882 		default:
1883 			ASSERT((int)value >= ZFS_CASE_SENSITIVE);
1884 			ASSERT((int)value <= ZFS_CASE_MIXED);
1885 			break;
1886 		}
1887 	}
1888 
1889 	if (check == 1)
1890 		if (zfs_check_version(poolname, SPA_VERSION_NORMALIZATION))
1891 			return (ENOTSUP);
1892 	return (0);
1893 }
1894 
1895 /*
1896  * inputs:
1897  * zc_objset_type	type of objset to create (fs vs zvol)
1898  * zc_name		name of new objset
1899  * zc_value		name of snapshot to clone from (may be empty)
1900  * zc_nvlist_src{_size}	nvlist of properties to apply
1901  *
1902  * outputs:		none
1903  */
1904 static int
1905 zfs_ioc_create(zfs_cmd_t *zc)
1906 {
1907 	objset_t *clone;
1908 	int error = 0;
1909 	zfs_creat_t zct;
1910 	nvlist_t *nvprops = NULL;
1911 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
1912 	dmu_objset_type_t type = zc->zc_objset_type;
1913 
1914 	switch (type) {
1915 
1916 	case DMU_OST_ZFS:
1917 		cbfunc = zfs_create_cb;
1918 		break;
1919 
1920 	case DMU_OST_ZVOL:
1921 		cbfunc = zvol_create_cb;
1922 		break;
1923 
1924 	default:
1925 		cbfunc = NULL;
1926 	}
1927 	if (strchr(zc->zc_name, '@') ||
1928 	    strchr(zc->zc_name, '%'))
1929 		return (EINVAL);
1930 
1931 	if (zc->zc_nvlist_src != NULL &&
1932 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1933 	    &nvprops)) != 0)
1934 		return (error);
1935 
1936 	zct.zct_norm = 0;
1937 	zct.zct_props = nvprops;
1938 
1939 	if (zc->zc_value[0] != '\0') {
1940 		/*
1941 		 * We're creating a clone of an existing snapshot.
1942 		 */
1943 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1944 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1945 			nvlist_free(nvprops);
1946 			return (EINVAL);
1947 		}
1948 
1949 		error = dmu_objset_open(zc->zc_value, type,
1950 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1951 		if (error) {
1952 			nvlist_free(nvprops);
1953 			return (error);
1954 		}
1955 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1956 		if (error) {
1957 			dmu_objset_close(clone);
1958 			nvlist_free(nvprops);
1959 			return (error);
1960 		}
1961 		/*
1962 		 * If caller did not provide any properties, allocate
1963 		 * an nvlist for properties, as we will be adding our set-once
1964 		 * properties to it.  This carries the choices made on the
1965 		 * original file system into the clone.
1966 		 */
1967 		if (nvprops == NULL)
1968 			VERIFY(nvlist_alloc(&nvprops,
1969 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
1970 
1971 		/*
1972 		 * We have to have normalization and case-folding
1973 		 * flags correct when we do the file system creation,
1974 		 * so go figure them out now.  All we really care about
1975 		 * here is getting these values into the property list.
1976 		 */
1977 		error = zfs_normalization_get(zc->zc_value, nvprops,
1978 		    &zct.zct_norm, B_TRUE);
1979 		if (error != 0) {
1980 			dmu_objset_close(clone);
1981 			nvlist_free(nvprops);
1982 			return (error);
1983 		}
1984 		dmu_objset_close(clone);
1985 	} else {
1986 		if (cbfunc == NULL) {
1987 			nvlist_free(nvprops);
1988 			return (EINVAL);
1989 		}
1990 
1991 		if (type == DMU_OST_ZVOL) {
1992 			uint64_t volsize, volblocksize;
1993 
1994 			if (nvprops == NULL ||
1995 			    nvlist_lookup_uint64(nvprops,
1996 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1997 			    &volsize) != 0) {
1998 				nvlist_free(nvprops);
1999 				return (EINVAL);
2000 			}
2001 
2002 			if ((error = nvlist_lookup_uint64(nvprops,
2003 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2004 			    &volblocksize)) != 0 && error != ENOENT) {
2005 				nvlist_free(nvprops);
2006 				return (EINVAL);
2007 			}
2008 
2009 			if (error != 0)
2010 				volblocksize = zfs_prop_default_numeric(
2011 				    ZFS_PROP_VOLBLOCKSIZE);
2012 
2013 			if ((error = zvol_check_volblocksize(
2014 			    volblocksize)) != 0 ||
2015 			    (error = zvol_check_volsize(volsize,
2016 			    volblocksize)) != 0) {
2017 				nvlist_free(nvprops);
2018 				return (error);
2019 			}
2020 		} else if (type == DMU_OST_ZFS) {
2021 			uint64_t version;
2022 			int error;
2023 
2024 			error = nvlist_lookup_uint64(nvprops,
2025 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
2026 
2027 			if (error == 0 && (version < ZPL_VERSION_INITIAL ||
2028 			    version > ZPL_VERSION)) {
2029 				nvlist_free(nvprops);
2030 				return (ENOTSUP);
2031 			} else if (error == 0 && version >= ZPL_VERSION_FUID &&
2032 			    zfs_check_version(zc->zc_name, SPA_VERSION_FUID)) {
2033 				nvlist_free(nvprops);
2034 				return (ENOTSUP);
2035 			}
2036 
2037 			/*
2038 			 * We have to have normalization and
2039 			 * case-folding flags correct when we do the
2040 			 * file system creation, so go figure them out
2041 			 * now.  The final argument to zfs_normalization_get()
2042 			 * tells that routine not to update the nvprops
2043 			 * list.
2044 			 */
2045 			error = zfs_normalization_get(zc->zc_name, nvprops,
2046 			    &zct.zct_norm, B_FALSE);
2047 			if (error != 0) {
2048 				nvlist_free(nvprops);
2049 				return (error);
2050 			}
2051 		}
2052 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
2053 		    &zct);
2054 	}
2055 
2056 	/*
2057 	 * It would be nice to do this atomically.
2058 	 */
2059 	if (error == 0) {
2060 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2061 			(void) dmu_objset_destroy(zc->zc_name);
2062 	}
2063 
2064 	nvlist_free(nvprops);
2065 	return (error);
2066 }
2067 
2068 /*
2069  * inputs:
2070  * zc_name	name of filesystem
2071  * zc_value	short name of snapshot
2072  * zc_cookie	recursive flag
2073  *
2074  * outputs:	none
2075  */
2076 static int
2077 zfs_ioc_snapshot(zfs_cmd_t *zc)
2078 {
2079 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2080 		return (EINVAL);
2081 	return (dmu_objset_snapshot(zc->zc_name,
2082 	    zc->zc_value, zc->zc_cookie));
2083 }
2084 
2085 int
2086 zfs_unmount_snap(char *name, void *arg)
2087 {
2088 	char *snapname = arg;
2089 	char *cp;
2090 	vfs_t *vfsp = NULL;
2091 
2092 	/*
2093 	 * Snapshots (which are under .zfs control) must be unmounted
2094 	 * before they can be destroyed.
2095 	 */
2096 
2097 	if (snapname) {
2098 		(void) strcat(name, "@");
2099 		(void) strcat(name, snapname);
2100 		vfsp = zfs_get_vfs(name);
2101 		cp = strchr(name, '@');
2102 		*cp = '\0';
2103 	} else if (strchr(name, '@')) {
2104 		vfsp = zfs_get_vfs(name);
2105 	}
2106 
2107 	if (vfsp) {
2108 		/*
2109 		 * Always force the unmount for snapshots.
2110 		 */
2111 		int flag = MS_FORCE;
2112 		int err;
2113 
2114 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2115 			VFS_RELE(vfsp);
2116 			return (err);
2117 		}
2118 		VFS_RELE(vfsp);
2119 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
2120 			return (err);
2121 	}
2122 	return (0);
2123 }
2124 
2125 /*
2126  * inputs:
2127  * zc_name	name of filesystem
2128  * zc_value	short name of snapshot
2129  *
2130  * outputs:	none
2131  */
2132 static int
2133 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2134 {
2135 	int err;
2136 
2137 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2138 		return (EINVAL);
2139 	err = dmu_objset_find(zc->zc_name,
2140 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2141 	if (err)
2142 		return (err);
2143 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2144 }
2145 
2146 /*
2147  * inputs:
2148  * zc_name		name of dataset to destroy
2149  * zc_objset_type	type of objset
2150  *
2151  * outputs:		none
2152  */
2153 static int
2154 zfs_ioc_destroy(zfs_cmd_t *zc)
2155 {
2156 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2157 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2158 		if (err)
2159 			return (err);
2160 	}
2161 
2162 	return (dmu_objset_destroy(zc->zc_name));
2163 }
2164 
2165 /*
2166  * inputs:
2167  * zc_name	name of snapshot to roll back to
2168  *
2169  * outputs:	none
2170  */
2171 static int
2172 zfs_ioc_rollback(zfs_cmd_t *zc)
2173 {
2174 	return (dmu_objset_rollback(zc->zc_name));
2175 }
2176 
2177 /*
2178  * inputs:
2179  * zc_name	old name of dataset
2180  * zc_value	new name of dataset
2181  * zc_cookie	recursive flag (only valid for snapshots)
2182  *
2183  * outputs:	none
2184  */
2185 static int
2186 zfs_ioc_rename(zfs_cmd_t *zc)
2187 {
2188 	boolean_t recursive = zc->zc_cookie & 1;
2189 
2190 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2191 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2192 	    strchr(zc->zc_value, '%'))
2193 		return (EINVAL);
2194 
2195 	/*
2196 	 * Unmount snapshot unless we're doing a recursive rename,
2197 	 * in which case the dataset code figures out which snapshots
2198 	 * to unmount.
2199 	 */
2200 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2201 	    zc->zc_objset_type == DMU_OST_ZFS) {
2202 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2203 		if (err)
2204 			return (err);
2205 	}
2206 
2207 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2208 }
2209 
2210 /*
2211  * inputs:
2212  * zc_name		name of containing filesystem
2213  * zc_nvlist_src{_size}	nvlist of properties to apply
2214  * zc_value		name of snapshot to create
2215  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
2216  * zc_cookie		file descriptor to recv from
2217  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
2218  * zc_guid		force flag
2219  *
2220  * outputs:
2221  * zc_cookie		number of bytes read
2222  */
2223 static int
2224 zfs_ioc_recv(zfs_cmd_t *zc)
2225 {
2226 	file_t *fp;
2227 	objset_t *os;
2228 	dmu_recv_cookie_t drc;
2229 	zfsvfs_t *zfsvfs = NULL;
2230 	boolean_t force = (boolean_t)zc->zc_guid;
2231 	int error, fd;
2232 	offset_t off;
2233 	nvlist_t *props = NULL;
2234 	objset_t *origin = NULL;
2235 	char *tosnap;
2236 	char tofs[ZFS_MAXNAMELEN];
2237 
2238 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2239 	    strchr(zc->zc_value, '@') == NULL ||
2240 	    strchr(zc->zc_value, '%'))
2241 		return (EINVAL);
2242 
2243 	(void) strcpy(tofs, zc->zc_value);
2244 	tosnap = strchr(tofs, '@');
2245 	*tosnap = '\0';
2246 	tosnap++;
2247 
2248 	if (zc->zc_nvlist_src != NULL &&
2249 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2250 	    &props)) != 0)
2251 		return (error);
2252 
2253 	fd = zc->zc_cookie;
2254 	fp = getf(fd);
2255 	if (fp == NULL) {
2256 		nvlist_free(props);
2257 		return (EBADF);
2258 	}
2259 
2260 	/*
2261 	 * Get the zfsvfs for the receiving objset. There
2262 	 * won't be one if we're operating on a zvol, if the
2263 	 * objset doesn't exist yet, or is not mounted.
2264 	 */
2265 
2266 	error = dmu_objset_open(tofs, DMU_OST_ANY,
2267 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
2268 	if (!error) {
2269 		if (dmu_objset_type(os) == DMU_OST_ZFS) {
2270 			mutex_enter(&os->os->os_user_ptr_lock);
2271 			zfsvfs = dmu_objset_get_user(os);
2272 			if (zfsvfs != NULL)
2273 				VFS_HOLD(zfsvfs->z_vfs);
2274 			mutex_exit(&os->os->os_user_ptr_lock);
2275 		}
2276 		dmu_objset_close(os);
2277 	}
2278 
2279 	if (zc->zc_string[0]) {
2280 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2281 		    DS_MODE_STANDARD | DS_MODE_READONLY, &origin);
2282 		if (error) {
2283 			if (zfsvfs != NULL)
2284 				VFS_RELE(zfsvfs->z_vfs);
2285 			nvlist_free(props);
2286 			releasef(fd);
2287 			return (error);
2288 		}
2289 	}
2290 
2291 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2292 	    force, origin, zfsvfs != NULL, &drc);
2293 	if (origin)
2294 		dmu_objset_close(origin);
2295 	if (error) {
2296 		if (zfsvfs != NULL)
2297 			VFS_RELE(zfsvfs->z_vfs);
2298 		nvlist_free(props);
2299 		releasef(fd);
2300 		return (error);
2301 	}
2302 
2303 	/*
2304 	 * If properties are supplied, they are to completely replace
2305 	 * the existing ones; "inherit" any existing properties.
2306 	 */
2307 	if (props) {
2308 		objset_t *os;
2309 		nvlist_t *nv = NULL;
2310 
2311 		error = dmu_objset_open(tofs, DMU_OST_ANY,
2312 		    DS_MODE_STANDARD | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
2313 		    &os);
2314 		if (error == 0) {
2315 			error = dsl_prop_get_all(os, &nv);
2316 			dmu_objset_close(os);
2317 		}
2318 		if (error == 0) {
2319 			nvpair_t *elem;
2320 			zfs_cmd_t zc2 = { 0 };
2321 
2322 			(void) strcpy(zc2.zc_name, tofs);
2323 			for (elem = nvlist_next_nvpair(nv, NULL); elem;
2324 			    elem = nvlist_next_nvpair(nv, elem)) {
2325 				(void) strcpy(zc2.zc_value, nvpair_name(elem));
2326 				if (zfs_secpolicy_inherit(&zc2, CRED()) == 0)
2327 					(void) zfs_ioc_inherit_prop(&zc2);
2328 			}
2329 		}
2330 		if (nv)
2331 			nvlist_free(nv);
2332 	}
2333 
2334 	/*
2335 	 * Set properties.  Note, we ignore errors.  Would be better to
2336 	 * do best-effort in zfs_set_prop_nvlist, too.
2337 	 */
2338 	(void) zfs_set_prop_nvlist(tofs, props);
2339 	nvlist_free(props);
2340 
2341 	off = fp->f_offset;
2342 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
2343 
2344 	if (error == 0) {
2345 		if (zfsvfs != NULL) {
2346 			char osname[MAXNAMELEN];
2347 			int mode;
2348 
2349 			(void) zfs_suspend_fs(zfsvfs, osname, &mode);
2350 			error = dmu_recv_end(&drc);
2351 			error |= zfs_resume_fs(zfsvfs, osname, mode);
2352 		} else {
2353 			error = dmu_recv_end(&drc);
2354 		}
2355 	}
2356 	if (zfsvfs != NULL)
2357 		VFS_RELE(zfsvfs->z_vfs);
2358 
2359 	zc->zc_cookie = off - fp->f_offset;
2360 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2361 		fp->f_offset = off;
2362 
2363 	releasef(fd);
2364 	return (error);
2365 }
2366 
2367 /*
2368  * inputs:
2369  * zc_name	name of snapshot to send
2370  * zc_value	short name of incremental fromsnap (may be empty)
2371  * zc_cookie	file descriptor to send stream to
2372  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
2373  *
2374  * outputs: none
2375  */
2376 static int
2377 zfs_ioc_send(zfs_cmd_t *zc)
2378 {
2379 	objset_t *fromsnap = NULL;
2380 	objset_t *tosnap;
2381 	file_t *fp;
2382 	int error;
2383 	offset_t off;
2384 
2385 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2386 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
2387 	if (error)
2388 		return (error);
2389 
2390 	if (zc->zc_value[0] != '\0') {
2391 		char buf[MAXPATHLEN];
2392 		char *cp;
2393 
2394 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
2395 		cp = strchr(buf, '@');
2396 		if (cp)
2397 			*(cp+1) = 0;
2398 		(void) strncat(buf, zc->zc_value, sizeof (buf));
2399 		error = dmu_objset_open(buf, DMU_OST_ANY,
2400 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
2401 		if (error) {
2402 			dmu_objset_close(tosnap);
2403 			return (error);
2404 		}
2405 	}
2406 
2407 	fp = getf(zc->zc_cookie);
2408 	if (fp == NULL) {
2409 		dmu_objset_close(tosnap);
2410 		if (fromsnap)
2411 			dmu_objset_close(fromsnap);
2412 		return (EBADF);
2413 	}
2414 
2415 	off = fp->f_offset;
2416 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
2417 
2418 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2419 		fp->f_offset = off;
2420 	releasef(zc->zc_cookie);
2421 	if (fromsnap)
2422 		dmu_objset_close(fromsnap);
2423 	dmu_objset_close(tosnap);
2424 	return (error);
2425 }
2426 
2427 static int
2428 zfs_ioc_inject_fault(zfs_cmd_t *zc)
2429 {
2430 	int id, error;
2431 
2432 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2433 	    &zc->zc_inject_record);
2434 
2435 	if (error == 0)
2436 		zc->zc_guid = (uint64_t)id;
2437 
2438 	return (error);
2439 }
2440 
2441 static int
2442 zfs_ioc_clear_fault(zfs_cmd_t *zc)
2443 {
2444 	return (zio_clear_fault((int)zc->zc_guid));
2445 }
2446 
2447 static int
2448 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2449 {
2450 	int id = (int)zc->zc_guid;
2451 	int error;
2452 
2453 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2454 	    &zc->zc_inject_record);
2455 
2456 	zc->zc_guid = id;
2457 
2458 	return (error);
2459 }
2460 
2461 static int
2462 zfs_ioc_error_log(zfs_cmd_t *zc)
2463 {
2464 	spa_t *spa;
2465 	int error;
2466 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2467 
2468 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2469 		return (error);
2470 
2471 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2472 	    &count);
2473 	if (error == 0)
2474 		zc->zc_nvlist_dst_size = count;
2475 	else
2476 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2477 
2478 	spa_close(spa, FTAG);
2479 
2480 	return (error);
2481 }
2482 
2483 static int
2484 zfs_ioc_clear(zfs_cmd_t *zc)
2485 {
2486 	spa_t *spa;
2487 	vdev_t *vd;
2488 	uint64_t txg;
2489 	int error;
2490 
2491 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2492 		return (error);
2493 
2494 	/*
2495 	 * Try to resume any I/Os which may have been suspended
2496 	 * as a result of a complete pool failure.
2497 	 */
2498 	if (!list_is_empty(&spa->spa_zio_list)) {
2499 		if (zio_vdev_resume_io(spa) != 0) {
2500 			spa_close(spa, FTAG);
2501 			return (EIO);
2502 		}
2503 	}
2504 
2505 	txg = spa_vdev_enter(spa);
2506 
2507 	if (zc->zc_guid == 0) {
2508 		vd = NULL;
2509 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
2510 		(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
2511 		spa_close(spa, FTAG);
2512 		return (ENODEV);
2513 	}
2514 
2515 	vdev_clear(spa, vd, B_TRUE);
2516 
2517 	(void) spa_vdev_exit(spa, NULL, txg, 0);
2518 
2519 	spa_close(spa, FTAG);
2520 
2521 	return (0);
2522 }
2523 
2524 /*
2525  * inputs:
2526  * zc_name	name of filesystem
2527  * zc_value	name of origin snapshot
2528  *
2529  * outputs:	none
2530  */
2531 static int
2532 zfs_ioc_promote(zfs_cmd_t *zc)
2533 {
2534 	char *cp;
2535 
2536 	/*
2537 	 * We don't need to unmount *all* the origin fs's snapshots, but
2538 	 * it's easier.
2539 	 */
2540 	cp = strchr(zc->zc_value, '@');
2541 	if (cp)
2542 		*cp = '\0';
2543 	(void) dmu_objset_find(zc->zc_value,
2544 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2545 	return (dsl_dataset_promote(zc->zc_name));
2546 }
2547 
2548 /*
2549  * We don't want to have a hard dependency
2550  * against some special symbols in sharefs
2551  * nfs, and smbsrv.  Determine them if needed when
2552  * the first file system is shared.
2553  * Neither sharefs, nfs or smbsrv are unloadable modules.
2554  */
2555 int (*znfsexport_fs)(void *arg);
2556 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2557 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2558 
2559 int zfs_nfsshare_inited;
2560 int zfs_smbshare_inited;
2561 
2562 ddi_modhandle_t nfs_mod;
2563 ddi_modhandle_t sharefs_mod;
2564 ddi_modhandle_t smbsrv_mod;
2565 kmutex_t zfs_share_lock;
2566 
2567 static int
2568 zfs_init_sharefs()
2569 {
2570 	int error;
2571 
2572 	ASSERT(MUTEX_HELD(&zfs_share_lock));
2573 	/* Both NFS and SMB shares also require sharetab support. */
2574 	if (sharefs_mod == NULL && ((sharefs_mod =
2575 	    ddi_modopen("fs/sharefs",
2576 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
2577 		return (ENOSYS);
2578 	}
2579 	if (zshare_fs == NULL && ((zshare_fs =
2580 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2581 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2582 		return (ENOSYS);
2583 	}
2584 	return (0);
2585 }
2586 
2587 static int
2588 zfs_ioc_share(zfs_cmd_t *zc)
2589 {
2590 	int error;
2591 	int opcode;
2592 
2593 	switch (zc->zc_share.z_sharetype) {
2594 	case ZFS_SHARE_NFS:
2595 	case ZFS_UNSHARE_NFS:
2596 		if (zfs_nfsshare_inited == 0) {
2597 			mutex_enter(&zfs_share_lock);
2598 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2599 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2600 				mutex_exit(&zfs_share_lock);
2601 				return (ENOSYS);
2602 			}
2603 			if (znfsexport_fs == NULL &&
2604 			    ((znfsexport_fs = (int (*)(void *))
2605 			    ddi_modsym(nfs_mod,
2606 			    "nfs_export", &error)) == NULL)) {
2607 				mutex_exit(&zfs_share_lock);
2608 				return (ENOSYS);
2609 			}
2610 			error = zfs_init_sharefs();
2611 			if (error) {
2612 				mutex_exit(&zfs_share_lock);
2613 				return (ENOSYS);
2614 			}
2615 			zfs_nfsshare_inited = 1;
2616 			mutex_exit(&zfs_share_lock);
2617 		}
2618 		break;
2619 	case ZFS_SHARE_SMB:
2620 	case ZFS_UNSHARE_SMB:
2621 		if (zfs_smbshare_inited == 0) {
2622 			mutex_enter(&zfs_share_lock);
2623 			if (smbsrv_mod == NULL && ((smbsrv_mod =
2624 			    ddi_modopen("drv/smbsrv",
2625 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2626 				mutex_exit(&zfs_share_lock);
2627 				return (ENOSYS);
2628 			}
2629 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2630 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2631 			    "lmshrd_share_upcall", &error)) == NULL)) {
2632 				mutex_exit(&zfs_share_lock);
2633 				return (ENOSYS);
2634 			}
2635 			error = zfs_init_sharefs();
2636 			if (error) {
2637 				mutex_exit(&zfs_share_lock);
2638 				return (ENOSYS);
2639 			}
2640 			zfs_smbshare_inited = 1;
2641 			mutex_exit(&zfs_share_lock);
2642 		}
2643 		break;
2644 	default:
2645 		return (EINVAL);
2646 	}
2647 
2648 	switch (zc->zc_share.z_sharetype) {
2649 	case ZFS_SHARE_NFS:
2650 	case ZFS_UNSHARE_NFS:
2651 		if (error =
2652 		    znfsexport_fs((void *)
2653 		    (uintptr_t)zc->zc_share.z_exportdata))
2654 			return (error);
2655 		break;
2656 	case ZFS_SHARE_SMB:
2657 	case ZFS_UNSHARE_SMB:
2658 		if (error = zsmbexport_fs((void *)
2659 		    (uintptr_t)zc->zc_share.z_exportdata,
2660 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2661 		    B_TRUE : B_FALSE)) {
2662 			return (error);
2663 		}
2664 		break;
2665 	}
2666 
2667 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2668 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2669 	    SHAREFS_ADD : SHAREFS_REMOVE;
2670 
2671 	/*
2672 	 * Add or remove share from sharetab
2673 	 */
2674 	error = zshare_fs(opcode,
2675 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2676 	    zc->zc_share.z_sharemax);
2677 
2678 	return (error);
2679 
2680 }
2681 
2682 /*
2683  * pool create, destroy, and export don't log the history as part of
2684  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2685  * do the logging of those commands.
2686  */
2687 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2688 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2689 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2690 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2691 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2692 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
2693 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2694 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2695 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2696 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2697 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
2698 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2699 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2700 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2701 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2702 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2703 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2704 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2705 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2706 	{ zfs_ioc_objset_version, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2707 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2708 	    DATASET_NAME, B_FALSE },
2709 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2710 	    DATASET_NAME, B_FALSE },
2711 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2712 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2713 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2714 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2715 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2716 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2717 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
2718 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2719 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2720 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
2721 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2722 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2723 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2724 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2725 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2726 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
2727 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2728 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2729 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2730 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2731 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2732 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2733 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2734 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2735 	    DATASET_NAME, B_FALSE },
2736 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2737 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2738 };
2739 
2740 static int
2741 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2742 {
2743 	zfs_cmd_t *zc;
2744 	uint_t vec;
2745 	int error, rc;
2746 
2747 	if (getminor(dev) != 0)
2748 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2749 
2750 	vec = cmd - ZFS_IOC;
2751 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
2752 
2753 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2754 		return (EINVAL);
2755 
2756 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2757 
2758 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2759 
2760 	if (error == 0)
2761 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2762 
2763 	/*
2764 	 * Ensure that all pool/dataset names are valid before we pass down to
2765 	 * the lower layers.
2766 	 */
2767 	if (error == 0) {
2768 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2769 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
2770 		case POOL_NAME:
2771 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2772 				error = EINVAL;
2773 			break;
2774 
2775 		case DATASET_NAME:
2776 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2777 				error = EINVAL;
2778 			break;
2779 
2780 		case NO_NAME:
2781 			break;
2782 		}
2783 	}
2784 
2785 	if (error == 0)
2786 		error = zfs_ioc_vec[vec].zvec_func(zc);
2787 
2788 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
2789 	if (error == 0) {
2790 		error = rc;
2791 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
2792 			zfs_log_history(zc);
2793 	}
2794 
2795 	kmem_free(zc, sizeof (zfs_cmd_t));
2796 	return (error);
2797 }
2798 
2799 static int
2800 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2801 {
2802 	if (cmd != DDI_ATTACH)
2803 		return (DDI_FAILURE);
2804 
2805 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
2806 	    DDI_PSEUDO, 0) == DDI_FAILURE)
2807 		return (DDI_FAILURE);
2808 
2809 	zfs_dip = dip;
2810 
2811 	ddi_report_dev(dip);
2812 
2813 	return (DDI_SUCCESS);
2814 }
2815 
2816 static int
2817 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2818 {
2819 	if (spa_busy() || zfs_busy() || zvol_busy())
2820 		return (DDI_FAILURE);
2821 
2822 	if (cmd != DDI_DETACH)
2823 		return (DDI_FAILURE);
2824 
2825 	zfs_dip = NULL;
2826 
2827 	ddi_prop_remove_all(dip);
2828 	ddi_remove_minor_node(dip, NULL);
2829 
2830 	return (DDI_SUCCESS);
2831 }
2832 
2833 /*ARGSUSED*/
2834 static int
2835 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2836 {
2837 	switch (infocmd) {
2838 	case DDI_INFO_DEVT2DEVINFO:
2839 		*result = zfs_dip;
2840 		return (DDI_SUCCESS);
2841 
2842 	case DDI_INFO_DEVT2INSTANCE:
2843 		*result = (void *)0;
2844 		return (DDI_SUCCESS);
2845 	}
2846 
2847 	return (DDI_FAILURE);
2848 }
2849 
2850 /*
2851  * OK, so this is a little weird.
2852  *
2853  * /dev/zfs is the control node, i.e. minor 0.
2854  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2855  *
2856  * /dev/zfs has basically nothing to do except serve up ioctls,
2857  * so most of the standard driver entry points are in zvol.c.
2858  */
2859 static struct cb_ops zfs_cb_ops = {
2860 	zvol_open,	/* open */
2861 	zvol_close,	/* close */
2862 	zvol_strategy,	/* strategy */
2863 	nodev,		/* print */
2864 	nodev,		/* dump */
2865 	zvol_read,	/* read */
2866 	zvol_write,	/* write */
2867 	zfsdev_ioctl,	/* ioctl */
2868 	nodev,		/* devmap */
2869 	nodev,		/* mmap */
2870 	nodev,		/* segmap */
2871 	nochpoll,	/* poll */
2872 	ddi_prop_op,	/* prop_op */
2873 	NULL,		/* streamtab */
2874 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
2875 	CB_REV,		/* version */
2876 	nodev,		/* async read */
2877 	nodev,		/* async write */
2878 };
2879 
2880 static struct dev_ops zfs_dev_ops = {
2881 	DEVO_REV,	/* version */
2882 	0,		/* refcnt */
2883 	zfs_info,	/* info */
2884 	nulldev,	/* identify */
2885 	nulldev,	/* probe */
2886 	zfs_attach,	/* attach */
2887 	zfs_detach,	/* detach */
2888 	nodev,		/* reset */
2889 	&zfs_cb_ops,	/* driver operations */
2890 	NULL		/* no bus operations */
2891 };
2892 
2893 static struct modldrv zfs_modldrv = {
2894 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
2895 	    &zfs_dev_ops
2896 };
2897 
2898 static struct modlinkage modlinkage = {
2899 	MODREV_1,
2900 	(void *)&zfs_modlfs,
2901 	(void *)&zfs_modldrv,
2902 	NULL
2903 };
2904 
2905 
2906 uint_t zfs_fsyncer_key;
2907 extern uint_t rrw_tsd_key;
2908 
2909 int
2910 _init(void)
2911 {
2912 	int error;
2913 
2914 	spa_init(FREAD | FWRITE);
2915 	zfs_init();
2916 	zvol_init();
2917 
2918 	if ((error = mod_install(&modlinkage)) != 0) {
2919 		zvol_fini();
2920 		zfs_fini();
2921 		spa_fini();
2922 		return (error);
2923 	}
2924 
2925 	tsd_create(&zfs_fsyncer_key, NULL);
2926 	tsd_create(&rrw_tsd_key, NULL);
2927 
2928 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
2929 	ASSERT(error == 0);
2930 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
2931 
2932 	return (0);
2933 }
2934 
2935 int
2936 _fini(void)
2937 {
2938 	int error;
2939 
2940 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
2941 		return (EBUSY);
2942 
2943 	if ((error = mod_remove(&modlinkage)) != 0)
2944 		return (error);
2945 
2946 	zvol_fini();
2947 	zfs_fini();
2948 	spa_fini();
2949 	if (zfs_nfsshare_inited)
2950 		(void) ddi_modclose(nfs_mod);
2951 	if (zfs_smbshare_inited)
2952 		(void) ddi_modclose(smbsrv_mod);
2953 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
2954 		(void) ddi_modclose(sharefs_mod);
2955 
2956 	tsd_destroy(&zfs_fsyncer_key);
2957 	ldi_ident_release(zfs_li);
2958 	zfs_li = NULL;
2959 	mutex_destroy(&zfs_share_lock);
2960 
2961 	return (error);
2962 }
2963 
2964 int
2965 _info(struct modinfo *modinfop)
2966 {
2967 	return (mod_info(&modlinkage, modinfop));
2968 }
2969