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