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