xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision bd00f61bf62a14e7e8914ca126c52af5c38e94e1)
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 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
865 		spa_close(spa, FTAG);
866 		return (EINVAL);
867 	}
868 
869 	spa_upgrade(spa, zc->zc_cookie);
870 	spa_close(spa, FTAG);
871 
872 	return (error);
873 }
874 
875 static int
876 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
877 {
878 	spa_t *spa;
879 	char *hist_buf;
880 	uint64_t size;
881 	int error;
882 
883 	if ((size = zc->zc_history_len) == 0)
884 		return (EINVAL);
885 
886 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
887 		return (error);
888 
889 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
890 		spa_close(spa, FTAG);
891 		return (ENOTSUP);
892 	}
893 
894 	hist_buf = kmem_alloc(size, KM_SLEEP);
895 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
896 	    &zc->zc_history_len, hist_buf)) == 0) {
897 		error = xcopyout(hist_buf,
898 		    (char *)(uintptr_t)zc->zc_history,
899 		    zc->zc_history_len);
900 	}
901 
902 	spa_close(spa, FTAG);
903 	kmem_free(hist_buf, size);
904 	return (error);
905 }
906 
907 static int
908 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
909 {
910 	int error;
911 
912 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
913 		return (error);
914 
915 	return (0);
916 }
917 
918 static int
919 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
920 {
921 	objset_t *osp;
922 	int error;
923 
924 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
925 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
926 		return (error);
927 
928 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
929 	    sizeof (zc->zc_value));
930 	dmu_objset_close(osp);
931 
932 	return (error);
933 }
934 
935 static int
936 zfs_ioc_vdev_add(zfs_cmd_t *zc)
937 {
938 	spa_t *spa;
939 	int error;
940 	nvlist_t *config;
941 
942 	error = spa_open(zc->zc_name, &spa, FTAG);
943 	if (error != 0)
944 		return (error);
945 
946 	/*
947 	 * A root pool with concatenated devices is not supported.
948 	 * Thus, can not add a device to a root pool with one device.
949 	 */
950 	if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0) {
951 		spa_close(spa, FTAG);
952 		return (EDOM);
953 	}
954 
955 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
956 	    &config)) == 0) {
957 		error = spa_vdev_add(spa, config);
958 		nvlist_free(config);
959 	}
960 	spa_close(spa, FTAG);
961 	return (error);
962 }
963 
964 static int
965 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
966 {
967 	spa_t *spa;
968 	int error;
969 
970 	error = spa_open(zc->zc_name, &spa, FTAG);
971 	if (error != 0)
972 		return (error);
973 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
974 	spa_close(spa, FTAG);
975 	return (error);
976 }
977 
978 static int
979 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
980 {
981 	spa_t *spa;
982 	int error;
983 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
984 
985 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
986 		return (error);
987 	switch (zc->zc_cookie) {
988 	case VDEV_STATE_ONLINE:
989 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
990 		break;
991 
992 	case VDEV_STATE_OFFLINE:
993 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
994 		break;
995 
996 	case VDEV_STATE_FAULTED:
997 		error = vdev_fault(spa, zc->zc_guid);
998 		break;
999 
1000 	case VDEV_STATE_DEGRADED:
1001 		error = vdev_degrade(spa, zc->zc_guid);
1002 		break;
1003 
1004 	default:
1005 		error = EINVAL;
1006 	}
1007 	zc->zc_cookie = newstate;
1008 	spa_close(spa, FTAG);
1009 	return (error);
1010 }
1011 
1012 static int
1013 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1014 {
1015 	spa_t *spa;
1016 	int replacing = zc->zc_cookie;
1017 	nvlist_t *config;
1018 	int error;
1019 
1020 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1021 		return (error);
1022 
1023 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1024 	    &config)) == 0) {
1025 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1026 		nvlist_free(config);
1027 	}
1028 
1029 	spa_close(spa, FTAG);
1030 	return (error);
1031 }
1032 
1033 static int
1034 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1035 {
1036 	spa_t *spa;
1037 	int error;
1038 
1039 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1040 		return (error);
1041 
1042 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1043 
1044 	spa_close(spa, FTAG);
1045 	return (error);
1046 }
1047 
1048 static int
1049 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1050 {
1051 	spa_t *spa;
1052 	char *path = zc->zc_value;
1053 	uint64_t guid = zc->zc_guid;
1054 	int error;
1055 
1056 	error = spa_open(zc->zc_name, &spa, FTAG);
1057 	if (error != 0)
1058 		return (error);
1059 
1060 	error = spa_vdev_setpath(spa, guid, path);
1061 	spa_close(spa, FTAG);
1062 	return (error);
1063 }
1064 
1065 static int
1066 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1067 {
1068 	objset_t *os = NULL;
1069 	int error;
1070 	nvlist_t *nv;
1071 
1072 retry:
1073 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1074 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1075 	if (error != 0) {
1076 		/*
1077 		 * This is ugly: dmu_objset_open() can return EBUSY if
1078 		 * the objset is held exclusively. Fortunately this hold is
1079 		 * only for a short while, so we retry here.
1080 		 * This avoids user code having to handle EBUSY,
1081 		 * for example for a "zfs list".
1082 		 */
1083 		if (error == EBUSY) {
1084 			delay(1);
1085 			goto retry;
1086 		}
1087 		return (error);
1088 	}
1089 
1090 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1091 
1092 	if (zc->zc_nvlist_dst != 0 &&
1093 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1094 		dmu_objset_stats(os, nv);
1095 		/*
1096 		 * NB: zvol_get_stats() will read the objset contents,
1097 		 * which we aren't supposed to do with a
1098 		 * DS_MODE_STANDARD open, because it could be
1099 		 * inconsistent.  So this is a bit of a workaround...
1100 		 */
1101 		if (!zc->zc_objset_stats.dds_inconsistent) {
1102 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1103 				VERIFY(zvol_get_stats(os, nv) == 0);
1104 		}
1105 		error = put_nvlist(zc, nv);
1106 		nvlist_free(nv);
1107 	}
1108 
1109 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
1110 
1111 	dmu_objset_close(os);
1112 	return (error);
1113 }
1114 
1115 static int
1116 zfs_ioc_objset_version(zfs_cmd_t *zc)
1117 {
1118 	objset_t *os = NULL;
1119 	int error;
1120 
1121 retry:
1122 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1123 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1124 	if (error != 0) {
1125 		/*
1126 		 * This is ugly: dmu_objset_open() can return EBUSY if
1127 		 * the objset is held exclusively. Fortunately this hold is
1128 		 * only for a short while, so we retry here.
1129 		 * This avoids user code having to handle EBUSY,
1130 		 * for example for a "zfs list".
1131 		 */
1132 		if (error == EBUSY) {
1133 			delay(1);
1134 			goto retry;
1135 		}
1136 		return (error);
1137 	}
1138 
1139 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1140 
1141 	/*
1142 	 * NB: zfs_get_version() will read the objset contents,
1143 	 * which we aren't supposed to do with a
1144 	 * DS_MODE_STANDARD open, because it could be
1145 	 * inconsistent.  So this is a bit of a workaround...
1146 	 */
1147 	zc->zc_cookie = 0;
1148 	if (!zc->zc_objset_stats.dds_inconsistent)
1149 		if (dmu_objset_type(os) == DMU_OST_ZFS)
1150 			(void) zfs_get_version(os, &zc->zc_cookie);
1151 
1152 	dmu_objset_close(os);
1153 	return (0);
1154 }
1155 
1156 static int
1157 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1158 {
1159 	objset_t *os;
1160 	int error;
1161 	char *p;
1162 
1163 retry:
1164 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1165 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1166 	if (error != 0) {
1167 		/*
1168 		 * This is ugly: dmu_objset_open() can return EBUSY if
1169 		 * the objset is held exclusively. Fortunately this hold is
1170 		 * only for a short while, so we retry here.
1171 		 * This avoids user code having to handle EBUSY,
1172 		 * for example for a "zfs list".
1173 		 */
1174 		if (error == EBUSY) {
1175 			delay(1);
1176 			goto retry;
1177 		}
1178 		if (error == ENOENT)
1179 			error = ESRCH;
1180 		return (error);
1181 	}
1182 
1183 	p = strrchr(zc->zc_name, '/');
1184 	if (p == NULL || p[1] != '\0')
1185 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1186 	p = zc->zc_name + strlen(zc->zc_name);
1187 
1188 	do {
1189 		error = dmu_dir_list_next(os,
1190 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1191 		    NULL, &zc->zc_cookie);
1192 		if (error == ENOENT)
1193 			error = ESRCH;
1194 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1195 	    !zone_dataset_visible(zc->zc_name, NULL));
1196 
1197 	/*
1198 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1199 	 * try to get stats for it.  Userland will skip over it.
1200 	 */
1201 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1202 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1203 
1204 	dmu_objset_close(os);
1205 	return (error);
1206 }
1207 
1208 static int
1209 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1210 {
1211 	objset_t *os;
1212 	int error;
1213 
1214 retry:
1215 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1216 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1217 	if (error != 0) {
1218 		/*
1219 		 * This is ugly: dmu_objset_open() can return EBUSY if
1220 		 * the objset is held exclusively. Fortunately this hold is
1221 		 * only for a short while, so we retry here.
1222 		 * This avoids user code having to handle EBUSY,
1223 		 * for example for a "zfs list".
1224 		 */
1225 		if (error == EBUSY) {
1226 			delay(1);
1227 			goto retry;
1228 		}
1229 		if (error == ENOENT)
1230 			error = ESRCH;
1231 		return (error);
1232 	}
1233 
1234 	/*
1235 	 * A dataset name of maximum length cannot have any snapshots,
1236 	 * so exit immediately.
1237 	 */
1238 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1239 		dmu_objset_close(os);
1240 		return (ESRCH);
1241 	}
1242 
1243 	error = dmu_snapshot_list_next(os,
1244 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1245 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
1246 	if (error == ENOENT)
1247 		error = ESRCH;
1248 
1249 	if (error == 0)
1250 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1251 
1252 	dmu_objset_close(os);
1253 	return (error);
1254 }
1255 
1256 static int
1257 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1258 {
1259 	nvpair_t *elem;
1260 	int error;
1261 	uint64_t intval;
1262 	char *strval;
1263 
1264 	/*
1265 	 * First validate permission to set all of the properties
1266 	 */
1267 	elem = NULL;
1268 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1269 		const char *propname = nvpair_name(elem);
1270 		zfs_prop_t prop = zfs_name_to_prop(propname);
1271 
1272 		if (prop == ZPROP_INVAL) {
1273 			/*
1274 			 * If this is a user-defined property, it must be a
1275 			 * string, and there is no further validation to do.
1276 			 */
1277 			if (!zfs_prop_user(propname) ||
1278 			    nvpair_type(elem) != DATA_TYPE_STRING)
1279 				return (EINVAL);
1280 
1281 			error = zfs_secpolicy_write_perms(name,
1282 			    ZFS_DELEG_PERM_USERPROP, CRED());
1283 			if (error)
1284 				return (error);
1285 			continue;
1286 		}
1287 
1288 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1289 			return (error);
1290 
1291 		/*
1292 		 * Check that this value is valid for this pool version
1293 		 */
1294 		switch (prop) {
1295 		case ZFS_PROP_COMPRESSION:
1296 			/*
1297 			 * If the user specified gzip compression, make sure
1298 			 * the SPA supports it. We ignore any errors here since
1299 			 * we'll catch them later.
1300 			 */
1301 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1302 			    nvpair_value_uint64(elem, &intval) == 0 &&
1303 			    intval >= ZIO_COMPRESS_GZIP_1 &&
1304 			    intval <= ZIO_COMPRESS_GZIP_9) {
1305 				spa_t *spa;
1306 
1307 				if (spa_open(name, &spa, FTAG) == 0) {
1308 					if (spa_version(spa) <
1309 					    SPA_VERSION_GZIP_COMPRESSION) {
1310 						spa_close(spa, FTAG);
1311 						return (ENOTSUP);
1312 					}
1313 
1314 					spa_close(spa, FTAG);
1315 				}
1316 			}
1317 			break;
1318 
1319 		case ZFS_PROP_COPIES:
1320 		{
1321 			spa_t *spa;
1322 
1323 			if (spa_open(name, &spa, FTAG) == 0) {
1324 				if (spa_version(spa) <
1325 				    SPA_VERSION_DITTO_BLOCKS) {
1326 					spa_close(spa, FTAG);
1327 					return (ENOTSUP);
1328 				}
1329 				spa_close(spa, FTAG);
1330 			}
1331 			break;
1332 		}
1333 		}
1334 	}
1335 
1336 	elem = NULL;
1337 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1338 		const char *propname = nvpair_name(elem);
1339 		zfs_prop_t prop = zfs_name_to_prop(propname);
1340 
1341 		if (prop == ZPROP_INVAL) {
1342 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1343 			error = dsl_prop_set(name, propname, 1,
1344 			    strlen(strval) + 1, strval);
1345 			if (error == 0)
1346 				continue;
1347 			else
1348 				return (error);
1349 		}
1350 
1351 		switch (prop) {
1352 		case ZFS_PROP_QUOTA:
1353 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1354 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1355 				return (error);
1356 			break;
1357 
1358 		case ZFS_PROP_RESERVATION:
1359 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1360 			    (error = dsl_dir_set_reservation(name,
1361 			    intval)) != 0)
1362 				return (error);
1363 			break;
1364 
1365 		case ZFS_PROP_VOLSIZE:
1366 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1367 			    (error = zvol_set_volsize(name,
1368 			    ddi_driver_major(zfs_dip), intval)) != 0)
1369 				return (error);
1370 			break;
1371 
1372 		case ZFS_PROP_VOLBLOCKSIZE:
1373 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1374 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1375 				return (error);
1376 			break;
1377 
1378 		case ZFS_PROP_VERSION:
1379 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1380 			    (error = zfs_set_version(name, intval)) != 0)
1381 				return (error);
1382 			break;
1383 
1384 		default:
1385 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1386 				if (zfs_prop_get_type(prop) !=
1387 				    PROP_TYPE_STRING)
1388 					return (EINVAL);
1389 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1390 				if ((error = dsl_prop_set(name,
1391 				    nvpair_name(elem), 1, strlen(strval) + 1,
1392 				    strval)) != 0)
1393 					return (error);
1394 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1395 				const char *unused;
1396 
1397 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1398 
1399 				switch (zfs_prop_get_type(prop)) {
1400 				case PROP_TYPE_NUMBER:
1401 					break;
1402 				case PROP_TYPE_STRING:
1403 					return (EINVAL);
1404 				case PROP_TYPE_INDEX:
1405 					if (zfs_prop_index_to_string(prop,
1406 					    intval, &unused) != 0)
1407 						return (EINVAL);
1408 					break;
1409 				default:
1410 					cmn_err(CE_PANIC,
1411 					    "unknown property type");
1412 					break;
1413 				}
1414 
1415 				if ((error = dsl_prop_set(name, propname,
1416 				    8, 1, &intval)) != 0)
1417 					return (error);
1418 			} else {
1419 				return (EINVAL);
1420 			}
1421 			break;
1422 		}
1423 	}
1424 
1425 	return (0);
1426 }
1427 
1428 static int
1429 zfs_ioc_set_prop(zfs_cmd_t *zc)
1430 {
1431 	nvlist_t *nvl;
1432 	int error;
1433 
1434 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1435 	    &nvl)) != 0)
1436 		return (error);
1437 
1438 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1439 
1440 	nvlist_free(nvl);
1441 	return (error);
1442 }
1443 
1444 static int
1445 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1446 {
1447 	/* the property name has been validated by zfs_secpolicy_inherit() */
1448 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1449 }
1450 
1451 static int
1452 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1453 {
1454 	nvlist_t *props;
1455 	spa_t *spa;
1456 	int error;
1457 
1458 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1459 	    &props)))
1460 		return (error);
1461 
1462 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1463 		nvlist_free(props);
1464 		return (error);
1465 	}
1466 
1467 	error = spa_prop_set(spa, props);
1468 
1469 	nvlist_free(props);
1470 	spa_close(spa, FTAG);
1471 
1472 	return (error);
1473 }
1474 
1475 static int
1476 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1477 {
1478 	spa_t *spa;
1479 	int error;
1480 	nvlist_t *nvp = NULL;
1481 
1482 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1483 		return (error);
1484 
1485 	error = spa_prop_get(spa, &nvp);
1486 
1487 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1488 		error = put_nvlist(zc, nvp);
1489 	else
1490 		error = EFAULT;
1491 
1492 	spa_close(spa, FTAG);
1493 
1494 	if (nvp)
1495 		nvlist_free(nvp);
1496 	return (error);
1497 }
1498 
1499 static int
1500 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1501 {
1502 	nvlist_t *nvp;
1503 	int error;
1504 	uint32_t uid;
1505 	uint32_t gid;
1506 	uint32_t *groups;
1507 	uint_t group_cnt;
1508 	cred_t	*usercred;
1509 
1510 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1511 	    &nvp)) != 0) {
1512 		return (error);
1513 	}
1514 
1515 	if ((error = nvlist_lookup_uint32(nvp,
1516 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1517 		nvlist_free(nvp);
1518 		return (EPERM);
1519 	}
1520 
1521 	if ((error = nvlist_lookup_uint32(nvp,
1522 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1523 		nvlist_free(nvp);
1524 		return (EPERM);
1525 	}
1526 
1527 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1528 	    &groups, &group_cnt)) != 0) {
1529 		nvlist_free(nvp);
1530 		return (EPERM);
1531 	}
1532 	usercred = cralloc();
1533 	if ((crsetugid(usercred, uid, gid) != 0) ||
1534 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1535 		nvlist_free(nvp);
1536 		crfree(usercred);
1537 		return (EPERM);
1538 	}
1539 	nvlist_free(nvp);
1540 	error = dsl_deleg_access(zc->zc_name,
1541 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1542 	crfree(usercred);
1543 	return (error);
1544 }
1545 
1546 static int
1547 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1548 {
1549 	int error;
1550 	nvlist_t *fsaclnv = NULL;
1551 
1552 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1553 	    &fsaclnv)) != 0)
1554 		return (error);
1555 
1556 	/*
1557 	 * Verify nvlist is constructed correctly
1558 	 */
1559 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1560 		nvlist_free(fsaclnv);
1561 		return (EINVAL);
1562 	}
1563 
1564 	/*
1565 	 * If we don't have PRIV_SYS_MOUNT, then validate
1566 	 * that user is allowed to hand out each permission in
1567 	 * the nvlist(s)
1568 	 */
1569 
1570 	error = secpolicy_zfs(CRED());
1571 	if (error) {
1572 		if (zc->zc_perm_action == B_FALSE) {
1573 			error = dsl_deleg_can_allow(zc->zc_name,
1574 			    fsaclnv, CRED());
1575 		} else {
1576 			error = dsl_deleg_can_unallow(zc->zc_name,
1577 			    fsaclnv, CRED());
1578 		}
1579 	}
1580 
1581 	if (error == 0)
1582 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1583 
1584 	nvlist_free(fsaclnv);
1585 	return (error);
1586 }
1587 
1588 static int
1589 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1590 {
1591 	nvlist_t *nvp;
1592 	int error;
1593 
1594 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1595 		error = put_nvlist(zc, nvp);
1596 		nvlist_free(nvp);
1597 	}
1598 
1599 	return (error);
1600 }
1601 
1602 static int
1603 zfs_ioc_create_minor(zfs_cmd_t *zc)
1604 {
1605 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1606 }
1607 
1608 static int
1609 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1610 {
1611 	return (zvol_remove_minor(zc->zc_name));
1612 }
1613 
1614 /*
1615  * Search the vfs list for a specified resource.  Returns a pointer to it
1616  * or NULL if no suitable entry is found. The caller of this routine
1617  * is responsible for releasing the returned vfs pointer.
1618  */
1619 static vfs_t *
1620 zfs_get_vfs(const char *resource)
1621 {
1622 	struct vfs *vfsp;
1623 	struct vfs *vfs_found = NULL;
1624 
1625 	vfs_list_read_lock();
1626 	vfsp = rootvfs;
1627 	do {
1628 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1629 			VFS_HOLD(vfsp);
1630 			vfs_found = vfsp;
1631 			break;
1632 		}
1633 		vfsp = vfsp->vfs_next;
1634 	} while (vfsp != rootvfs);
1635 	vfs_list_unlock();
1636 	return (vfs_found);
1637 }
1638 
1639 /* ARGSUSED */
1640 static void
1641 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1642 {
1643 	nvlist_t *nvprops = arg;
1644 	uint64_t version = ZPL_VERSION;
1645 
1646 	(void) nvlist_lookup_uint64(nvprops,
1647 	    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
1648 
1649 	zfs_create_fs(os, cr, version, tx);
1650 }
1651 
1652 static int
1653 zfs_ioc_create(zfs_cmd_t *zc)
1654 {
1655 	objset_t *clone;
1656 	int error = 0;
1657 	nvlist_t *nvprops = NULL;
1658 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
1659 	dmu_objset_type_t type = zc->zc_objset_type;
1660 
1661 	switch (type) {
1662 
1663 	case DMU_OST_ZFS:
1664 		cbfunc = zfs_create_cb;
1665 		break;
1666 
1667 	case DMU_OST_ZVOL:
1668 		cbfunc = zvol_create_cb;
1669 		break;
1670 
1671 	default:
1672 		cbfunc = NULL;
1673 	}
1674 	if (strchr(zc->zc_name, '@'))
1675 		return (EINVAL);
1676 
1677 	if (zc->zc_nvlist_src != NULL &&
1678 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1679 	    &nvprops)) != 0)
1680 		return (error);
1681 
1682 	if (zc->zc_value[0] != '\0') {
1683 		/*
1684 		 * We're creating a clone of an existing snapshot.
1685 		 */
1686 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1687 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1688 			nvlist_free(nvprops);
1689 			return (EINVAL);
1690 		}
1691 
1692 		error = dmu_objset_open(zc->zc_value, type,
1693 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1694 		if (error) {
1695 			nvlist_free(nvprops);
1696 			return (error);
1697 		}
1698 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1699 		dmu_objset_close(clone);
1700 	} else {
1701 		if (cbfunc == NULL) {
1702 			nvlist_free(nvprops);
1703 			return (EINVAL);
1704 		}
1705 
1706 		if (type == DMU_OST_ZVOL) {
1707 			uint64_t volsize, volblocksize;
1708 
1709 			if (nvprops == NULL ||
1710 			    nvlist_lookup_uint64(nvprops,
1711 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1712 			    &volsize) != 0) {
1713 				nvlist_free(nvprops);
1714 				return (EINVAL);
1715 			}
1716 
1717 			if ((error = nvlist_lookup_uint64(nvprops,
1718 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1719 			    &volblocksize)) != 0 && error != ENOENT) {
1720 				nvlist_free(nvprops);
1721 				return (EINVAL);
1722 			}
1723 
1724 			if (error != 0)
1725 				volblocksize = zfs_prop_default_numeric(
1726 				    ZFS_PROP_VOLBLOCKSIZE);
1727 
1728 			if ((error = zvol_check_volblocksize(
1729 			    volblocksize)) != 0 ||
1730 			    (error = zvol_check_volsize(volsize,
1731 			    volblocksize)) != 0) {
1732 				nvlist_free(nvprops);
1733 				return (error);
1734 			}
1735 		} else if (type == DMU_OST_ZFS) {
1736 			uint64_t version;
1737 
1738 			if (0 == nvlist_lookup_uint64(nvprops,
1739 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version) &&
1740 			    (version < ZPL_VERSION_INITIAL ||
1741 			    version > ZPL_VERSION)) {
1742 				nvlist_free(nvprops);
1743 				return (EINVAL);
1744 			}
1745 		}
1746 
1747 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
1748 		    nvprops);
1749 	}
1750 
1751 	/*
1752 	 * It would be nice to do this atomically.
1753 	 */
1754 	if (error == 0) {
1755 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
1756 			(void) dmu_objset_destroy(zc->zc_name);
1757 	}
1758 
1759 	nvlist_free(nvprops);
1760 	return (error);
1761 }
1762 
1763 static int
1764 zfs_ioc_snapshot(zfs_cmd_t *zc)
1765 {
1766 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1767 		return (EINVAL);
1768 	return (dmu_objset_snapshot(zc->zc_name,
1769 	    zc->zc_value, zc->zc_cookie));
1770 }
1771 
1772 int
1773 zfs_unmount_snap(char *name, void *arg)
1774 {
1775 	char *snapname = arg;
1776 	char *cp;
1777 	vfs_t *vfsp = NULL;
1778 
1779 	/*
1780 	 * Snapshots (which are under .zfs control) must be unmounted
1781 	 * before they can be destroyed.
1782 	 */
1783 
1784 	if (snapname) {
1785 		(void) strcat(name, "@");
1786 		(void) strcat(name, snapname);
1787 		vfsp = zfs_get_vfs(name);
1788 		cp = strchr(name, '@');
1789 		*cp = '\0';
1790 	} else if (strchr(name, '@')) {
1791 		vfsp = zfs_get_vfs(name);
1792 	}
1793 
1794 	if (vfsp) {
1795 		/*
1796 		 * Always force the unmount for snapshots.
1797 		 */
1798 		int flag = MS_FORCE;
1799 		int err;
1800 
1801 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
1802 			VFS_RELE(vfsp);
1803 			return (err);
1804 		}
1805 		VFS_RELE(vfsp);
1806 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
1807 			return (err);
1808 	}
1809 	return (0);
1810 }
1811 
1812 static int
1813 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
1814 {
1815 	int err;
1816 
1817 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1818 		return (EINVAL);
1819 	err = dmu_objset_find(zc->zc_name,
1820 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
1821 	if (err)
1822 		return (err);
1823 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
1824 }
1825 
1826 static int
1827 zfs_ioc_destroy(zfs_cmd_t *zc)
1828 {
1829 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
1830 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1831 		if (err)
1832 			return (err);
1833 	}
1834 
1835 	return (dmu_objset_destroy(zc->zc_name));
1836 }
1837 
1838 static int
1839 zfs_ioc_rollback(zfs_cmd_t *zc)
1840 {
1841 	return (dmu_objset_rollback(zc->zc_name));
1842 }
1843 
1844 static int
1845 zfs_ioc_rename(zfs_cmd_t *zc)
1846 {
1847 	boolean_t recursive = zc->zc_cookie & 1;
1848 
1849 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1850 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0)
1851 		return (EINVAL);
1852 
1853 	/*
1854 	 * Unmount snapshot unless we're doing a recursive rename,
1855 	 * in which case the dataset code figures out which snapshots
1856 	 * to unmount.
1857 	 */
1858 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
1859 	    zc->zc_objset_type == DMU_OST_ZFS) {
1860 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1861 		if (err)
1862 			return (err);
1863 	}
1864 
1865 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
1866 }
1867 
1868 static int
1869 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1870 {
1871 	file_t *fp;
1872 	int error, fd;
1873 	offset_t new_off;
1874 
1875 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
1876 	    strchr(zc->zc_value, '@') == NULL)
1877 		return (EINVAL);
1878 
1879 	fd = zc->zc_cookie;
1880 	fp = getf(fd);
1881 	if (fp == NULL)
1882 		return (EBADF);
1883 	error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record,
1884 	    &zc->zc_cookie, (boolean_t)zc->zc_guid, fp->f_vnode,
1885 	    fp->f_offset);
1886 
1887 	new_off = fp->f_offset + zc->zc_cookie;
1888 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &new_off) == 0)
1889 		fp->f_offset = new_off;
1890 
1891 	releasef(fd);
1892 	return (error);
1893 }
1894 
1895 static int
1896 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1897 {
1898 	objset_t *fromsnap = NULL;
1899 	objset_t *tosnap;
1900 	file_t *fp;
1901 	int error;
1902 
1903 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1904 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1905 	if (error)
1906 		return (error);
1907 
1908 	if (zc->zc_value[0] != '\0') {
1909 		char buf[MAXPATHLEN];
1910 		char *cp;
1911 
1912 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
1913 		cp = strchr(buf, '@');
1914 		if (cp)
1915 			*(cp+1) = 0;
1916 		(void) strncat(buf, zc->zc_value, sizeof (buf));
1917 		error = dmu_objset_open(buf, DMU_OST_ANY,
1918 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1919 		if (error) {
1920 			dmu_objset_close(tosnap);
1921 			return (error);
1922 		}
1923 	}
1924 
1925 	fp = getf(zc->zc_cookie);
1926 	if (fp == NULL) {
1927 		dmu_objset_close(tosnap);
1928 		if (fromsnap)
1929 			dmu_objset_close(fromsnap);
1930 		return (EBADF);
1931 	}
1932 
1933 	error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode);
1934 
1935 	releasef(zc->zc_cookie);
1936 	if (fromsnap)
1937 		dmu_objset_close(fromsnap);
1938 	dmu_objset_close(tosnap);
1939 	return (error);
1940 }
1941 
1942 static int
1943 zfs_ioc_inject_fault(zfs_cmd_t *zc)
1944 {
1945 	int id, error;
1946 
1947 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
1948 	    &zc->zc_inject_record);
1949 
1950 	if (error == 0)
1951 		zc->zc_guid = (uint64_t)id;
1952 
1953 	return (error);
1954 }
1955 
1956 static int
1957 zfs_ioc_clear_fault(zfs_cmd_t *zc)
1958 {
1959 	return (zio_clear_fault((int)zc->zc_guid));
1960 }
1961 
1962 static int
1963 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
1964 {
1965 	int id = (int)zc->zc_guid;
1966 	int error;
1967 
1968 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
1969 	    &zc->zc_inject_record);
1970 
1971 	zc->zc_guid = id;
1972 
1973 	return (error);
1974 }
1975 
1976 static int
1977 zfs_ioc_error_log(zfs_cmd_t *zc)
1978 {
1979 	spa_t *spa;
1980 	int error;
1981 	size_t count = (size_t)zc->zc_nvlist_dst_size;
1982 
1983 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1984 		return (error);
1985 
1986 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
1987 	    &count);
1988 	if (error == 0)
1989 		zc->zc_nvlist_dst_size = count;
1990 	else
1991 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
1992 
1993 	spa_close(spa, FTAG);
1994 
1995 	return (error);
1996 }
1997 
1998 static int
1999 zfs_ioc_clear(zfs_cmd_t *zc)
2000 {
2001 	spa_t *spa;
2002 	vdev_t *vd;
2003 	uint64_t txg;
2004 	int error;
2005 
2006 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2007 		return (error);
2008 
2009 	txg = spa_vdev_enter(spa);
2010 
2011 	if (zc->zc_guid == 0) {
2012 		vd = NULL;
2013 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
2014 		(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
2015 		spa_close(spa, FTAG);
2016 		return (ENODEV);
2017 	}
2018 
2019 	vdev_clear(spa, vd);
2020 
2021 	(void) spa_vdev_exit(spa, NULL, txg, 0);
2022 
2023 	spa_close(spa, FTAG);
2024 
2025 	return (0);
2026 }
2027 
2028 static int
2029 zfs_ioc_promote(zfs_cmd_t *zc)
2030 {
2031 	char *cp;
2032 
2033 	/*
2034 	 * We don't need to unmount *all* the origin fs's snapshots, but
2035 	 * it's easier.
2036 	 */
2037 	cp = strchr(zc->zc_value, '@');
2038 	if (cp)
2039 		*cp = '\0';
2040 	(void) dmu_objset_find(zc->zc_value,
2041 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2042 	return (dsl_dataset_promote(zc->zc_name));
2043 }
2044 
2045 /*
2046  * We don't want to have a hard dependency
2047  * against some special symbols in sharefs
2048  * and nfs.  Determine them if needed when
2049  * the first file system is shared.
2050  * Neither sharefs or nfs are unloadable modules.
2051  */
2052 int (*zexport_fs)(void *arg);
2053 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2054 
2055 int zfs_share_inited;
2056 ddi_modhandle_t nfs_mod;
2057 ddi_modhandle_t sharefs_mod;
2058 kmutex_t zfs_share_lock;
2059 
2060 static int
2061 zfs_ioc_share(zfs_cmd_t *zc)
2062 {
2063 	int error;
2064 	int opcode;
2065 
2066 	if (zfs_share_inited == 0) {
2067 		mutex_enter(&zfs_share_lock);
2068 		nfs_mod = ddi_modopen("fs/nfs", KRTLD_MODE_FIRST, &error);
2069 		sharefs_mod = ddi_modopen("fs/sharefs",
2070 		    KRTLD_MODE_FIRST, &error);
2071 		if (nfs_mod == NULL || sharefs_mod == NULL) {
2072 			mutex_exit(&zfs_share_lock);
2073 			return (ENOSYS);
2074 		}
2075 		if (zexport_fs == NULL && ((zexport_fs = (int (*)(void *))
2076 		    ddi_modsym(nfs_mod, "nfs_export", &error)) == NULL)) {
2077 			mutex_exit(&zfs_share_lock);
2078 			return (ENOSYS);
2079 		}
2080 
2081 		if (zshare_fs == NULL && ((zshare_fs =
2082 		    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2083 		    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2084 			mutex_exit(&zfs_share_lock);
2085 			return (ENOSYS);
2086 		}
2087 		zfs_share_inited = 1;
2088 		mutex_exit(&zfs_share_lock);
2089 	}
2090 
2091 	if (error = zexport_fs((void *)(uintptr_t)zc->zc_share.z_exportdata))
2092 		return (error);
2093 
2094 	opcode = (zc->zc_share.z_sharetype == B_TRUE) ?
2095 	    SHAREFS_ADD : SHAREFS_REMOVE;
2096 
2097 	error = zshare_fs(opcode,
2098 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2099 	    zc->zc_share.z_sharemax);
2100 
2101 	return (error);
2102 
2103 }
2104 
2105 /*
2106  * pool create, destroy, and export don't log the history as part of
2107  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2108  * do the logging of those commands.
2109  */
2110 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2111 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2112 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2113 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2114 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2115 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
2116 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2117 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2118 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2119 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2120 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
2121 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2122 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2123 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2124 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2125 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2126 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2127 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2128 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2129 	{ zfs_ioc_objset_version, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2130 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2131 	    DATASET_NAME, B_FALSE },
2132 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2133 	    DATASET_NAME, B_FALSE },
2134 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2135 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2136 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2137 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2138 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2139 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2140 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
2141 	{ zfs_ioc_recvbackup, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2142 	{ zfs_ioc_sendbackup, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2143 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
2144 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2145 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2146 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2147 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2148 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2149 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
2150 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2151 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2152 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2153 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2154 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2155 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2156 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2157 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2158 	    DATASET_NAME, B_FALSE },
2159 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2160 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2161 };
2162 
2163 static int
2164 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2165 {
2166 	zfs_cmd_t *zc;
2167 	uint_t vec;
2168 	int error, rc;
2169 
2170 	if (getminor(dev) != 0)
2171 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2172 
2173 	vec = cmd - ZFS_IOC;
2174 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
2175 
2176 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2177 		return (EINVAL);
2178 
2179 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2180 
2181 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2182 
2183 	if (error == 0)
2184 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2185 
2186 	/*
2187 	 * Ensure that all pool/dataset names are valid before we pass down to
2188 	 * the lower layers.
2189 	 */
2190 	if (error == 0) {
2191 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2192 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
2193 		case POOL_NAME:
2194 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2195 				error = EINVAL;
2196 			break;
2197 
2198 		case DATASET_NAME:
2199 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2200 				error = EINVAL;
2201 			break;
2202 
2203 		case NO_NAME:
2204 			break;
2205 		}
2206 	}
2207 
2208 	if (error == 0)
2209 		error = zfs_ioc_vec[vec].zvec_func(zc);
2210 
2211 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
2212 	if (error == 0) {
2213 		error = rc;
2214 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
2215 			zfs_log_history(zc);
2216 	}
2217 
2218 	kmem_free(zc, sizeof (zfs_cmd_t));
2219 	return (error);
2220 }
2221 
2222 static int
2223 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2224 {
2225 	if (cmd != DDI_ATTACH)
2226 		return (DDI_FAILURE);
2227 
2228 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
2229 	    DDI_PSEUDO, 0) == DDI_FAILURE)
2230 		return (DDI_FAILURE);
2231 
2232 	zfs_dip = dip;
2233 
2234 	ddi_report_dev(dip);
2235 
2236 	return (DDI_SUCCESS);
2237 }
2238 
2239 static int
2240 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2241 {
2242 	if (spa_busy() || zfs_busy() || zvol_busy())
2243 		return (DDI_FAILURE);
2244 
2245 	if (cmd != DDI_DETACH)
2246 		return (DDI_FAILURE);
2247 
2248 	zfs_dip = NULL;
2249 
2250 	ddi_prop_remove_all(dip);
2251 	ddi_remove_minor_node(dip, NULL);
2252 
2253 	return (DDI_SUCCESS);
2254 }
2255 
2256 /*ARGSUSED*/
2257 static int
2258 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2259 {
2260 	switch (infocmd) {
2261 	case DDI_INFO_DEVT2DEVINFO:
2262 		*result = zfs_dip;
2263 		return (DDI_SUCCESS);
2264 
2265 	case DDI_INFO_DEVT2INSTANCE:
2266 		*result = (void *)0;
2267 		return (DDI_SUCCESS);
2268 	}
2269 
2270 	return (DDI_FAILURE);
2271 }
2272 
2273 /*
2274  * OK, so this is a little weird.
2275  *
2276  * /dev/zfs is the control node, i.e. minor 0.
2277  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2278  *
2279  * /dev/zfs has basically nothing to do except serve up ioctls,
2280  * so most of the standard driver entry points are in zvol.c.
2281  */
2282 static struct cb_ops zfs_cb_ops = {
2283 	zvol_open,	/* open */
2284 	zvol_close,	/* close */
2285 	zvol_strategy,	/* strategy */
2286 	nodev,		/* print */
2287 	nodev,		/* dump */
2288 	zvol_read,	/* read */
2289 	zvol_write,	/* write */
2290 	zfsdev_ioctl,	/* ioctl */
2291 	nodev,		/* devmap */
2292 	nodev,		/* mmap */
2293 	nodev,		/* segmap */
2294 	nochpoll,	/* poll */
2295 	ddi_prop_op,	/* prop_op */
2296 	NULL,		/* streamtab */
2297 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
2298 	CB_REV,		/* version */
2299 	nodev,		/* async read */
2300 	nodev,		/* async write */
2301 };
2302 
2303 static struct dev_ops zfs_dev_ops = {
2304 	DEVO_REV,	/* version */
2305 	0,		/* refcnt */
2306 	zfs_info,	/* info */
2307 	nulldev,	/* identify */
2308 	nulldev,	/* probe */
2309 	zfs_attach,	/* attach */
2310 	zfs_detach,	/* detach */
2311 	nodev,		/* reset */
2312 	&zfs_cb_ops,	/* driver operations */
2313 	NULL		/* no bus operations */
2314 };
2315 
2316 static struct modldrv zfs_modldrv = {
2317 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
2318 	    &zfs_dev_ops
2319 };
2320 
2321 static struct modlinkage modlinkage = {
2322 	MODREV_1,
2323 	(void *)&zfs_modlfs,
2324 	(void *)&zfs_modldrv,
2325 	NULL
2326 };
2327 
2328 
2329 uint_t zfs_fsyncer_key;
2330 
2331 int
2332 _init(void)
2333 {
2334 	int error;
2335 
2336 	spa_init(FREAD | FWRITE);
2337 	zfs_init();
2338 	zvol_init();
2339 
2340 	if ((error = mod_install(&modlinkage)) != 0) {
2341 		zvol_fini();
2342 		zfs_fini();
2343 		spa_fini();
2344 		return (error);
2345 	}
2346 
2347 	tsd_create(&zfs_fsyncer_key, NULL);
2348 
2349 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
2350 	ASSERT(error == 0);
2351 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
2352 
2353 	return (0);
2354 }
2355 
2356 int
2357 _fini(void)
2358 {
2359 	int error;
2360 
2361 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
2362 		return (EBUSY);
2363 
2364 	if ((error = mod_remove(&modlinkage)) != 0)
2365 		return (error);
2366 
2367 	zvol_fini();
2368 	zfs_fini();
2369 	spa_fini();
2370 	if (zfs_share_inited) {
2371 		(void) ddi_modclose(nfs_mod);
2372 		(void) ddi_modclose(sharefs_mod);
2373 	}
2374 
2375 	tsd_destroy(&zfs_fsyncer_key);
2376 	ldi_ident_release(zfs_li);
2377 	zfs_li = NULL;
2378 	mutex_destroy(&zfs_share_lock);
2379 
2380 	return (error);
2381 }
2382 
2383 int
2384 _info(struct modinfo *modinfop)
2385 {
2386 	return (mod_info(&modlinkage, modinfop));
2387 }
2388