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