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