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