xref: /titanic_41/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 8ec6c6f1a5917f5001e9b7ca20eed09b22e22c35)
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 2008 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/zfs_znode.h>
42 #include <sys/zap.h>
43 #include <sys/spa.h>
44 #include <sys/spa_impl.h>
45 #include <sys/vdev.h>
46 #include <sys/vdev_impl.h>
47 #include <sys/dmu.h>
48 #include <sys/dsl_dir.h>
49 #include <sys/dsl_dataset.h>
50 #include <sys/dsl_prop.h>
51 #include <sys/dsl_deleg.h>
52 #include <sys/dmu_objset.h>
53 #include <sys/ddi.h>
54 #include <sys/sunddi.h>
55 #include <sys/sunldi.h>
56 #include <sys/policy.h>
57 #include <sys/zone.h>
58 #include <sys/nvpair.h>
59 #include <sys/pathname.h>
60 #include <sys/mount.h>
61 #include <sys/sdt.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/zfs_ctldir.h>
64 #include <sys/zfs_dir.h>
65 #include <sys/zvol.h>
66 #include <sharefs/share.h>
67 #include <sys/dmu_objset.h>
68 
69 #include "zfs_namecheck.h"
70 #include "zfs_prop.h"
71 #include "zfs_deleg.h"
72 
73 extern struct modlfs zfs_modlfs;
74 
75 extern void zfs_init(void);
76 extern void zfs_fini(void);
77 
78 ldi_ident_t zfs_li = NULL;
79 dev_info_t *zfs_dip;
80 
81 typedef int zfs_ioc_func_t(zfs_cmd_t *);
82 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
83 
84 typedef struct zfs_ioc_vec {
85 	zfs_ioc_func_t		*zvec_func;
86 	zfs_secpolicy_func_t	*zvec_secpolicy;
87 	enum {
88 		NO_NAME,
89 		POOL_NAME,
90 		DATASET_NAME
91 	} zvec_namecheck;
92 	boolean_t		zvec_his_log;
93 } zfs_ioc_vec_t;
94 
95 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
96 void
97 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
98 {
99 	const char *newfile;
100 	char buf[256];
101 	va_list adx;
102 
103 	/*
104 	 * Get rid of annoying "../common/" prefix to filename.
105 	 */
106 	newfile = strrchr(file, '/');
107 	if (newfile != NULL) {
108 		newfile = newfile + 1; /* Get rid of leading / */
109 	} else {
110 		newfile = file;
111 	}
112 
113 	va_start(adx, fmt);
114 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
115 	va_end(adx);
116 
117 	/*
118 	 * To get this data, use the zfs-dprintf probe as so:
119 	 * dtrace -q -n 'zfs-dprintf \
120 	 *	/stringof(arg0) == "dbuf.c"/ \
121 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
122 	 * arg0 = file name
123 	 * arg1 = function name
124 	 * arg2 = line number
125 	 * arg3 = message
126 	 */
127 	DTRACE_PROBE4(zfs__dprintf,
128 	    char *, newfile, char *, func, int, line, char *, buf);
129 }
130 
131 static void
132 history_str_free(char *buf)
133 {
134 	kmem_free(buf, HIS_MAX_RECORD_LEN);
135 }
136 
137 static char *
138 history_str_get(zfs_cmd_t *zc)
139 {
140 	char *buf;
141 
142 	if (zc->zc_history == NULL)
143 		return (NULL);
144 
145 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
146 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
147 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
148 		history_str_free(buf);
149 		return (NULL);
150 	}
151 
152 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
153 
154 	return (buf);
155 }
156 
157 /*
158  * zfs_check_version
159  *
160  *	Return non-zero if the spa version is less than requested version.
161  */
162 static int
163 zfs_check_version(const char *name, int version)
164 {
165 
166 	spa_t *spa;
167 
168 	if (spa_open(name, &spa, FTAG) == 0) {
169 		if (spa_version(spa) < version) {
170 			spa_close(spa, FTAG);
171 			return (1);
172 		}
173 		spa_close(spa, FTAG);
174 	}
175 	return (0);
176 }
177 
178 /*
179  * zpl_check_version
180  *
181  * Return non-zero if the ZPL version is less than requested version.
182  */
183 static int
184 zpl_check_version(const char *name, int version)
185 {
186 	objset_t *os;
187 	int rc = 1;
188 
189 	if (dmu_objset_open(name, DMU_OST_ANY,
190 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
191 		uint64_t propversion;
192 
193 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION,
194 		    &propversion) == 0) {
195 			rc = !(propversion >= version);
196 		}
197 		dmu_objset_close(os);
198 	}
199 	return (rc);
200 }
201 
202 static void
203 zfs_log_history(zfs_cmd_t *zc)
204 {
205 	spa_t *spa;
206 	char *buf;
207 
208 	if ((buf = history_str_get(zc)) == NULL)
209 		return;
210 
211 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
212 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
213 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
214 		spa_close(spa, FTAG);
215 	}
216 	history_str_free(buf);
217 }
218 
219 /*
220  * Policy for top-level read operations (list pools).  Requires no privileges,
221  * and can be used in the local zone, as there is no associated dataset.
222  */
223 /* ARGSUSED */
224 static int
225 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
226 {
227 	return (0);
228 }
229 
230 /*
231  * Policy for dataset read operations (list children, get statistics).  Requires
232  * no privileges, but must be visible in the local zone.
233  */
234 /* ARGSUSED */
235 static int
236 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
237 {
238 	if (INGLOBALZONE(curproc) ||
239 	    zone_dataset_visible(zc->zc_name, NULL))
240 		return (0);
241 
242 	return (ENOENT);
243 }
244 
245 static int
246 zfs_dozonecheck(const char *dataset, cred_t *cr)
247 {
248 	uint64_t zoned;
249 	int writable = 1;
250 
251 	/*
252 	 * The dataset must be visible by this zone -- check this first
253 	 * so they don't see EPERM on something they shouldn't know about.
254 	 */
255 	if (!INGLOBALZONE(curproc) &&
256 	    !zone_dataset_visible(dataset, &writable))
257 		return (ENOENT);
258 
259 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
260 		return (ENOENT);
261 
262 	if (INGLOBALZONE(curproc)) {
263 		/*
264 		 * If the fs is zoned, only root can access it from the
265 		 * global zone.
266 		 */
267 		if (secpolicy_zfs(cr) && zoned)
268 			return (EPERM);
269 	} else {
270 		/*
271 		 * If we are in a local zone, the 'zoned' property must be set.
272 		 */
273 		if (!zoned)
274 			return (EPERM);
275 
276 		/* must be writable by this zone */
277 		if (!writable)
278 			return (EPERM);
279 	}
280 	return (0);
281 }
282 
283 int
284 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
285 {
286 	int error;
287 
288 	error = zfs_dozonecheck(name, cr);
289 	if (error == 0) {
290 		error = secpolicy_zfs(cr);
291 		if (error)
292 			error = dsl_deleg_access(name, perm, cr);
293 	}
294 	return (error);
295 }
296 
297 static int
298 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
299 {
300 	/*
301 	 * Check permissions for special properties.
302 	 */
303 	switch (prop) {
304 	case ZFS_PROP_ZONED:
305 		/*
306 		 * Disallow setting of 'zoned' from within a local zone.
307 		 */
308 		if (!INGLOBALZONE(curproc))
309 			return (EPERM);
310 		break;
311 
312 	case ZFS_PROP_QUOTA:
313 		if (!INGLOBALZONE(curproc)) {
314 			uint64_t zoned;
315 			char setpoint[MAXNAMELEN];
316 			/*
317 			 * Unprivileged users are allowed to modify the
318 			 * quota on things *under* (ie. contained by)
319 			 * the thing they own.
320 			 */
321 			if (dsl_prop_get_integer(name, "zoned", &zoned,
322 			    setpoint))
323 				return (EPERM);
324 			if (!zoned || strlen(name) <= strlen(setpoint))
325 				return (EPERM);
326 		}
327 		break;
328 	}
329 
330 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
331 }
332 
333 int
334 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
335 {
336 	int error;
337 
338 	error = zfs_dozonecheck(zc->zc_name, cr);
339 	if (error)
340 		return (error);
341 
342 	/*
343 	 * permission to set permissions will be evaluated later in
344 	 * dsl_deleg_can_allow()
345 	 */
346 	return (0);
347 }
348 
349 int
350 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
351 {
352 	int error;
353 	error = zfs_secpolicy_write_perms(zc->zc_name,
354 	    ZFS_DELEG_PERM_ROLLBACK, cr);
355 	if (error == 0)
356 		error = zfs_secpolicy_write_perms(zc->zc_name,
357 		    ZFS_DELEG_PERM_MOUNT, cr);
358 	return (error);
359 }
360 
361 int
362 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
363 {
364 	return (zfs_secpolicy_write_perms(zc->zc_name,
365 	    ZFS_DELEG_PERM_SEND, cr));
366 }
367 
368 int
369 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
370 {
371 	if (!INGLOBALZONE(curproc))
372 		return (EPERM);
373 
374 	if (secpolicy_nfs(cr) == 0) {
375 		return (0);
376 	} else {
377 		vnode_t *vp;
378 		int error;
379 
380 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
381 		    NO_FOLLOW, NULL, &vp)) != 0)
382 			return (error);
383 
384 		/* Now make sure mntpnt and dataset are ZFS */
385 
386 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
387 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
388 		    zc->zc_name) != 0)) {
389 			VN_RELE(vp);
390 			return (EPERM);
391 		}
392 
393 		VN_RELE(vp);
394 		return (dsl_deleg_access(zc->zc_name,
395 		    ZFS_DELEG_PERM_SHARE, cr));
396 	}
397 }
398 
399 static int
400 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
401 {
402 	char *cp;
403 
404 	/*
405 	 * Remove the @bla or /bla from the end of the name to get the parent.
406 	 */
407 	(void) strncpy(parent, datasetname, parentsize);
408 	cp = strrchr(parent, '@');
409 	if (cp != NULL) {
410 		cp[0] = '\0';
411 	} else {
412 		cp = strrchr(parent, '/');
413 		if (cp == NULL)
414 			return (ENOENT);
415 		cp[0] = '\0';
416 	}
417 
418 	return (0);
419 }
420 
421 int
422 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
423 {
424 	int error;
425 
426 	if ((error = zfs_secpolicy_write_perms(name,
427 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
428 		return (error);
429 
430 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
431 }
432 
433 static int
434 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
435 {
436 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
437 }
438 
439 /*
440  * Must have sys_config privilege to check the iscsi permission
441  */
442 /* ARGSUSED */
443 static int
444 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
445 {
446 	return (secpolicy_zfs(cr));
447 }
448 
449 int
450 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
451 {
452 	char 	parentname[MAXNAMELEN];
453 	int	error;
454 
455 	if ((error = zfs_secpolicy_write_perms(from,
456 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
457 		return (error);
458 
459 	if ((error = zfs_secpolicy_write_perms(from,
460 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
461 		return (error);
462 
463 	if ((error = zfs_get_parent(to, parentname,
464 	    sizeof (parentname))) != 0)
465 		return (error);
466 
467 	if ((error = zfs_secpolicy_write_perms(parentname,
468 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
469 		return (error);
470 
471 	if ((error = zfs_secpolicy_write_perms(parentname,
472 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
473 		return (error);
474 
475 	return (error);
476 }
477 
478 static int
479 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
480 {
481 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
482 }
483 
484 static int
485 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
486 {
487 	char 	parentname[MAXNAMELEN];
488 	objset_t *clone;
489 	int error;
490 
491 	error = zfs_secpolicy_write_perms(zc->zc_name,
492 	    ZFS_DELEG_PERM_PROMOTE, cr);
493 	if (error)
494 		return (error);
495 
496 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
497 	    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
498 
499 	if (error == 0) {
500 		dsl_dataset_t *pclone = NULL;
501 		dsl_dir_t *dd;
502 		dd = clone->os->os_dsl_dataset->ds_dir;
503 
504 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
505 		error = dsl_dataset_open_obj(dd->dd_pool,
506 		    dd->dd_phys->dd_origin_obj, NULL,
507 		    DS_MODE_NONE, FTAG, &pclone);
508 		rw_exit(&dd->dd_pool->dp_config_rwlock);
509 		if (error) {
510 			dmu_objset_close(clone);
511 			return (error);
512 		}
513 
514 		error = zfs_secpolicy_write_perms(zc->zc_name,
515 		    ZFS_DELEG_PERM_MOUNT, cr);
516 
517 		dsl_dataset_name(pclone, parentname);
518 		dmu_objset_close(clone);
519 		dsl_dataset_close(pclone, DS_MODE_NONE, FTAG);
520 		if (error == 0)
521 			error = zfs_secpolicy_write_perms(parentname,
522 			    ZFS_DELEG_PERM_PROMOTE, cr);
523 	}
524 	return (error);
525 }
526 
527 static int
528 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
529 {
530 	int error;
531 
532 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
533 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
534 		return (error);
535 
536 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
537 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
538 		return (error);
539 
540 	return (zfs_secpolicy_write_perms(zc->zc_name,
541 	    ZFS_DELEG_PERM_CREATE, cr));
542 }
543 
544 int
545 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
546 {
547 	int error;
548 
549 	if ((error = zfs_secpolicy_write_perms(name,
550 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
551 		return (error);
552 
553 	error = zfs_secpolicy_write_perms(name,
554 	    ZFS_DELEG_PERM_MOUNT, cr);
555 
556 	return (error);
557 }
558 
559 static int
560 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
561 {
562 
563 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
564 }
565 
566 static int
567 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
568 {
569 	char 	parentname[MAXNAMELEN];
570 	int 	error;
571 
572 	if ((error = zfs_get_parent(zc->zc_name, parentname,
573 	    sizeof (parentname))) != 0)
574 		return (error);
575 
576 	if (zc->zc_value[0] != '\0') {
577 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
578 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
579 			return (error);
580 	}
581 
582 	if ((error = zfs_secpolicy_write_perms(parentname,
583 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
584 		return (error);
585 
586 	error = zfs_secpolicy_write_perms(parentname,
587 	    ZFS_DELEG_PERM_MOUNT, cr);
588 
589 	return (error);
590 }
591 
592 static int
593 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
594 {
595 	int error;
596 
597 	error = secpolicy_fs_unmount(cr, NULL);
598 	if (error) {
599 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
600 	}
601 	return (error);
602 }
603 
604 /*
605  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
606  * SYS_CONFIG privilege, which is not available in a local zone.
607  */
608 /* ARGSUSED */
609 static int
610 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
611 {
612 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
613 		return (EPERM);
614 
615 	return (0);
616 }
617 
618 /*
619  * Just like zfs_secpolicy_config, except that we will check for
620  * mount permission on the dataset for permission to create/remove
621  * the minor nodes.
622  */
623 static int
624 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
625 {
626 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
627 		return (dsl_deleg_access(zc->zc_name,
628 		    ZFS_DELEG_PERM_MOUNT, cr));
629 	}
630 
631 	return (0);
632 }
633 
634 /*
635  * Policy for fault injection.  Requires all privileges.
636  */
637 /* ARGSUSED */
638 static int
639 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
640 {
641 	return (secpolicy_zinject(cr));
642 }
643 
644 static int
645 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
646 {
647 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
648 
649 	if (prop == ZPROP_INVAL) {
650 		if (!zfs_prop_user(zc->zc_value))
651 			return (EINVAL);
652 		return (zfs_secpolicy_write_perms(zc->zc_name,
653 		    ZFS_DELEG_PERM_USERPROP, cr));
654 	} else {
655 		if (!zfs_prop_inheritable(prop))
656 			return (EINVAL);
657 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
658 	}
659 }
660 
661 /*
662  * Returns the nvlist as specified by the user in the zfs_cmd_t.
663  */
664 static int
665 get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
666 {
667 	char *packed;
668 	int error;
669 	nvlist_t *list = NULL;
670 
671 	/*
672 	 * Read in and unpack the user-supplied nvlist.
673 	 */
674 	if (size == 0)
675 		return (EINVAL);
676 
677 	packed = kmem_alloc(size, KM_SLEEP);
678 
679 	if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
680 		kmem_free(packed, size);
681 		return (error);
682 	}
683 
684 	if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
685 		kmem_free(packed, size);
686 		return (error);
687 	}
688 
689 	kmem_free(packed, size);
690 
691 	*nvp = list;
692 	return (0);
693 }
694 
695 static int
696 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
697 {
698 	char *packed = NULL;
699 	size_t size;
700 	int error;
701 
702 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
703 
704 	if (size > zc->zc_nvlist_dst_size) {
705 		error = ENOMEM;
706 	} else {
707 		packed = kmem_alloc(size, KM_SLEEP);
708 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
709 		    KM_SLEEP) == 0);
710 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
711 		    size);
712 		kmem_free(packed, size);
713 	}
714 
715 	zc->zc_nvlist_dst_size = size;
716 	return (error);
717 }
718 
719 static int
720 zfs_ioc_pool_create(zfs_cmd_t *zc)
721 {
722 	int error;
723 	nvlist_t *config, *props = NULL;
724 	char *buf;
725 
726 	if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
727 	    &config))
728 		return (error);
729 
730 	if (zc->zc_nvlist_src_size != 0 && (error =
731 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
732 		nvlist_free(config);
733 		return (error);
734 	}
735 
736 	buf = history_str_get(zc);
737 
738 	error = spa_create(zc->zc_name, config, props, buf);
739 
740 	if (buf != NULL)
741 		history_str_free(buf);
742 
743 	nvlist_free(config);
744 
745 	if (props)
746 		nvlist_free(props);
747 
748 	return (error);
749 }
750 
751 static int
752 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
753 {
754 	int error;
755 	zfs_log_history(zc);
756 	error = spa_destroy(zc->zc_name);
757 	return (error);
758 }
759 
760 static int
761 zfs_ioc_pool_import(zfs_cmd_t *zc)
762 {
763 	int error;
764 	nvlist_t *config, *props = NULL;
765 	uint64_t guid;
766 
767 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
768 	    &config)) != 0)
769 		return (error);
770 
771 	if (zc->zc_nvlist_src_size != 0 && (error =
772 	    get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
773 		nvlist_free(config);
774 		return (error);
775 	}
776 
777 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
778 	    guid != zc->zc_guid)
779 		error = EINVAL;
780 	else
781 		error = spa_import(zc->zc_name, config, props);
782 
783 	nvlist_free(config);
784 
785 	if (props)
786 		nvlist_free(props);
787 
788 	return (error);
789 }
790 
791 static int
792 zfs_ioc_pool_export(zfs_cmd_t *zc)
793 {
794 	int error;
795 	zfs_log_history(zc);
796 	error = spa_export(zc->zc_name, NULL);
797 	return (error);
798 }
799 
800 static int
801 zfs_ioc_pool_configs(zfs_cmd_t *zc)
802 {
803 	nvlist_t *configs;
804 	int error;
805 
806 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
807 		return (EEXIST);
808 
809 	error = put_nvlist(zc, configs);
810 
811 	nvlist_free(configs);
812 
813 	return (error);
814 }
815 
816 static int
817 zfs_ioc_pool_stats(zfs_cmd_t *zc)
818 {
819 	nvlist_t *config;
820 	int error;
821 	int ret = 0;
822 
823 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
824 	    sizeof (zc->zc_value));
825 
826 	if (config != NULL) {
827 		ret = put_nvlist(zc, config);
828 		nvlist_free(config);
829 
830 		/*
831 		 * The config may be present even if 'error' is non-zero.
832 		 * In this case we return success, and preserve the real errno
833 		 * in 'zc_cookie'.
834 		 */
835 		zc->zc_cookie = error;
836 	} else {
837 		ret = error;
838 	}
839 
840 	return (ret);
841 }
842 
843 /*
844  * Try to import the given pool, returning pool stats as appropriate so that
845  * user land knows which devices are available and overall pool health.
846  */
847 static int
848 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
849 {
850 	nvlist_t *tryconfig, *config;
851 	int error;
852 
853 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
854 	    &tryconfig)) != 0)
855 		return (error);
856 
857 	config = spa_tryimport(tryconfig);
858 
859 	nvlist_free(tryconfig);
860 
861 	if (config == NULL)
862 		return (EINVAL);
863 
864 	error = put_nvlist(zc, config);
865 	nvlist_free(config);
866 
867 	return (error);
868 }
869 
870 static int
871 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
872 {
873 	spa_t *spa;
874 	int error;
875 
876 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
877 		return (error);
878 
879 	mutex_enter(&spa_namespace_lock);
880 	error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
881 	mutex_exit(&spa_namespace_lock);
882 
883 	spa_close(spa, FTAG);
884 
885 	return (error);
886 }
887 
888 static int
889 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
890 {
891 	spa_t *spa;
892 	int error;
893 
894 	error = spa_open(zc->zc_name, &spa, FTAG);
895 	if (error == 0) {
896 		spa_freeze(spa);
897 		spa_close(spa, FTAG);
898 	}
899 	return (error);
900 }
901 
902 static int
903 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
904 {
905 	spa_t *spa;
906 	int error;
907 
908 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
909 		return (error);
910 
911 	if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
912 		spa_close(spa, FTAG);
913 		return (EINVAL);
914 	}
915 
916 	spa_upgrade(spa, zc->zc_cookie);
917 	spa_close(spa, FTAG);
918 
919 	return (error);
920 }
921 
922 static int
923 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
924 {
925 	spa_t *spa;
926 	char *hist_buf;
927 	uint64_t size;
928 	int error;
929 
930 	if ((size = zc->zc_history_len) == 0)
931 		return (EINVAL);
932 
933 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
934 		return (error);
935 
936 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
937 		spa_close(spa, FTAG);
938 		return (ENOTSUP);
939 	}
940 
941 	hist_buf = kmem_alloc(size, KM_SLEEP);
942 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
943 	    &zc->zc_history_len, hist_buf)) == 0) {
944 		error = xcopyout(hist_buf,
945 		    (char *)(uintptr_t)zc->zc_history,
946 		    zc->zc_history_len);
947 	}
948 
949 	spa_close(spa, FTAG);
950 	kmem_free(hist_buf, size);
951 	return (error);
952 }
953 
954 static int
955 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
956 {
957 	int error;
958 
959 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
960 		return (error);
961 
962 	return (0);
963 }
964 
965 static int
966 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
967 {
968 	objset_t *osp;
969 	int error;
970 
971 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
972 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
973 		return (error);
974 
975 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
976 	    sizeof (zc->zc_value));
977 	dmu_objset_close(osp);
978 
979 	return (error);
980 }
981 
982 static int
983 zfs_ioc_vdev_add(zfs_cmd_t *zc)
984 {
985 	spa_t *spa;
986 	int error;
987 	nvlist_t *config, **l2cache;
988 	uint_t nl2cache;
989 
990 	error = spa_open(zc->zc_name, &spa, FTAG);
991 	if (error != 0)
992 		return (error);
993 
994 	error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
995 	    &config);
996 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
997 	    &l2cache, &nl2cache);
998 
999 	/*
1000 	 * A root pool with concatenated devices is not supported.
1001 	 * Thus, can not add a device to a root pool with one device.
1002 	 * Allow for l2cache devices to be added.
1003 	 */
1004 	if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0 &&
1005 	    nl2cache == 0) {
1006 		spa_close(spa, FTAG);
1007 		return (EDOM);
1008 	}
1009 
1010 	if (error == 0) {
1011 		error = spa_vdev_add(spa, config);
1012 		nvlist_free(config);
1013 	}
1014 	spa_close(spa, FTAG);
1015 	return (error);
1016 }
1017 
1018 static int
1019 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1020 {
1021 	spa_t *spa;
1022 	int error;
1023 
1024 	error = spa_open(zc->zc_name, &spa, FTAG);
1025 	if (error != 0)
1026 		return (error);
1027 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1028 	spa_close(spa, FTAG);
1029 	return (error);
1030 }
1031 
1032 static int
1033 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1034 {
1035 	spa_t *spa;
1036 	int error;
1037 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1038 
1039 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1040 		return (error);
1041 	switch (zc->zc_cookie) {
1042 	case VDEV_STATE_ONLINE:
1043 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1044 		break;
1045 
1046 	case VDEV_STATE_OFFLINE:
1047 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1048 		break;
1049 
1050 	case VDEV_STATE_FAULTED:
1051 		error = vdev_fault(spa, zc->zc_guid);
1052 		break;
1053 
1054 	case VDEV_STATE_DEGRADED:
1055 		error = vdev_degrade(spa, zc->zc_guid);
1056 		break;
1057 
1058 	default:
1059 		error = EINVAL;
1060 	}
1061 	zc->zc_cookie = newstate;
1062 	spa_close(spa, FTAG);
1063 	return (error);
1064 }
1065 
1066 static int
1067 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1068 {
1069 	spa_t *spa;
1070 	int replacing = zc->zc_cookie;
1071 	nvlist_t *config;
1072 	int error;
1073 
1074 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1075 		return (error);
1076 
1077 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1078 	    &config)) == 0) {
1079 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1080 		nvlist_free(config);
1081 	}
1082 
1083 	spa_close(spa, FTAG);
1084 	return (error);
1085 }
1086 
1087 static int
1088 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1089 {
1090 	spa_t *spa;
1091 	int error;
1092 
1093 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1094 		return (error);
1095 
1096 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1097 
1098 	spa_close(spa, FTAG);
1099 	return (error);
1100 }
1101 
1102 static int
1103 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1104 {
1105 	spa_t *spa;
1106 	char *path = zc->zc_value;
1107 	uint64_t guid = zc->zc_guid;
1108 	int error;
1109 
1110 	error = spa_open(zc->zc_name, &spa, FTAG);
1111 	if (error != 0)
1112 		return (error);
1113 
1114 	error = spa_vdev_setpath(spa, guid, path);
1115 	spa_close(spa, FTAG);
1116 	return (error);
1117 }
1118 
1119 static int
1120 zfs_os_open_retry(char *name, objset_t **os)
1121 {
1122 	int error;
1123 
1124 retry:
1125 	error = dmu_objset_open(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 	}
1140 	return (error);
1141 }
1142 
1143 /*
1144  * inputs:
1145  * zc_name		name of filesystem
1146  * zc_nvlist_dst_size	size of buffer for property nvlist
1147  *
1148  * outputs:
1149  * zc_objset_stats	stats
1150  * zc_nvlist_dst	property nvlist
1151  * zc_nvlist_dst_size	size of property nvlist
1152  * zc_value		alternate root
1153  */
1154 static int
1155 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1156 {
1157 	objset_t *os = NULL;
1158 	int error;
1159 	nvlist_t *nv;
1160 
1161 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0)
1162 		return (error);
1163 
1164 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1165 
1166 	if (zc->zc_nvlist_dst != 0 &&
1167 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1168 		dmu_objset_stats(os, nv);
1169 		/*
1170 		 * NB: zvol_get_stats() will read the objset contents,
1171 		 * which we aren't supposed to do with a
1172 		 * DS_MODE_STANDARD open, because it could be
1173 		 * inconsistent.  So this is a bit of a workaround...
1174 		 */
1175 		if (!zc->zc_objset_stats.dds_inconsistent) {
1176 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1177 				VERIFY(zvol_get_stats(os, nv) == 0);
1178 		}
1179 		error = put_nvlist(zc, nv);
1180 		nvlist_free(nv);
1181 	}
1182 
1183 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
1184 
1185 	dmu_objset_close(os);
1186 	return (error);
1187 }
1188 
1189 static int
1190 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1191 {
1192 	uint64_t value;
1193 	int error;
1194 
1195 	/*
1196 	 * zfs_get_zplprop() will either find a value or give us
1197 	 * the default value (if there is one).
1198 	 */
1199 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1200 		return (error);
1201 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1202 	return (0);
1203 }
1204 
1205 /*
1206  * inputs:
1207  * zc_name		name of filesystem
1208  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
1209  *
1210  * outputs:
1211  * zc_nvlist_dst	zpl property nvlist
1212  * zc_nvlist_dst_size	size of zpl property nvlist
1213  */
1214 static int
1215 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1216 {
1217 	objset_t *os;
1218 	int err;
1219 
1220 	if ((err = zfs_os_open_retry(zc->zc_name, &os)) != 0)
1221 		return (err);
1222 
1223 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1224 
1225 	/*
1226 	 * NB: nvl_add_zplprop() will read the objset contents,
1227 	 * which we aren't supposed to do with a DS_MODE_STANDARD
1228 	 * open, because it could be inconsistent.
1229 	 */
1230 	if (zc->zc_nvlist_dst != NULL &&
1231 	    !zc->zc_objset_stats.dds_inconsistent &&
1232 	    dmu_objset_type(os) == DMU_OST_ZFS) {
1233 		nvlist_t *nv;
1234 
1235 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1236 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1237 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1238 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1239 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1240 			err = put_nvlist(zc, nv);
1241 		nvlist_free(nv);
1242 	} else {
1243 		err = ENOENT;
1244 	}
1245 	dmu_objset_close(os);
1246 	return (err);
1247 }
1248 
1249 /*
1250  * inputs:
1251  * zc_name		name of filesystem
1252  * zc_cookie		zap cursor
1253  * zc_nvlist_dst_size	size of buffer for property nvlist
1254  *
1255  * outputs:
1256  * zc_name		name of next filesystem
1257  * zc_objset_stats	stats
1258  * zc_nvlist_dst	property nvlist
1259  * zc_nvlist_dst_size	size of property nvlist
1260  * zc_value		alternate root
1261  */
1262 static int
1263 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1264 {
1265 	objset_t *os;
1266 	int error;
1267 	char *p;
1268 
1269 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) {
1270 		if (error == ENOENT)
1271 			error = ESRCH;
1272 		return (error);
1273 	}
1274 
1275 	p = strrchr(zc->zc_name, '/');
1276 	if (p == NULL || p[1] != '\0')
1277 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1278 	p = zc->zc_name + strlen(zc->zc_name);
1279 
1280 	do {
1281 		error = dmu_dir_list_next(os,
1282 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1283 		    NULL, &zc->zc_cookie);
1284 		if (error == ENOENT)
1285 			error = ESRCH;
1286 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1287 	    !zone_dataset_visible(zc->zc_name, NULL));
1288 
1289 	/*
1290 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1291 	 * try to get stats for it.  Userland will skip over it.
1292 	 */
1293 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1294 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1295 
1296 	dmu_objset_close(os);
1297 	return (error);
1298 }
1299 
1300 /*
1301  * inputs:
1302  * zc_name		name of filesystem
1303  * zc_cookie		zap cursor
1304  * zc_nvlist_dst_size	size of buffer for property nvlist
1305  *
1306  * outputs:
1307  * zc_name		name of next snapshot
1308  * zc_objset_stats	stats
1309  * zc_nvlist_dst	property nvlist
1310  * zc_nvlist_dst_size	size of property nvlist
1311  * zc_value		alternate root
1312  */
1313 static int
1314 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1315 {
1316 	objset_t *os;
1317 	int error;
1318 
1319 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) {
1320 		if (error == ENOENT)
1321 			error = ESRCH;
1322 		return (error);
1323 	}
1324 
1325 	/*
1326 	 * A dataset name of maximum length cannot have any snapshots,
1327 	 * so exit immediately.
1328 	 */
1329 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1330 		dmu_objset_close(os);
1331 		return (ESRCH);
1332 	}
1333 
1334 	error = dmu_snapshot_list_next(os,
1335 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1336 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1337 	if (error == ENOENT)
1338 		error = ESRCH;
1339 
1340 	if (error == 0)
1341 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1342 
1343 	/* if we failed, undo the @ that we tacked on to zc_name */
1344 	if (error != 0)
1345 		*strchr(zc->zc_name, '@') = '\0';
1346 
1347 	dmu_objset_close(os);
1348 	return (error);
1349 }
1350 
1351 static int
1352 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1353 {
1354 	nvpair_t *elem;
1355 	int error;
1356 	uint64_t intval;
1357 	char *strval;
1358 
1359 	/*
1360 	 * First validate permission to set all of the properties
1361 	 */
1362 	elem = NULL;
1363 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1364 		const char *propname = nvpair_name(elem);
1365 		zfs_prop_t prop = zfs_name_to_prop(propname);
1366 
1367 		if (prop == ZPROP_INVAL) {
1368 			/*
1369 			 * If this is a user-defined property, it must be a
1370 			 * string, and there is no further validation to do.
1371 			 */
1372 			if (!zfs_prop_user(propname) ||
1373 			    nvpair_type(elem) != DATA_TYPE_STRING)
1374 				return (EINVAL);
1375 
1376 			if (error = zfs_secpolicy_write_perms(name,
1377 			    ZFS_DELEG_PERM_USERPROP, CRED()))
1378 				return (error);
1379 			continue;
1380 		}
1381 
1382 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1383 			return (error);
1384 
1385 		/*
1386 		 * Check that this value is valid for this pool version
1387 		 */
1388 		switch (prop) {
1389 		case ZFS_PROP_COMPRESSION:
1390 			/*
1391 			 * If the user specified gzip compression, make sure
1392 			 * the SPA supports it. We ignore any errors here since
1393 			 * we'll catch them later.
1394 			 */
1395 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1396 			    nvpair_value_uint64(elem, &intval) == 0 &&
1397 			    intval >= ZIO_COMPRESS_GZIP_1 &&
1398 			    intval <= ZIO_COMPRESS_GZIP_9) {
1399 				if (zfs_check_version(name,
1400 				    SPA_VERSION_GZIP_COMPRESSION))
1401 					return (ENOTSUP);
1402 			}
1403 			break;
1404 
1405 		case ZFS_PROP_COPIES:
1406 			if (zfs_check_version(name, SPA_VERSION_DITTO_BLOCKS))
1407 				return (ENOTSUP);
1408 			break;
1409 
1410 		case ZFS_PROP_SHARESMB:
1411 			if (zpl_check_version(name, ZPL_VERSION_FUID))
1412 				return (ENOTSUP);
1413 			break;
1414 		}
1415 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1416 			return (error);
1417 	}
1418 
1419 	elem = NULL;
1420 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1421 		const char *propname = nvpair_name(elem);
1422 		zfs_prop_t prop = zfs_name_to_prop(propname);
1423 
1424 		if (prop == ZPROP_INVAL) {
1425 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1426 			error = dsl_prop_set(name, propname, 1,
1427 			    strlen(strval) + 1, strval);
1428 			if (error == 0)
1429 				continue;
1430 			else
1431 				return (error);
1432 		}
1433 
1434 		switch (prop) {
1435 		case ZFS_PROP_QUOTA:
1436 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1437 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1438 				return (error);
1439 			break;
1440 
1441 		case ZFS_PROP_REFQUOTA:
1442 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1443 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
1444 				return (error);
1445 			break;
1446 
1447 		case ZFS_PROP_RESERVATION:
1448 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1449 			    (error = dsl_dir_set_reservation(name,
1450 			    intval)) != 0)
1451 				return (error);
1452 			break;
1453 
1454 		case ZFS_PROP_REFRESERVATION:
1455 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1456 			    (error = dsl_dataset_set_reservation(name,
1457 			    intval)) != 0)
1458 				return (error);
1459 			break;
1460 
1461 		case ZFS_PROP_VOLSIZE:
1462 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1463 			    (error = zvol_set_volsize(name,
1464 			    ddi_driver_major(zfs_dip), intval)) != 0)
1465 				return (error);
1466 			break;
1467 
1468 		case ZFS_PROP_VOLBLOCKSIZE:
1469 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1470 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1471 				return (error);
1472 			break;
1473 
1474 		case ZFS_PROP_VERSION:
1475 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1476 			    (error = zfs_set_version(name, intval)) != 0)
1477 				return (error);
1478 			break;
1479 
1480 		default:
1481 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1482 				if (zfs_prop_get_type(prop) !=
1483 				    PROP_TYPE_STRING)
1484 					return (EINVAL);
1485 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1486 				if ((error = dsl_prop_set(name,
1487 				    nvpair_name(elem), 1, strlen(strval) + 1,
1488 				    strval)) != 0)
1489 					return (error);
1490 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1491 				const char *unused;
1492 
1493 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1494 
1495 				switch (zfs_prop_get_type(prop)) {
1496 				case PROP_TYPE_NUMBER:
1497 					break;
1498 				case PROP_TYPE_STRING:
1499 					return (EINVAL);
1500 				case PROP_TYPE_INDEX:
1501 					if (zfs_prop_index_to_string(prop,
1502 					    intval, &unused) != 0)
1503 						return (EINVAL);
1504 					break;
1505 				default:
1506 					cmn_err(CE_PANIC,
1507 					    "unknown property type");
1508 					break;
1509 				}
1510 
1511 				if ((error = dsl_prop_set(name, propname,
1512 				    8, 1, &intval)) != 0)
1513 					return (error);
1514 			} else {
1515 				return (EINVAL);
1516 			}
1517 			break;
1518 		}
1519 	}
1520 
1521 	return (0);
1522 }
1523 
1524 /*
1525  * inputs:
1526  * zc_name		name of filesystem
1527  * zc_value		name of property to inherit
1528  * zc_nvlist_src{_size}	nvlist of properties to apply
1529  *
1530  * outputs:		none
1531  */
1532 static int
1533 zfs_ioc_set_prop(zfs_cmd_t *zc)
1534 {
1535 	nvlist_t *nvl;
1536 	int error;
1537 
1538 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1539 	    &nvl)) != 0)
1540 		return (error);
1541 
1542 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1543 
1544 	nvlist_free(nvl);
1545 	return (error);
1546 }
1547 
1548 /*
1549  * inputs:
1550  * zc_name		name of filesystem
1551  * zc_value		name of property to inherit
1552  *
1553  * outputs:		none
1554  */
1555 static int
1556 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1557 {
1558 	/* the property name has been validated by zfs_secpolicy_inherit() */
1559 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1560 }
1561 
1562 static int
1563 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1564 {
1565 	nvlist_t *props;
1566 	spa_t *spa;
1567 	int error;
1568 
1569 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1570 	    &props)))
1571 		return (error);
1572 
1573 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1574 		nvlist_free(props);
1575 		return (error);
1576 	}
1577 
1578 	error = spa_prop_set(spa, props);
1579 
1580 	nvlist_free(props);
1581 	spa_close(spa, FTAG);
1582 
1583 	return (error);
1584 }
1585 
1586 static int
1587 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1588 {
1589 	spa_t *spa;
1590 	int error;
1591 	nvlist_t *nvp = NULL;
1592 
1593 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1594 		return (error);
1595 
1596 	error = spa_prop_get(spa, &nvp);
1597 
1598 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1599 		error = put_nvlist(zc, nvp);
1600 	else
1601 		error = EFAULT;
1602 
1603 	spa_close(spa, FTAG);
1604 
1605 	if (nvp)
1606 		nvlist_free(nvp);
1607 	return (error);
1608 }
1609 
1610 static int
1611 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1612 {
1613 	nvlist_t *nvp;
1614 	int error;
1615 	uint32_t uid;
1616 	uint32_t gid;
1617 	uint32_t *groups;
1618 	uint_t group_cnt;
1619 	cred_t	*usercred;
1620 
1621 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1622 	    &nvp)) != 0) {
1623 		return (error);
1624 	}
1625 
1626 	if ((error = nvlist_lookup_uint32(nvp,
1627 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1628 		nvlist_free(nvp);
1629 		return (EPERM);
1630 	}
1631 
1632 	if ((error = nvlist_lookup_uint32(nvp,
1633 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1634 		nvlist_free(nvp);
1635 		return (EPERM);
1636 	}
1637 
1638 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1639 	    &groups, &group_cnt)) != 0) {
1640 		nvlist_free(nvp);
1641 		return (EPERM);
1642 	}
1643 	usercred = cralloc();
1644 	if ((crsetugid(usercred, uid, gid) != 0) ||
1645 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1646 		nvlist_free(nvp);
1647 		crfree(usercred);
1648 		return (EPERM);
1649 	}
1650 	nvlist_free(nvp);
1651 	error = dsl_deleg_access(zc->zc_name,
1652 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1653 	crfree(usercred);
1654 	return (error);
1655 }
1656 
1657 /*
1658  * inputs:
1659  * zc_name		name of filesystem
1660  * zc_nvlist_src{_size}	nvlist of delegated permissions
1661  * zc_perm_action	allow/unallow flag
1662  *
1663  * outputs:		none
1664  */
1665 static int
1666 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1667 {
1668 	int error;
1669 	nvlist_t *fsaclnv = NULL;
1670 
1671 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1672 	    &fsaclnv)) != 0)
1673 		return (error);
1674 
1675 	/*
1676 	 * Verify nvlist is constructed correctly
1677 	 */
1678 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1679 		nvlist_free(fsaclnv);
1680 		return (EINVAL);
1681 	}
1682 
1683 	/*
1684 	 * If we don't have PRIV_SYS_MOUNT, then validate
1685 	 * that user is allowed to hand out each permission in
1686 	 * the nvlist(s)
1687 	 */
1688 
1689 	error = secpolicy_zfs(CRED());
1690 	if (error) {
1691 		if (zc->zc_perm_action == B_FALSE) {
1692 			error = dsl_deleg_can_allow(zc->zc_name,
1693 			    fsaclnv, CRED());
1694 		} else {
1695 			error = dsl_deleg_can_unallow(zc->zc_name,
1696 			    fsaclnv, CRED());
1697 		}
1698 	}
1699 
1700 	if (error == 0)
1701 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1702 
1703 	nvlist_free(fsaclnv);
1704 	return (error);
1705 }
1706 
1707 /*
1708  * inputs:
1709  * zc_name		name of filesystem
1710  *
1711  * outputs:
1712  * zc_nvlist_src{_size}	nvlist of delegated permissions
1713  */
1714 static int
1715 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1716 {
1717 	nvlist_t *nvp;
1718 	int error;
1719 
1720 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1721 		error = put_nvlist(zc, nvp);
1722 		nvlist_free(nvp);
1723 	}
1724 
1725 	return (error);
1726 }
1727 
1728 /*
1729  * inputs:
1730  * zc_name		name of volume
1731  *
1732  * outputs:		none
1733  */
1734 static int
1735 zfs_ioc_create_minor(zfs_cmd_t *zc)
1736 {
1737 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1738 }
1739 
1740 /*
1741  * inputs:
1742  * zc_name		name of volume
1743  *
1744  * outputs:		none
1745  */
1746 static int
1747 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1748 {
1749 	return (zvol_remove_minor(zc->zc_name));
1750 }
1751 
1752 /*
1753  * Search the vfs list for a specified resource.  Returns a pointer to it
1754  * or NULL if no suitable entry is found. The caller of this routine
1755  * is responsible for releasing the returned vfs pointer.
1756  */
1757 static vfs_t *
1758 zfs_get_vfs(const char *resource)
1759 {
1760 	struct vfs *vfsp;
1761 	struct vfs *vfs_found = NULL;
1762 
1763 	vfs_list_read_lock();
1764 	vfsp = rootvfs;
1765 	do {
1766 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1767 			VFS_HOLD(vfsp);
1768 			vfs_found = vfsp;
1769 			break;
1770 		}
1771 		vfsp = vfsp->vfs_next;
1772 	} while (vfsp != rootvfs);
1773 	vfs_list_unlock();
1774 	return (vfs_found);
1775 }
1776 
1777 /* ARGSUSED */
1778 static void
1779 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1780 {
1781 	zfs_creat_t *zct = arg;
1782 
1783 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
1784 }
1785 
1786 #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
1787 
1788 /*
1789  * inputs:
1790  * createprops	list of properties requested by creator
1791  * dataset	name of dataset we are creating
1792  *
1793  * outputs:
1794  * zplprops	values for the zplprops we attach to the master node object
1795  *
1796  * Determine the settings for utf8only, normalization and
1797  * casesensitivity.  Specific values may have been requested by the
1798  * creator and/or we can inherit values from the parent dataset.  If
1799  * the file system is of too early a vintage, a creator can not
1800  * request settings for these properties, even if the requested
1801  * setting is the default value.  We don't actually want to create dsl
1802  * properties for these, so remove them from the source nvlist after
1803  * processing.
1804  */
1805 static int
1806 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
1807     nvlist_t *zplprops, uint64_t zplver)
1808 {
1809 	objset_t *os;
1810 	char parentname[MAXNAMELEN];
1811 	char *cp;
1812 	uint64_t sense = ZFS_PROP_UNDEFINED;
1813 	uint64_t norm = ZFS_PROP_UNDEFINED;
1814 	uint64_t u8 = ZFS_PROP_UNDEFINED;
1815 	int error = 0;
1816 
1817 	ASSERT(zplprops != NULL);
1818 
1819 	(void) strlcpy(parentname, dataset, sizeof (parentname));
1820 	cp = strrchr(parentname, '/');
1821 	ASSERT(cp != NULL);
1822 	cp[0] = '\0';
1823 
1824 	/*
1825 	 * Pull out creator prop choices, if any.
1826 	 */
1827 	if (createprops) {
1828 		(void) nvlist_lookup_uint64(createprops,
1829 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
1830 		(void) nvlist_remove_all(createprops,
1831 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
1832 		(void) nvlist_lookup_uint64(createprops,
1833 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
1834 		(void) nvlist_remove_all(createprops,
1835 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1836 		(void) nvlist_lookup_uint64(createprops,
1837 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
1838 		(void) nvlist_remove_all(createprops,
1839 		    zfs_prop_to_name(ZFS_PROP_CASE));
1840 	}
1841 
1842 	/*
1843 	 * If the file system or pool is version is too "young" to
1844 	 * support normalization and the creator tried to set a value
1845 	 * for one of the props, error out.  We only need check the
1846 	 * ZPL version because we've already checked by now that the
1847 	 * SPA version is compatible with the selected ZPL version.
1848 	 */
1849 	if (zplver < ZPL_VERSION_NORMALIZATION &&
1850 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
1851 	    sense != ZFS_PROP_UNDEFINED))
1852 		return (ENOTSUP);
1853 
1854 	/*
1855 	 * Put the version in the zplprops
1856 	 */
1857 	VERIFY(nvlist_add_uint64(zplprops,
1858 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
1859 
1860 	/*
1861 	 * Open parent object set so we can inherit zplprop values if
1862 	 * necessary.
1863 	 */
1864 	if ((error = zfs_os_open_retry(parentname, &os)) != 0)
1865 		return (error);
1866 
1867 	if (norm == ZFS_PROP_UNDEFINED)
1868 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
1869 	VERIFY(nvlist_add_uint64(zplprops,
1870 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
1871 
1872 	/*
1873 	 * If we're normalizing, names must always be valid UTF-8 strings.
1874 	 */
1875 	if (norm)
1876 		u8 = 1;
1877 	if (u8 == ZFS_PROP_UNDEFINED)
1878 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
1879 	VERIFY(nvlist_add_uint64(zplprops,
1880 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
1881 
1882 	if (sense == ZFS_PROP_UNDEFINED)
1883 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
1884 	VERIFY(nvlist_add_uint64(zplprops,
1885 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
1886 
1887 	dmu_objset_close(os);
1888 	return (0);
1889 }
1890 
1891 /*
1892  * inputs:
1893  * zc_objset_type	type of objset to create (fs vs zvol)
1894  * zc_name		name of new objset
1895  * zc_value		name of snapshot to clone from (may be empty)
1896  * zc_nvlist_src{_size}	nvlist of properties to apply
1897  *
1898  * outputs: none
1899  */
1900 static int
1901 zfs_ioc_create(zfs_cmd_t *zc)
1902 {
1903 	objset_t *clone;
1904 	int error = 0;
1905 	zfs_creat_t zct;
1906 	nvlist_t *nvprops = NULL;
1907 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
1908 	dmu_objset_type_t type = zc->zc_objset_type;
1909 
1910 	switch (type) {
1911 
1912 	case DMU_OST_ZFS:
1913 		cbfunc = zfs_create_cb;
1914 		break;
1915 
1916 	case DMU_OST_ZVOL:
1917 		cbfunc = zvol_create_cb;
1918 		break;
1919 
1920 	default:
1921 		cbfunc = NULL;
1922 	}
1923 	if (strchr(zc->zc_name, '@') ||
1924 	    strchr(zc->zc_name, '%'))
1925 		return (EINVAL);
1926 
1927 	if (zc->zc_nvlist_src != NULL &&
1928 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1929 	    &nvprops)) != 0)
1930 		return (error);
1931 
1932 	zct.zct_zplprops = NULL;
1933 	zct.zct_props = nvprops;
1934 
1935 	if (zc->zc_value[0] != '\0') {
1936 		/*
1937 		 * We're creating a clone of an existing snapshot.
1938 		 */
1939 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1940 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1941 			nvlist_free(nvprops);
1942 			return (EINVAL);
1943 		}
1944 
1945 		error = dmu_objset_open(zc->zc_value, type,
1946 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1947 		if (error) {
1948 			nvlist_free(nvprops);
1949 			return (error);
1950 		}
1951 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1952 		if (error) {
1953 			dmu_objset_close(clone);
1954 			nvlist_free(nvprops);
1955 			return (error);
1956 		}
1957 		dmu_objset_close(clone);
1958 	} else {
1959 		if (cbfunc == NULL) {
1960 			nvlist_free(nvprops);
1961 			return (EINVAL);
1962 		}
1963 
1964 		if (type == DMU_OST_ZVOL) {
1965 			uint64_t volsize, volblocksize;
1966 
1967 			if (nvprops == NULL ||
1968 			    nvlist_lookup_uint64(nvprops,
1969 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1970 			    &volsize) != 0) {
1971 				nvlist_free(nvprops);
1972 				return (EINVAL);
1973 			}
1974 
1975 			if ((error = nvlist_lookup_uint64(nvprops,
1976 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1977 			    &volblocksize)) != 0 && error != ENOENT) {
1978 				nvlist_free(nvprops);
1979 				return (EINVAL);
1980 			}
1981 
1982 			if (error != 0)
1983 				volblocksize = zfs_prop_default_numeric(
1984 				    ZFS_PROP_VOLBLOCKSIZE);
1985 
1986 			if ((error = zvol_check_volblocksize(
1987 			    volblocksize)) != 0 ||
1988 			    (error = zvol_check_volsize(volsize,
1989 			    volblocksize)) != 0) {
1990 				nvlist_free(nvprops);
1991 				return (error);
1992 			}
1993 		} else if (type == DMU_OST_ZFS) {
1994 			uint64_t version;
1995 			int error;
1996 
1997 			/*
1998 			 * Default ZPL version to non-FUID capable if the
1999 			 * pool is not upgraded to support FUIDs.
2000 			 */
2001 			if (zfs_check_version(zc->zc_name, SPA_VERSION_FUID))
2002 				version = ZPL_VERSION_FUID - 1;
2003 			else
2004 				version = ZPL_VERSION;
2005 
2006 			/*
2007 			 * Potentially override default ZPL version based
2008 			 * on creator's request.
2009 			 */
2010 			(void) nvlist_lookup_uint64(nvprops,
2011 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
2012 
2013 			/*
2014 			 * Make sure version we ended up with is kosher
2015 			 */
2016 			if ((version < ZPL_VERSION_INITIAL ||
2017 			    version > ZPL_VERSION) ||
2018 			    (version >= ZPL_VERSION_FUID &&
2019 			    zfs_check_version(zc->zc_name, SPA_VERSION_FUID))) {
2020 				nvlist_free(nvprops);
2021 				return (ENOTSUP);
2022 			}
2023 
2024 			/*
2025 			 * We have to have normalization and
2026 			 * case-folding flags correct when we do the
2027 			 * file system creation, so go figure them out
2028 			 * now.
2029 			 */
2030 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2031 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2032 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
2033 			    zct.zct_zplprops, version);
2034 			if (error != 0) {
2035 				nvlist_free(nvprops);
2036 				nvlist_free(zct.zct_zplprops);
2037 				return (error);
2038 			}
2039 		}
2040 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
2041 		    &zct);
2042 		nvlist_free(zct.zct_zplprops);
2043 	}
2044 
2045 	/*
2046 	 * It would be nice to do this atomically.
2047 	 */
2048 	if (error == 0) {
2049 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2050 			(void) dmu_objset_destroy(zc->zc_name);
2051 	}
2052 
2053 	nvlist_free(nvprops);
2054 	return (error);
2055 }
2056 
2057 /*
2058  * inputs:
2059  * zc_name	name of filesystem
2060  * zc_value	short name of snapshot
2061  * zc_cookie	recursive flag
2062  *
2063  * outputs:	none
2064  */
2065 static int
2066 zfs_ioc_snapshot(zfs_cmd_t *zc)
2067 {
2068 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2069 		return (EINVAL);
2070 	return (dmu_objset_snapshot(zc->zc_name,
2071 	    zc->zc_value, zc->zc_cookie));
2072 }
2073 
2074 int
2075 zfs_unmount_snap(char *name, void *arg)
2076 {
2077 	char *snapname = arg;
2078 	char *cp;
2079 	vfs_t *vfsp = NULL;
2080 
2081 	/*
2082 	 * Snapshots (which are under .zfs control) must be unmounted
2083 	 * before they can be destroyed.
2084 	 */
2085 
2086 	if (snapname) {
2087 		(void) strcat(name, "@");
2088 		(void) strcat(name, snapname);
2089 		vfsp = zfs_get_vfs(name);
2090 		cp = strchr(name, '@');
2091 		*cp = '\0';
2092 	} else if (strchr(name, '@')) {
2093 		vfsp = zfs_get_vfs(name);
2094 	}
2095 
2096 	if (vfsp) {
2097 		/*
2098 		 * Always force the unmount for snapshots.
2099 		 */
2100 		int flag = MS_FORCE;
2101 		int err;
2102 
2103 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2104 			VFS_RELE(vfsp);
2105 			return (err);
2106 		}
2107 		VFS_RELE(vfsp);
2108 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
2109 			return (err);
2110 	}
2111 	return (0);
2112 }
2113 
2114 /*
2115  * inputs:
2116  * zc_name	name of filesystem
2117  * zc_value	short name of snapshot
2118  *
2119  * outputs:	none
2120  */
2121 static int
2122 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2123 {
2124 	int err;
2125 
2126 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2127 		return (EINVAL);
2128 	err = dmu_objset_find(zc->zc_name,
2129 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2130 	if (err)
2131 		return (err);
2132 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2133 }
2134 
2135 /*
2136  * inputs:
2137  * zc_name		name of dataset to destroy
2138  * zc_objset_type	type of objset
2139  *
2140  * outputs:		none
2141  */
2142 static int
2143 zfs_ioc_destroy(zfs_cmd_t *zc)
2144 {
2145 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2146 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2147 		if (err)
2148 			return (err);
2149 	}
2150 
2151 	return (dmu_objset_destroy(zc->zc_name));
2152 }
2153 
2154 /*
2155  * inputs:
2156  * zc_name	name of dataset to rollback (to most recent snapshot)
2157  *
2158  * outputs:	none
2159  */
2160 static int
2161 zfs_ioc_rollback(zfs_cmd_t *zc)
2162 {
2163 	objset_t *os;
2164 	int error;
2165 	zfsvfs_t *zfsvfs = NULL;
2166 
2167 	/*
2168 	 * Get the zfsvfs for the receiving objset. There
2169 	 * won't be one if we're operating on a zvol, if the
2170 	 * objset doesn't exist yet, or is not mounted.
2171 	 */
2172 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2173 	    DS_MODE_STANDARD, &os);
2174 	if (error)
2175 		return (error);
2176 
2177 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
2178 		mutex_enter(&os->os->os_user_ptr_lock);
2179 		zfsvfs = dmu_objset_get_user(os);
2180 		if (zfsvfs != NULL)
2181 			VFS_HOLD(zfsvfs->z_vfs);
2182 		mutex_exit(&os->os->os_user_ptr_lock);
2183 	}
2184 
2185 	if (zfsvfs != NULL) {
2186 		char osname[MAXNAMELEN];
2187 		int mode;
2188 
2189 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
2190 		if (error == 0) {
2191 			int resume_err;
2192 
2193 			ASSERT(strcmp(osname, zc->zc_name) == 0);
2194 			error = dmu_objset_rollback(os);
2195 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2196 			error = error ? error : resume_err;
2197 		} else {
2198 			dmu_objset_close(os);
2199 		}
2200 		VFS_RELE(zfsvfs->z_vfs);
2201 	} else {
2202 		error = dmu_objset_rollback(os);
2203 	}
2204 	/* Note, the dmu_objset_rollback() closes the objset for us. */
2205 
2206 	return (error);
2207 }
2208 
2209 /*
2210  * inputs:
2211  * zc_name	old name of dataset
2212  * zc_value	new name of dataset
2213  * zc_cookie	recursive flag (only valid for snapshots)
2214  *
2215  * outputs:	none
2216  */
2217 static int
2218 zfs_ioc_rename(zfs_cmd_t *zc)
2219 {
2220 	boolean_t recursive = zc->zc_cookie & 1;
2221 
2222 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2223 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2224 	    strchr(zc->zc_value, '%'))
2225 		return (EINVAL);
2226 
2227 	/*
2228 	 * Unmount snapshot unless we're doing a recursive rename,
2229 	 * in which case the dataset code figures out which snapshots
2230 	 * to unmount.
2231 	 */
2232 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2233 	    zc->zc_objset_type == DMU_OST_ZFS) {
2234 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2235 		if (err)
2236 			return (err);
2237 	}
2238 
2239 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2240 }
2241 
2242 /*
2243  * inputs:
2244  * zc_name		name of containing filesystem
2245  * zc_nvlist_src{_size}	nvlist of properties to apply
2246  * zc_value		name of snapshot to create
2247  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
2248  * zc_cookie		file descriptor to recv from
2249  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
2250  * zc_guid		force flag
2251  *
2252  * outputs:
2253  * zc_cookie		number of bytes read
2254  */
2255 static int
2256 zfs_ioc_recv(zfs_cmd_t *zc)
2257 {
2258 	file_t *fp;
2259 	objset_t *os;
2260 	dmu_recv_cookie_t drc;
2261 	zfsvfs_t *zfsvfs = NULL;
2262 	boolean_t force = (boolean_t)zc->zc_guid;
2263 	int error, fd;
2264 	offset_t off;
2265 	nvlist_t *props = NULL;
2266 	objset_t *origin = NULL;
2267 	char *tosnap;
2268 	char tofs[ZFS_MAXNAMELEN];
2269 
2270 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2271 	    strchr(zc->zc_value, '@') == NULL ||
2272 	    strchr(zc->zc_value, '%'))
2273 		return (EINVAL);
2274 
2275 	(void) strcpy(tofs, zc->zc_value);
2276 	tosnap = strchr(tofs, '@');
2277 	*tosnap = '\0';
2278 	tosnap++;
2279 
2280 	if (zc->zc_nvlist_src != NULL &&
2281 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2282 	    &props)) != 0)
2283 		return (error);
2284 
2285 	fd = zc->zc_cookie;
2286 	fp = getf(fd);
2287 	if (fp == NULL) {
2288 		nvlist_free(props);
2289 		return (EBADF);
2290 	}
2291 
2292 	/*
2293 	 * Get the zfsvfs for the receiving objset. There
2294 	 * won't be one if we're operating on a zvol, if the
2295 	 * objset doesn't exist yet, or is not mounted.
2296 	 */
2297 
2298 	error = dmu_objset_open(tofs, DMU_OST_ZFS,
2299 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
2300 	if (!error) {
2301 		mutex_enter(&os->os->os_user_ptr_lock);
2302 		zfsvfs = dmu_objset_get_user(os);
2303 		if (zfsvfs != NULL) {
2304 			VFS_HOLD(zfsvfs->z_vfs);
2305 			mutex_exit(&os->os->os_user_ptr_lock);
2306 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2307 				VFS_RELE(zfsvfs->z_vfs);
2308 				dmu_objset_close(os);
2309 				nvlist_free(props);
2310 				releasef(fd);
2311 				return (EBUSY);
2312 			}
2313 		} else {
2314 			mutex_exit(&os->os->os_user_ptr_lock);
2315 		}
2316 		dmu_objset_close(os);
2317 	}
2318 
2319 	if (zc->zc_string[0]) {
2320 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2321 		    DS_MODE_STANDARD | DS_MODE_READONLY, &origin);
2322 		if (error) {
2323 			if (zfsvfs != NULL) {
2324 				mutex_exit(&zfsvfs->z_online_recv_lock);
2325 				VFS_RELE(zfsvfs->z_vfs);
2326 			}
2327 			nvlist_free(props);
2328 			releasef(fd);
2329 			return (error);
2330 		}
2331 	}
2332 
2333 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2334 	    force, origin, zfsvfs != NULL, &drc);
2335 	if (origin)
2336 		dmu_objset_close(origin);
2337 	if (error) {
2338 		if (zfsvfs != NULL) {
2339 			mutex_exit(&zfsvfs->z_online_recv_lock);
2340 			VFS_RELE(zfsvfs->z_vfs);
2341 		}
2342 		nvlist_free(props);
2343 		releasef(fd);
2344 		return (error);
2345 	}
2346 
2347 	/*
2348 	 * If properties are supplied, they are to completely replace
2349 	 * the existing ones; "inherit" any existing properties.
2350 	 */
2351 	if (props) {
2352 		objset_t *os;
2353 		nvlist_t *nv = NULL;
2354 
2355 		error = dmu_objset_open(tofs, DMU_OST_ANY,
2356 		    DS_MODE_STANDARD | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
2357 		    &os);
2358 		if (error == 0) {
2359 			error = dsl_prop_get_all(os, &nv);
2360 			dmu_objset_close(os);
2361 		}
2362 		if (error == 0) {
2363 			nvpair_t *elem;
2364 			zfs_cmd_t *zc2;
2365 			zc2 = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2366 
2367 			(void) strcpy(zc2->zc_name, tofs);
2368 			for (elem = nvlist_next_nvpair(nv, NULL); elem;
2369 			    elem = nvlist_next_nvpair(nv, elem)) {
2370 				(void) strcpy(zc2->zc_value, nvpair_name(elem));
2371 				if (zfs_secpolicy_inherit(zc2, CRED()) == 0)
2372 					(void) zfs_ioc_inherit_prop(zc2);
2373 			}
2374 			kmem_free(zc2, sizeof (zfs_cmd_t));
2375 		}
2376 		if (nv)
2377 			nvlist_free(nv);
2378 	}
2379 
2380 	/*
2381 	 * Set properties.  Note, we ignore errors.  Would be better to
2382 	 * do best-effort in zfs_set_prop_nvlist, too.
2383 	 */
2384 	(void) zfs_set_prop_nvlist(tofs, props);
2385 	nvlist_free(props);
2386 
2387 	off = fp->f_offset;
2388 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
2389 
2390 	if (error == 0) {
2391 		if (zfsvfs != NULL) {
2392 			char osname[MAXNAMELEN];
2393 			int mode;
2394 
2395 			error = zfs_suspend_fs(zfsvfs, osname, &mode);
2396 			if (error == 0) {
2397 				int resume_err;
2398 
2399 				error = dmu_recv_end(&drc);
2400 				resume_err = zfs_resume_fs(zfsvfs,
2401 				    osname, mode);
2402 				error = error ? error : resume_err;
2403 			} else {
2404 				dmu_recv_abort_cleanup(&drc);
2405 			}
2406 		} else {
2407 			error = dmu_recv_end(&drc);
2408 		}
2409 	}
2410 	if (zfsvfs != NULL) {
2411 		mutex_exit(&zfsvfs->z_online_recv_lock);
2412 		VFS_RELE(zfsvfs->z_vfs);
2413 	}
2414 
2415 	zc->zc_cookie = off - fp->f_offset;
2416 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2417 		fp->f_offset = off;
2418 
2419 	releasef(fd);
2420 	return (error);
2421 }
2422 
2423 /*
2424  * inputs:
2425  * zc_name	name of snapshot to send
2426  * zc_value	short name of incremental fromsnap (may be empty)
2427  * zc_cookie	file descriptor to send stream to
2428  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
2429  *
2430  * outputs: none
2431  */
2432 static int
2433 zfs_ioc_send(zfs_cmd_t *zc)
2434 {
2435 	objset_t *fromsnap = NULL;
2436 	objset_t *tosnap;
2437 	file_t *fp;
2438 	int error;
2439 	offset_t off;
2440 
2441 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2442 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
2443 	if (error)
2444 		return (error);
2445 
2446 	if (zc->zc_value[0] != '\0') {
2447 		char buf[MAXPATHLEN];
2448 		char *cp;
2449 
2450 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
2451 		cp = strchr(buf, '@');
2452 		if (cp)
2453 			*(cp+1) = 0;
2454 		(void) strncat(buf, zc->zc_value, sizeof (buf));
2455 		error = dmu_objset_open(buf, DMU_OST_ANY,
2456 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
2457 		if (error) {
2458 			dmu_objset_close(tosnap);
2459 			return (error);
2460 		}
2461 	}
2462 
2463 	fp = getf(zc->zc_cookie);
2464 	if (fp == NULL) {
2465 		dmu_objset_close(tosnap);
2466 		if (fromsnap)
2467 			dmu_objset_close(fromsnap);
2468 		return (EBADF);
2469 	}
2470 
2471 	off = fp->f_offset;
2472 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
2473 
2474 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2475 		fp->f_offset = off;
2476 	releasef(zc->zc_cookie);
2477 	if (fromsnap)
2478 		dmu_objset_close(fromsnap);
2479 	dmu_objset_close(tosnap);
2480 	return (error);
2481 }
2482 
2483 static int
2484 zfs_ioc_inject_fault(zfs_cmd_t *zc)
2485 {
2486 	int id, error;
2487 
2488 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2489 	    &zc->zc_inject_record);
2490 
2491 	if (error == 0)
2492 		zc->zc_guid = (uint64_t)id;
2493 
2494 	return (error);
2495 }
2496 
2497 static int
2498 zfs_ioc_clear_fault(zfs_cmd_t *zc)
2499 {
2500 	return (zio_clear_fault((int)zc->zc_guid));
2501 }
2502 
2503 static int
2504 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2505 {
2506 	int id = (int)zc->zc_guid;
2507 	int error;
2508 
2509 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2510 	    &zc->zc_inject_record);
2511 
2512 	zc->zc_guid = id;
2513 
2514 	return (error);
2515 }
2516 
2517 static int
2518 zfs_ioc_error_log(zfs_cmd_t *zc)
2519 {
2520 	spa_t *spa;
2521 	int error;
2522 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2523 
2524 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2525 		return (error);
2526 
2527 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2528 	    &count);
2529 	if (error == 0)
2530 		zc->zc_nvlist_dst_size = count;
2531 	else
2532 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2533 
2534 	spa_close(spa, FTAG);
2535 
2536 	return (error);
2537 }
2538 
2539 static int
2540 zfs_ioc_clear(zfs_cmd_t *zc)
2541 {
2542 	spa_t *spa;
2543 	vdev_t *vd;
2544 	uint64_t txg;
2545 	int error;
2546 
2547 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2548 		return (error);
2549 
2550 	/*
2551 	 * Try to resume any I/Os which may have been suspended
2552 	 * as a result of a complete pool failure.
2553 	 */
2554 	if (!list_is_empty(&spa->spa_zio_list)) {
2555 		if (zio_vdev_resume_io(spa) != 0) {
2556 			spa_close(spa, FTAG);
2557 			return (EIO);
2558 		}
2559 	}
2560 
2561 	txg = spa_vdev_enter(spa);
2562 
2563 	if (zc->zc_guid == 0) {
2564 		vd = NULL;
2565 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
2566 		spa_aux_vdev_t *sav;
2567 		int i;
2568 
2569 		/*
2570 		 * Check if this is an l2cache device.
2571 		 */
2572 		ASSERT(spa != NULL);
2573 		sav = &spa->spa_l2cache;
2574 		for (i = 0; i < sav->sav_count; i++) {
2575 			if (sav->sav_vdevs[i]->vdev_guid == zc->zc_guid) {
2576 				vd = sav->sav_vdevs[i];
2577 				break;
2578 			}
2579 		}
2580 
2581 		if (vd == NULL) {
2582 			(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
2583 			spa_close(spa, FTAG);
2584 			return (ENODEV);
2585 		}
2586 	}
2587 
2588 	vdev_clear(spa, vd, B_TRUE);
2589 
2590 	(void) spa_vdev_exit(spa, NULL, txg, 0);
2591 
2592 	spa_close(spa, FTAG);
2593 
2594 	return (0);
2595 }
2596 
2597 /*
2598  * inputs:
2599  * zc_name	name of filesystem
2600  * zc_value	name of origin snapshot
2601  *
2602  * outputs:	none
2603  */
2604 static int
2605 zfs_ioc_promote(zfs_cmd_t *zc)
2606 {
2607 	char *cp;
2608 
2609 	/*
2610 	 * We don't need to unmount *all* the origin fs's snapshots, but
2611 	 * it's easier.
2612 	 */
2613 	cp = strchr(zc->zc_value, '@');
2614 	if (cp)
2615 		*cp = '\0';
2616 	(void) dmu_objset_find(zc->zc_value,
2617 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2618 	return (dsl_dataset_promote(zc->zc_name));
2619 }
2620 
2621 /*
2622  * We don't want to have a hard dependency
2623  * against some special symbols in sharefs
2624  * nfs, and smbsrv.  Determine them if needed when
2625  * the first file system is shared.
2626  * Neither sharefs, nfs or smbsrv are unloadable modules.
2627  */
2628 int (*znfsexport_fs)(void *arg);
2629 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2630 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2631 
2632 int zfs_nfsshare_inited;
2633 int zfs_smbshare_inited;
2634 
2635 ddi_modhandle_t nfs_mod;
2636 ddi_modhandle_t sharefs_mod;
2637 ddi_modhandle_t smbsrv_mod;
2638 kmutex_t zfs_share_lock;
2639 
2640 static int
2641 zfs_init_sharefs()
2642 {
2643 	int error;
2644 
2645 	ASSERT(MUTEX_HELD(&zfs_share_lock));
2646 	/* Both NFS and SMB shares also require sharetab support. */
2647 	if (sharefs_mod == NULL && ((sharefs_mod =
2648 	    ddi_modopen("fs/sharefs",
2649 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
2650 		return (ENOSYS);
2651 	}
2652 	if (zshare_fs == NULL && ((zshare_fs =
2653 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2654 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2655 		return (ENOSYS);
2656 	}
2657 	return (0);
2658 }
2659 
2660 static int
2661 zfs_ioc_share(zfs_cmd_t *zc)
2662 {
2663 	int error;
2664 	int opcode;
2665 
2666 	switch (zc->zc_share.z_sharetype) {
2667 	case ZFS_SHARE_NFS:
2668 	case ZFS_UNSHARE_NFS:
2669 		if (zfs_nfsshare_inited == 0) {
2670 			mutex_enter(&zfs_share_lock);
2671 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2672 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2673 				mutex_exit(&zfs_share_lock);
2674 				return (ENOSYS);
2675 			}
2676 			if (znfsexport_fs == NULL &&
2677 			    ((znfsexport_fs = (int (*)(void *))
2678 			    ddi_modsym(nfs_mod,
2679 			    "nfs_export", &error)) == NULL)) {
2680 				mutex_exit(&zfs_share_lock);
2681 				return (ENOSYS);
2682 			}
2683 			error = zfs_init_sharefs();
2684 			if (error) {
2685 				mutex_exit(&zfs_share_lock);
2686 				return (ENOSYS);
2687 			}
2688 			zfs_nfsshare_inited = 1;
2689 			mutex_exit(&zfs_share_lock);
2690 		}
2691 		break;
2692 	case ZFS_SHARE_SMB:
2693 	case ZFS_UNSHARE_SMB:
2694 		if (zfs_smbshare_inited == 0) {
2695 			mutex_enter(&zfs_share_lock);
2696 			if (smbsrv_mod == NULL && ((smbsrv_mod =
2697 			    ddi_modopen("drv/smbsrv",
2698 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2699 				mutex_exit(&zfs_share_lock);
2700 				return (ENOSYS);
2701 			}
2702 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2703 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2704 			    "smb_server_share", &error)) == NULL)) {
2705 				mutex_exit(&zfs_share_lock);
2706 				return (ENOSYS);
2707 			}
2708 			error = zfs_init_sharefs();
2709 			if (error) {
2710 				mutex_exit(&zfs_share_lock);
2711 				return (ENOSYS);
2712 			}
2713 			zfs_smbshare_inited = 1;
2714 			mutex_exit(&zfs_share_lock);
2715 		}
2716 		break;
2717 	default:
2718 		return (EINVAL);
2719 	}
2720 
2721 	switch (zc->zc_share.z_sharetype) {
2722 	case ZFS_SHARE_NFS:
2723 	case ZFS_UNSHARE_NFS:
2724 		if (error =
2725 		    znfsexport_fs((void *)
2726 		    (uintptr_t)zc->zc_share.z_exportdata))
2727 			return (error);
2728 		break;
2729 	case ZFS_SHARE_SMB:
2730 	case ZFS_UNSHARE_SMB:
2731 		if (error = zsmbexport_fs((void *)
2732 		    (uintptr_t)zc->zc_share.z_exportdata,
2733 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2734 		    B_TRUE : B_FALSE)) {
2735 			return (error);
2736 		}
2737 		break;
2738 	}
2739 
2740 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2741 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2742 	    SHAREFS_ADD : SHAREFS_REMOVE;
2743 
2744 	/*
2745 	 * Add or remove share from sharetab
2746 	 */
2747 	error = zshare_fs(opcode,
2748 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2749 	    zc->zc_share.z_sharemax);
2750 
2751 	return (error);
2752 
2753 }
2754 
2755 /*
2756  * pool create, destroy, and export don't log the history as part of
2757  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2758  * do the logging of those commands.
2759  */
2760 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2761 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2762 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2763 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2764 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2765 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
2766 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2767 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2768 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2769 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2770 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
2771 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2772 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2773 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2774 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2775 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2776 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2777 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2778 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2779 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2780 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2781 	    DATASET_NAME, B_FALSE },
2782 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2783 	    DATASET_NAME, B_FALSE },
2784 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2785 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2786 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2787 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2788 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2789 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2790 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
2791 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2792 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2793 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
2794 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2795 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2796 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2797 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2798 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2799 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
2800 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2801 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2802 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2803 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2804 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2805 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2806 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2807 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2808 	    DATASET_NAME, B_FALSE },
2809 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2810 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2811 };
2812 
2813 static int
2814 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2815 {
2816 	zfs_cmd_t *zc;
2817 	uint_t vec;
2818 	int error, rc;
2819 
2820 	if (getminor(dev) != 0)
2821 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2822 
2823 	vec = cmd - ZFS_IOC;
2824 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
2825 
2826 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2827 		return (EINVAL);
2828 
2829 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2830 
2831 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2832 
2833 	if (error == 0)
2834 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2835 
2836 	/*
2837 	 * Ensure that all pool/dataset names are valid before we pass down to
2838 	 * the lower layers.
2839 	 */
2840 	if (error == 0) {
2841 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2842 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
2843 		case POOL_NAME:
2844 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2845 				error = EINVAL;
2846 			break;
2847 
2848 		case DATASET_NAME:
2849 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2850 				error = EINVAL;
2851 			break;
2852 
2853 		case NO_NAME:
2854 			break;
2855 		}
2856 	}
2857 
2858 	if (error == 0)
2859 		error = zfs_ioc_vec[vec].zvec_func(zc);
2860 
2861 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
2862 	if (error == 0) {
2863 		error = rc;
2864 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
2865 			zfs_log_history(zc);
2866 	}
2867 
2868 	kmem_free(zc, sizeof (zfs_cmd_t));
2869 	return (error);
2870 }
2871 
2872 static int
2873 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2874 {
2875 	if (cmd != DDI_ATTACH)
2876 		return (DDI_FAILURE);
2877 
2878 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
2879 	    DDI_PSEUDO, 0) == DDI_FAILURE)
2880 		return (DDI_FAILURE);
2881 
2882 	zfs_dip = dip;
2883 
2884 	ddi_report_dev(dip);
2885 
2886 	return (DDI_SUCCESS);
2887 }
2888 
2889 static int
2890 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2891 {
2892 	if (spa_busy() || zfs_busy() || zvol_busy())
2893 		return (DDI_FAILURE);
2894 
2895 	if (cmd != DDI_DETACH)
2896 		return (DDI_FAILURE);
2897 
2898 	zfs_dip = NULL;
2899 
2900 	ddi_prop_remove_all(dip);
2901 	ddi_remove_minor_node(dip, NULL);
2902 
2903 	return (DDI_SUCCESS);
2904 }
2905 
2906 /*ARGSUSED*/
2907 static int
2908 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2909 {
2910 	switch (infocmd) {
2911 	case DDI_INFO_DEVT2DEVINFO:
2912 		*result = zfs_dip;
2913 		return (DDI_SUCCESS);
2914 
2915 	case DDI_INFO_DEVT2INSTANCE:
2916 		*result = (void *)0;
2917 		return (DDI_SUCCESS);
2918 	}
2919 
2920 	return (DDI_FAILURE);
2921 }
2922 
2923 /*
2924  * OK, so this is a little weird.
2925  *
2926  * /dev/zfs is the control node, i.e. minor 0.
2927  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2928  *
2929  * /dev/zfs has basically nothing to do except serve up ioctls,
2930  * so most of the standard driver entry points are in zvol.c.
2931  */
2932 static struct cb_ops zfs_cb_ops = {
2933 	zvol_open,	/* open */
2934 	zvol_close,	/* close */
2935 	zvol_strategy,	/* strategy */
2936 	nodev,		/* print */
2937 	nodev,		/* dump */
2938 	zvol_read,	/* read */
2939 	zvol_write,	/* write */
2940 	zfsdev_ioctl,	/* ioctl */
2941 	nodev,		/* devmap */
2942 	nodev,		/* mmap */
2943 	nodev,		/* segmap */
2944 	nochpoll,	/* poll */
2945 	ddi_prop_op,	/* prop_op */
2946 	NULL,		/* streamtab */
2947 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
2948 	CB_REV,		/* version */
2949 	nodev,		/* async read */
2950 	nodev,		/* async write */
2951 };
2952 
2953 static struct dev_ops zfs_dev_ops = {
2954 	DEVO_REV,	/* version */
2955 	0,		/* refcnt */
2956 	zfs_info,	/* info */
2957 	nulldev,	/* identify */
2958 	nulldev,	/* probe */
2959 	zfs_attach,	/* attach */
2960 	zfs_detach,	/* detach */
2961 	nodev,		/* reset */
2962 	&zfs_cb_ops,	/* driver operations */
2963 	NULL		/* no bus operations */
2964 };
2965 
2966 static struct modldrv zfs_modldrv = {
2967 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
2968 	    &zfs_dev_ops
2969 };
2970 
2971 static struct modlinkage modlinkage = {
2972 	MODREV_1,
2973 	(void *)&zfs_modlfs,
2974 	(void *)&zfs_modldrv,
2975 	NULL
2976 };
2977 
2978 
2979 uint_t zfs_fsyncer_key;
2980 extern uint_t rrw_tsd_key;
2981 
2982 int
2983 _init(void)
2984 {
2985 	int error;
2986 
2987 	spa_init(FREAD | FWRITE);
2988 	zfs_init();
2989 	zvol_init();
2990 
2991 	if ((error = mod_install(&modlinkage)) != 0) {
2992 		zvol_fini();
2993 		zfs_fini();
2994 		spa_fini();
2995 		return (error);
2996 	}
2997 
2998 	tsd_create(&zfs_fsyncer_key, NULL);
2999 	tsd_create(&rrw_tsd_key, NULL);
3000 
3001 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3002 	ASSERT(error == 0);
3003 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3004 
3005 	return (0);
3006 }
3007 
3008 int
3009 _fini(void)
3010 {
3011 	int error;
3012 
3013 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3014 		return (EBUSY);
3015 
3016 	if ((error = mod_remove(&modlinkage)) != 0)
3017 		return (error);
3018 
3019 	zvol_fini();
3020 	zfs_fini();
3021 	spa_fini();
3022 	if (zfs_nfsshare_inited)
3023 		(void) ddi_modclose(nfs_mod);
3024 	if (zfs_smbshare_inited)
3025 		(void) ddi_modclose(smbsrv_mod);
3026 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
3027 		(void) ddi_modclose(sharefs_mod);
3028 
3029 	tsd_destroy(&zfs_fsyncer_key);
3030 	ldi_ident_release(zfs_li);
3031 	zfs_li = NULL;
3032 	mutex_destroy(&zfs_share_lock);
3033 
3034 	return (error);
3035 }
3036 
3037 int
3038 _info(struct modinfo *modinfop)
3039 {
3040 	return (mod_info(&modlinkage, modinfop));
3041 }
3042