xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 40feaa914945406e86e193599d115ea71a171d18)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/uio.h>
32 #include <sys/buf.h>
33 #include <sys/modctl.h>
34 #include <sys/open.h>
35 #include <sys/file.h>
36 #include <sys/kmem.h>
37 #include <sys/conf.h>
38 #include <sys/cmn_err.h>
39 #include <sys/stat.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/spa.h>
43 #include <sys/spa_impl.h>
44 #include <sys/vdev.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/dmu.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/dsl_deleg.h>
51 #include <sys/dmu_objset.h>
52 #include <sys/ddi.h>
53 #include <sys/sunddi.h>
54 #include <sys/sunldi.h>
55 #include <sys/policy.h>
56 #include <sys/zone.h>
57 #include <sys/nvpair.h>
58 #include <sys/pathname.h>
59 #include <sys/mount.h>
60 #include <sys/sdt.h>
61 #include <sys/fs/zfs.h>
62 #include <sys/zfs_ctldir.h>
63 #include <sys/zvol.h>
64 #include <sharefs/share.h>
65 #include <sys/zfs_znode.h>
66 
67 #include "zfs_namecheck.h"
68 #include "zfs_prop.h"
69 #include "zfs_deleg.h"
70 
71 extern struct modlfs zfs_modlfs;
72 
73 extern void zfs_init(void);
74 extern void zfs_fini(void);
75 
76 ldi_ident_t zfs_li = NULL;
77 dev_info_t *zfs_dip;
78 
79 typedef int zfs_ioc_func_t(zfs_cmd_t *);
80 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
81 
82 typedef struct zfs_ioc_vec {
83 	zfs_ioc_func_t		*zvec_func;
84 	zfs_secpolicy_func_t	*zvec_secpolicy;
85 	enum {
86 		NO_NAME,
87 		POOL_NAME,
88 		DATASET_NAME
89 	} zvec_namecheck;
90 	boolean_t		zvec_his_log;
91 } zfs_ioc_vec_t;
92 
93 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
94 void
95 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
96 {
97 	const char *newfile;
98 	char buf[256];
99 	va_list adx;
100 
101 	/*
102 	 * Get rid of annoying "../common/" prefix to filename.
103 	 */
104 	newfile = strrchr(file, '/');
105 	if (newfile != NULL) {
106 		newfile = newfile + 1; /* Get rid of leading / */
107 	} else {
108 		newfile = file;
109 	}
110 
111 	va_start(adx, fmt);
112 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
113 	va_end(adx);
114 
115 	/*
116 	 * To get this data, use the zfs-dprintf probe as so:
117 	 * dtrace -q -n 'zfs-dprintf \
118 	 *	/stringof(arg0) == "dbuf.c"/ \
119 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
120 	 * arg0 = file name
121 	 * arg1 = function name
122 	 * arg2 = line number
123 	 * arg3 = message
124 	 */
125 	DTRACE_PROBE4(zfs__dprintf,
126 	    char *, newfile, char *, func, int, line, char *, buf);
127 }
128 
129 static void
130 zfs_log_history(zfs_cmd_t *zc)
131 {
132 	spa_t *spa;
133 	char *buf;
134 
135 	if (zc->zc_history == NULL)
136 		return;
137 
138 	if (zc->zc_history_offset != LOG_CMD_POOL_CREATE &&
139 	    zc->zc_history_offset != LOG_CMD_NORMAL)
140 		return;
141 
142 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
143 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
144 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
145 		kmem_free(buf, HIS_MAX_RECORD_LEN);
146 		return;
147 	}
148 
149 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
150 
151 	if (spa_open(zc->zc_name, &spa, FTAG) != 0) {
152 		kmem_free(buf, HIS_MAX_RECORD_LEN);
153 		return;
154 	}
155 
156 	if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
157 		(void) spa_history_log(spa, buf, zc->zc_history_offset);
158 
159 	spa_close(spa, FTAG);
160 	kmem_free(buf, HIS_MAX_RECORD_LEN);
161 }
162 
163 /*
164  * Policy for top-level read operations (list pools).  Requires no privileges,
165  * and can be used in the local zone, as there is no associated dataset.
166  */
167 /* ARGSUSED */
168 static int
169 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
170 {
171 	return (0);
172 }
173 
174 /*
175  * Policy for dataset read operations (list children, get statistics).  Requires
176  * no privileges, but must be visible in the local zone.
177  */
178 /* ARGSUSED */
179 static int
180 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
181 {
182 	if (INGLOBALZONE(curproc) ||
183 	    zone_dataset_visible(zc->zc_name, NULL))
184 		return (0);
185 
186 	return (ENOENT);
187 }
188 
189 static int
190 zfs_dozonecheck(const char *dataset, cred_t *cr)
191 {
192 	uint64_t zoned;
193 	int writable = 1;
194 
195 	/*
196 	 * The dataset must be visible by this zone -- check this first
197 	 * so they don't see EPERM on something they shouldn't know about.
198 	 */
199 	if (!INGLOBALZONE(curproc) &&
200 	    !zone_dataset_visible(dataset, &writable))
201 		return (ENOENT);
202 
203 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
204 		return (ENOENT);
205 
206 	if (INGLOBALZONE(curproc)) {
207 		/*
208 		 * If the fs is zoned, only root can access it from the
209 		 * global zone.
210 		 */
211 		if (secpolicy_zfs(cr) && zoned)
212 			return (EPERM);
213 	} else {
214 		/*
215 		 * If we are in a local zone, the 'zoned' property must be set.
216 		 */
217 		if (!zoned)
218 			return (EPERM);
219 
220 		/* must be writable by this zone */
221 		if (!writable)
222 			return (EPERM);
223 	}
224 	return (0);
225 }
226 
227 int
228 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
229 {
230 	int error;
231 
232 	error = zfs_dozonecheck(name, cr);
233 	if (error == 0) {
234 		error = secpolicy_zfs(cr);
235 		if (error) {
236 			error = dsl_deleg_access(name, perm, cr);
237 		}
238 	}
239 	return (error);
240 }
241 
242 static int
243 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
244 {
245 	int error = 0;
246 
247 	/*
248 	 * Check permissions for special properties.
249 	 */
250 	switch (prop) {
251 	case ZFS_PROP_ZONED:
252 		/*
253 		 * Disallow setting of 'zoned' from within a local zone.
254 		 */
255 		if (!INGLOBALZONE(curproc))
256 			return (EPERM);
257 		break;
258 
259 	case ZFS_PROP_QUOTA:
260 		if (error =
261 		    zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_QUOTA, cr))
262 			return (error);
263 
264 		if (!INGLOBALZONE(curproc)) {
265 			uint64_t zoned;
266 			char setpoint[MAXNAMELEN];
267 			int dslen;
268 			/*
269 			 * Unprivileged users are allowed to modify the
270 			 * quota on things *under* (ie. contained by)
271 			 * the thing they own.
272 			 */
273 			if (dsl_prop_get_integer(name, "zoned", &zoned,
274 			    setpoint))
275 				return (EPERM);
276 			if (!zoned) /* this shouldn't happen */
277 				return (EPERM);
278 			dslen = strlen(name);
279 			if (dslen <= strlen(setpoint))
280 				return (EPERM);
281 		}
282 	default:
283 		error = zfs_secpolicy_write_perms(name,
284 		    zfs_prop_perm(prop), cr);
285 	}
286 
287 	return (error);
288 }
289 
290 int
291 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
292 {
293 	int error;
294 
295 	error = zfs_dozonecheck(zc->zc_name, cr);
296 	if (error)
297 		return (error);
298 
299 	/*
300 	 * permission to set permissions will be evaluated later in
301 	 * dsl_deleg_can_allow()
302 	 */
303 	return (0);
304 }
305 
306 int
307 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
308 {
309 	int error;
310 	error = zfs_secpolicy_write_perms(zc->zc_name,
311 	    ZFS_DELEG_PERM_ROLLBACK, cr);
312 	if (error == 0)
313 		error = zfs_secpolicy_write_perms(zc->zc_name,
314 		    ZFS_DELEG_PERM_MOUNT, cr);
315 	return (error);
316 }
317 
318 int
319 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
320 {
321 	return (zfs_secpolicy_write_perms(zc->zc_name,
322 	    ZFS_DELEG_PERM_SEND, cr));
323 }
324 
325 int
326 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
327 {
328 	if (!INGLOBALZONE(curproc))
329 		return (EPERM);
330 
331 	if (secpolicy_nfs(CRED()) == 0) {
332 		return (0);
333 	} else {
334 		vnode_t *vp;
335 		int error;
336 
337 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
338 		    NO_FOLLOW, NULL, &vp)) != 0)
339 			return (error);
340 
341 		/* Now make sure mntpnt and dataset are ZFS */
342 
343 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
344 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
345 		    zc->zc_name) != 0)) {
346 			VN_RELE(vp);
347 			return (EPERM);
348 		}
349 
350 		VN_RELE(vp);
351 		return (dsl_deleg_access(zc->zc_name,
352 		    ZFS_DELEG_PERM_SHARE, cr));
353 	}
354 }
355 
356 static int
357 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
358 {
359 	char *cp;
360 
361 	/*
362 	 * Remove the @bla or /bla from the end of the name to get the parent.
363 	 */
364 	(void) strncpy(parent, datasetname, parentsize);
365 	cp = strrchr(parent, '@');
366 	if (cp != NULL) {
367 		cp[0] = '\0';
368 	} else {
369 		cp = strrchr(parent, '/');
370 		if (cp == NULL)
371 			return (ENOENT);
372 		cp[0] = '\0';
373 	}
374 
375 	return (0);
376 }
377 
378 int
379 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
380 {
381 	int error;
382 
383 	if ((error = zfs_secpolicy_write_perms(name,
384 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
385 		return (error);
386 
387 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
388 }
389 
390 static int
391 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
392 {
393 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
394 }
395 
396 /*
397  * Must have sys_config privilege to check the iscsi permission
398  */
399 /* ARGSUSED */
400 static int
401 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
402 {
403 	return (secpolicy_zfs(cr));
404 }
405 
406 int
407 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
408 {
409 	char 	parentname[MAXNAMELEN];
410 	int	error;
411 
412 	if ((error = zfs_secpolicy_write_perms(from,
413 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
414 		return (error);
415 
416 	if ((error = zfs_secpolicy_write_perms(from,
417 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
418 		return (error);
419 
420 	if ((error = zfs_get_parent(to, parentname,
421 	    sizeof (parentname))) != 0)
422 		return (error);
423 
424 	if ((error = zfs_secpolicy_write_perms(parentname,
425 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
426 		return (error);
427 
428 	if ((error = zfs_secpolicy_write_perms(parentname,
429 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
430 		return (error);
431 
432 	return (error);
433 }
434 
435 static int
436 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
437 {
438 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
439 }
440 
441 static int
442 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
443 {
444 	char 	parentname[MAXNAMELEN];
445 	objset_t *clone;
446 	int error;
447 
448 	error = zfs_secpolicy_write_perms(zc->zc_name,
449 	    ZFS_DELEG_PERM_PROMOTE, cr);
450 	if (error)
451 		return (error);
452 
453 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
454 	    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
455 
456 	if (error == 0) {
457 		dsl_dataset_t *pclone = NULL;
458 		dsl_dir_t *dd;
459 		dd = clone->os->os_dsl_dataset->ds_dir;
460 
461 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
462 		error = dsl_dataset_open_obj(dd->dd_pool,
463 		    dd->dd_phys->dd_clone_parent_obj, NULL,
464 		    DS_MODE_NONE, FTAG, &pclone);
465 		rw_exit(&dd->dd_pool->dp_config_rwlock);
466 		if (error) {
467 			dmu_objset_close(clone);
468 			return (error);
469 		}
470 
471 		error = zfs_secpolicy_write_perms(zc->zc_name,
472 		    ZFS_DELEG_PERM_MOUNT, cr);
473 
474 		dsl_dataset_name(pclone, parentname);
475 		dmu_objset_close(clone);
476 		dsl_dataset_close(pclone, DS_MODE_NONE, FTAG);
477 		if (error == 0)
478 			error = zfs_secpolicy_write_perms(parentname,
479 			    ZFS_DELEG_PERM_PROMOTE, cr);
480 	}
481 	return (error);
482 }
483 
484 static int
485 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
486 {
487 	int error;
488 
489 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
490 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
491 		return (error);
492 
493 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
494 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
495 		return (error);
496 
497 	return (zfs_secpolicy_write_perms(zc->zc_name,
498 	    ZFS_DELEG_PERM_CREATE, cr));
499 }
500 
501 int
502 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
503 {
504 	int error;
505 
506 	if ((error = zfs_secpolicy_write_perms(name,
507 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
508 		return (error);
509 
510 	error = zfs_secpolicy_write_perms(name,
511 	    ZFS_DELEG_PERM_MOUNT, cr);
512 
513 	return (error);
514 }
515 
516 static int
517 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
518 {
519 
520 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
521 }
522 
523 static int
524 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
525 {
526 	char 	parentname[MAXNAMELEN];
527 	int 	error;
528 
529 	if ((error = zfs_get_parent(zc->zc_name, parentname,
530 	    sizeof (parentname))) != 0)
531 		return (error);
532 
533 	if (zc->zc_value[0] != '\0') {
534 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
535 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
536 			return (error);
537 	}
538 
539 	if ((error = zfs_secpolicy_write_perms(parentname,
540 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
541 		return (error);
542 
543 	error = zfs_secpolicy_write_perms(parentname,
544 	    ZFS_DELEG_PERM_MOUNT, cr);
545 
546 	return (error);
547 }
548 
549 static int
550 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
551 {
552 	int error;
553 
554 	error = secpolicy_fs_unmount(cr, NULL);
555 	if (error) {
556 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
557 	}
558 	return (error);
559 }
560 
561 /*
562  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
563  * SYS_CONFIG privilege, which is not available in a local zone.
564  */
565 /* ARGSUSED */
566 static int
567 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
568 {
569 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
570 		return (EPERM);
571 
572 	return (0);
573 }
574 
575 /*
576  * Just like zfs_secpolicy_config, except that we will check for
577  * mount permission on the dataset for permission to create/remove
578  * the minor nodes.
579  */
580 static int
581 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
582 {
583 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
584 		return (dsl_deleg_access(zc->zc_name,
585 		    ZFS_DELEG_PERM_MOUNT, cr));
586 	}
587 
588 	return (0);
589 }
590 
591 /*
592  * Policy for fault injection.  Requires all privileges.
593  */
594 /* ARGSUSED */
595 static int
596 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
597 {
598 	return (secpolicy_zinject(cr));
599 }
600 
601 /*
602  * Returns the nvlist as specified by the user in the zfs_cmd_t.
603  */
604 static int
605 get_nvlist(zfs_cmd_t *zc, nvlist_t **nvp)
606 {
607 	char *packed;
608 	size_t size;
609 	int error;
610 	nvlist_t *config = NULL;
611 
612 	/*
613 	 * Read in and unpack the user-supplied nvlist.
614 	 */
615 	if ((size = zc->zc_nvlist_src_size) == 0)
616 		return (EINVAL);
617 
618 	packed = kmem_alloc(size, KM_SLEEP);
619 
620 	if ((error = xcopyin((void *)(uintptr_t)zc->zc_nvlist_src, packed,
621 	    size)) != 0) {
622 		kmem_free(packed, size);
623 		return (error);
624 	}
625 
626 	if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) {
627 		kmem_free(packed, size);
628 		return (error);
629 	}
630 
631 	kmem_free(packed, size);
632 
633 	*nvp = config;
634 	return (0);
635 }
636 
637 static int
638 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
639 {
640 	char *packed = NULL;
641 	size_t size;
642 	int error;
643 
644 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
645 
646 	if (size > zc->zc_nvlist_dst_size) {
647 		error = ENOMEM;
648 	} else {
649 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
650 		    KM_SLEEP) == 0);
651 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
652 		    size);
653 		kmem_free(packed, size);
654 	}
655 
656 	zc->zc_nvlist_dst_size = size;
657 	return (error);
658 }
659 
660 static int
661 zfs_ioc_pool_create(zfs_cmd_t *zc)
662 {
663 	int error;
664 	nvlist_t *config;
665 
666 	if ((error = get_nvlist(zc, &config)) != 0)
667 		return (error);
668 
669 	error = spa_create(zc->zc_name, config, zc->zc_value[0] == '\0' ?
670 	    NULL : zc->zc_value);
671 
672 	nvlist_free(config);
673 
674 	return (error);
675 }
676 
677 static int
678 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
679 {
680 	int error;
681 	zfs_log_history(zc);
682 	error = spa_destroy(zc->zc_name);
683 	return (error);
684 }
685 
686 static int
687 zfs_ioc_pool_import(zfs_cmd_t *zc)
688 {
689 	int error;
690 	nvlist_t *config;
691 	uint64_t guid;
692 
693 	if ((error = get_nvlist(zc, &config)) != 0)
694 		return (error);
695 
696 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
697 	    guid != zc->zc_guid)
698 		error = EINVAL;
699 	else
700 		error = spa_import(zc->zc_name, config,
701 		    zc->zc_value[0] == '\0' ? NULL : zc->zc_value);
702 
703 	nvlist_free(config);
704 
705 	return (error);
706 }
707 
708 static int
709 zfs_ioc_pool_export(zfs_cmd_t *zc)
710 {
711 	int error;
712 	zfs_log_history(zc);
713 	error = spa_export(zc->zc_name, NULL);
714 	return (error);
715 }
716 
717 static int
718 zfs_ioc_pool_configs(zfs_cmd_t *zc)
719 {
720 	nvlist_t *configs;
721 	int error;
722 
723 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
724 		return (EEXIST);
725 
726 	error = put_nvlist(zc, configs);
727 
728 	nvlist_free(configs);
729 
730 	return (error);
731 }
732 
733 static int
734 zfs_ioc_pool_stats(zfs_cmd_t *zc)
735 {
736 	nvlist_t *config;
737 	int error;
738 	int ret = 0;
739 
740 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
741 	    sizeof (zc->zc_value));
742 
743 	if (config != NULL) {
744 		ret = put_nvlist(zc, config);
745 		nvlist_free(config);
746 
747 		/*
748 		 * The config may be present even if 'error' is non-zero.
749 		 * In this case we return success, and preserve the real errno
750 		 * in 'zc_cookie'.
751 		 */
752 		zc->zc_cookie = error;
753 	} else {
754 		ret = error;
755 	}
756 
757 	return (ret);
758 }
759 
760 /*
761  * Try to import the given pool, returning pool stats as appropriate so that
762  * user land knows which devices are available and overall pool health.
763  */
764 static int
765 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
766 {
767 	nvlist_t *tryconfig, *config;
768 	int error;
769 
770 	if ((error = get_nvlist(zc, &tryconfig)) != 0)
771 		return (error);
772 
773 	config = spa_tryimport(tryconfig);
774 
775 	nvlist_free(tryconfig);
776 
777 	if (config == NULL)
778 		return (EINVAL);
779 
780 	error = put_nvlist(zc, config);
781 	nvlist_free(config);
782 
783 	return (error);
784 }
785 
786 static int
787 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
788 {
789 	spa_t *spa;
790 	int error;
791 
792 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
793 		return (error);
794 
795 	spa_config_enter(spa, RW_READER, FTAG);
796 	error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
797 	spa_config_exit(spa, FTAG);
798 
799 	spa_close(spa, FTAG);
800 
801 	return (error);
802 }
803 
804 static int
805 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
806 {
807 	spa_t *spa;
808 	int error;
809 
810 	error = spa_open(zc->zc_name, &spa, FTAG);
811 	if (error == 0) {
812 		spa_freeze(spa);
813 		spa_close(spa, FTAG);
814 	}
815 	return (error);
816 }
817 
818 static int
819 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
820 {
821 	spa_t *spa;
822 	int error;
823 
824 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
825 		return (error);
826 
827 	spa_upgrade(spa);
828 	spa_close(spa, FTAG);
829 
830 	return (error);
831 }
832 
833 static int
834 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
835 {
836 	spa_t *spa;
837 	char *hist_buf;
838 	uint64_t size;
839 	int error;
840 
841 	if ((size = zc->zc_history_len) == 0)
842 		return (EINVAL);
843 
844 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
845 		return (error);
846 
847 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
848 		spa_close(spa, FTAG);
849 		return (ENOTSUP);
850 	}
851 
852 	hist_buf = kmem_alloc(size, KM_SLEEP);
853 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
854 	    &zc->zc_history_len, hist_buf)) == 0) {
855 		error = xcopyout(hist_buf,
856 		    (char *)(uintptr_t)zc->zc_history,
857 		    zc->zc_history_len);
858 	}
859 
860 	spa_close(spa, FTAG);
861 	kmem_free(hist_buf, size);
862 	return (error);
863 }
864 
865 static int
866 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
867 {
868 	int error;
869 
870 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
871 		return (error);
872 
873 	return (0);
874 }
875 
876 static int
877 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
878 {
879 	objset_t *osp;
880 	int error;
881 
882 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
883 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
884 		return (error);
885 
886 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
887 	    sizeof (zc->zc_value));
888 	dmu_objset_close(osp);
889 
890 	return (error);
891 }
892 
893 static int
894 zfs_ioc_vdev_add(zfs_cmd_t *zc)
895 {
896 	spa_t *spa;
897 	int error;
898 	nvlist_t *config;
899 
900 	error = spa_open(zc->zc_name, &spa, FTAG);
901 	if (error != 0)
902 		return (error);
903 
904 	/*
905 	 * A root pool with concatenated devices is not supported.
906 	 * Thus, can not add a device to a root pool with one device.
907 	 */
908 	if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0) {
909 		spa_close(spa, FTAG);
910 		return (EDOM);
911 	}
912 
913 	if ((error = get_nvlist(zc, &config)) == 0) {
914 		error = spa_vdev_add(spa, config);
915 		nvlist_free(config);
916 	}
917 	spa_close(spa, FTAG);
918 	return (error);
919 }
920 
921 static int
922 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
923 {
924 	spa_t *spa;
925 	int error;
926 
927 	error = spa_open(zc->zc_name, &spa, FTAG);
928 	if (error != 0)
929 		return (error);
930 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
931 	spa_close(spa, FTAG);
932 	return (error);
933 }
934 
935 static int
936 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
937 {
938 	spa_t *spa;
939 	int error;
940 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
941 
942 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
943 		return (error);
944 	switch (zc->zc_cookie) {
945 	case VDEV_STATE_ONLINE:
946 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
947 		break;
948 
949 	case VDEV_STATE_OFFLINE:
950 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
951 		break;
952 
953 	case VDEV_STATE_FAULTED:
954 		error = vdev_fault(spa, zc->zc_guid);
955 		break;
956 
957 	case VDEV_STATE_DEGRADED:
958 		error = vdev_degrade(spa, zc->zc_guid);
959 		break;
960 
961 	default:
962 		error = EINVAL;
963 	}
964 	zc->zc_cookie = newstate;
965 	spa_close(spa, FTAG);
966 	return (error);
967 }
968 
969 static int
970 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
971 {
972 	spa_t *spa;
973 	int replacing = zc->zc_cookie;
974 	nvlist_t *config;
975 	int error;
976 
977 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
978 		return (error);
979 
980 	if ((error = get_nvlist(zc, &config)) == 0) {
981 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
982 		nvlist_free(config);
983 	}
984 
985 	spa_close(spa, FTAG);
986 	return (error);
987 }
988 
989 static int
990 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
991 {
992 	spa_t *spa;
993 	int error;
994 
995 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
996 		return (error);
997 
998 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
999 
1000 	spa_close(spa, FTAG);
1001 	return (error);
1002 }
1003 
1004 static int
1005 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1006 {
1007 	spa_t *spa;
1008 	char *path = zc->zc_value;
1009 	uint64_t guid = zc->zc_guid;
1010 	int error;
1011 
1012 	error = spa_open(zc->zc_name, &spa, FTAG);
1013 	if (error != 0)
1014 		return (error);
1015 
1016 	error = spa_vdev_setpath(spa, guid, path);
1017 	spa_close(spa, FTAG);
1018 	return (error);
1019 }
1020 
1021 static int
1022 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1023 {
1024 	objset_t *os = NULL;
1025 	int error;
1026 	nvlist_t *nv;
1027 
1028 retry:
1029 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1030 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1031 	if (error != 0) {
1032 		/*
1033 		 * This is ugly: dmu_objset_open() can return EBUSY if
1034 		 * the objset is held exclusively. Fortunately this hold is
1035 		 * only for a short while, so we retry here.
1036 		 * This avoids user code having to handle EBUSY,
1037 		 * for example for a "zfs list".
1038 		 */
1039 		if (error == EBUSY) {
1040 			delay(1);
1041 			goto retry;
1042 		}
1043 		return (error);
1044 	}
1045 
1046 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1047 
1048 	if (zc->zc_nvlist_dst != 0 &&
1049 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1050 		dmu_objset_stats(os, nv);
1051 		/*
1052 		 * NB: {zpl,zvol}_get_stats() will read the objset contents,
1053 		 * which we aren't supposed to do with a
1054 		 * DS_MODE_STANDARD open, because it could be
1055 		 * inconsistent.  So this is a bit of a workaround...
1056 		 */
1057 		if (!zc->zc_objset_stats.dds_inconsistent) {
1058 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1059 				VERIFY(zvol_get_stats(os, nv) == 0);
1060 			else if (dmu_objset_type(os) == DMU_OST_ZFS)
1061 				(void) zfs_get_stats(os, nv);
1062 		}
1063 		error = put_nvlist(zc, nv);
1064 		nvlist_free(nv);
1065 	}
1066 
1067 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
1068 
1069 	dmu_objset_close(os);
1070 	return (error);
1071 }
1072 
1073 static int
1074 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1075 {
1076 	objset_t *os;
1077 	int error;
1078 	char *p;
1079 
1080 retry:
1081 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1082 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1083 	if (error != 0) {
1084 		/*
1085 		 * This is ugly: dmu_objset_open() can return EBUSY if
1086 		 * the objset is held exclusively. Fortunately this hold is
1087 		 * only for a short while, so we retry here.
1088 		 * This avoids user code having to handle EBUSY,
1089 		 * for example for a "zfs list".
1090 		 */
1091 		if (error == EBUSY) {
1092 			delay(1);
1093 			goto retry;
1094 		}
1095 		if (error == ENOENT)
1096 			error = ESRCH;
1097 		return (error);
1098 	}
1099 
1100 	p = strrchr(zc->zc_name, '/');
1101 	if (p == NULL || p[1] != '\0')
1102 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1103 	p = zc->zc_name + strlen(zc->zc_name);
1104 
1105 	do {
1106 		error = dmu_dir_list_next(os,
1107 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1108 		    NULL, &zc->zc_cookie);
1109 		if (error == ENOENT)
1110 			error = ESRCH;
1111 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1112 	    !zone_dataset_visible(zc->zc_name, NULL));
1113 
1114 	/*
1115 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1116 	 * try to get stats for it.  Userland will skip over it.
1117 	 */
1118 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1119 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1120 
1121 	dmu_objset_close(os);
1122 	return (error);
1123 }
1124 
1125 static int
1126 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1127 {
1128 	objset_t *os;
1129 	int error;
1130 
1131 retry:
1132 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1133 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1134 	if (error != 0) {
1135 		/*
1136 		 * This is ugly: dmu_objset_open() can return EBUSY if
1137 		 * the objset is held exclusively. Fortunately this hold is
1138 		 * only for a short while, so we retry here.
1139 		 * This avoids user code having to handle EBUSY,
1140 		 * for example for a "zfs list".
1141 		 */
1142 		if (error == EBUSY) {
1143 			delay(1);
1144 			goto retry;
1145 		}
1146 		if (error == ENOENT)
1147 			error = ESRCH;
1148 		return (error);
1149 	}
1150 
1151 	/*
1152 	 * A dataset name of maximum length cannot have any snapshots,
1153 	 * so exit immediately.
1154 	 */
1155 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1156 		dmu_objset_close(os);
1157 		return (ESRCH);
1158 	}
1159 
1160 	error = dmu_snapshot_list_next(os,
1161 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1162 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
1163 	if (error == ENOENT)
1164 		error = ESRCH;
1165 
1166 	if (error == 0)
1167 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1168 
1169 	dmu_objset_close(os);
1170 	return (error);
1171 }
1172 
1173 static int
1174 zfs_set_prop_nvlist(const char *name, dev_t dev, cred_t *cr, nvlist_t *nvl)
1175 {
1176 	nvpair_t *elem;
1177 	int error;
1178 	const char *propname;
1179 	zfs_prop_t prop;
1180 	uint64_t intval;
1181 	char *strval;
1182 
1183 	/*
1184 	 * First validate permission to set all of the properties
1185 	 */
1186 	elem = NULL;
1187 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1188 		propname = nvpair_name(elem);
1189 
1190 		if ((prop = zfs_name_to_prop(propname)) ==
1191 		    ZFS_PROP_INVAL) {
1192 			/*
1193 			 * If this is a user-defined property, it must be a
1194 			 * string, and there is no further validation to do.
1195 			 */
1196 			if (!zfs_prop_user(propname) ||
1197 			    nvpair_type(elem) != DATA_TYPE_STRING)
1198 				return (EINVAL);
1199 
1200 			error = zfs_secpolicy_write_perms(name,
1201 			    ZFS_DELEG_PERM_USERPROP, cr);
1202 			if (error) {
1203 				return (EPERM);
1204 			}
1205 			continue;
1206 		}
1207 
1208 		/*
1209 		 * Check permissions for special properties
1210 		 */
1211 
1212 		switch (prop) {
1213 		case ZFS_PROP_ZONED:
1214 			/*
1215 			 * Disallow setting of 'zoned' from within a local zone.
1216 			 */
1217 			if (!INGLOBALZONE(curproc))
1218 				return (EPERM);
1219 			break;
1220 
1221 		case ZFS_PROP_QUOTA:
1222 			if (error = zfs_dozonecheck(name, cr))
1223 				return (error);
1224 
1225 			if (!INGLOBALZONE(curproc)) {
1226 				uint64_t zoned;
1227 				char setpoint[MAXNAMELEN];
1228 				int dslen;
1229 				/*
1230 				 * Unprivileged users are allowed to modify the
1231 				 * quota on things *under* (ie. contained by)
1232 				 * the thing they own.
1233 				 */
1234 				if (dsl_prop_get_integer(name, "zoned", &zoned,
1235 				    setpoint))
1236 					return (EPERM);
1237 				if (!zoned) /* this shouldn't happen */
1238 					return (EPERM);
1239 				dslen = strlen(name);
1240 				if (dslen <= strlen(setpoint))
1241 					return (EPERM);
1242 			}
1243 			break;
1244 
1245 		case ZFS_PROP_COMPRESSION:
1246 			/*
1247 			 * If the user specified gzip compression, make sure
1248 			 * the SPA supports it. We ignore any errors here since
1249 			 * we'll catch them later.
1250 			 */
1251 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1252 			    nvpair_value_uint64(elem, &intval) == 0 &&
1253 			    intval >= ZIO_COMPRESS_GZIP_1 &&
1254 			    intval <= ZIO_COMPRESS_GZIP_9) {
1255 				spa_t *spa;
1256 
1257 				if (spa_open(name, &spa, FTAG) == 0) {
1258 					if (spa_version(spa) <
1259 					    SPA_VERSION_GZIP_COMPRESSION) {
1260 						spa_close(spa, FTAG);
1261 						return (ENOTSUP);
1262 					}
1263 
1264 					spa_close(spa, FTAG);
1265 				}
1266 			}
1267 			break;
1268 
1269 		case ZFS_PROP_COPIES:
1270 		{
1271 			spa_t *spa;
1272 
1273 			if (spa_open(name, &spa, FTAG) == 0) {
1274 				if (spa_version(spa) <
1275 				    SPA_VERSION_DITTO_BLOCKS) {
1276 					spa_close(spa, FTAG);
1277 					return (ENOTSUP);
1278 				}
1279 				spa_close(spa, FTAG);
1280 			}
1281 			break;
1282 		}
1283 		}
1284 		if ((error = zfs_secpolicy_setprop(name, prop, cr)) != 0)
1285 			return (error);
1286 	}
1287 
1288 	elem = NULL;
1289 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1290 		propname = nvpair_name(elem);
1291 
1292 		if ((prop = zfs_name_to_prop(propname)) ==
1293 		    ZFS_PROP_INVAL) {
1294 
1295 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1296 			error = dsl_prop_set(name, propname, 1,
1297 			    strlen(strval) + 1, strval);
1298 			if (error == 0)
1299 				continue;
1300 			else
1301 				return (error);
1302 		}
1303 
1304 		switch (prop) {
1305 		case ZFS_PROP_QUOTA:
1306 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1307 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1308 				return (error);
1309 			break;
1310 
1311 		case ZFS_PROP_RESERVATION:
1312 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1313 			    (error = dsl_dir_set_reservation(name,
1314 			    intval)) != 0)
1315 				return (error);
1316 			break;
1317 
1318 		case ZFS_PROP_VOLSIZE:
1319 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1320 			    (error = zvol_set_volsize(name, dev, intval)) != 0)
1321 				return (error);
1322 			break;
1323 
1324 		case ZFS_PROP_VOLBLOCKSIZE:
1325 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1326 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1327 				return (error);
1328 			break;
1329 
1330 		case ZFS_PROP_VERSION:
1331 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1332 			    (error = zfs_set_version(name, intval)) != 0)
1333 				return (error);
1334 			break;
1335 
1336 		default:
1337 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1338 				if (zfs_prop_get_type(prop) !=
1339 				    prop_type_string)
1340 					return (EINVAL);
1341 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1342 				if ((error = dsl_prop_set(name,
1343 				    nvpair_name(elem), 1, strlen(strval) + 1,
1344 				    strval)) != 0)
1345 					return (error);
1346 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1347 				const char *unused;
1348 
1349 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1350 
1351 				switch (zfs_prop_get_type(prop)) {
1352 				case prop_type_number:
1353 					break;
1354 				case prop_type_boolean:
1355 					if (intval > 1)
1356 						return (EINVAL);
1357 					break;
1358 				case prop_type_string:
1359 					return (EINVAL);
1360 				case prop_type_index:
1361 					if (zfs_prop_index_to_string(prop,
1362 					    intval, &unused) != 0)
1363 						return (EINVAL);
1364 					break;
1365 				default:
1366 					cmn_err(CE_PANIC,
1367 					    "unknown property type");
1368 					break;
1369 				}
1370 
1371 				if ((error = dsl_prop_set(name, propname,
1372 				    8, 1, &intval)) != 0)
1373 					return (error);
1374 			} else {
1375 				return (EINVAL);
1376 			}
1377 			break;
1378 		}
1379 	}
1380 
1381 	return (0);
1382 }
1383 
1384 static int
1385 zfs_ioc_set_prop(zfs_cmd_t *zc)
1386 {
1387 	nvlist_t *nvl;
1388 	int error;
1389 	zfs_prop_t prop;
1390 
1391 	/*
1392 	 * If zc_value is set, then this is an attempt to inherit a value.
1393 	 * Otherwise, zc_nvlist refers to a list of properties to set.
1394 	 */
1395 	if (zc->zc_value[0] != '\0') {
1396 		if (!zfs_prop_user(zc->zc_value) &&
1397 		    ((prop = zfs_name_to_prop(zc->zc_value)) ==
1398 		    ZFS_PROP_INVAL ||
1399 		    !zfs_prop_inheritable(prop)))
1400 			return (EINVAL);
1401 
1402 		return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1403 	}
1404 
1405 	if ((error = get_nvlist(zc, &nvl)) != 0)
1406 		return (error);
1407 
1408 	error = zfs_set_prop_nvlist(zc->zc_name, zc->zc_dev,
1409 	    (cred_t *)(uintptr_t)zc->zc_cred, nvl);
1410 
1411 	nvlist_free(nvl);
1412 	return (error);
1413 }
1414 
1415 static int
1416 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1417 {
1418 	nvlist_t *nvl;
1419 	int error, reset_bootfs = 0;
1420 	uint64_t objnum;
1421 	uint64_t intval;
1422 	zpool_prop_t prop;
1423 	nvpair_t *elem;
1424 	char *propname, *strval;
1425 	spa_t *spa;
1426 	vdev_t *rvdev;
1427 	char *vdev_type;
1428 	objset_t *os;
1429 
1430 	if ((error = get_nvlist(zc, &nvl)) != 0)
1431 		return (error);
1432 
1433 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1434 		nvlist_free(nvl);
1435 		return (error);
1436 	}
1437 
1438 	if (spa_version(spa) < SPA_VERSION_BOOTFS) {
1439 		nvlist_free(nvl);
1440 		spa_close(spa, FTAG);
1441 		return (ENOTSUP);
1442 	}
1443 
1444 	elem = NULL;
1445 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1446 
1447 		propname = nvpair_name(elem);
1448 
1449 		if ((prop = zpool_name_to_prop(propname)) ==
1450 		    ZFS_PROP_INVAL) {
1451 			nvlist_free(nvl);
1452 			spa_close(spa, FTAG);
1453 			return (EINVAL);
1454 		}
1455 
1456 		switch (prop) {
1457 		case ZPOOL_PROP_DELEGATION:
1458 			VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1459 			if (intval > 1)
1460 				error = EINVAL;
1461 			break;
1462 		case ZPOOL_PROP_BOOTFS:
1463 			/*
1464 			 * A bootable filesystem can not be on a RAIDZ pool
1465 			 * nor a striped pool with more than 1 device.
1466 			 */
1467 			rvdev = spa->spa_root_vdev;
1468 			vdev_type =
1469 			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
1470 			if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
1471 			    (strcmp(vdev_type, VDEV_TYPE_MIRROR) != 0 &&
1472 			    rvdev->vdev_children > 1)) {
1473 				error = ENOTSUP;
1474 				break;
1475 			}
1476 
1477 			reset_bootfs = 1;
1478 
1479 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1480 			if (strval == NULL || strval[0] == '\0') {
1481 				objnum = zpool_prop_default_numeric(
1482 				    ZPOOL_PROP_BOOTFS);
1483 				break;
1484 			}
1485 
1486 			if (error = dmu_objset_open(strval, DMU_OST_ZFS,
1487 			    DS_MODE_STANDARD | DS_MODE_READONLY, &os))
1488 				break;
1489 			objnum = dmu_objset_id(os);
1490 			dmu_objset_close(os);
1491 			break;
1492 		}
1493 
1494 		if (error)
1495 			break;
1496 	}
1497 	if (error == 0) {
1498 		if (reset_bootfs) {
1499 			VERIFY(nvlist_remove(nvl,
1500 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1501 			    DATA_TYPE_STRING) == 0);
1502 			VERIFY(nvlist_add_uint64(nvl,
1503 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1504 			    objnum) == 0);
1505 		}
1506 		error = spa_set_props(spa, nvl);
1507 	}
1508 
1509 	nvlist_free(nvl);
1510 	spa_close(spa, FTAG);
1511 
1512 	return (error);
1513 }
1514 
1515 static int
1516 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1517 {
1518 	spa_t *spa;
1519 	int error;
1520 	nvlist_t *nvp = NULL;
1521 
1522 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1523 		return (error);
1524 
1525 	error = spa_get_props(spa, &nvp);
1526 
1527 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1528 		error = put_nvlist(zc, nvp);
1529 	else
1530 		error = EFAULT;
1531 
1532 	spa_close(spa, FTAG);
1533 
1534 	if (nvp)
1535 		nvlist_free(nvp);
1536 	return (error);
1537 }
1538 
1539 static int
1540 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1541 {
1542 	nvlist_t *nvp;
1543 	int error;
1544 	uint32_t uid;
1545 	uint32_t gid;
1546 	uint32_t *groups;
1547 	uint_t group_cnt;
1548 	cred_t	*usercred;
1549 
1550 	if ((error = get_nvlist(zc, &nvp)) != 0) {
1551 		return (error);
1552 	}
1553 
1554 	if ((error = nvlist_lookup_uint32(nvp,
1555 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1556 		nvlist_free(nvp);
1557 		return (EPERM);
1558 	}
1559 
1560 	if ((error = nvlist_lookup_uint32(nvp,
1561 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1562 		nvlist_free(nvp);
1563 		return (EPERM);
1564 	}
1565 
1566 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1567 	    &groups, &group_cnt)) != 0) {
1568 		nvlist_free(nvp);
1569 		return (EPERM);
1570 	}
1571 	usercred = cralloc();
1572 	if ((crsetugid(usercred, uid, gid) != 0) ||
1573 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1574 		nvlist_free(nvp);
1575 		crfree(usercred);
1576 		return (EPERM);
1577 	}
1578 	nvlist_free(nvp);
1579 	error = dsl_deleg_access(zc->zc_name,
1580 	    ZFS_DELEG_PERM_SHAREISCSI, usercred);
1581 	crfree(usercred);
1582 	return (error);
1583 }
1584 
1585 static int
1586 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1587 {
1588 	int error;
1589 	nvlist_t *fsaclnv = NULL;
1590 	cred_t *cr;
1591 
1592 	if ((error = get_nvlist(zc, &fsaclnv)) != 0)
1593 		return (error);
1594 
1595 	/*
1596 	 * Verify nvlist is constructed correctly
1597 	 */
1598 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1599 		nvlist_free(fsaclnv);
1600 		return (EINVAL);
1601 	}
1602 
1603 	/*
1604 	 * If we don't have PRIV_SYS_MOUNT, then validate
1605 	 * that user is allowed to hand out each permission in
1606 	 * the nvlist(s)
1607 	 */
1608 
1609 	cr = (cred_t *)(uintptr_t)zc->zc_cred;
1610 	error = secpolicy_zfs(cr);
1611 	if (error) {
1612 		if (zc->zc_perm_action == B_FALSE)
1613 			error = dsl_deleg_can_allow(zc->zc_name, fsaclnv, cr);
1614 		else
1615 			error = dsl_deleg_can_unallow(zc->zc_name, fsaclnv, cr);
1616 	}
1617 
1618 	if (error == 0)
1619 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1620 
1621 	nvlist_free(fsaclnv);
1622 	return (error);
1623 }
1624 
1625 static int
1626 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1627 {
1628 	nvlist_t *nvp;
1629 	int error;
1630 
1631 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1632 		error = put_nvlist(zc, nvp);
1633 		nvlist_free(nvp);
1634 	}
1635 
1636 	return (error);
1637 }
1638 
1639 static int
1640 zfs_ioc_create_minor(zfs_cmd_t *zc)
1641 {
1642 	return (zvol_create_minor(zc->zc_name, zc->zc_dev));
1643 }
1644 
1645 static int
1646 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1647 {
1648 	return (zvol_remove_minor(zc->zc_name));
1649 }
1650 
1651 /*
1652  * Search the vfs list for a specified resource.  Returns a pointer to it
1653  * or NULL if no suitable entry is found. The caller of this routine
1654  * is responsible for releasing the returned vfs pointer.
1655  */
1656 static vfs_t *
1657 zfs_get_vfs(const char *resource)
1658 {
1659 	struct vfs *vfsp;
1660 	struct vfs *vfs_found = NULL;
1661 
1662 	vfs_list_read_lock();
1663 	vfsp = rootvfs;
1664 	do {
1665 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1666 			VFS_HOLD(vfsp);
1667 			vfs_found = vfsp;
1668 			break;
1669 		}
1670 		vfsp = vfsp->vfs_next;
1671 	} while (vfsp != rootvfs);
1672 	vfs_list_unlock();
1673 	return (vfs_found);
1674 }
1675 
1676 /* ARGSUSED */
1677 static void
1678 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1679 {
1680 	nvlist_t *nvprops = arg;
1681 	uint64_t version = ZPL_VERSION;
1682 
1683 	(void) nvlist_lookup_uint64(nvprops,
1684 	    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
1685 
1686 	zfs_create_fs(os, cr, version, tx);
1687 }
1688 
1689 static int
1690 zfs_ioc_create(zfs_cmd_t *zc)
1691 {
1692 	objset_t *clone;
1693 	int error = 0;
1694 	nvlist_t *nvprops = NULL;
1695 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
1696 	dmu_objset_type_t type = zc->zc_objset_type;
1697 
1698 	switch (type) {
1699 
1700 	case DMU_OST_ZFS:
1701 		cbfunc = zfs_create_cb;
1702 		break;
1703 
1704 	case DMU_OST_ZVOL:
1705 		cbfunc = zvol_create_cb;
1706 		break;
1707 
1708 	default:
1709 		cbfunc = NULL;
1710 	}
1711 	if (strchr(zc->zc_name, '@'))
1712 		return (EINVAL);
1713 
1714 	if (zc->zc_nvlist_src != NULL &&
1715 	    (error = get_nvlist(zc, &nvprops)) != 0)
1716 		return (error);
1717 
1718 	if (zc->zc_value[0] != '\0') {
1719 		/*
1720 		 * We're creating a clone of an existing snapshot.
1721 		 */
1722 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1723 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1724 			nvlist_free(nvprops);
1725 			return (EINVAL);
1726 		}
1727 
1728 		error = dmu_objset_open(zc->zc_value, type,
1729 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1730 		if (error) {
1731 			nvlist_free(nvprops);
1732 			return (error);
1733 		}
1734 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1735 		dmu_objset_close(clone);
1736 	} else {
1737 		if (cbfunc == NULL) {
1738 			nvlist_free(nvprops);
1739 			return (EINVAL);
1740 		}
1741 
1742 		if (type == DMU_OST_ZVOL) {
1743 			uint64_t volsize, volblocksize;
1744 
1745 			if (nvprops == NULL ||
1746 			    nvlist_lookup_uint64(nvprops,
1747 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1748 			    &volsize) != 0) {
1749 				nvlist_free(nvprops);
1750 				return (EINVAL);
1751 			}
1752 
1753 			if ((error = nvlist_lookup_uint64(nvprops,
1754 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1755 			    &volblocksize)) != 0 && error != ENOENT) {
1756 				nvlist_free(nvprops);
1757 				return (EINVAL);
1758 			}
1759 
1760 			if (error != 0)
1761 				volblocksize = zfs_prop_default_numeric(
1762 				    ZFS_PROP_VOLBLOCKSIZE);
1763 
1764 			if ((error = zvol_check_volblocksize(
1765 			    volblocksize)) != 0 ||
1766 			    (error = zvol_check_volsize(volsize,
1767 			    volblocksize)) != 0) {
1768 				nvlist_free(nvprops);
1769 				return (error);
1770 			}
1771 		} else if (type == DMU_OST_ZFS) {
1772 			uint64_t version;
1773 
1774 			if (0 == nvlist_lookup_uint64(nvprops,
1775 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version) &&
1776 			    (version < ZPL_VERSION_INITIAL ||
1777 			    version > ZPL_VERSION)) {
1778 				nvlist_free(nvprops);
1779 				return (EINVAL);
1780 			}
1781 		}
1782 
1783 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
1784 		    nvprops);
1785 	}
1786 
1787 	/*
1788 	 * It would be nice to do this atomically.
1789 	 */
1790 	if (error == 0) {
1791 		if ((error = zfs_set_prop_nvlist(zc->zc_name,
1792 		    zc->zc_dev, (cred_t *)(uintptr_t)zc->zc_cred,
1793 		    nvprops)) != 0)
1794 			(void) dmu_objset_destroy(zc->zc_name);
1795 	}
1796 
1797 	nvlist_free(nvprops);
1798 	return (error);
1799 }
1800 
1801 static int
1802 zfs_ioc_snapshot(zfs_cmd_t *zc)
1803 {
1804 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1805 		return (EINVAL);
1806 	return (dmu_objset_snapshot(zc->zc_name,
1807 	    zc->zc_value, zc->zc_cookie));
1808 }
1809 
1810 int
1811 zfs_unmount_snap(char *name, void *arg)
1812 {
1813 	char *snapname = arg;
1814 	char *cp;
1815 	vfs_t *vfsp = NULL;
1816 
1817 	/*
1818 	 * Snapshots (which are under .zfs control) must be unmounted
1819 	 * before they can be destroyed.
1820 	 */
1821 
1822 	if (snapname) {
1823 		(void) strcat(name, "@");
1824 		(void) strcat(name, snapname);
1825 		vfsp = zfs_get_vfs(name);
1826 		cp = strchr(name, '@');
1827 		*cp = '\0';
1828 	} else if (strchr(name, '@')) {
1829 		vfsp = zfs_get_vfs(name);
1830 	}
1831 
1832 	if (vfsp) {
1833 		/*
1834 		 * Always force the unmount for snapshots.
1835 		 */
1836 		int flag = MS_FORCE;
1837 		int err;
1838 
1839 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
1840 			VFS_RELE(vfsp);
1841 			return (err);
1842 		}
1843 		VFS_RELE(vfsp);
1844 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
1845 			return (err);
1846 	}
1847 	return (0);
1848 }
1849 
1850 static int
1851 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
1852 {
1853 	int err;
1854 
1855 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1856 		return (EINVAL);
1857 	err = dmu_objset_find(zc->zc_name,
1858 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
1859 	if (err)
1860 		return (err);
1861 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
1862 }
1863 
1864 static int
1865 zfs_ioc_destroy(zfs_cmd_t *zc)
1866 {
1867 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
1868 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1869 		if (err)
1870 			return (err);
1871 	}
1872 
1873 	return (dmu_objset_destroy(zc->zc_name));
1874 }
1875 
1876 static int
1877 zfs_ioc_rollback(zfs_cmd_t *zc)
1878 {
1879 	return (dmu_objset_rollback(zc->zc_name));
1880 }
1881 
1882 static int
1883 zfs_ioc_rename(zfs_cmd_t *zc)
1884 {
1885 	boolean_t recursive = zc->zc_cookie & 1;
1886 
1887 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1888 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0)
1889 		return (EINVAL);
1890 
1891 	/*
1892 	 * Unmount snapshot unless we're doing a recursive rename,
1893 	 * in which case the dataset code figures out which snapshots
1894 	 * to unmount.
1895 	 */
1896 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
1897 	    zc->zc_objset_type == DMU_OST_ZFS) {
1898 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1899 		if (err)
1900 			return (err);
1901 	}
1902 
1903 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
1904 }
1905 
1906 static int
1907 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1908 {
1909 	file_t *fp;
1910 	int error, fd;
1911 	offset_t new_off;
1912 
1913 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
1914 	    strchr(zc->zc_value, '@') == NULL)
1915 		return (EINVAL);
1916 
1917 	fd = zc->zc_cookie;
1918 	fp = getf(fd);
1919 	if (fp == NULL)
1920 		return (EBADF);
1921 	error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record,
1922 	    &zc->zc_cookie, (boolean_t)zc->zc_guid, fp->f_vnode,
1923 	    fp->f_offset);
1924 
1925 	new_off = fp->f_offset + zc->zc_cookie;
1926 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &new_off) == 0)
1927 		fp->f_offset = new_off;
1928 
1929 	releasef(fd);
1930 	return (error);
1931 }
1932 
1933 static int
1934 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1935 {
1936 	objset_t *fromsnap = NULL;
1937 	objset_t *tosnap;
1938 	file_t *fp;
1939 	int error;
1940 
1941 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1942 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1943 	if (error)
1944 		return (error);
1945 
1946 	if (zc->zc_value[0] != '\0') {
1947 		char buf[MAXPATHLEN];
1948 		char *cp;
1949 
1950 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
1951 		cp = strchr(buf, '@');
1952 		if (cp)
1953 			*(cp+1) = 0;
1954 		(void) strncat(buf, zc->zc_value, sizeof (buf));
1955 		error = dmu_objset_open(buf, DMU_OST_ANY,
1956 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1957 		if (error) {
1958 			dmu_objset_close(tosnap);
1959 			return (error);
1960 		}
1961 	}
1962 
1963 	fp = getf(zc->zc_cookie);
1964 	if (fp == NULL) {
1965 		dmu_objset_close(tosnap);
1966 		if (fromsnap)
1967 			dmu_objset_close(fromsnap);
1968 		return (EBADF);
1969 	}
1970 
1971 	error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode);
1972 
1973 	releasef(zc->zc_cookie);
1974 	if (fromsnap)
1975 		dmu_objset_close(fromsnap);
1976 	dmu_objset_close(tosnap);
1977 	return (error);
1978 }
1979 
1980 static int
1981 zfs_ioc_inject_fault(zfs_cmd_t *zc)
1982 {
1983 	int id, error;
1984 
1985 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
1986 	    &zc->zc_inject_record);
1987 
1988 	if (error == 0)
1989 		zc->zc_guid = (uint64_t)id;
1990 
1991 	return (error);
1992 }
1993 
1994 static int
1995 zfs_ioc_clear_fault(zfs_cmd_t *zc)
1996 {
1997 	return (zio_clear_fault((int)zc->zc_guid));
1998 }
1999 
2000 static int
2001 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2002 {
2003 	int id = (int)zc->zc_guid;
2004 	int error;
2005 
2006 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2007 	    &zc->zc_inject_record);
2008 
2009 	zc->zc_guid = id;
2010 
2011 	return (error);
2012 }
2013 
2014 static int
2015 zfs_ioc_error_log(zfs_cmd_t *zc)
2016 {
2017 	spa_t *spa;
2018 	int error;
2019 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2020 
2021 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2022 		return (error);
2023 
2024 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2025 	    &count);
2026 	if (error == 0)
2027 		zc->zc_nvlist_dst_size = count;
2028 	else
2029 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2030 
2031 	spa_close(spa, FTAG);
2032 
2033 	return (error);
2034 }
2035 
2036 static int
2037 zfs_ioc_clear(zfs_cmd_t *zc)
2038 {
2039 	spa_t *spa;
2040 	vdev_t *vd;
2041 	int error;
2042 	uint64_t txg;
2043 
2044 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2045 		return (error);
2046 
2047 	txg = spa_vdev_enter(spa);
2048 
2049 	if (zc->zc_guid == 0) {
2050 		vd = NULL;
2051 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
2052 		(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
2053 		spa_close(spa, FTAG);
2054 		return (ENODEV);
2055 	}
2056 
2057 	vdev_clear(spa, vd);
2058 
2059 	(void) spa_vdev_exit(spa, NULL, txg, 0);
2060 
2061 	spa_close(spa, FTAG);
2062 
2063 	return (0);
2064 }
2065 
2066 static int
2067 zfs_ioc_promote(zfs_cmd_t *zc)
2068 {
2069 	char *cp;
2070 
2071 	/*
2072 	 * We don't need to unmount *all* the origin fs's snapshots, but
2073 	 * it's easier.
2074 	 */
2075 	cp = strchr(zc->zc_value, '@');
2076 	if (cp)
2077 		*cp = '\0';
2078 	(void) dmu_objset_find(zc->zc_value,
2079 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2080 	return (dsl_dataset_promote(zc->zc_name));
2081 }
2082 
2083 /*
2084  * We don't want to have a hard dependency
2085  * against some special symbols in sharefs
2086  * and nfs.  Determine them if needed when
2087  * the first file system is shared.
2088  * Neither sharefs or nfs are unloadable modules.
2089  */
2090 int (*zexport_fs)(void *arg);
2091 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2092 
2093 int zfs_share_inited;
2094 ddi_modhandle_t nfs_mod;
2095 ddi_modhandle_t sharefs_mod;
2096 kmutex_t zfs_share_lock;
2097 
2098 static int
2099 zfs_ioc_share(zfs_cmd_t *zc)
2100 {
2101 	int error;
2102 	int opcode;
2103 
2104 	if (zfs_share_inited == 0) {
2105 		mutex_enter(&zfs_share_lock);
2106 		nfs_mod = ddi_modopen("fs/nfs", KRTLD_MODE_FIRST, &error);
2107 		sharefs_mod = ddi_modopen("fs/sharefs",
2108 		    KRTLD_MODE_FIRST, &error);
2109 		if (nfs_mod == NULL || sharefs_mod == NULL) {
2110 			mutex_exit(&zfs_share_lock);
2111 			return (ENOSYS);
2112 		}
2113 		if (zexport_fs == NULL && ((zexport_fs = (int (*)(void *))
2114 		    ddi_modsym(nfs_mod, "nfs_export", &error)) == NULL)) {
2115 			mutex_exit(&zfs_share_lock);
2116 			return (ENOSYS);
2117 		}
2118 
2119 		if (zshare_fs == NULL && ((zshare_fs =
2120 		    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2121 		    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2122 			mutex_exit(&zfs_share_lock);
2123 			return (ENOSYS);
2124 		}
2125 		zfs_share_inited = 1;
2126 		mutex_exit(&zfs_share_lock);
2127 	}
2128 
2129 	if (error = zexport_fs((void *)(uintptr_t)zc->zc_share.z_exportdata))
2130 		return (error);
2131 
2132 	opcode = (zc->zc_share.z_sharetype == B_TRUE) ?
2133 	    SHAREFS_ADD : SHAREFS_REMOVE;
2134 
2135 	error = zshare_fs(opcode,
2136 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2137 	    zc->zc_share.z_sharemax);
2138 
2139 	return (error);
2140 
2141 }
2142 
2143 /*
2144  * pool destroy and pool export don't log the history as part of zfsdev_ioctl,
2145  * but rather zfs_ioc_pool_create, and zfs_ioc_pool_export do the loggin
2146  * of those commands.
2147  */
2148 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2149 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2150 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2151 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2152 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2153 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
2154 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2155 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2156 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2157 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2158 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
2159 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2160 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2161 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2162 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2163 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2164 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2165 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2166 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2167 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2168 	    DATASET_NAME, B_FALSE },
2169 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2170 	    DATASET_NAME, B_FALSE },
2171 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2172 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2173 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2174 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2175 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2176 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2177 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
2178 	{ zfs_ioc_recvbackup, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2179 	{ zfs_ioc_sendbackup, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2180 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
2181 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2182 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2183 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2184 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2185 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2186 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
2187 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2188 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2189 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2190 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2191 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2192 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2193 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2194 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2195 	    DATASET_NAME, B_FALSE },
2196 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE }
2197 };
2198 
2199 static int
2200 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2201 {
2202 	zfs_cmd_t *zc;
2203 	uint_t vec;
2204 	int error, rc;
2205 
2206 	if (getminor(dev) != 0)
2207 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2208 
2209 	vec = cmd - ZFS_IOC;
2210 
2211 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2212 		return (EINVAL);
2213 
2214 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2215 
2216 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2217 
2218 	if (error == 0) {
2219 		zc->zc_cred = (uintptr_t)cr;
2220 		zc->zc_dev = dev;
2221 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2222 	}
2223 
2224 	/*
2225 	 * Ensure that all pool/dataset names are valid before we pass down to
2226 	 * the lower layers.
2227 	 */
2228 	if (error == 0) {
2229 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2230 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
2231 		case POOL_NAME:
2232 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2233 				error = EINVAL;
2234 			break;
2235 
2236 		case DATASET_NAME:
2237 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2238 				error = EINVAL;
2239 			break;
2240 
2241 		case NO_NAME:
2242 			break;
2243 		}
2244 	}
2245 
2246 	if (error == 0)
2247 		error = zfs_ioc_vec[vec].zvec_func(zc);
2248 
2249 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
2250 	if (error == 0) {
2251 		error = rc;
2252 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
2253 			zfs_log_history(zc);
2254 	}
2255 
2256 	kmem_free(zc, sizeof (zfs_cmd_t));
2257 	return (error);
2258 }
2259 
2260 static int
2261 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2262 {
2263 	if (cmd != DDI_ATTACH)
2264 		return (DDI_FAILURE);
2265 
2266 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
2267 	    DDI_PSEUDO, 0) == DDI_FAILURE)
2268 		return (DDI_FAILURE);
2269 
2270 	zfs_dip = dip;
2271 
2272 	ddi_report_dev(dip);
2273 
2274 	return (DDI_SUCCESS);
2275 }
2276 
2277 static int
2278 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2279 {
2280 	if (spa_busy() || zfs_busy() || zvol_busy())
2281 		return (DDI_FAILURE);
2282 
2283 	if (cmd != DDI_DETACH)
2284 		return (DDI_FAILURE);
2285 
2286 	zfs_dip = NULL;
2287 
2288 	ddi_prop_remove_all(dip);
2289 	ddi_remove_minor_node(dip, NULL);
2290 
2291 	return (DDI_SUCCESS);
2292 }
2293 
2294 /*ARGSUSED*/
2295 static int
2296 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2297 {
2298 	switch (infocmd) {
2299 	case DDI_INFO_DEVT2DEVINFO:
2300 		*result = zfs_dip;
2301 		return (DDI_SUCCESS);
2302 
2303 	case DDI_INFO_DEVT2INSTANCE:
2304 		*result = (void *)0;
2305 		return (DDI_SUCCESS);
2306 	}
2307 
2308 	return (DDI_FAILURE);
2309 }
2310 
2311 /*
2312  * OK, so this is a little weird.
2313  *
2314  * /dev/zfs is the control node, i.e. minor 0.
2315  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2316  *
2317  * /dev/zfs has basically nothing to do except serve up ioctls,
2318  * so most of the standard driver entry points are in zvol.c.
2319  */
2320 static struct cb_ops zfs_cb_ops = {
2321 	zvol_open,	/* open */
2322 	zvol_close,	/* close */
2323 	zvol_strategy,	/* strategy */
2324 	nodev,		/* print */
2325 	nodev,		/* dump */
2326 	zvol_read,	/* read */
2327 	zvol_write,	/* write */
2328 	zfsdev_ioctl,	/* ioctl */
2329 	nodev,		/* devmap */
2330 	nodev,		/* mmap */
2331 	nodev,		/* segmap */
2332 	nochpoll,	/* poll */
2333 	ddi_prop_op,	/* prop_op */
2334 	NULL,		/* streamtab */
2335 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
2336 	CB_REV,		/* version */
2337 	nodev,		/* async read */
2338 	nodev,		/* async write */
2339 };
2340 
2341 static struct dev_ops zfs_dev_ops = {
2342 	DEVO_REV,	/* version */
2343 	0,		/* refcnt */
2344 	zfs_info,	/* info */
2345 	nulldev,	/* identify */
2346 	nulldev,	/* probe */
2347 	zfs_attach,	/* attach */
2348 	zfs_detach,	/* detach */
2349 	nodev,		/* reset */
2350 	&zfs_cb_ops,	/* driver operations */
2351 	NULL		/* no bus operations */
2352 };
2353 
2354 static struct modldrv zfs_modldrv = {
2355 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
2356 	    &zfs_dev_ops
2357 };
2358 
2359 static struct modlinkage modlinkage = {
2360 	MODREV_1,
2361 	(void *)&zfs_modlfs,
2362 	(void *)&zfs_modldrv,
2363 	NULL
2364 };
2365 
2366 int
2367 _init(void)
2368 {
2369 	int error;
2370 
2371 	spa_init(FREAD | FWRITE);
2372 	zfs_init();
2373 	zvol_init();
2374 
2375 	if ((error = mod_install(&modlinkage)) != 0) {
2376 		zvol_fini();
2377 		zfs_fini();
2378 		spa_fini();
2379 		return (error);
2380 	}
2381 
2382 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
2383 	ASSERT(error == 0);
2384 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
2385 
2386 	return (0);
2387 }
2388 
2389 int
2390 _fini(void)
2391 {
2392 	int error;
2393 
2394 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
2395 		return (EBUSY);
2396 
2397 	if ((error = mod_remove(&modlinkage)) != 0)
2398 		return (error);
2399 
2400 	zvol_fini();
2401 	zfs_fini();
2402 	spa_fini();
2403 	if (zfs_share_inited) {
2404 		(void) ddi_modclose(nfs_mod);
2405 		(void) ddi_modclose(sharefs_mod);
2406 	}
2407 
2408 	ldi_ident_release(zfs_li);
2409 	zfs_li = NULL;
2410 	mutex_destroy(&zfs_share_lock);
2411 
2412 	return (error);
2413 }
2414 
2415 int
2416 _info(struct modinfo *modinfop)
2417 {
2418 	return (mod_info(&modlinkage, modinfop));
2419 }
2420