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