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