xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
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, **spares;
988 	uint_t nl2cache = 0, nspares = 0;
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 	(void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1000 	    &spares, &nspares);
1001 
1002 	/*
1003 	 * A root pool with concatenated devices is not supported.
1004 	 * Thus, can not add a device to a root pool.
1005 	 *
1006 	 * Intent log device can not be added to a rootpool because
1007 	 * during mountroot, zil is replayed, a seperated log device
1008 	 * can not be accessed during the mountroot time.
1009 	 *
1010 	 * l2cache and spare devices are ok to be added to a rootpool.
1011 	 */
1012 	if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1013 		spa_close(spa, FTAG);
1014 		return (EDOM);
1015 	}
1016 
1017 	if (error == 0) {
1018 		error = spa_vdev_add(spa, config);
1019 		nvlist_free(config);
1020 	}
1021 	spa_close(spa, FTAG);
1022 	return (error);
1023 }
1024 
1025 static int
1026 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1027 {
1028 	spa_t *spa;
1029 	int error;
1030 
1031 	error = spa_open(zc->zc_name, &spa, FTAG);
1032 	if (error != 0)
1033 		return (error);
1034 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1035 	spa_close(spa, FTAG);
1036 	return (error);
1037 }
1038 
1039 static int
1040 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1041 {
1042 	spa_t *spa;
1043 	int error;
1044 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1045 
1046 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1047 		return (error);
1048 	switch (zc->zc_cookie) {
1049 	case VDEV_STATE_ONLINE:
1050 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1051 		break;
1052 
1053 	case VDEV_STATE_OFFLINE:
1054 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1055 		break;
1056 
1057 	case VDEV_STATE_FAULTED:
1058 		error = vdev_fault(spa, zc->zc_guid);
1059 		break;
1060 
1061 	case VDEV_STATE_DEGRADED:
1062 		error = vdev_degrade(spa, zc->zc_guid);
1063 		break;
1064 
1065 	default:
1066 		error = EINVAL;
1067 	}
1068 	zc->zc_cookie = newstate;
1069 	spa_close(spa, FTAG);
1070 	return (error);
1071 }
1072 
1073 static int
1074 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1075 {
1076 	spa_t *spa;
1077 	int replacing = zc->zc_cookie;
1078 	nvlist_t *config;
1079 	int error;
1080 
1081 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1082 		return (error);
1083 
1084 	if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1085 	    &config)) == 0) {
1086 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1087 		nvlist_free(config);
1088 	}
1089 
1090 	spa_close(spa, FTAG);
1091 	return (error);
1092 }
1093 
1094 static int
1095 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1096 {
1097 	spa_t *spa;
1098 	int error;
1099 
1100 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1101 		return (error);
1102 
1103 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1104 
1105 	spa_close(spa, FTAG);
1106 	return (error);
1107 }
1108 
1109 static int
1110 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1111 {
1112 	spa_t *spa;
1113 	char *path = zc->zc_value;
1114 	uint64_t guid = zc->zc_guid;
1115 	int error;
1116 
1117 	error = spa_open(zc->zc_name, &spa, FTAG);
1118 	if (error != 0)
1119 		return (error);
1120 
1121 	error = spa_vdev_setpath(spa, guid, path);
1122 	spa_close(spa, FTAG);
1123 	return (error);
1124 }
1125 
1126 static int
1127 zfs_os_open_retry(char *name, objset_t **os)
1128 {
1129 	int error;
1130 
1131 retry:
1132 	error = dmu_objset_open(name, DMU_OST_ANY,
1133 	    DS_MODE_STANDARD | DS_MODE_READONLY, os);
1134 	if (error != 0) {
1135 		/*
1136 		 * This is ugly: dmu_objset_open() can return EBUSY if
1137 		 * the objset is held exclusively. Fortunately this hold is
1138 		 * only for a short while, so we retry here.
1139 		 * This avoids user code having to handle EBUSY,
1140 		 * for example for a "zfs list".
1141 		 */
1142 		if (error == EBUSY) {
1143 			delay(1);
1144 			goto retry;
1145 		}
1146 	}
1147 	return (error);
1148 }
1149 
1150 /*
1151  * inputs:
1152  * zc_name		name of filesystem
1153  * zc_nvlist_dst_size	size of buffer for property nvlist
1154  *
1155  * outputs:
1156  * zc_objset_stats	stats
1157  * zc_nvlist_dst	property nvlist
1158  * zc_nvlist_dst_size	size of property nvlist
1159  * zc_value		alternate root
1160  */
1161 static int
1162 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1163 {
1164 	objset_t *os = NULL;
1165 	int error;
1166 	nvlist_t *nv;
1167 
1168 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0)
1169 		return (error);
1170 
1171 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1172 
1173 	if (zc->zc_nvlist_dst != 0 &&
1174 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1175 		dmu_objset_stats(os, nv);
1176 		/*
1177 		 * NB: zvol_get_stats() will read the objset contents,
1178 		 * which we aren't supposed to do with a
1179 		 * DS_MODE_STANDARD open, because it could be
1180 		 * inconsistent.  So this is a bit of a workaround...
1181 		 */
1182 		if (!zc->zc_objset_stats.dds_inconsistent) {
1183 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1184 				VERIFY(zvol_get_stats(os, nv) == 0);
1185 		}
1186 		error = put_nvlist(zc, nv);
1187 		nvlist_free(nv);
1188 	}
1189 
1190 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
1191 
1192 	dmu_objset_close(os);
1193 	return (error);
1194 }
1195 
1196 static int
1197 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1198 {
1199 	uint64_t value;
1200 	int error;
1201 
1202 	/*
1203 	 * zfs_get_zplprop() will either find a value or give us
1204 	 * the default value (if there is one).
1205 	 */
1206 	if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1207 		return (error);
1208 	VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1209 	return (0);
1210 }
1211 
1212 /*
1213  * inputs:
1214  * zc_name		name of filesystem
1215  * zc_nvlist_dst_size	size of buffer for zpl property nvlist
1216  *
1217  * outputs:
1218  * zc_nvlist_dst	zpl property nvlist
1219  * zc_nvlist_dst_size	size of zpl property nvlist
1220  */
1221 static int
1222 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1223 {
1224 	objset_t *os;
1225 	int err;
1226 
1227 	if ((err = zfs_os_open_retry(zc->zc_name, &os)) != 0)
1228 		return (err);
1229 
1230 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1231 
1232 	/*
1233 	 * NB: nvl_add_zplprop() will read the objset contents,
1234 	 * which we aren't supposed to do with a DS_MODE_STANDARD
1235 	 * open, because it could be inconsistent.
1236 	 */
1237 	if (zc->zc_nvlist_dst != NULL &&
1238 	    !zc->zc_objset_stats.dds_inconsistent &&
1239 	    dmu_objset_type(os) == DMU_OST_ZFS) {
1240 		nvlist_t *nv;
1241 
1242 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1243 		if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1244 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1245 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1246 		    (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1247 			err = put_nvlist(zc, nv);
1248 		nvlist_free(nv);
1249 	} else {
1250 		err = ENOENT;
1251 	}
1252 	dmu_objset_close(os);
1253 	return (err);
1254 }
1255 
1256 /*
1257  * inputs:
1258  * zc_name		name of filesystem
1259  * zc_cookie		zap cursor
1260  * zc_nvlist_dst_size	size of buffer for property nvlist
1261  *
1262  * outputs:
1263  * zc_name		name of next filesystem
1264  * zc_objset_stats	stats
1265  * zc_nvlist_dst	property nvlist
1266  * zc_nvlist_dst_size	size of property nvlist
1267  * zc_value		alternate root
1268  */
1269 static int
1270 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1271 {
1272 	objset_t *os;
1273 	int error;
1274 	char *p;
1275 
1276 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) {
1277 		if (error == ENOENT)
1278 			error = ESRCH;
1279 		return (error);
1280 	}
1281 
1282 	p = strrchr(zc->zc_name, '/');
1283 	if (p == NULL || p[1] != '\0')
1284 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1285 	p = zc->zc_name + strlen(zc->zc_name);
1286 
1287 	do {
1288 		error = dmu_dir_list_next(os,
1289 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1290 		    NULL, &zc->zc_cookie);
1291 		if (error == ENOENT)
1292 			error = ESRCH;
1293 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1294 	    !zone_dataset_visible(zc->zc_name, NULL));
1295 
1296 	/*
1297 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1298 	 * try to get stats for it.  Userland will skip over it.
1299 	 */
1300 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1301 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1302 
1303 	dmu_objset_close(os);
1304 	return (error);
1305 }
1306 
1307 /*
1308  * inputs:
1309  * zc_name		name of filesystem
1310  * zc_cookie		zap cursor
1311  * zc_nvlist_dst_size	size of buffer for property nvlist
1312  *
1313  * outputs:
1314  * zc_name		name of next snapshot
1315  * zc_objset_stats	stats
1316  * zc_nvlist_dst	property nvlist
1317  * zc_nvlist_dst_size	size of property nvlist
1318  * zc_value		alternate root
1319  */
1320 static int
1321 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1322 {
1323 	objset_t *os;
1324 	int error;
1325 
1326 	if ((error = zfs_os_open_retry(zc->zc_name, &os)) != 0) {
1327 		if (error == ENOENT)
1328 			error = ESRCH;
1329 		return (error);
1330 	}
1331 
1332 	/*
1333 	 * A dataset name of maximum length cannot have any snapshots,
1334 	 * so exit immediately.
1335 	 */
1336 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1337 		dmu_objset_close(os);
1338 		return (ESRCH);
1339 	}
1340 
1341 	error = dmu_snapshot_list_next(os,
1342 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1343 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1344 	if (error == ENOENT)
1345 		error = ESRCH;
1346 
1347 	if (error == 0)
1348 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1349 
1350 	/* if we failed, undo the @ that we tacked on to zc_name */
1351 	if (error != 0)
1352 		*strchr(zc->zc_name, '@') = '\0';
1353 
1354 	dmu_objset_close(os);
1355 	return (error);
1356 }
1357 
1358 int
1359 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1360 {
1361 	nvpair_t *elem;
1362 	int error;
1363 	uint64_t intval;
1364 	char *strval;
1365 
1366 	/*
1367 	 * First validate permission to set all of the properties
1368 	 */
1369 	elem = NULL;
1370 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1371 		const char *propname = nvpair_name(elem);
1372 		zfs_prop_t prop = zfs_name_to_prop(propname);
1373 
1374 		if (prop == ZPROP_INVAL) {
1375 			/*
1376 			 * If this is a user-defined property, it must be a
1377 			 * string, and there is no further validation to do.
1378 			 */
1379 			if (!zfs_prop_user(propname) ||
1380 			    nvpair_type(elem) != DATA_TYPE_STRING)
1381 				return (EINVAL);
1382 
1383 			if (error = zfs_secpolicy_write_perms(name,
1384 			    ZFS_DELEG_PERM_USERPROP, CRED()))
1385 				return (error);
1386 			continue;
1387 		}
1388 
1389 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1390 			return (error);
1391 
1392 		/*
1393 		 * Check that this value is valid for this pool version
1394 		 */
1395 		switch (prop) {
1396 		case ZFS_PROP_COMPRESSION:
1397 			/*
1398 			 * If the user specified gzip compression, make sure
1399 			 * the SPA supports it. We ignore any errors here since
1400 			 * we'll catch them later.
1401 			 */
1402 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1403 			    nvpair_value_uint64(elem, &intval) == 0 &&
1404 			    intval >= ZIO_COMPRESS_GZIP_1 &&
1405 			    intval <= ZIO_COMPRESS_GZIP_9) {
1406 				if (zfs_check_version(name,
1407 				    SPA_VERSION_GZIP_COMPRESSION))
1408 					return (ENOTSUP);
1409 			}
1410 			break;
1411 
1412 		case ZFS_PROP_COPIES:
1413 			if (zfs_check_version(name, SPA_VERSION_DITTO_BLOCKS))
1414 				return (ENOTSUP);
1415 			break;
1416 
1417 		case ZFS_PROP_SHARESMB:
1418 			if (zpl_check_version(name, ZPL_VERSION_FUID))
1419 				return (ENOTSUP);
1420 			break;
1421 		}
1422 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1423 			return (error);
1424 	}
1425 
1426 	elem = NULL;
1427 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1428 		const char *propname = nvpair_name(elem);
1429 		zfs_prop_t prop = zfs_name_to_prop(propname);
1430 
1431 		if (prop == ZPROP_INVAL) {
1432 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1433 			error = dsl_prop_set(name, propname, 1,
1434 			    strlen(strval) + 1, strval);
1435 			if (error == 0)
1436 				continue;
1437 			else
1438 				return (error);
1439 		}
1440 
1441 		switch (prop) {
1442 		case ZFS_PROP_QUOTA:
1443 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1444 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1445 				return (error);
1446 			break;
1447 
1448 		case ZFS_PROP_REFQUOTA:
1449 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1450 			    (error = dsl_dataset_set_quota(name, intval)) != 0)
1451 				return (error);
1452 			break;
1453 
1454 		case ZFS_PROP_RESERVATION:
1455 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1456 			    (error = dsl_dir_set_reservation(name,
1457 			    intval)) != 0)
1458 				return (error);
1459 			break;
1460 
1461 		case ZFS_PROP_REFRESERVATION:
1462 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1463 			    (error = dsl_dataset_set_reservation(name,
1464 			    intval)) != 0)
1465 				return (error);
1466 			break;
1467 
1468 		case ZFS_PROP_VOLSIZE:
1469 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1470 			    (error = zvol_set_volsize(name,
1471 			    ddi_driver_major(zfs_dip), intval)) != 0)
1472 				return (error);
1473 			break;
1474 
1475 		case ZFS_PROP_VOLBLOCKSIZE:
1476 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1477 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1478 				return (error);
1479 			break;
1480 
1481 		case ZFS_PROP_VERSION:
1482 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1483 			    (error = zfs_set_version(name, intval)) != 0)
1484 				return (error);
1485 			break;
1486 
1487 		default:
1488 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1489 				if (zfs_prop_get_type(prop) !=
1490 				    PROP_TYPE_STRING)
1491 					return (EINVAL);
1492 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1493 				if ((error = dsl_prop_set(name,
1494 				    nvpair_name(elem), 1, strlen(strval) + 1,
1495 				    strval)) != 0)
1496 					return (error);
1497 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1498 				const char *unused;
1499 
1500 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1501 
1502 				switch (zfs_prop_get_type(prop)) {
1503 				case PROP_TYPE_NUMBER:
1504 					break;
1505 				case PROP_TYPE_STRING:
1506 					return (EINVAL);
1507 				case PROP_TYPE_INDEX:
1508 					if (zfs_prop_index_to_string(prop,
1509 					    intval, &unused) != 0)
1510 						return (EINVAL);
1511 					break;
1512 				default:
1513 					cmn_err(CE_PANIC,
1514 					    "unknown property type");
1515 					break;
1516 				}
1517 
1518 				if ((error = dsl_prop_set(name, propname,
1519 				    8, 1, &intval)) != 0)
1520 					return (error);
1521 			} else {
1522 				return (EINVAL);
1523 			}
1524 			break;
1525 		}
1526 	}
1527 
1528 	return (0);
1529 }
1530 
1531 /*
1532  * inputs:
1533  * zc_name		name of filesystem
1534  * zc_value		name of property to inherit
1535  * zc_nvlist_src{_size}	nvlist of properties to apply
1536  *
1537  * outputs:		none
1538  */
1539 static int
1540 zfs_ioc_set_prop(zfs_cmd_t *zc)
1541 {
1542 	nvlist_t *nvl;
1543 	int error;
1544 
1545 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1546 	    &nvl)) != 0)
1547 		return (error);
1548 
1549 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1550 
1551 	nvlist_free(nvl);
1552 	return (error);
1553 }
1554 
1555 /*
1556  * inputs:
1557  * zc_name		name of filesystem
1558  * zc_value		name of property to inherit
1559  *
1560  * outputs:		none
1561  */
1562 static int
1563 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1564 {
1565 	/* the property name has been validated by zfs_secpolicy_inherit() */
1566 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1567 }
1568 
1569 static int
1570 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1571 {
1572 	nvlist_t *props;
1573 	spa_t *spa;
1574 	int error;
1575 
1576 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1577 	    &props)))
1578 		return (error);
1579 
1580 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1581 		nvlist_free(props);
1582 		return (error);
1583 	}
1584 
1585 	error = spa_prop_set(spa, props);
1586 
1587 	nvlist_free(props);
1588 	spa_close(spa, FTAG);
1589 
1590 	return (error);
1591 }
1592 
1593 static int
1594 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1595 {
1596 	spa_t *spa;
1597 	int error;
1598 	nvlist_t *nvp = NULL;
1599 
1600 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1601 		return (error);
1602 
1603 	error = spa_prop_get(spa, &nvp);
1604 
1605 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1606 		error = put_nvlist(zc, nvp);
1607 	else
1608 		error = EFAULT;
1609 
1610 	spa_close(spa, FTAG);
1611 
1612 	if (nvp)
1613 		nvlist_free(nvp);
1614 	return (error);
1615 }
1616 
1617 static int
1618 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1619 {
1620 	nvlist_t *nvp;
1621 	int error;
1622 	uint32_t uid;
1623 	uint32_t gid;
1624 	uint32_t *groups;
1625 	uint_t group_cnt;
1626 	cred_t	*usercred;
1627 
1628 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1629 	    &nvp)) != 0) {
1630 		return (error);
1631 	}
1632 
1633 	if ((error = nvlist_lookup_uint32(nvp,
1634 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1635 		nvlist_free(nvp);
1636 		return (EPERM);
1637 	}
1638 
1639 	if ((error = nvlist_lookup_uint32(nvp,
1640 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1641 		nvlist_free(nvp);
1642 		return (EPERM);
1643 	}
1644 
1645 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1646 	    &groups, &group_cnt)) != 0) {
1647 		nvlist_free(nvp);
1648 		return (EPERM);
1649 	}
1650 	usercred = cralloc();
1651 	if ((crsetugid(usercred, uid, gid) != 0) ||
1652 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1653 		nvlist_free(nvp);
1654 		crfree(usercred);
1655 		return (EPERM);
1656 	}
1657 	nvlist_free(nvp);
1658 	error = dsl_deleg_access(zc->zc_name,
1659 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1660 	crfree(usercred);
1661 	return (error);
1662 }
1663 
1664 /*
1665  * inputs:
1666  * zc_name		name of filesystem
1667  * zc_nvlist_src{_size}	nvlist of delegated permissions
1668  * zc_perm_action	allow/unallow flag
1669  *
1670  * outputs:		none
1671  */
1672 static int
1673 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1674 {
1675 	int error;
1676 	nvlist_t *fsaclnv = NULL;
1677 
1678 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1679 	    &fsaclnv)) != 0)
1680 		return (error);
1681 
1682 	/*
1683 	 * Verify nvlist is constructed correctly
1684 	 */
1685 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1686 		nvlist_free(fsaclnv);
1687 		return (EINVAL);
1688 	}
1689 
1690 	/*
1691 	 * If we don't have PRIV_SYS_MOUNT, then validate
1692 	 * that user is allowed to hand out each permission in
1693 	 * the nvlist(s)
1694 	 */
1695 
1696 	error = secpolicy_zfs(CRED());
1697 	if (error) {
1698 		if (zc->zc_perm_action == B_FALSE) {
1699 			error = dsl_deleg_can_allow(zc->zc_name,
1700 			    fsaclnv, CRED());
1701 		} else {
1702 			error = dsl_deleg_can_unallow(zc->zc_name,
1703 			    fsaclnv, CRED());
1704 		}
1705 	}
1706 
1707 	if (error == 0)
1708 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1709 
1710 	nvlist_free(fsaclnv);
1711 	return (error);
1712 }
1713 
1714 /*
1715  * inputs:
1716  * zc_name		name of filesystem
1717  *
1718  * outputs:
1719  * zc_nvlist_src{_size}	nvlist of delegated permissions
1720  */
1721 static int
1722 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1723 {
1724 	nvlist_t *nvp;
1725 	int error;
1726 
1727 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1728 		error = put_nvlist(zc, nvp);
1729 		nvlist_free(nvp);
1730 	}
1731 
1732 	return (error);
1733 }
1734 
1735 /*
1736  * inputs:
1737  * zc_name		name of volume
1738  *
1739  * outputs:		none
1740  */
1741 static int
1742 zfs_ioc_create_minor(zfs_cmd_t *zc)
1743 {
1744 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1745 }
1746 
1747 /*
1748  * inputs:
1749  * zc_name		name of volume
1750  *
1751  * outputs:		none
1752  */
1753 static int
1754 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1755 {
1756 	return (zvol_remove_minor(zc->zc_name));
1757 }
1758 
1759 /*
1760  * Search the vfs list for a specified resource.  Returns a pointer to it
1761  * or NULL if no suitable entry is found. The caller of this routine
1762  * is responsible for releasing the returned vfs pointer.
1763  */
1764 static vfs_t *
1765 zfs_get_vfs(const char *resource)
1766 {
1767 	struct vfs *vfsp;
1768 	struct vfs *vfs_found = NULL;
1769 
1770 	vfs_list_read_lock();
1771 	vfsp = rootvfs;
1772 	do {
1773 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1774 			VFS_HOLD(vfsp);
1775 			vfs_found = vfsp;
1776 			break;
1777 		}
1778 		vfsp = vfsp->vfs_next;
1779 	} while (vfsp != rootvfs);
1780 	vfs_list_unlock();
1781 	return (vfs_found);
1782 }
1783 
1784 /* ARGSUSED */
1785 static void
1786 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1787 {
1788 	zfs_creat_t *zct = arg;
1789 
1790 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
1791 }
1792 
1793 #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
1794 
1795 /*
1796  * inputs:
1797  * createprops	list of properties requested by creator
1798  * dataset	name of dataset we are creating
1799  *
1800  * outputs:
1801  * zplprops	values for the zplprops we attach to the master node object
1802  *
1803  * Determine the settings for utf8only, normalization and
1804  * casesensitivity.  Specific values may have been requested by the
1805  * creator and/or we can inherit values from the parent dataset.  If
1806  * the file system is of too early a vintage, a creator can not
1807  * request settings for these properties, even if the requested
1808  * setting is the default value.  We don't actually want to create dsl
1809  * properties for these, so remove them from the source nvlist after
1810  * processing.
1811  */
1812 static int
1813 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
1814     nvlist_t *zplprops, uint64_t zplver)
1815 {
1816 	objset_t *os;
1817 	char parentname[MAXNAMELEN];
1818 	char *cp;
1819 	uint64_t sense = ZFS_PROP_UNDEFINED;
1820 	uint64_t norm = ZFS_PROP_UNDEFINED;
1821 	uint64_t u8 = ZFS_PROP_UNDEFINED;
1822 	int error = 0;
1823 
1824 	ASSERT(zplprops != NULL);
1825 
1826 	(void) strlcpy(parentname, dataset, sizeof (parentname));
1827 	cp = strrchr(parentname, '/');
1828 	ASSERT(cp != NULL);
1829 	cp[0] = '\0';
1830 
1831 	/*
1832 	 * Pull out creator prop choices, if any.
1833 	 */
1834 	if (createprops) {
1835 		(void) nvlist_lookup_uint64(createprops,
1836 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
1837 		(void) nvlist_remove_all(createprops,
1838 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
1839 		(void) nvlist_lookup_uint64(createprops,
1840 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
1841 		(void) nvlist_remove_all(createprops,
1842 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1843 		(void) nvlist_lookup_uint64(createprops,
1844 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
1845 		(void) nvlist_remove_all(createprops,
1846 		    zfs_prop_to_name(ZFS_PROP_CASE));
1847 	}
1848 
1849 	/*
1850 	 * If the file system or pool is version is too "young" to
1851 	 * support normalization and the creator tried to set a value
1852 	 * for one of the props, error out.  We only need check the
1853 	 * ZPL version because we've already checked by now that the
1854 	 * SPA version is compatible with the selected ZPL version.
1855 	 */
1856 	if (zplver < ZPL_VERSION_NORMALIZATION &&
1857 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
1858 	    sense != ZFS_PROP_UNDEFINED))
1859 		return (ENOTSUP);
1860 
1861 	/*
1862 	 * Put the version in the zplprops
1863 	 */
1864 	VERIFY(nvlist_add_uint64(zplprops,
1865 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
1866 
1867 	/*
1868 	 * Open parent object set so we can inherit zplprop values if
1869 	 * necessary.
1870 	 */
1871 	if ((error = zfs_os_open_retry(parentname, &os)) != 0)
1872 		return (error);
1873 
1874 	if (norm == ZFS_PROP_UNDEFINED)
1875 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
1876 	VERIFY(nvlist_add_uint64(zplprops,
1877 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
1878 
1879 	/*
1880 	 * If we're normalizing, names must always be valid UTF-8 strings.
1881 	 */
1882 	if (norm)
1883 		u8 = 1;
1884 	if (u8 == ZFS_PROP_UNDEFINED)
1885 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
1886 	VERIFY(nvlist_add_uint64(zplprops,
1887 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
1888 
1889 	if (sense == ZFS_PROP_UNDEFINED)
1890 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
1891 	VERIFY(nvlist_add_uint64(zplprops,
1892 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
1893 
1894 	dmu_objset_close(os);
1895 	return (0);
1896 }
1897 
1898 /*
1899  * inputs:
1900  * zc_objset_type	type of objset to create (fs vs zvol)
1901  * zc_name		name of new objset
1902  * zc_value		name of snapshot to clone from (may be empty)
1903  * zc_nvlist_src{_size}	nvlist of properties to apply
1904  *
1905  * outputs: none
1906  */
1907 static int
1908 zfs_ioc_create(zfs_cmd_t *zc)
1909 {
1910 	objset_t *clone;
1911 	int error = 0;
1912 	zfs_creat_t zct;
1913 	nvlist_t *nvprops = NULL;
1914 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
1915 	dmu_objset_type_t type = zc->zc_objset_type;
1916 
1917 	switch (type) {
1918 
1919 	case DMU_OST_ZFS:
1920 		cbfunc = zfs_create_cb;
1921 		break;
1922 
1923 	case DMU_OST_ZVOL:
1924 		cbfunc = zvol_create_cb;
1925 		break;
1926 
1927 	default:
1928 		cbfunc = NULL;
1929 		break;
1930 	}
1931 	if (strchr(zc->zc_name, '@') ||
1932 	    strchr(zc->zc_name, '%'))
1933 		return (EINVAL);
1934 
1935 	if (zc->zc_nvlist_src != NULL &&
1936 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1937 	    &nvprops)) != 0)
1938 		return (error);
1939 
1940 	zct.zct_zplprops = NULL;
1941 	zct.zct_props = nvprops;
1942 
1943 	if (zc->zc_value[0] != '\0') {
1944 		/*
1945 		 * We're creating a clone of an existing snapshot.
1946 		 */
1947 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1948 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1949 			nvlist_free(nvprops);
1950 			return (EINVAL);
1951 		}
1952 
1953 		error = dmu_objset_open(zc->zc_value, type,
1954 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1955 		if (error) {
1956 			nvlist_free(nvprops);
1957 			return (error);
1958 		}
1959 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1960 		if (error) {
1961 			dmu_objset_close(clone);
1962 			nvlist_free(nvprops);
1963 			return (error);
1964 		}
1965 		dmu_objset_close(clone);
1966 	} else {
1967 		if (cbfunc == NULL) {
1968 			nvlist_free(nvprops);
1969 			return (EINVAL);
1970 		}
1971 
1972 		if (type == DMU_OST_ZVOL) {
1973 			uint64_t volsize, volblocksize;
1974 
1975 			if (nvprops == NULL ||
1976 			    nvlist_lookup_uint64(nvprops,
1977 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1978 			    &volsize) != 0) {
1979 				nvlist_free(nvprops);
1980 				return (EINVAL);
1981 			}
1982 
1983 			if ((error = nvlist_lookup_uint64(nvprops,
1984 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1985 			    &volblocksize)) != 0 && error != ENOENT) {
1986 				nvlist_free(nvprops);
1987 				return (EINVAL);
1988 			}
1989 
1990 			if (error != 0)
1991 				volblocksize = zfs_prop_default_numeric(
1992 				    ZFS_PROP_VOLBLOCKSIZE);
1993 
1994 			if ((error = zvol_check_volblocksize(
1995 			    volblocksize)) != 0 ||
1996 			    (error = zvol_check_volsize(volsize,
1997 			    volblocksize)) != 0) {
1998 				nvlist_free(nvprops);
1999 				return (error);
2000 			}
2001 		} else if (type == DMU_OST_ZFS) {
2002 			uint64_t version;
2003 			int error;
2004 
2005 			/*
2006 			 * Default ZPL version to non-FUID capable if the
2007 			 * pool is not upgraded to support FUIDs.
2008 			 */
2009 			if (zfs_check_version(zc->zc_name, SPA_VERSION_FUID))
2010 				version = ZPL_VERSION_FUID - 1;
2011 			else
2012 				version = ZPL_VERSION;
2013 
2014 			/*
2015 			 * Potentially override default ZPL version based
2016 			 * on creator's request.
2017 			 */
2018 			(void) nvlist_lookup_uint64(nvprops,
2019 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
2020 
2021 			/*
2022 			 * Make sure version we ended up with is kosher
2023 			 */
2024 			if ((version < ZPL_VERSION_INITIAL ||
2025 			    version > ZPL_VERSION) ||
2026 			    (version >= ZPL_VERSION_FUID &&
2027 			    zfs_check_version(zc->zc_name, SPA_VERSION_FUID))) {
2028 				nvlist_free(nvprops);
2029 				return (ENOTSUP);
2030 			}
2031 
2032 			/*
2033 			 * We have to have normalization and
2034 			 * case-folding flags correct when we do the
2035 			 * file system creation, so go figure them out
2036 			 * now.
2037 			 */
2038 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2039 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2040 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
2041 			    zct.zct_zplprops, version);
2042 			if (error != 0) {
2043 				nvlist_free(nvprops);
2044 				nvlist_free(zct.zct_zplprops);
2045 				return (error);
2046 			}
2047 		}
2048 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
2049 		    &zct);
2050 		nvlist_free(zct.zct_zplprops);
2051 
2052 	}
2053 
2054 	/*
2055 	 * It would be nice to do this atomically.
2056 	 */
2057 	if (error == 0) {
2058 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2059 			(void) dmu_objset_destroy(zc->zc_name);
2060 	}
2061 	nvlist_free(nvprops);
2062 	return (error);
2063 }
2064 
2065 /*
2066  * inputs:
2067  * zc_name	name of filesystem
2068  * zc_value	short name of snapshot
2069  * zc_cookie	recursive flag
2070  *
2071  * outputs:	none
2072  */
2073 static int
2074 zfs_ioc_snapshot(zfs_cmd_t *zc)
2075 {
2076 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2077 		return (EINVAL);
2078 	return (dmu_objset_snapshot(zc->zc_name,
2079 	    zc->zc_value, zc->zc_cookie));
2080 }
2081 
2082 int
2083 zfs_unmount_snap(char *name, void *arg)
2084 {
2085 	char *snapname = arg;
2086 	char *cp;
2087 	vfs_t *vfsp = NULL;
2088 
2089 	/*
2090 	 * Snapshots (which are under .zfs control) must be unmounted
2091 	 * before they can be destroyed.
2092 	 */
2093 
2094 	if (snapname) {
2095 		(void) strcat(name, "@");
2096 		(void) strcat(name, snapname);
2097 		vfsp = zfs_get_vfs(name);
2098 		cp = strchr(name, '@');
2099 		*cp = '\0';
2100 	} else if (strchr(name, '@')) {
2101 		vfsp = zfs_get_vfs(name);
2102 	}
2103 
2104 	if (vfsp) {
2105 		/*
2106 		 * Always force the unmount for snapshots.
2107 		 */
2108 		int flag = MS_FORCE;
2109 		int err;
2110 
2111 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2112 			VFS_RELE(vfsp);
2113 			return (err);
2114 		}
2115 		VFS_RELE(vfsp);
2116 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
2117 			return (err);
2118 	}
2119 	return (0);
2120 }
2121 
2122 /*
2123  * inputs:
2124  * zc_name	name of filesystem
2125  * zc_value	short name of snapshot
2126  *
2127  * outputs:	none
2128  */
2129 static int
2130 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2131 {
2132 	int err;
2133 
2134 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2135 		return (EINVAL);
2136 	err = dmu_objset_find(zc->zc_name,
2137 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2138 	if (err)
2139 		return (err);
2140 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2141 }
2142 
2143 /*
2144  * inputs:
2145  * zc_name		name of dataset to destroy
2146  * zc_objset_type	type of objset
2147  *
2148  * outputs:		none
2149  */
2150 static int
2151 zfs_ioc_destroy(zfs_cmd_t *zc)
2152 {
2153 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2154 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2155 		if (err)
2156 			return (err);
2157 	}
2158 
2159 	return (dmu_objset_destroy(zc->zc_name));
2160 }
2161 
2162 /*
2163  * inputs:
2164  * zc_name	name of dataset to rollback (to most recent snapshot)
2165  *
2166  * outputs:	none
2167  */
2168 static int
2169 zfs_ioc_rollback(zfs_cmd_t *zc)
2170 {
2171 	objset_t *os;
2172 	int error;
2173 	zfsvfs_t *zfsvfs = NULL;
2174 
2175 	/*
2176 	 * Get the zfsvfs for the receiving objset. There
2177 	 * won't be one if we're operating on a zvol, if the
2178 	 * objset doesn't exist yet, or is not mounted.
2179 	 */
2180 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2181 	    DS_MODE_STANDARD, &os);
2182 	if (error)
2183 		return (error);
2184 
2185 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
2186 		mutex_enter(&os->os->os_user_ptr_lock);
2187 		zfsvfs = dmu_objset_get_user(os);
2188 		if (zfsvfs != NULL)
2189 			VFS_HOLD(zfsvfs->z_vfs);
2190 		mutex_exit(&os->os->os_user_ptr_lock);
2191 	}
2192 
2193 	if (zfsvfs != NULL) {
2194 		char osname[MAXNAMELEN];
2195 		int mode;
2196 
2197 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
2198 		if (error == 0) {
2199 			int resume_err;
2200 
2201 			ASSERT(strcmp(osname, zc->zc_name) == 0);
2202 			error = dmu_objset_rollback(os);
2203 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2204 			error = error ? error : resume_err;
2205 		} else {
2206 			dmu_objset_close(os);
2207 		}
2208 		VFS_RELE(zfsvfs->z_vfs);
2209 	} else {
2210 		error = dmu_objset_rollback(os);
2211 	}
2212 	/* Note, the dmu_objset_rollback() closes the objset for us. */
2213 
2214 	return (error);
2215 }
2216 
2217 /*
2218  * inputs:
2219  * zc_name	old name of dataset
2220  * zc_value	new name of dataset
2221  * zc_cookie	recursive flag (only valid for snapshots)
2222  *
2223  * outputs:	none
2224  */
2225 static int
2226 zfs_ioc_rename(zfs_cmd_t *zc)
2227 {
2228 	boolean_t recursive = zc->zc_cookie & 1;
2229 
2230 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2231 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2232 	    strchr(zc->zc_value, '%'))
2233 		return (EINVAL);
2234 
2235 	/*
2236 	 * Unmount snapshot unless we're doing a recursive rename,
2237 	 * in which case the dataset code figures out which snapshots
2238 	 * to unmount.
2239 	 */
2240 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2241 	    zc->zc_objset_type == DMU_OST_ZFS) {
2242 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2243 		if (err)
2244 			return (err);
2245 	}
2246 
2247 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2248 }
2249 
2250 /*
2251  * inputs:
2252  * zc_name		name of containing filesystem
2253  * zc_nvlist_src{_size}	nvlist of properties to apply
2254  * zc_value		name of snapshot to create
2255  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
2256  * zc_cookie		file descriptor to recv from
2257  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
2258  * zc_guid		force flag
2259  *
2260  * outputs:
2261  * zc_cookie		number of bytes read
2262  */
2263 static int
2264 zfs_ioc_recv(zfs_cmd_t *zc)
2265 {
2266 	file_t *fp;
2267 	objset_t *os;
2268 	dmu_recv_cookie_t drc;
2269 	zfsvfs_t *zfsvfs = NULL;
2270 	boolean_t force = (boolean_t)zc->zc_guid;
2271 	int error, fd;
2272 	offset_t off;
2273 	nvlist_t *props = NULL;
2274 	objset_t *origin = NULL;
2275 	char *tosnap;
2276 	char tofs[ZFS_MAXNAMELEN];
2277 
2278 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2279 	    strchr(zc->zc_value, '@') == NULL ||
2280 	    strchr(zc->zc_value, '%'))
2281 		return (EINVAL);
2282 
2283 	(void) strcpy(tofs, zc->zc_value);
2284 	tosnap = strchr(tofs, '@');
2285 	*tosnap = '\0';
2286 	tosnap++;
2287 
2288 	if (zc->zc_nvlist_src != NULL &&
2289 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2290 	    &props)) != 0)
2291 		return (error);
2292 
2293 	fd = zc->zc_cookie;
2294 	fp = getf(fd);
2295 	if (fp == NULL) {
2296 		nvlist_free(props);
2297 		return (EBADF);
2298 	}
2299 
2300 	/*
2301 	 * Get the zfsvfs for the receiving objset. There
2302 	 * won't be one if we're operating on a zvol, if the
2303 	 * objset doesn't exist yet, or is not mounted.
2304 	 */
2305 
2306 	error = dmu_objset_open(tofs, DMU_OST_ZFS,
2307 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
2308 	if (!error) {
2309 		mutex_enter(&os->os->os_user_ptr_lock);
2310 		zfsvfs = dmu_objset_get_user(os);
2311 		if (zfsvfs != NULL) {
2312 			VFS_HOLD(zfsvfs->z_vfs);
2313 			mutex_exit(&os->os->os_user_ptr_lock);
2314 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2315 				VFS_RELE(zfsvfs->z_vfs);
2316 				dmu_objset_close(os);
2317 				nvlist_free(props);
2318 				releasef(fd);
2319 				return (EBUSY);
2320 			}
2321 		} else {
2322 			mutex_exit(&os->os->os_user_ptr_lock);
2323 		}
2324 		dmu_objset_close(os);
2325 	}
2326 
2327 	if (zc->zc_string[0]) {
2328 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2329 		    DS_MODE_STANDARD | DS_MODE_READONLY, &origin);
2330 		if (error) {
2331 			if (zfsvfs != NULL) {
2332 				mutex_exit(&zfsvfs->z_online_recv_lock);
2333 				VFS_RELE(zfsvfs->z_vfs);
2334 			}
2335 			nvlist_free(props);
2336 			releasef(fd);
2337 			return (error);
2338 		}
2339 	}
2340 
2341 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2342 	    force, origin, zfsvfs != NULL, &drc);
2343 	if (origin)
2344 		dmu_objset_close(origin);
2345 	if (error) {
2346 		if (zfsvfs != NULL) {
2347 			mutex_exit(&zfsvfs->z_online_recv_lock);
2348 			VFS_RELE(zfsvfs->z_vfs);
2349 		}
2350 		nvlist_free(props);
2351 		releasef(fd);
2352 		return (error);
2353 	}
2354 
2355 	/*
2356 	 * If properties are supplied, they are to completely replace
2357 	 * the existing ones; "inherit" any existing properties.
2358 	 */
2359 	if (props) {
2360 		objset_t *os;
2361 		nvlist_t *nv = NULL;
2362 
2363 		error = dmu_objset_open(tofs, DMU_OST_ANY,
2364 		    DS_MODE_STANDARD | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
2365 		    &os);
2366 		if (error == 0) {
2367 			error = dsl_prop_get_all(os, &nv);
2368 			dmu_objset_close(os);
2369 		}
2370 		if (error == 0) {
2371 			nvpair_t *elem;
2372 			zfs_cmd_t *zc2;
2373 			zc2 = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2374 
2375 			(void) strcpy(zc2->zc_name, tofs);
2376 			for (elem = nvlist_next_nvpair(nv, NULL); elem;
2377 			    elem = nvlist_next_nvpair(nv, elem)) {
2378 				(void) strcpy(zc2->zc_value, nvpair_name(elem));
2379 				if (zfs_secpolicy_inherit(zc2, CRED()) == 0)
2380 					(void) zfs_ioc_inherit_prop(zc2);
2381 			}
2382 			kmem_free(zc2, sizeof (zfs_cmd_t));
2383 		}
2384 		if (nv)
2385 			nvlist_free(nv);
2386 	}
2387 
2388 	/*
2389 	 * Set properties.  Note, we ignore errors.  Would be better to
2390 	 * do best-effort in zfs_set_prop_nvlist, too.
2391 	 */
2392 	(void) zfs_set_prop_nvlist(tofs, props);
2393 	nvlist_free(props);
2394 
2395 	off = fp->f_offset;
2396 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
2397 
2398 	if (error == 0) {
2399 		if (zfsvfs != NULL) {
2400 			char osname[MAXNAMELEN];
2401 			int mode;
2402 
2403 			error = zfs_suspend_fs(zfsvfs, osname, &mode);
2404 			if (error == 0) {
2405 				int resume_err;
2406 
2407 				error = dmu_recv_end(&drc);
2408 				resume_err = zfs_resume_fs(zfsvfs,
2409 				    osname, mode);
2410 				error = error ? error : resume_err;
2411 			} else {
2412 				dmu_recv_abort_cleanup(&drc);
2413 			}
2414 		} else {
2415 			error = dmu_recv_end(&drc);
2416 		}
2417 	}
2418 	if (zfsvfs != NULL) {
2419 		mutex_exit(&zfsvfs->z_online_recv_lock);
2420 		VFS_RELE(zfsvfs->z_vfs);
2421 	}
2422 
2423 	zc->zc_cookie = off - fp->f_offset;
2424 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2425 		fp->f_offset = off;
2426 
2427 	releasef(fd);
2428 	return (error);
2429 }
2430 
2431 /*
2432  * inputs:
2433  * zc_name	name of snapshot to send
2434  * zc_value	short name of incremental fromsnap (may be empty)
2435  * zc_cookie	file descriptor to send stream to
2436  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
2437  *
2438  * outputs: none
2439  */
2440 static int
2441 zfs_ioc_send(zfs_cmd_t *zc)
2442 {
2443 	objset_t *fromsnap = NULL;
2444 	objset_t *tosnap;
2445 	file_t *fp;
2446 	int error;
2447 	offset_t off;
2448 
2449 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2450 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
2451 	if (error)
2452 		return (error);
2453 
2454 	if (zc->zc_value[0] != '\0') {
2455 		char buf[MAXPATHLEN];
2456 		char *cp;
2457 
2458 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
2459 		cp = strchr(buf, '@');
2460 		if (cp)
2461 			*(cp+1) = 0;
2462 		(void) strncat(buf, zc->zc_value, sizeof (buf));
2463 		error = dmu_objset_open(buf, DMU_OST_ANY,
2464 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
2465 		if (error) {
2466 			dmu_objset_close(tosnap);
2467 			return (error);
2468 		}
2469 	}
2470 
2471 	fp = getf(zc->zc_cookie);
2472 	if (fp == NULL) {
2473 		dmu_objset_close(tosnap);
2474 		if (fromsnap)
2475 			dmu_objset_close(fromsnap);
2476 		return (EBADF);
2477 	}
2478 
2479 	off = fp->f_offset;
2480 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
2481 
2482 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2483 		fp->f_offset = off;
2484 	releasef(zc->zc_cookie);
2485 	if (fromsnap)
2486 		dmu_objset_close(fromsnap);
2487 	dmu_objset_close(tosnap);
2488 	return (error);
2489 }
2490 
2491 static int
2492 zfs_ioc_inject_fault(zfs_cmd_t *zc)
2493 {
2494 	int id, error;
2495 
2496 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2497 	    &zc->zc_inject_record);
2498 
2499 	if (error == 0)
2500 		zc->zc_guid = (uint64_t)id;
2501 
2502 	return (error);
2503 }
2504 
2505 static int
2506 zfs_ioc_clear_fault(zfs_cmd_t *zc)
2507 {
2508 	return (zio_clear_fault((int)zc->zc_guid));
2509 }
2510 
2511 static int
2512 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2513 {
2514 	int id = (int)zc->zc_guid;
2515 	int error;
2516 
2517 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2518 	    &zc->zc_inject_record);
2519 
2520 	zc->zc_guid = id;
2521 
2522 	return (error);
2523 }
2524 
2525 static int
2526 zfs_ioc_error_log(zfs_cmd_t *zc)
2527 {
2528 	spa_t *spa;
2529 	int error;
2530 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2531 
2532 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2533 		return (error);
2534 
2535 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2536 	    &count);
2537 	if (error == 0)
2538 		zc->zc_nvlist_dst_size = count;
2539 	else
2540 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2541 
2542 	spa_close(spa, FTAG);
2543 
2544 	return (error);
2545 }
2546 
2547 static int
2548 zfs_ioc_clear(zfs_cmd_t *zc)
2549 {
2550 	spa_t *spa;
2551 	vdev_t *vd;
2552 	uint64_t txg;
2553 	int error;
2554 
2555 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2556 		return (error);
2557 
2558 	/*
2559 	 * Try to resume any I/Os which may have been suspended
2560 	 * as a result of a complete pool failure.
2561 	 */
2562 	if (!list_is_empty(&spa->spa_zio_list)) {
2563 		if (zio_vdev_resume_io(spa) != 0) {
2564 			spa_close(spa, FTAG);
2565 			return (EIO);
2566 		}
2567 	}
2568 
2569 	txg = spa_vdev_enter(spa);
2570 
2571 	if (zc->zc_guid == 0) {
2572 		vd = NULL;
2573 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
2574 		spa_aux_vdev_t *sav;
2575 		int i;
2576 
2577 		/*
2578 		 * Check if this is an l2cache device.
2579 		 */
2580 		ASSERT(spa != NULL);
2581 		sav = &spa->spa_l2cache;
2582 		for (i = 0; i < sav->sav_count; i++) {
2583 			if (sav->sav_vdevs[i]->vdev_guid == zc->zc_guid) {
2584 				vd = sav->sav_vdevs[i];
2585 				break;
2586 			}
2587 		}
2588 
2589 		if (vd == NULL) {
2590 			(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
2591 			spa_close(spa, FTAG);
2592 			return (ENODEV);
2593 		}
2594 	}
2595 
2596 	vdev_clear(spa, vd, B_TRUE);
2597 
2598 	(void) spa_vdev_exit(spa, NULL, txg, 0);
2599 
2600 	spa_close(spa, FTAG);
2601 
2602 	return (0);
2603 }
2604 
2605 /*
2606  * inputs:
2607  * zc_name	name of filesystem
2608  * zc_value	name of origin snapshot
2609  *
2610  * outputs:	none
2611  */
2612 static int
2613 zfs_ioc_promote(zfs_cmd_t *zc)
2614 {
2615 	char *cp;
2616 
2617 	/*
2618 	 * We don't need to unmount *all* the origin fs's snapshots, but
2619 	 * it's easier.
2620 	 */
2621 	cp = strchr(zc->zc_value, '@');
2622 	if (cp)
2623 		*cp = '\0';
2624 	(void) dmu_objset_find(zc->zc_value,
2625 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2626 	return (dsl_dataset_promote(zc->zc_name));
2627 }
2628 
2629 /*
2630  * We don't want to have a hard dependency
2631  * against some special symbols in sharefs
2632  * nfs, and smbsrv.  Determine them if needed when
2633  * the first file system is shared.
2634  * Neither sharefs, nfs or smbsrv are unloadable modules.
2635  */
2636 int (*znfsexport_fs)(void *arg);
2637 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2638 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2639 
2640 int zfs_nfsshare_inited;
2641 int zfs_smbshare_inited;
2642 
2643 ddi_modhandle_t nfs_mod;
2644 ddi_modhandle_t sharefs_mod;
2645 ddi_modhandle_t smbsrv_mod;
2646 kmutex_t zfs_share_lock;
2647 
2648 static int
2649 zfs_init_sharefs()
2650 {
2651 	int error;
2652 
2653 	ASSERT(MUTEX_HELD(&zfs_share_lock));
2654 	/* Both NFS and SMB shares also require sharetab support. */
2655 	if (sharefs_mod == NULL && ((sharefs_mod =
2656 	    ddi_modopen("fs/sharefs",
2657 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
2658 		return (ENOSYS);
2659 	}
2660 	if (zshare_fs == NULL && ((zshare_fs =
2661 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2662 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2663 		return (ENOSYS);
2664 	}
2665 	return (0);
2666 }
2667 
2668 static int
2669 zfs_ioc_share(zfs_cmd_t *zc)
2670 {
2671 	int error;
2672 	int opcode;
2673 
2674 	switch (zc->zc_share.z_sharetype) {
2675 	case ZFS_SHARE_NFS:
2676 	case ZFS_UNSHARE_NFS:
2677 		if (zfs_nfsshare_inited == 0) {
2678 			mutex_enter(&zfs_share_lock);
2679 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2680 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2681 				mutex_exit(&zfs_share_lock);
2682 				return (ENOSYS);
2683 			}
2684 			if (znfsexport_fs == NULL &&
2685 			    ((znfsexport_fs = (int (*)(void *))
2686 			    ddi_modsym(nfs_mod,
2687 			    "nfs_export", &error)) == NULL)) {
2688 				mutex_exit(&zfs_share_lock);
2689 				return (ENOSYS);
2690 			}
2691 			error = zfs_init_sharefs();
2692 			if (error) {
2693 				mutex_exit(&zfs_share_lock);
2694 				return (ENOSYS);
2695 			}
2696 			zfs_nfsshare_inited = 1;
2697 			mutex_exit(&zfs_share_lock);
2698 		}
2699 		break;
2700 	case ZFS_SHARE_SMB:
2701 	case ZFS_UNSHARE_SMB:
2702 		if (zfs_smbshare_inited == 0) {
2703 			mutex_enter(&zfs_share_lock);
2704 			if (smbsrv_mod == NULL && ((smbsrv_mod =
2705 			    ddi_modopen("drv/smbsrv",
2706 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2707 				mutex_exit(&zfs_share_lock);
2708 				return (ENOSYS);
2709 			}
2710 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2711 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2712 			    "smb_server_share", &error)) == NULL)) {
2713 				mutex_exit(&zfs_share_lock);
2714 				return (ENOSYS);
2715 			}
2716 			error = zfs_init_sharefs();
2717 			if (error) {
2718 				mutex_exit(&zfs_share_lock);
2719 				return (ENOSYS);
2720 			}
2721 			zfs_smbshare_inited = 1;
2722 			mutex_exit(&zfs_share_lock);
2723 		}
2724 		break;
2725 	default:
2726 		return (EINVAL);
2727 	}
2728 
2729 	switch (zc->zc_share.z_sharetype) {
2730 	case ZFS_SHARE_NFS:
2731 	case ZFS_UNSHARE_NFS:
2732 		if (error =
2733 		    znfsexport_fs((void *)
2734 		    (uintptr_t)zc->zc_share.z_exportdata))
2735 			return (error);
2736 		break;
2737 	case ZFS_SHARE_SMB:
2738 	case ZFS_UNSHARE_SMB:
2739 		if (error = zsmbexport_fs((void *)
2740 		    (uintptr_t)zc->zc_share.z_exportdata,
2741 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2742 		    B_TRUE : B_FALSE)) {
2743 			return (error);
2744 		}
2745 		break;
2746 	}
2747 
2748 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2749 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2750 	    SHAREFS_ADD : SHAREFS_REMOVE;
2751 
2752 	/*
2753 	 * Add or remove share from sharetab
2754 	 */
2755 	error = zshare_fs(opcode,
2756 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2757 	    zc->zc_share.z_sharemax);
2758 
2759 	return (error);
2760 
2761 }
2762 
2763 /*
2764  * pool create, destroy, and export don't log the history as part of
2765  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2766  * do the logging of those commands.
2767  */
2768 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2769 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2770 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2771 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2772 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2773 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
2774 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2775 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2776 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2777 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2778 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
2779 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2780 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2781 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2782 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2783 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2784 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2785 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2786 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2787 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2788 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2789 	    DATASET_NAME, B_FALSE },
2790 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2791 	    DATASET_NAME, B_FALSE },
2792 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2793 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2794 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2795 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2796 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2797 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2798 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
2799 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2800 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2801 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
2802 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2803 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2804 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2805 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2806 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2807 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
2808 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2809 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2810 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2811 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2812 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2813 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2814 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2815 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2816 	    DATASET_NAME, B_FALSE },
2817 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2818 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2819 };
2820 
2821 static int
2822 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2823 {
2824 	zfs_cmd_t *zc;
2825 	uint_t vec;
2826 	int error, rc;
2827 
2828 	if (getminor(dev) != 0)
2829 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2830 
2831 	vec = cmd - ZFS_IOC;
2832 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
2833 
2834 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2835 		return (EINVAL);
2836 
2837 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2838 
2839 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2840 
2841 	if (error == 0)
2842 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2843 
2844 	/*
2845 	 * Ensure that all pool/dataset names are valid before we pass down to
2846 	 * the lower layers.
2847 	 */
2848 	if (error == 0) {
2849 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2850 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
2851 		case POOL_NAME:
2852 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2853 				error = EINVAL;
2854 			break;
2855 
2856 		case DATASET_NAME:
2857 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2858 				error = EINVAL;
2859 			break;
2860 
2861 		case NO_NAME:
2862 			break;
2863 		}
2864 	}
2865 
2866 	if (error == 0)
2867 		error = zfs_ioc_vec[vec].zvec_func(zc);
2868 
2869 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
2870 	if (error == 0) {
2871 		error = rc;
2872 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
2873 			zfs_log_history(zc);
2874 	}
2875 
2876 	kmem_free(zc, sizeof (zfs_cmd_t));
2877 	return (error);
2878 }
2879 
2880 static int
2881 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2882 {
2883 	if (cmd != DDI_ATTACH)
2884 		return (DDI_FAILURE);
2885 
2886 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
2887 	    DDI_PSEUDO, 0) == DDI_FAILURE)
2888 		return (DDI_FAILURE);
2889 
2890 	zfs_dip = dip;
2891 
2892 	ddi_report_dev(dip);
2893 
2894 	return (DDI_SUCCESS);
2895 }
2896 
2897 static int
2898 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2899 {
2900 	if (spa_busy() || zfs_busy() || zvol_busy())
2901 		return (DDI_FAILURE);
2902 
2903 	if (cmd != DDI_DETACH)
2904 		return (DDI_FAILURE);
2905 
2906 	zfs_dip = NULL;
2907 
2908 	ddi_prop_remove_all(dip);
2909 	ddi_remove_minor_node(dip, NULL);
2910 
2911 	return (DDI_SUCCESS);
2912 }
2913 
2914 /*ARGSUSED*/
2915 static int
2916 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2917 {
2918 	switch (infocmd) {
2919 	case DDI_INFO_DEVT2DEVINFO:
2920 		*result = zfs_dip;
2921 		return (DDI_SUCCESS);
2922 
2923 	case DDI_INFO_DEVT2INSTANCE:
2924 		*result = (void *)0;
2925 		return (DDI_SUCCESS);
2926 	}
2927 
2928 	return (DDI_FAILURE);
2929 }
2930 
2931 /*
2932  * OK, so this is a little weird.
2933  *
2934  * /dev/zfs is the control node, i.e. minor 0.
2935  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2936  *
2937  * /dev/zfs has basically nothing to do except serve up ioctls,
2938  * so most of the standard driver entry points are in zvol.c.
2939  */
2940 static struct cb_ops zfs_cb_ops = {
2941 	zvol_open,	/* open */
2942 	zvol_close,	/* close */
2943 	zvol_strategy,	/* strategy */
2944 	nodev,		/* print */
2945 	zvol_dump,	/* dump */
2946 	zvol_read,	/* read */
2947 	zvol_write,	/* write */
2948 	zfsdev_ioctl,	/* ioctl */
2949 	nodev,		/* devmap */
2950 	nodev,		/* mmap */
2951 	nodev,		/* segmap */
2952 	nochpoll,	/* poll */
2953 	ddi_prop_op,	/* prop_op */
2954 	NULL,		/* streamtab */
2955 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
2956 	CB_REV,		/* version */
2957 	nodev,		/* async read */
2958 	nodev,		/* async write */
2959 };
2960 
2961 static struct dev_ops zfs_dev_ops = {
2962 	DEVO_REV,	/* version */
2963 	0,		/* refcnt */
2964 	zfs_info,	/* info */
2965 	nulldev,	/* identify */
2966 	nulldev,	/* probe */
2967 	zfs_attach,	/* attach */
2968 	zfs_detach,	/* detach */
2969 	nodev,		/* reset */
2970 	&zfs_cb_ops,	/* driver operations */
2971 	NULL		/* no bus operations */
2972 };
2973 
2974 static struct modldrv zfs_modldrv = {
2975 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
2976 	    &zfs_dev_ops
2977 };
2978 
2979 static struct modlinkage modlinkage = {
2980 	MODREV_1,
2981 	(void *)&zfs_modlfs,
2982 	(void *)&zfs_modldrv,
2983 	NULL
2984 };
2985 
2986 
2987 uint_t zfs_fsyncer_key;
2988 extern uint_t rrw_tsd_key;
2989 
2990 int
2991 _init(void)
2992 {
2993 	int error;
2994 
2995 	spa_init(FREAD | FWRITE);
2996 	zfs_init();
2997 	zvol_init();
2998 
2999 	if ((error = mod_install(&modlinkage)) != 0) {
3000 		zvol_fini();
3001 		zfs_fini();
3002 		spa_fini();
3003 		return (error);
3004 	}
3005 
3006 	tsd_create(&zfs_fsyncer_key, NULL);
3007 	tsd_create(&rrw_tsd_key, NULL);
3008 
3009 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3010 	ASSERT(error == 0);
3011 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3012 
3013 	return (0);
3014 }
3015 
3016 int
3017 _fini(void)
3018 {
3019 	int error;
3020 
3021 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3022 		return (EBUSY);
3023 
3024 	if ((error = mod_remove(&modlinkage)) != 0)
3025 		return (error);
3026 
3027 	zvol_fini();
3028 	zfs_fini();
3029 	spa_fini();
3030 	if (zfs_nfsshare_inited)
3031 		(void) ddi_modclose(nfs_mod);
3032 	if (zfs_smbshare_inited)
3033 		(void) ddi_modclose(smbsrv_mod);
3034 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
3035 		(void) ddi_modclose(sharefs_mod);
3036 
3037 	tsd_destroy(&zfs_fsyncer_key);
3038 	ldi_ident_release(zfs_li);
3039 	zfs_li = NULL;
3040 	mutex_destroy(&zfs_share_lock);
3041 
3042 	return (error);
3043 }
3044 
3045 int
3046 _info(struct modinfo *modinfop)
3047 {
3048 	return (mod_info(&modlinkage, modinfop));
3049 }
3050