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