xref: /titanic_52/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision d2b9c6763c7b3aad7a4c2609ca71e0b02114bca9)
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 2007 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/zap.h>
42 #include <sys/spa.h>
43 #include <sys/spa_impl.h>
44 #include <sys/vdev.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/dmu.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/dsl_deleg.h>
51 #include <sys/dmu_objset.h>
52 #include <sys/ddi.h>
53 #include <sys/sunddi.h>
54 #include <sys/sunldi.h>
55 #include <sys/policy.h>
56 #include <sys/zone.h>
57 #include <sys/nvpair.h>
58 #include <sys/pathname.h>
59 #include <sys/mount.h>
60 #include <sys/sdt.h>
61 #include <sys/fs/zfs.h>
62 #include <sys/zfs_ctldir.h>
63 #include <sys/zvol.h>
64 #include <sharefs/share.h>
65 #include <sys/zfs_znode.h>
66 
67 #include "zfs_namecheck.h"
68 #include "zfs_prop.h"
69 #include "zfs_deleg.h"
70 
71 extern struct modlfs zfs_modlfs;
72 
73 extern void zfs_init(void);
74 extern void zfs_fini(void);
75 
76 ldi_ident_t zfs_li = NULL;
77 dev_info_t *zfs_dip;
78 
79 typedef int zfs_ioc_func_t(zfs_cmd_t *);
80 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
81 
82 typedef struct zfs_ioc_vec {
83 	zfs_ioc_func_t		*zvec_func;
84 	zfs_secpolicy_func_t	*zvec_secpolicy;
85 	enum {
86 		NO_NAME,
87 		POOL_NAME,
88 		DATASET_NAME
89 	} zvec_namecheck;
90 	boolean_t		zvec_his_log;
91 } zfs_ioc_vec_t;
92 
93 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
94 void
95 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
96 {
97 	const char *newfile;
98 	char buf[256];
99 	va_list adx;
100 
101 	/*
102 	 * Get rid of annoying "../common/" prefix to filename.
103 	 */
104 	newfile = strrchr(file, '/');
105 	if (newfile != NULL) {
106 		newfile = newfile + 1; /* Get rid of leading / */
107 	} else {
108 		newfile = file;
109 	}
110 
111 	va_start(adx, fmt);
112 	(void) vsnprintf(buf, sizeof (buf), fmt, adx);
113 	va_end(adx);
114 
115 	/*
116 	 * To get this data, use the zfs-dprintf probe as so:
117 	 * dtrace -q -n 'zfs-dprintf \
118 	 *	/stringof(arg0) == "dbuf.c"/ \
119 	 *	{printf("%s: %s", stringof(arg1), stringof(arg3))}'
120 	 * arg0 = file name
121 	 * arg1 = function name
122 	 * arg2 = line number
123 	 * arg3 = message
124 	 */
125 	DTRACE_PROBE4(zfs__dprintf,
126 	    char *, newfile, char *, func, int, line, char *, buf);
127 }
128 
129 static void
130 history_str_free(char *buf)
131 {
132 	kmem_free(buf, HIS_MAX_RECORD_LEN);
133 }
134 
135 static char *
136 history_str_get(zfs_cmd_t *zc)
137 {
138 	char *buf;
139 
140 	if (zc->zc_history == NULL)
141 		return (NULL);
142 
143 	buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
144 	if (copyinstr((void *)(uintptr_t)zc->zc_history,
145 	    buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
146 		history_str_free(buf);
147 		return (NULL);
148 	}
149 
150 	buf[HIS_MAX_RECORD_LEN -1] = '\0';
151 
152 	return (buf);
153 }
154 
155 static void
156 zfs_log_history(zfs_cmd_t *zc)
157 {
158 	spa_t *spa;
159 	char *buf;
160 
161 	if ((buf = history_str_get(zc)) == NULL)
162 		return;
163 
164 	if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
165 		if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
166 			(void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
167 		spa_close(spa, FTAG);
168 	}
169 	history_str_free(buf);
170 }
171 
172 /*
173  * Policy for top-level read operations (list pools).  Requires no privileges,
174  * and can be used in the local zone, as there is no associated dataset.
175  */
176 /* ARGSUSED */
177 static int
178 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
179 {
180 	return (0);
181 }
182 
183 /*
184  * Policy for dataset read operations (list children, get statistics).  Requires
185  * no privileges, but must be visible in the local zone.
186  */
187 /* ARGSUSED */
188 static int
189 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
190 {
191 	if (INGLOBALZONE(curproc) ||
192 	    zone_dataset_visible(zc->zc_name, NULL))
193 		return (0);
194 
195 	return (ENOENT);
196 }
197 
198 static int
199 zfs_dozonecheck(const char *dataset, cred_t *cr)
200 {
201 	uint64_t zoned;
202 	int writable = 1;
203 
204 	/*
205 	 * The dataset must be visible by this zone -- check this first
206 	 * so they don't see EPERM on something they shouldn't know about.
207 	 */
208 	if (!INGLOBALZONE(curproc) &&
209 	    !zone_dataset_visible(dataset, &writable))
210 		return (ENOENT);
211 
212 	if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
213 		return (ENOENT);
214 
215 	if (INGLOBALZONE(curproc)) {
216 		/*
217 		 * If the fs is zoned, only root can access it from the
218 		 * global zone.
219 		 */
220 		if (secpolicy_zfs(cr) && zoned)
221 			return (EPERM);
222 	} else {
223 		/*
224 		 * If we are in a local zone, the 'zoned' property must be set.
225 		 */
226 		if (!zoned)
227 			return (EPERM);
228 
229 		/* must be writable by this zone */
230 		if (!writable)
231 			return (EPERM);
232 	}
233 	return (0);
234 }
235 
236 int
237 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
238 {
239 	int error;
240 
241 	error = zfs_dozonecheck(name, cr);
242 	if (error == 0) {
243 		error = secpolicy_zfs(cr);
244 		if (error)
245 			error = dsl_deleg_access(name, perm, cr);
246 	}
247 	return (error);
248 }
249 
250 static int
251 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
252 {
253 	/*
254 	 * Check permissions for special properties.
255 	 */
256 	switch (prop) {
257 	case ZFS_PROP_ZONED:
258 		/*
259 		 * Disallow setting of 'zoned' from within a local zone.
260 		 */
261 		if (!INGLOBALZONE(curproc))
262 			return (EPERM);
263 		break;
264 
265 	case ZFS_PROP_QUOTA:
266 		if (!INGLOBALZONE(curproc)) {
267 			uint64_t zoned;
268 			char setpoint[MAXNAMELEN];
269 			/*
270 			 * Unprivileged users are allowed to modify the
271 			 * quota on things *under* (ie. contained by)
272 			 * the thing they own.
273 			 */
274 			if (dsl_prop_get_integer(name, "zoned", &zoned,
275 			    setpoint))
276 				return (EPERM);
277 			if (!zoned || strlen(name) <= strlen(setpoint))
278 				return (EPERM);
279 		}
280 		break;
281 	}
282 
283 	return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
284 }
285 
286 int
287 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
288 {
289 	int error;
290 
291 	error = zfs_dozonecheck(zc->zc_name, cr);
292 	if (error)
293 		return (error);
294 
295 	/*
296 	 * permission to set permissions will be evaluated later in
297 	 * dsl_deleg_can_allow()
298 	 */
299 	return (0);
300 }
301 
302 int
303 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
304 {
305 	int error;
306 	error = zfs_secpolicy_write_perms(zc->zc_name,
307 	    ZFS_DELEG_PERM_ROLLBACK, cr);
308 	if (error == 0)
309 		error = zfs_secpolicy_write_perms(zc->zc_name,
310 		    ZFS_DELEG_PERM_MOUNT, cr);
311 	return (error);
312 }
313 
314 int
315 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
316 {
317 	return (zfs_secpolicy_write_perms(zc->zc_name,
318 	    ZFS_DELEG_PERM_SEND, cr));
319 }
320 
321 int
322 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
323 {
324 	if (!INGLOBALZONE(curproc))
325 		return (EPERM);
326 
327 	if (secpolicy_nfs(CRED()) == 0) {
328 		return (0);
329 	} else {
330 		vnode_t *vp;
331 		int error;
332 
333 		if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
334 		    NO_FOLLOW, NULL, &vp)) != 0)
335 			return (error);
336 
337 		/* Now make sure mntpnt and dataset are ZFS */
338 
339 		if (vp->v_vfsp->vfs_fstype != zfsfstype ||
340 		    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
341 		    zc->zc_name) != 0)) {
342 			VN_RELE(vp);
343 			return (EPERM);
344 		}
345 
346 		VN_RELE(vp);
347 		return (dsl_deleg_access(zc->zc_name,
348 		    ZFS_DELEG_PERM_SHARE, cr));
349 	}
350 }
351 
352 static int
353 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
354 {
355 	char *cp;
356 
357 	/*
358 	 * Remove the @bla or /bla from the end of the name to get the parent.
359 	 */
360 	(void) strncpy(parent, datasetname, parentsize);
361 	cp = strrchr(parent, '@');
362 	if (cp != NULL) {
363 		cp[0] = '\0';
364 	} else {
365 		cp = strrchr(parent, '/');
366 		if (cp == NULL)
367 			return (ENOENT);
368 		cp[0] = '\0';
369 	}
370 
371 	return (0);
372 }
373 
374 int
375 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
376 {
377 	int error;
378 
379 	if ((error = zfs_secpolicy_write_perms(name,
380 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
381 		return (error);
382 
383 	return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
384 }
385 
386 static int
387 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
388 {
389 	return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
390 }
391 
392 /*
393  * Must have sys_config privilege to check the iscsi permission
394  */
395 /* ARGSUSED */
396 static int
397 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
398 {
399 	return (secpolicy_zfs(cr));
400 }
401 
402 int
403 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
404 {
405 	char 	parentname[MAXNAMELEN];
406 	int	error;
407 
408 	if ((error = zfs_secpolicy_write_perms(from,
409 	    ZFS_DELEG_PERM_RENAME, cr)) != 0)
410 		return (error);
411 
412 	if ((error = zfs_secpolicy_write_perms(from,
413 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
414 		return (error);
415 
416 	if ((error = zfs_get_parent(to, parentname,
417 	    sizeof (parentname))) != 0)
418 		return (error);
419 
420 	if ((error = zfs_secpolicy_write_perms(parentname,
421 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
422 		return (error);
423 
424 	if ((error = zfs_secpolicy_write_perms(parentname,
425 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
426 		return (error);
427 
428 	return (error);
429 }
430 
431 static int
432 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
433 {
434 	return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
435 }
436 
437 static int
438 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
439 {
440 	char 	parentname[MAXNAMELEN];
441 	objset_t *clone;
442 	int error;
443 
444 	error = zfs_secpolicy_write_perms(zc->zc_name,
445 	    ZFS_DELEG_PERM_PROMOTE, cr);
446 	if (error)
447 		return (error);
448 
449 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
450 	    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
451 
452 	if (error == 0) {
453 		dsl_dataset_t *pclone = NULL;
454 		dsl_dir_t *dd;
455 		dd = clone->os->os_dsl_dataset->ds_dir;
456 
457 		rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
458 		error = dsl_dataset_open_obj(dd->dd_pool,
459 		    dd->dd_phys->dd_clone_parent_obj, NULL,
460 		    DS_MODE_NONE, FTAG, &pclone);
461 		rw_exit(&dd->dd_pool->dp_config_rwlock);
462 		if (error) {
463 			dmu_objset_close(clone);
464 			return (error);
465 		}
466 
467 		error = zfs_secpolicy_write_perms(zc->zc_name,
468 		    ZFS_DELEG_PERM_MOUNT, cr);
469 
470 		dsl_dataset_name(pclone, parentname);
471 		dmu_objset_close(clone);
472 		dsl_dataset_close(pclone, DS_MODE_NONE, FTAG);
473 		if (error == 0)
474 			error = zfs_secpolicy_write_perms(parentname,
475 			    ZFS_DELEG_PERM_PROMOTE, cr);
476 	}
477 	return (error);
478 }
479 
480 static int
481 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
482 {
483 	int error;
484 
485 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
486 	    ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
487 		return (error);
488 
489 	if ((error = zfs_secpolicy_write_perms(zc->zc_name,
490 	    ZFS_DELEG_PERM_MOUNT, cr)) != 0)
491 		return (error);
492 
493 	return (zfs_secpolicy_write_perms(zc->zc_name,
494 	    ZFS_DELEG_PERM_CREATE, cr));
495 }
496 
497 int
498 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
499 {
500 	int error;
501 
502 	if ((error = zfs_secpolicy_write_perms(name,
503 	    ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
504 		return (error);
505 
506 	error = zfs_secpolicy_write_perms(name,
507 	    ZFS_DELEG_PERM_MOUNT, cr);
508 
509 	return (error);
510 }
511 
512 static int
513 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
514 {
515 
516 	return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
517 }
518 
519 static int
520 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
521 {
522 	char 	parentname[MAXNAMELEN];
523 	int 	error;
524 
525 	if ((error = zfs_get_parent(zc->zc_name, parentname,
526 	    sizeof (parentname))) != 0)
527 		return (error);
528 
529 	if (zc->zc_value[0] != '\0') {
530 		if ((error = zfs_secpolicy_write_perms(zc->zc_value,
531 		    ZFS_DELEG_PERM_CLONE, cr)) != 0)
532 			return (error);
533 	}
534 
535 	if ((error = zfs_secpolicy_write_perms(parentname,
536 	    ZFS_DELEG_PERM_CREATE, cr)) != 0)
537 		return (error);
538 
539 	error = zfs_secpolicy_write_perms(parentname,
540 	    ZFS_DELEG_PERM_MOUNT, cr);
541 
542 	return (error);
543 }
544 
545 static int
546 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
547 {
548 	int error;
549 
550 	error = secpolicy_fs_unmount(cr, NULL);
551 	if (error) {
552 		error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
553 	}
554 	return (error);
555 }
556 
557 /*
558  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
559  * SYS_CONFIG privilege, which is not available in a local zone.
560  */
561 /* ARGSUSED */
562 static int
563 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
564 {
565 	if (secpolicy_sys_config(cr, B_FALSE) != 0)
566 		return (EPERM);
567 
568 	return (0);
569 }
570 
571 /*
572  * Just like zfs_secpolicy_config, except that we will check for
573  * mount permission on the dataset for permission to create/remove
574  * the minor nodes.
575  */
576 static int
577 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
578 {
579 	if (secpolicy_sys_config(cr, B_FALSE) != 0) {
580 		return (dsl_deleg_access(zc->zc_name,
581 		    ZFS_DELEG_PERM_MOUNT, cr));
582 	}
583 
584 	return (0);
585 }
586 
587 /*
588  * Policy for fault injection.  Requires all privileges.
589  */
590 /* ARGSUSED */
591 static int
592 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
593 {
594 	return (secpolicy_zinject(cr));
595 }
596 
597 static int
598 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
599 {
600 	zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
601 
602 	if (prop == ZFS_PROP_INVAL) {
603 		if (!zfs_prop_user(zc->zc_value))
604 			return (EINVAL);
605 		return (zfs_secpolicy_write_perms(zc->zc_name,
606 		    ZFS_DELEG_PERM_USERPROP, cr));
607 	} else {
608 		if (!zfs_prop_inheritable(prop))
609 			return (EINVAL);
610 		return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
611 	}
612 }
613 
614 /*
615  * Returns the nvlist as specified by the user in the zfs_cmd_t.
616  */
617 static int
618 get_nvlist(zfs_cmd_t *zc, nvlist_t **nvp)
619 {
620 	char *packed;
621 	size_t size;
622 	int error;
623 	nvlist_t *config = NULL;
624 
625 	/*
626 	 * Read in and unpack the user-supplied nvlist.
627 	 */
628 	if ((size = zc->zc_nvlist_src_size) == 0)
629 		return (EINVAL);
630 
631 	packed = kmem_alloc(size, KM_SLEEP);
632 
633 	if ((error = xcopyin((void *)(uintptr_t)zc->zc_nvlist_src, packed,
634 	    size)) != 0) {
635 		kmem_free(packed, size);
636 		return (error);
637 	}
638 
639 	if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) {
640 		kmem_free(packed, size);
641 		return (error);
642 	}
643 
644 	kmem_free(packed, size);
645 
646 	*nvp = config;
647 	return (0);
648 }
649 
650 static int
651 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
652 {
653 	char *packed = NULL;
654 	size_t size;
655 	int error;
656 
657 	VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
658 
659 	if (size > zc->zc_nvlist_dst_size) {
660 		error = ENOMEM;
661 	} else {
662 		packed = kmem_alloc(size, KM_SLEEP);
663 		VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
664 		    KM_SLEEP) == 0);
665 		error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
666 		    size);
667 		kmem_free(packed, size);
668 	}
669 
670 	zc->zc_nvlist_dst_size = size;
671 	return (error);
672 }
673 
674 static int
675 zfs_ioc_pool_create(zfs_cmd_t *zc)
676 {
677 	int error;
678 	nvlist_t *config;
679 	char *buf;
680 
681 	if ((error = get_nvlist(zc, &config)) != 0)
682 		return (error);
683 
684 	buf = history_str_get(zc);
685 
686 	error = spa_create(zc->zc_name, config, zc->zc_value[0] == '\0' ?
687 	    NULL : zc->zc_value, buf);
688 
689 	if (buf != NULL)
690 		history_str_free(buf);
691 	nvlist_free(config);
692 
693 	return (error);
694 }
695 
696 static int
697 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
698 {
699 	int error;
700 	zfs_log_history(zc);
701 	error = spa_destroy(zc->zc_name);
702 	return (error);
703 }
704 
705 static int
706 zfs_ioc_pool_import(zfs_cmd_t *zc)
707 {
708 	int error;
709 	nvlist_t *config;
710 	uint64_t guid;
711 
712 	if ((error = get_nvlist(zc, &config)) != 0)
713 		return (error);
714 
715 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
716 	    guid != zc->zc_guid)
717 		error = EINVAL;
718 	else
719 		error = spa_import(zc->zc_name, config,
720 		    zc->zc_value[0] == '\0' ? NULL : zc->zc_value);
721 
722 	nvlist_free(config);
723 
724 	return (error);
725 }
726 
727 static int
728 zfs_ioc_pool_export(zfs_cmd_t *zc)
729 {
730 	int error;
731 	zfs_log_history(zc);
732 	error = spa_export(zc->zc_name, NULL);
733 	return (error);
734 }
735 
736 static int
737 zfs_ioc_pool_configs(zfs_cmd_t *zc)
738 {
739 	nvlist_t *configs;
740 	int error;
741 
742 	if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
743 		return (EEXIST);
744 
745 	error = put_nvlist(zc, configs);
746 
747 	nvlist_free(configs);
748 
749 	return (error);
750 }
751 
752 static int
753 zfs_ioc_pool_stats(zfs_cmd_t *zc)
754 {
755 	nvlist_t *config;
756 	int error;
757 	int ret = 0;
758 
759 	error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
760 	    sizeof (zc->zc_value));
761 
762 	if (config != NULL) {
763 		ret = put_nvlist(zc, config);
764 		nvlist_free(config);
765 
766 		/*
767 		 * The config may be present even if 'error' is non-zero.
768 		 * In this case we return success, and preserve the real errno
769 		 * in 'zc_cookie'.
770 		 */
771 		zc->zc_cookie = error;
772 	} else {
773 		ret = error;
774 	}
775 
776 	return (ret);
777 }
778 
779 /*
780  * Try to import the given pool, returning pool stats as appropriate so that
781  * user land knows which devices are available and overall pool health.
782  */
783 static int
784 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
785 {
786 	nvlist_t *tryconfig, *config;
787 	int error;
788 
789 	if ((error = get_nvlist(zc, &tryconfig)) != 0)
790 		return (error);
791 
792 	config = spa_tryimport(tryconfig);
793 
794 	nvlist_free(tryconfig);
795 
796 	if (config == NULL)
797 		return (EINVAL);
798 
799 	error = put_nvlist(zc, config);
800 	nvlist_free(config);
801 
802 	return (error);
803 }
804 
805 static int
806 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
807 {
808 	spa_t *spa;
809 	int error;
810 
811 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
812 		return (error);
813 
814 	mutex_enter(&spa_namespace_lock);
815 	error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
816 	mutex_exit(&spa_namespace_lock);
817 
818 	spa_close(spa, FTAG);
819 
820 	return (error);
821 }
822 
823 static int
824 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
825 {
826 	spa_t *spa;
827 	int error;
828 
829 	error = spa_open(zc->zc_name, &spa, FTAG);
830 	if (error == 0) {
831 		spa_freeze(spa);
832 		spa_close(spa, FTAG);
833 	}
834 	return (error);
835 }
836 
837 static int
838 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
839 {
840 	spa_t *spa;
841 	int error;
842 
843 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
844 		return (error);
845 
846 	spa_upgrade(spa);
847 	spa_close(spa, FTAG);
848 
849 	return (error);
850 }
851 
852 static int
853 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
854 {
855 	spa_t *spa;
856 	char *hist_buf;
857 	uint64_t size;
858 	int error;
859 
860 	if ((size = zc->zc_history_len) == 0)
861 		return (EINVAL);
862 
863 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
864 		return (error);
865 
866 	if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
867 		spa_close(spa, FTAG);
868 		return (ENOTSUP);
869 	}
870 
871 	hist_buf = kmem_alloc(size, KM_SLEEP);
872 	if ((error = spa_history_get(spa, &zc->zc_history_offset,
873 	    &zc->zc_history_len, hist_buf)) == 0) {
874 		error = xcopyout(hist_buf,
875 		    (char *)(uintptr_t)zc->zc_history,
876 		    zc->zc_history_len);
877 	}
878 
879 	spa_close(spa, FTAG);
880 	kmem_free(hist_buf, size);
881 	return (error);
882 }
883 
884 static int
885 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
886 {
887 	int error;
888 
889 	if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
890 		return (error);
891 
892 	return (0);
893 }
894 
895 static int
896 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
897 {
898 	objset_t *osp;
899 	int error;
900 
901 	if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
902 	    DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
903 		return (error);
904 
905 	error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
906 	    sizeof (zc->zc_value));
907 	dmu_objset_close(osp);
908 
909 	return (error);
910 }
911 
912 static int
913 zfs_ioc_vdev_add(zfs_cmd_t *zc)
914 {
915 	spa_t *spa;
916 	int error;
917 	nvlist_t *config;
918 
919 	error = spa_open(zc->zc_name, &spa, FTAG);
920 	if (error != 0)
921 		return (error);
922 
923 	/*
924 	 * A root pool with concatenated devices is not supported.
925 	 * Thus, can not add a device to a root pool with one device.
926 	 */
927 	if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0) {
928 		spa_close(spa, FTAG);
929 		return (EDOM);
930 	}
931 
932 	if ((error = get_nvlist(zc, &config)) == 0) {
933 		error = spa_vdev_add(spa, config);
934 		nvlist_free(config);
935 	}
936 	spa_close(spa, FTAG);
937 	return (error);
938 }
939 
940 static int
941 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
942 {
943 	spa_t *spa;
944 	int error;
945 
946 	error = spa_open(zc->zc_name, &spa, FTAG);
947 	if (error != 0)
948 		return (error);
949 	error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
950 	spa_close(spa, FTAG);
951 	return (error);
952 }
953 
954 static int
955 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
956 {
957 	spa_t *spa;
958 	int error;
959 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
960 
961 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
962 		return (error);
963 	switch (zc->zc_cookie) {
964 	case VDEV_STATE_ONLINE:
965 		error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
966 		break;
967 
968 	case VDEV_STATE_OFFLINE:
969 		error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
970 		break;
971 
972 	case VDEV_STATE_FAULTED:
973 		error = vdev_fault(spa, zc->zc_guid);
974 		break;
975 
976 	case VDEV_STATE_DEGRADED:
977 		error = vdev_degrade(spa, zc->zc_guid);
978 		break;
979 
980 	default:
981 		error = EINVAL;
982 	}
983 	zc->zc_cookie = newstate;
984 	spa_close(spa, FTAG);
985 	return (error);
986 }
987 
988 static int
989 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
990 {
991 	spa_t *spa;
992 	int replacing = zc->zc_cookie;
993 	nvlist_t *config;
994 	int error;
995 
996 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
997 		return (error);
998 
999 	if ((error = get_nvlist(zc, &config)) == 0) {
1000 		error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1001 		nvlist_free(config);
1002 	}
1003 
1004 	spa_close(spa, FTAG);
1005 	return (error);
1006 }
1007 
1008 static int
1009 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1010 {
1011 	spa_t *spa;
1012 	int error;
1013 
1014 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1015 		return (error);
1016 
1017 	error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1018 
1019 	spa_close(spa, FTAG);
1020 	return (error);
1021 }
1022 
1023 static int
1024 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1025 {
1026 	spa_t *spa;
1027 	char *path = zc->zc_value;
1028 	uint64_t guid = zc->zc_guid;
1029 	int error;
1030 
1031 	error = spa_open(zc->zc_name, &spa, FTAG);
1032 	if (error != 0)
1033 		return (error);
1034 
1035 	error = spa_vdev_setpath(spa, guid, path);
1036 	spa_close(spa, FTAG);
1037 	return (error);
1038 }
1039 
1040 static int
1041 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1042 {
1043 	objset_t *os = NULL;
1044 	int error;
1045 	nvlist_t *nv;
1046 
1047 retry:
1048 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1049 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1050 	if (error != 0) {
1051 		/*
1052 		 * This is ugly: dmu_objset_open() can return EBUSY if
1053 		 * the objset is held exclusively. Fortunately this hold is
1054 		 * only for a short while, so we retry here.
1055 		 * This avoids user code having to handle EBUSY,
1056 		 * for example for a "zfs list".
1057 		 */
1058 		if (error == EBUSY) {
1059 			delay(1);
1060 			goto retry;
1061 		}
1062 		return (error);
1063 	}
1064 
1065 	dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1066 
1067 	if (zc->zc_nvlist_dst != 0 &&
1068 	    (error = dsl_prop_get_all(os, &nv)) == 0) {
1069 		dmu_objset_stats(os, nv);
1070 		/*
1071 		 * NB: {zpl,zvol}_get_stats() will read the objset contents,
1072 		 * which we aren't supposed to do with a
1073 		 * DS_MODE_STANDARD open, because it could be
1074 		 * inconsistent.  So this is a bit of a workaround...
1075 		 */
1076 		if (!zc->zc_objset_stats.dds_inconsistent) {
1077 			if (dmu_objset_type(os) == DMU_OST_ZVOL)
1078 				VERIFY(zvol_get_stats(os, nv) == 0);
1079 			else if (dmu_objset_type(os) == DMU_OST_ZFS)
1080 				(void) zfs_get_stats(os, nv);
1081 		}
1082 		error = put_nvlist(zc, nv);
1083 		nvlist_free(nv);
1084 	}
1085 
1086 	spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
1087 
1088 	dmu_objset_close(os);
1089 	return (error);
1090 }
1091 
1092 static int
1093 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1094 {
1095 	objset_t *os;
1096 	int error;
1097 	char *p;
1098 
1099 retry:
1100 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1101 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1102 	if (error != 0) {
1103 		/*
1104 		 * This is ugly: dmu_objset_open() can return EBUSY if
1105 		 * the objset is held exclusively. Fortunately this hold is
1106 		 * only for a short while, so we retry here.
1107 		 * This avoids user code having to handle EBUSY,
1108 		 * for example for a "zfs list".
1109 		 */
1110 		if (error == EBUSY) {
1111 			delay(1);
1112 			goto retry;
1113 		}
1114 		if (error == ENOENT)
1115 			error = ESRCH;
1116 		return (error);
1117 	}
1118 
1119 	p = strrchr(zc->zc_name, '/');
1120 	if (p == NULL || p[1] != '\0')
1121 		(void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1122 	p = zc->zc_name + strlen(zc->zc_name);
1123 
1124 	do {
1125 		error = dmu_dir_list_next(os,
1126 		    sizeof (zc->zc_name) - (p - zc->zc_name), p,
1127 		    NULL, &zc->zc_cookie);
1128 		if (error == ENOENT)
1129 			error = ESRCH;
1130 	} while (error == 0 && !INGLOBALZONE(curproc) &&
1131 	    !zone_dataset_visible(zc->zc_name, NULL));
1132 
1133 	/*
1134 	 * If it's a hidden dataset (ie. with a '$' in its name), don't
1135 	 * try to get stats for it.  Userland will skip over it.
1136 	 */
1137 	if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1138 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1139 
1140 	dmu_objset_close(os);
1141 	return (error);
1142 }
1143 
1144 static int
1145 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1146 {
1147 	objset_t *os;
1148 	int error;
1149 
1150 retry:
1151 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1152 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1153 	if (error != 0) {
1154 		/*
1155 		 * This is ugly: dmu_objset_open() can return EBUSY if
1156 		 * the objset is held exclusively. Fortunately this hold is
1157 		 * only for a short while, so we retry here.
1158 		 * This avoids user code having to handle EBUSY,
1159 		 * for example for a "zfs list".
1160 		 */
1161 		if (error == EBUSY) {
1162 			delay(1);
1163 			goto retry;
1164 		}
1165 		if (error == ENOENT)
1166 			error = ESRCH;
1167 		return (error);
1168 	}
1169 
1170 	/*
1171 	 * A dataset name of maximum length cannot have any snapshots,
1172 	 * so exit immediately.
1173 	 */
1174 	if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1175 		dmu_objset_close(os);
1176 		return (ESRCH);
1177 	}
1178 
1179 	error = dmu_snapshot_list_next(os,
1180 	    sizeof (zc->zc_name) - strlen(zc->zc_name),
1181 	    zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
1182 	if (error == ENOENT)
1183 		error = ESRCH;
1184 
1185 	if (error == 0)
1186 		error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1187 
1188 	dmu_objset_close(os);
1189 	return (error);
1190 }
1191 
1192 static int
1193 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1194 {
1195 	nvpair_t *elem;
1196 	int error;
1197 	uint64_t intval;
1198 	char *strval;
1199 
1200 	/*
1201 	 * First validate permission to set all of the properties
1202 	 */
1203 	elem = NULL;
1204 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1205 		const char *propname = nvpair_name(elem);
1206 		zfs_prop_t prop = zfs_name_to_prop(propname);
1207 
1208 		if (prop == ZFS_PROP_INVAL) {
1209 			/*
1210 			 * If this is a user-defined property, it must be a
1211 			 * string, and there is no further validation to do.
1212 			 */
1213 			if (!zfs_prop_user(propname) ||
1214 			    nvpair_type(elem) != DATA_TYPE_STRING)
1215 				return (EINVAL);
1216 
1217 			error = zfs_secpolicy_write_perms(name,
1218 			    ZFS_DELEG_PERM_USERPROP, CRED());
1219 			if (error)
1220 				return (error);
1221 			continue;
1222 		}
1223 
1224 		if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1225 			return (error);
1226 
1227 		/*
1228 		 * Check that this value is valid for this pool version
1229 		 */
1230 		switch (prop) {
1231 		case ZFS_PROP_COMPRESSION:
1232 			/*
1233 			 * If the user specified gzip compression, make sure
1234 			 * the SPA supports it. We ignore any errors here since
1235 			 * we'll catch them later.
1236 			 */
1237 			if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1238 			    nvpair_value_uint64(elem, &intval) == 0 &&
1239 			    intval >= ZIO_COMPRESS_GZIP_1 &&
1240 			    intval <= ZIO_COMPRESS_GZIP_9) {
1241 				spa_t *spa;
1242 
1243 				if (spa_open(name, &spa, FTAG) == 0) {
1244 					if (spa_version(spa) <
1245 					    SPA_VERSION_GZIP_COMPRESSION) {
1246 						spa_close(spa, FTAG);
1247 						return (ENOTSUP);
1248 					}
1249 
1250 					spa_close(spa, FTAG);
1251 				}
1252 			}
1253 			break;
1254 
1255 		case ZFS_PROP_COPIES:
1256 		{
1257 			spa_t *spa;
1258 
1259 			if (spa_open(name, &spa, FTAG) == 0) {
1260 				if (spa_version(spa) <
1261 				    SPA_VERSION_DITTO_BLOCKS) {
1262 					spa_close(spa, FTAG);
1263 					return (ENOTSUP);
1264 				}
1265 				spa_close(spa, FTAG);
1266 			}
1267 			break;
1268 		}
1269 		}
1270 	}
1271 
1272 	elem = NULL;
1273 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1274 		const char *propname = nvpair_name(elem);
1275 		zfs_prop_t prop = zfs_name_to_prop(propname);
1276 
1277 		if (prop == ZFS_PROP_INVAL) {
1278 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1279 			error = dsl_prop_set(name, propname, 1,
1280 			    strlen(strval) + 1, strval);
1281 			if (error == 0)
1282 				continue;
1283 			else
1284 				return (error);
1285 		}
1286 
1287 		switch (prop) {
1288 		case ZFS_PROP_QUOTA:
1289 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1290 			    (error = dsl_dir_set_quota(name, intval)) != 0)
1291 				return (error);
1292 			break;
1293 
1294 		case ZFS_PROP_RESERVATION:
1295 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1296 			    (error = dsl_dir_set_reservation(name,
1297 			    intval)) != 0)
1298 				return (error);
1299 			break;
1300 
1301 		case ZFS_PROP_VOLSIZE:
1302 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1303 			    (error = zvol_set_volsize(name,
1304 			    ddi_driver_major(zfs_dip), intval)) != 0)
1305 				return (error);
1306 			break;
1307 
1308 		case ZFS_PROP_VOLBLOCKSIZE:
1309 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1310 			    (error = zvol_set_volblocksize(name, intval)) != 0)
1311 				return (error);
1312 			break;
1313 
1314 		case ZFS_PROP_VERSION:
1315 			if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1316 			    (error = zfs_set_version(name, intval)) != 0)
1317 				return (error);
1318 			break;
1319 
1320 		default:
1321 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1322 				if (zfs_prop_get_type(prop) !=
1323 				    PROP_TYPE_STRING)
1324 					return (EINVAL);
1325 				VERIFY(nvpair_value_string(elem, &strval) == 0);
1326 				if ((error = dsl_prop_set(name,
1327 				    nvpair_name(elem), 1, strlen(strval) + 1,
1328 				    strval)) != 0)
1329 					return (error);
1330 			} else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1331 				const char *unused;
1332 
1333 				VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1334 
1335 				switch (zfs_prop_get_type(prop)) {
1336 				case PROP_TYPE_NUMBER:
1337 					break;
1338 				case PROP_TYPE_STRING:
1339 					return (EINVAL);
1340 				case PROP_TYPE_INDEX:
1341 					if (zfs_prop_index_to_string(prop,
1342 					    intval, &unused) != 0)
1343 						return (EINVAL);
1344 					break;
1345 				default:
1346 					cmn_err(CE_PANIC,
1347 					    "unknown property type");
1348 					break;
1349 				}
1350 
1351 				if ((error = dsl_prop_set(name, propname,
1352 				    8, 1, &intval)) != 0)
1353 					return (error);
1354 			} else {
1355 				return (EINVAL);
1356 			}
1357 			break;
1358 		}
1359 	}
1360 
1361 	return (0);
1362 }
1363 
1364 static int
1365 zfs_ioc_set_prop(zfs_cmd_t *zc)
1366 {
1367 	nvlist_t *nvl;
1368 	int error;
1369 
1370 	if ((error = get_nvlist(zc, &nvl)) != 0)
1371 		return (error);
1372 
1373 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1374 
1375 	nvlist_free(nvl);
1376 	return (error);
1377 }
1378 
1379 static int
1380 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1381 {
1382 	/* the property name has been validated by zfs_secpolicy_inherit() */
1383 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1384 }
1385 
1386 static int
1387 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1388 {
1389 	nvlist_t *nvl;
1390 	int error, reset_bootfs = 0;
1391 	uint64_t objnum;
1392 	uint64_t intval;
1393 	zpool_prop_t prop;
1394 	nvpair_t *elem;
1395 	char *propname, *strval;
1396 	spa_t *spa;
1397 	vdev_t *rvdev;
1398 	char *vdev_type;
1399 	objset_t *os;
1400 
1401 	if ((error = get_nvlist(zc, &nvl)) != 0)
1402 		return (error);
1403 
1404 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1405 		nvlist_free(nvl);
1406 		return (error);
1407 	}
1408 
1409 	if (spa_version(spa) < SPA_VERSION_BOOTFS) {
1410 		nvlist_free(nvl);
1411 		spa_close(spa, FTAG);
1412 		return (ENOTSUP);
1413 	}
1414 
1415 	elem = NULL;
1416 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1417 
1418 		propname = nvpair_name(elem);
1419 
1420 		if ((prop = zpool_name_to_prop(propname)) ==
1421 		    ZFS_PROP_INVAL) {
1422 			nvlist_free(nvl);
1423 			spa_close(spa, FTAG);
1424 			return (EINVAL);
1425 		}
1426 
1427 		switch (prop) {
1428 		case ZPOOL_PROP_DELEGATION:
1429 			VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1430 			if (intval > 1)
1431 				error = EINVAL;
1432 			break;
1433 		case ZPOOL_PROP_BOOTFS:
1434 			/*
1435 			 * A bootable filesystem can not be on a RAIDZ pool
1436 			 * nor a striped pool with more than 1 device.
1437 			 */
1438 			rvdev = spa->spa_root_vdev;
1439 			vdev_type =
1440 			    rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
1441 			if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
1442 			    (strcmp(vdev_type, VDEV_TYPE_MIRROR) != 0 &&
1443 			    rvdev->vdev_children > 1)) {
1444 				error = ENOTSUP;
1445 				break;
1446 			}
1447 
1448 			reset_bootfs = 1;
1449 
1450 			VERIFY(nvpair_value_string(elem, &strval) == 0);
1451 			if (strval == NULL || strval[0] == '\0') {
1452 				objnum = zpool_prop_default_numeric(
1453 				    ZPOOL_PROP_BOOTFS);
1454 				break;
1455 			}
1456 
1457 			if (error = dmu_objset_open(strval, DMU_OST_ZFS,
1458 			    DS_MODE_STANDARD | DS_MODE_READONLY, &os))
1459 				break;
1460 			objnum = dmu_objset_id(os);
1461 			dmu_objset_close(os);
1462 			break;
1463 		}
1464 
1465 		if (error)
1466 			break;
1467 	}
1468 	if (error == 0) {
1469 		if (reset_bootfs) {
1470 			VERIFY(nvlist_remove(nvl,
1471 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1472 			    DATA_TYPE_STRING) == 0);
1473 			VERIFY(nvlist_add_uint64(nvl,
1474 			    zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1475 			    objnum) == 0);
1476 		}
1477 		error = spa_set_props(spa, nvl);
1478 	}
1479 
1480 	nvlist_free(nvl);
1481 	spa_close(spa, FTAG);
1482 
1483 	return (error);
1484 }
1485 
1486 static int
1487 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1488 {
1489 	spa_t *spa;
1490 	int error;
1491 	nvlist_t *nvp = NULL;
1492 
1493 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1494 		return (error);
1495 
1496 	error = spa_get_props(spa, &nvp);
1497 
1498 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1499 		error = put_nvlist(zc, nvp);
1500 	else
1501 		error = EFAULT;
1502 
1503 	spa_close(spa, FTAG);
1504 
1505 	if (nvp)
1506 		nvlist_free(nvp);
1507 	return (error);
1508 }
1509 
1510 static int
1511 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1512 {
1513 	nvlist_t *nvp;
1514 	int error;
1515 	uint32_t uid;
1516 	uint32_t gid;
1517 	uint32_t *groups;
1518 	uint_t group_cnt;
1519 	cred_t	*usercred;
1520 
1521 	if ((error = get_nvlist(zc, &nvp)) != 0) {
1522 		return (error);
1523 	}
1524 
1525 	if ((error = nvlist_lookup_uint32(nvp,
1526 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1527 		nvlist_free(nvp);
1528 		return (EPERM);
1529 	}
1530 
1531 	if ((error = nvlist_lookup_uint32(nvp,
1532 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1533 		nvlist_free(nvp);
1534 		return (EPERM);
1535 	}
1536 
1537 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1538 	    &groups, &group_cnt)) != 0) {
1539 		nvlist_free(nvp);
1540 		return (EPERM);
1541 	}
1542 	usercred = cralloc();
1543 	if ((crsetugid(usercred, uid, gid) != 0) ||
1544 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1545 		nvlist_free(nvp);
1546 		crfree(usercred);
1547 		return (EPERM);
1548 	}
1549 	nvlist_free(nvp);
1550 	error = dsl_deleg_access(zc->zc_name,
1551 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1552 	crfree(usercred);
1553 	return (error);
1554 }
1555 
1556 static int
1557 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1558 {
1559 	int error;
1560 	nvlist_t *fsaclnv = NULL;
1561 
1562 	if ((error = get_nvlist(zc, &fsaclnv)) != 0)
1563 		return (error);
1564 
1565 	/*
1566 	 * Verify nvlist is constructed correctly
1567 	 */
1568 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1569 		nvlist_free(fsaclnv);
1570 		return (EINVAL);
1571 	}
1572 
1573 	/*
1574 	 * If we don't have PRIV_SYS_MOUNT, then validate
1575 	 * that user is allowed to hand out each permission in
1576 	 * the nvlist(s)
1577 	 */
1578 
1579 	error = secpolicy_zfs(CRED());
1580 	if (error) {
1581 		if (zc->zc_perm_action == B_FALSE) {
1582 			error = dsl_deleg_can_allow(zc->zc_name,
1583 			    fsaclnv, CRED());
1584 		} else {
1585 			error = dsl_deleg_can_unallow(zc->zc_name,
1586 			    fsaclnv, CRED());
1587 		}
1588 	}
1589 
1590 	if (error == 0)
1591 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1592 
1593 	nvlist_free(fsaclnv);
1594 	return (error);
1595 }
1596 
1597 static int
1598 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1599 {
1600 	nvlist_t *nvp;
1601 	int error;
1602 
1603 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1604 		error = put_nvlist(zc, nvp);
1605 		nvlist_free(nvp);
1606 	}
1607 
1608 	return (error);
1609 }
1610 
1611 static int
1612 zfs_ioc_create_minor(zfs_cmd_t *zc)
1613 {
1614 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1615 }
1616 
1617 static int
1618 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1619 {
1620 	return (zvol_remove_minor(zc->zc_name));
1621 }
1622 
1623 /*
1624  * Search the vfs list for a specified resource.  Returns a pointer to it
1625  * or NULL if no suitable entry is found. The caller of this routine
1626  * is responsible for releasing the returned vfs pointer.
1627  */
1628 static vfs_t *
1629 zfs_get_vfs(const char *resource)
1630 {
1631 	struct vfs *vfsp;
1632 	struct vfs *vfs_found = NULL;
1633 
1634 	vfs_list_read_lock();
1635 	vfsp = rootvfs;
1636 	do {
1637 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1638 			VFS_HOLD(vfsp);
1639 			vfs_found = vfsp;
1640 			break;
1641 		}
1642 		vfsp = vfsp->vfs_next;
1643 	} while (vfsp != rootvfs);
1644 	vfs_list_unlock();
1645 	return (vfs_found);
1646 }
1647 
1648 /* ARGSUSED */
1649 static void
1650 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1651 {
1652 	nvlist_t *nvprops = arg;
1653 	uint64_t version = ZPL_VERSION;
1654 
1655 	(void) nvlist_lookup_uint64(nvprops,
1656 	    zfs_prop_to_name(ZFS_PROP_VERSION), &version);
1657 
1658 	zfs_create_fs(os, cr, version, tx);
1659 }
1660 
1661 static int
1662 zfs_ioc_create(zfs_cmd_t *zc)
1663 {
1664 	objset_t *clone;
1665 	int error = 0;
1666 	nvlist_t *nvprops = NULL;
1667 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
1668 	dmu_objset_type_t type = zc->zc_objset_type;
1669 
1670 	switch (type) {
1671 
1672 	case DMU_OST_ZFS:
1673 		cbfunc = zfs_create_cb;
1674 		break;
1675 
1676 	case DMU_OST_ZVOL:
1677 		cbfunc = zvol_create_cb;
1678 		break;
1679 
1680 	default:
1681 		cbfunc = NULL;
1682 	}
1683 	if (strchr(zc->zc_name, '@'))
1684 		return (EINVAL);
1685 
1686 	if (zc->zc_nvlist_src != NULL &&
1687 	    (error = get_nvlist(zc, &nvprops)) != 0)
1688 		return (error);
1689 
1690 	if (zc->zc_value[0] != '\0') {
1691 		/*
1692 		 * We're creating a clone of an existing snapshot.
1693 		 */
1694 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1695 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1696 			nvlist_free(nvprops);
1697 			return (EINVAL);
1698 		}
1699 
1700 		error = dmu_objset_open(zc->zc_value, type,
1701 		    DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1702 		if (error) {
1703 			nvlist_free(nvprops);
1704 			return (error);
1705 		}
1706 		error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1707 		dmu_objset_close(clone);
1708 	} else {
1709 		if (cbfunc == NULL) {
1710 			nvlist_free(nvprops);
1711 			return (EINVAL);
1712 		}
1713 
1714 		if (type == DMU_OST_ZVOL) {
1715 			uint64_t volsize, volblocksize;
1716 
1717 			if (nvprops == NULL ||
1718 			    nvlist_lookup_uint64(nvprops,
1719 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1720 			    &volsize) != 0) {
1721 				nvlist_free(nvprops);
1722 				return (EINVAL);
1723 			}
1724 
1725 			if ((error = nvlist_lookup_uint64(nvprops,
1726 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1727 			    &volblocksize)) != 0 && error != ENOENT) {
1728 				nvlist_free(nvprops);
1729 				return (EINVAL);
1730 			}
1731 
1732 			if (error != 0)
1733 				volblocksize = zfs_prop_default_numeric(
1734 				    ZFS_PROP_VOLBLOCKSIZE);
1735 
1736 			if ((error = zvol_check_volblocksize(
1737 			    volblocksize)) != 0 ||
1738 			    (error = zvol_check_volsize(volsize,
1739 			    volblocksize)) != 0) {
1740 				nvlist_free(nvprops);
1741 				return (error);
1742 			}
1743 		} else if (type == DMU_OST_ZFS) {
1744 			uint64_t version;
1745 
1746 			if (0 == nvlist_lookup_uint64(nvprops,
1747 			    zfs_prop_to_name(ZFS_PROP_VERSION), &version) &&
1748 			    (version < ZPL_VERSION_INITIAL ||
1749 			    version > ZPL_VERSION)) {
1750 				nvlist_free(nvprops);
1751 				return (EINVAL);
1752 			}
1753 		}
1754 
1755 		error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
1756 		    nvprops);
1757 	}
1758 
1759 	/*
1760 	 * It would be nice to do this atomically.
1761 	 */
1762 	if (error == 0) {
1763 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
1764 			(void) dmu_objset_destroy(zc->zc_name);
1765 	}
1766 
1767 	nvlist_free(nvprops);
1768 	return (error);
1769 }
1770 
1771 static int
1772 zfs_ioc_snapshot(zfs_cmd_t *zc)
1773 {
1774 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1775 		return (EINVAL);
1776 	return (dmu_objset_snapshot(zc->zc_name,
1777 	    zc->zc_value, zc->zc_cookie));
1778 }
1779 
1780 int
1781 zfs_unmount_snap(char *name, void *arg)
1782 {
1783 	char *snapname = arg;
1784 	char *cp;
1785 	vfs_t *vfsp = NULL;
1786 
1787 	/*
1788 	 * Snapshots (which are under .zfs control) must be unmounted
1789 	 * before they can be destroyed.
1790 	 */
1791 
1792 	if (snapname) {
1793 		(void) strcat(name, "@");
1794 		(void) strcat(name, snapname);
1795 		vfsp = zfs_get_vfs(name);
1796 		cp = strchr(name, '@');
1797 		*cp = '\0';
1798 	} else if (strchr(name, '@')) {
1799 		vfsp = zfs_get_vfs(name);
1800 	}
1801 
1802 	if (vfsp) {
1803 		/*
1804 		 * Always force the unmount for snapshots.
1805 		 */
1806 		int flag = MS_FORCE;
1807 		int err;
1808 
1809 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
1810 			VFS_RELE(vfsp);
1811 			return (err);
1812 		}
1813 		VFS_RELE(vfsp);
1814 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
1815 			return (err);
1816 	}
1817 	return (0);
1818 }
1819 
1820 static int
1821 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
1822 {
1823 	int err;
1824 
1825 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1826 		return (EINVAL);
1827 	err = dmu_objset_find(zc->zc_name,
1828 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
1829 	if (err)
1830 		return (err);
1831 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
1832 }
1833 
1834 static int
1835 zfs_ioc_destroy(zfs_cmd_t *zc)
1836 {
1837 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
1838 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1839 		if (err)
1840 			return (err);
1841 	}
1842 
1843 	return (dmu_objset_destroy(zc->zc_name));
1844 }
1845 
1846 static int
1847 zfs_ioc_rollback(zfs_cmd_t *zc)
1848 {
1849 	return (dmu_objset_rollback(zc->zc_name));
1850 }
1851 
1852 static int
1853 zfs_ioc_rename(zfs_cmd_t *zc)
1854 {
1855 	boolean_t recursive = zc->zc_cookie & 1;
1856 
1857 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1858 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0)
1859 		return (EINVAL);
1860 
1861 	/*
1862 	 * Unmount snapshot unless we're doing a recursive rename,
1863 	 * in which case the dataset code figures out which snapshots
1864 	 * to unmount.
1865 	 */
1866 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
1867 	    zc->zc_objset_type == DMU_OST_ZFS) {
1868 		int err = zfs_unmount_snap(zc->zc_name, NULL);
1869 		if (err)
1870 			return (err);
1871 	}
1872 
1873 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
1874 }
1875 
1876 static int
1877 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1878 {
1879 	file_t *fp;
1880 	int error, fd;
1881 	offset_t new_off;
1882 
1883 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
1884 	    strchr(zc->zc_value, '@') == NULL)
1885 		return (EINVAL);
1886 
1887 	fd = zc->zc_cookie;
1888 	fp = getf(fd);
1889 	if (fp == NULL)
1890 		return (EBADF);
1891 	error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record,
1892 	    &zc->zc_cookie, (boolean_t)zc->zc_guid, fp->f_vnode,
1893 	    fp->f_offset);
1894 
1895 	new_off = fp->f_offset + zc->zc_cookie;
1896 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &new_off) == 0)
1897 		fp->f_offset = new_off;
1898 
1899 	releasef(fd);
1900 	return (error);
1901 }
1902 
1903 static int
1904 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1905 {
1906 	objset_t *fromsnap = NULL;
1907 	objset_t *tosnap;
1908 	file_t *fp;
1909 	int error;
1910 
1911 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1912 	    DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1913 	if (error)
1914 		return (error);
1915 
1916 	if (zc->zc_value[0] != '\0') {
1917 		char buf[MAXPATHLEN];
1918 		char *cp;
1919 
1920 		(void) strncpy(buf, zc->zc_name, sizeof (buf));
1921 		cp = strchr(buf, '@');
1922 		if (cp)
1923 			*(cp+1) = 0;
1924 		(void) strncat(buf, zc->zc_value, sizeof (buf));
1925 		error = dmu_objset_open(buf, DMU_OST_ANY,
1926 		    DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1927 		if (error) {
1928 			dmu_objset_close(tosnap);
1929 			return (error);
1930 		}
1931 	}
1932 
1933 	fp = getf(zc->zc_cookie);
1934 	if (fp == NULL) {
1935 		dmu_objset_close(tosnap);
1936 		if (fromsnap)
1937 			dmu_objset_close(fromsnap);
1938 		return (EBADF);
1939 	}
1940 
1941 	error = dmu_sendbackup(tosnap, fromsnap, fp->f_vnode);
1942 
1943 	releasef(zc->zc_cookie);
1944 	if (fromsnap)
1945 		dmu_objset_close(fromsnap);
1946 	dmu_objset_close(tosnap);
1947 	return (error);
1948 }
1949 
1950 static int
1951 zfs_ioc_inject_fault(zfs_cmd_t *zc)
1952 {
1953 	int id, error;
1954 
1955 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
1956 	    &zc->zc_inject_record);
1957 
1958 	if (error == 0)
1959 		zc->zc_guid = (uint64_t)id;
1960 
1961 	return (error);
1962 }
1963 
1964 static int
1965 zfs_ioc_clear_fault(zfs_cmd_t *zc)
1966 {
1967 	return (zio_clear_fault((int)zc->zc_guid));
1968 }
1969 
1970 static int
1971 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
1972 {
1973 	int id = (int)zc->zc_guid;
1974 	int error;
1975 
1976 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
1977 	    &zc->zc_inject_record);
1978 
1979 	zc->zc_guid = id;
1980 
1981 	return (error);
1982 }
1983 
1984 static int
1985 zfs_ioc_error_log(zfs_cmd_t *zc)
1986 {
1987 	spa_t *spa;
1988 	int error;
1989 	size_t count = (size_t)zc->zc_nvlist_dst_size;
1990 
1991 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1992 		return (error);
1993 
1994 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
1995 	    &count);
1996 	if (error == 0)
1997 		zc->zc_nvlist_dst_size = count;
1998 	else
1999 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2000 
2001 	spa_close(spa, FTAG);
2002 
2003 	return (error);
2004 }
2005 
2006 static int
2007 zfs_ioc_clear(zfs_cmd_t *zc)
2008 {
2009 	spa_t *spa;
2010 	vdev_t *vd;
2011 	uint64_t txg;
2012 	int error;
2013 
2014 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2015 		return (error);
2016 
2017 	txg = spa_vdev_enter(spa);
2018 
2019 	if (zc->zc_guid == 0) {
2020 		vd = NULL;
2021 	} else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
2022 		(void) spa_vdev_exit(spa, NULL, txg, ENODEV);
2023 		spa_close(spa, FTAG);
2024 		return (ENODEV);
2025 	}
2026 
2027 	vdev_clear(spa, vd);
2028 
2029 	(void) spa_vdev_exit(spa, NULL, txg, 0);
2030 
2031 	spa_close(spa, FTAG);
2032 
2033 	return (0);
2034 }
2035 
2036 static int
2037 zfs_ioc_promote(zfs_cmd_t *zc)
2038 {
2039 	char *cp;
2040 
2041 	/*
2042 	 * We don't need to unmount *all* the origin fs's snapshots, but
2043 	 * it's easier.
2044 	 */
2045 	cp = strchr(zc->zc_value, '@');
2046 	if (cp)
2047 		*cp = '\0';
2048 	(void) dmu_objset_find(zc->zc_value,
2049 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2050 	return (dsl_dataset_promote(zc->zc_name));
2051 }
2052 
2053 /*
2054  * We don't want to have a hard dependency
2055  * against some special symbols in sharefs
2056  * and nfs.  Determine them if needed when
2057  * the first file system is shared.
2058  * Neither sharefs or nfs are unloadable modules.
2059  */
2060 int (*zexport_fs)(void *arg);
2061 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2062 
2063 int zfs_share_inited;
2064 ddi_modhandle_t nfs_mod;
2065 ddi_modhandle_t sharefs_mod;
2066 kmutex_t zfs_share_lock;
2067 
2068 static int
2069 zfs_ioc_share(zfs_cmd_t *zc)
2070 {
2071 	int error;
2072 	int opcode;
2073 
2074 	if (zfs_share_inited == 0) {
2075 		mutex_enter(&zfs_share_lock);
2076 		nfs_mod = ddi_modopen("fs/nfs", KRTLD_MODE_FIRST, &error);
2077 		sharefs_mod = ddi_modopen("fs/sharefs",
2078 		    KRTLD_MODE_FIRST, &error);
2079 		if (nfs_mod == NULL || sharefs_mod == NULL) {
2080 			mutex_exit(&zfs_share_lock);
2081 			return (ENOSYS);
2082 		}
2083 		if (zexport_fs == NULL && ((zexport_fs = (int (*)(void *))
2084 		    ddi_modsym(nfs_mod, "nfs_export", &error)) == NULL)) {
2085 			mutex_exit(&zfs_share_lock);
2086 			return (ENOSYS);
2087 		}
2088 
2089 		if (zshare_fs == NULL && ((zshare_fs =
2090 		    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2091 		    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2092 			mutex_exit(&zfs_share_lock);
2093 			return (ENOSYS);
2094 		}
2095 		zfs_share_inited = 1;
2096 		mutex_exit(&zfs_share_lock);
2097 	}
2098 
2099 	if (error = zexport_fs((void *)(uintptr_t)zc->zc_share.z_exportdata))
2100 		return (error);
2101 
2102 	opcode = (zc->zc_share.z_sharetype == B_TRUE) ?
2103 	    SHAREFS_ADD : SHAREFS_REMOVE;
2104 
2105 	error = zshare_fs(opcode,
2106 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2107 	    zc->zc_share.z_sharemax);
2108 
2109 	return (error);
2110 
2111 }
2112 
2113 /*
2114  * pool create, destroy, and export don't log the history as part of
2115  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2116  * do the logging of those commands.
2117  */
2118 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2119 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2120 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2121 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2122 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2123 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE },
2124 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2125 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2126 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2127 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2128 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE },
2129 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2130 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2131 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2132 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2133 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2134 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2135 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE },
2136 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2137 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2138 	    DATASET_NAME, B_FALSE },
2139 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2140 	    DATASET_NAME, B_FALSE },
2141 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2142 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2143 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2144 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2145 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2146 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2147 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE },
2148 	{ zfs_ioc_recvbackup, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2149 	{ zfs_ioc_sendbackup, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2150 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE },
2151 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2152 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2153 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2154 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2155 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2156 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE },
2157 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2158 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2159 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2160 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE },
2161 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2162 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2163 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2164 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2165 	    DATASET_NAME, B_FALSE },
2166 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2167 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2168 };
2169 
2170 static int
2171 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2172 {
2173 	zfs_cmd_t *zc;
2174 	uint_t vec;
2175 	int error, rc;
2176 
2177 	if (getminor(dev) != 0)
2178 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
2179 
2180 	vec = cmd - ZFS_IOC;
2181 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
2182 
2183 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2184 		return (EINVAL);
2185 
2186 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2187 
2188 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
2189 
2190 	if (error == 0)
2191 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
2192 
2193 	/*
2194 	 * Ensure that all pool/dataset names are valid before we pass down to
2195 	 * the lower layers.
2196 	 */
2197 	if (error == 0) {
2198 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
2199 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
2200 		case POOL_NAME:
2201 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2202 				error = EINVAL;
2203 			break;
2204 
2205 		case DATASET_NAME:
2206 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2207 				error = EINVAL;
2208 			break;
2209 
2210 		case NO_NAME:
2211 			break;
2212 		}
2213 	}
2214 
2215 	if (error == 0)
2216 		error = zfs_ioc_vec[vec].zvec_func(zc);
2217 
2218 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
2219 	if (error == 0) {
2220 		error = rc;
2221 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
2222 			zfs_log_history(zc);
2223 	}
2224 
2225 	kmem_free(zc, sizeof (zfs_cmd_t));
2226 	return (error);
2227 }
2228 
2229 static int
2230 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2231 {
2232 	if (cmd != DDI_ATTACH)
2233 		return (DDI_FAILURE);
2234 
2235 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
2236 	    DDI_PSEUDO, 0) == DDI_FAILURE)
2237 		return (DDI_FAILURE);
2238 
2239 	zfs_dip = dip;
2240 
2241 	ddi_report_dev(dip);
2242 
2243 	return (DDI_SUCCESS);
2244 }
2245 
2246 static int
2247 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2248 {
2249 	if (spa_busy() || zfs_busy() || zvol_busy())
2250 		return (DDI_FAILURE);
2251 
2252 	if (cmd != DDI_DETACH)
2253 		return (DDI_FAILURE);
2254 
2255 	zfs_dip = NULL;
2256 
2257 	ddi_prop_remove_all(dip);
2258 	ddi_remove_minor_node(dip, NULL);
2259 
2260 	return (DDI_SUCCESS);
2261 }
2262 
2263 /*ARGSUSED*/
2264 static int
2265 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2266 {
2267 	switch (infocmd) {
2268 	case DDI_INFO_DEVT2DEVINFO:
2269 		*result = zfs_dip;
2270 		return (DDI_SUCCESS);
2271 
2272 	case DDI_INFO_DEVT2INSTANCE:
2273 		*result = (void *)0;
2274 		return (DDI_SUCCESS);
2275 	}
2276 
2277 	return (DDI_FAILURE);
2278 }
2279 
2280 /*
2281  * OK, so this is a little weird.
2282  *
2283  * /dev/zfs is the control node, i.e. minor 0.
2284  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
2285  *
2286  * /dev/zfs has basically nothing to do except serve up ioctls,
2287  * so most of the standard driver entry points are in zvol.c.
2288  */
2289 static struct cb_ops zfs_cb_ops = {
2290 	zvol_open,	/* open */
2291 	zvol_close,	/* close */
2292 	zvol_strategy,	/* strategy */
2293 	nodev,		/* print */
2294 	nodev,		/* dump */
2295 	zvol_read,	/* read */
2296 	zvol_write,	/* write */
2297 	zfsdev_ioctl,	/* ioctl */
2298 	nodev,		/* devmap */
2299 	nodev,		/* mmap */
2300 	nodev,		/* segmap */
2301 	nochpoll,	/* poll */
2302 	ddi_prop_op,	/* prop_op */
2303 	NULL,		/* streamtab */
2304 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
2305 	CB_REV,		/* version */
2306 	nodev,		/* async read */
2307 	nodev,		/* async write */
2308 };
2309 
2310 static struct dev_ops zfs_dev_ops = {
2311 	DEVO_REV,	/* version */
2312 	0,		/* refcnt */
2313 	zfs_info,	/* info */
2314 	nulldev,	/* identify */
2315 	nulldev,	/* probe */
2316 	zfs_attach,	/* attach */
2317 	zfs_detach,	/* detach */
2318 	nodev,		/* reset */
2319 	&zfs_cb_ops,	/* driver operations */
2320 	NULL		/* no bus operations */
2321 };
2322 
2323 static struct modldrv zfs_modldrv = {
2324 	&mod_driverops, "ZFS storage pool version " SPA_VERSION_STRING,
2325 	    &zfs_dev_ops
2326 };
2327 
2328 static struct modlinkage modlinkage = {
2329 	MODREV_1,
2330 	(void *)&zfs_modlfs,
2331 	(void *)&zfs_modldrv,
2332 	NULL
2333 };
2334 
2335 
2336 uint_t zfs_fsyncer_key;
2337 
2338 int
2339 _init(void)
2340 {
2341 	int error;
2342 
2343 	spa_init(FREAD | FWRITE);
2344 	zfs_init();
2345 	zvol_init();
2346 
2347 	if ((error = mod_install(&modlinkage)) != 0) {
2348 		zvol_fini();
2349 		zfs_fini();
2350 		spa_fini();
2351 		return (error);
2352 	}
2353 
2354 	tsd_create(&zfs_fsyncer_key, NULL);
2355 
2356 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
2357 	ASSERT(error == 0);
2358 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
2359 
2360 	return (0);
2361 }
2362 
2363 int
2364 _fini(void)
2365 {
2366 	int error;
2367 
2368 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
2369 		return (EBUSY);
2370 
2371 	if ((error = mod_remove(&modlinkage)) != 0)
2372 		return (error);
2373 
2374 	zvol_fini();
2375 	zfs_fini();
2376 	spa_fini();
2377 	if (zfs_share_inited) {
2378 		(void) ddi_modclose(nfs_mod);
2379 		(void) ddi_modclose(sharefs_mod);
2380 	}
2381 
2382 	tsd_destroy(&zfs_fsyncer_key);
2383 	ldi_ident_release(zfs_li);
2384 	zfs_li = NULL;
2385 	mutex_destroy(&zfs_share_lock);
2386 
2387 	return (error);
2388 }
2389 
2390 int
2391 _info(struct modinfo *modinfop)
2392 {
2393 	return (mod_info(&modlinkage, modinfop));
2394 }
2395