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