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