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