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