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