xref: /titanic_50/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision 5797170a6d0f645128884c864c6920747318e414)
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  * inputs:
1627  * zc_name		name of filesystem
1628  * zc_value		name of property to set
1629  * zc_nvlist_src{_size}	nvlist of properties to apply
1630  * zc_cookie		clear existing local props?
1631  *
1632  * outputs:		none
1633  */
1634 static int
1635 zfs_ioc_set_prop(zfs_cmd_t *zc)
1636 {
1637 	nvlist_t *nvl;
1638 	int error;
1639 
1640 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1641 	    &nvl)) != 0)
1642 		return (error);
1643 
1644 	if (zc->zc_cookie) {
1645 		nvlist_t *origprops;
1646 		objset_t *os;
1647 
1648 		if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1649 		    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1650 			if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
1651 				clear_props(zc->zc_name, origprops, nvl);
1652 				nvlist_free(origprops);
1653 			}
1654 			dmu_objset_close(os);
1655 		}
1656 
1657 	}
1658 
1659 	error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1660 
1661 	nvlist_free(nvl);
1662 	return (error);
1663 }
1664 
1665 /*
1666  * inputs:
1667  * zc_name		name of filesystem
1668  * zc_value		name of property to inherit
1669  *
1670  * outputs:		none
1671  */
1672 static int
1673 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1674 {
1675 	/* the property name has been validated by zfs_secpolicy_inherit() */
1676 	return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1677 }
1678 
1679 static int
1680 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1681 {
1682 	nvlist_t *props;
1683 	spa_t *spa;
1684 	int error;
1685 	nvpair_t *elem;
1686 
1687 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1688 	    &props)))
1689 		return (error);
1690 
1691 	/*
1692 	 * If the only property is the configfile, then just do a spa_lookup()
1693 	 * to handle the faulted case.
1694 	 */
1695 	elem = nvlist_next_nvpair(props, NULL);
1696 	if (elem != NULL && strcmp(nvpair_name(elem),
1697 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
1698 	    nvlist_next_nvpair(props, elem) == NULL) {
1699 		mutex_enter(&spa_namespace_lock);
1700 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
1701 			spa_configfile_set(spa, props, B_FALSE);
1702 			spa_config_sync(spa, B_FALSE, B_TRUE);
1703 		}
1704 		mutex_exit(&spa_namespace_lock);
1705 		if (spa != NULL)
1706 			return (0);
1707 	}
1708 
1709 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1710 		nvlist_free(props);
1711 		return (error);
1712 	}
1713 
1714 	error = spa_prop_set(spa, props);
1715 
1716 	nvlist_free(props);
1717 	spa_close(spa, FTAG);
1718 
1719 	return (error);
1720 }
1721 
1722 static int
1723 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1724 {
1725 	spa_t *spa;
1726 	int error;
1727 	nvlist_t *nvp = NULL;
1728 
1729 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1730 		/*
1731 		 * If the pool is faulted, there may be properties we can still
1732 		 * get (such as altroot and cachefile), so attempt to get them
1733 		 * anyway.
1734 		 */
1735 		mutex_enter(&spa_namespace_lock);
1736 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
1737 			error = spa_prop_get(spa, &nvp);
1738 		mutex_exit(&spa_namespace_lock);
1739 	} else {
1740 		error = spa_prop_get(spa, &nvp);
1741 		spa_close(spa, FTAG);
1742 	}
1743 
1744 	if (error == 0 && zc->zc_nvlist_dst != NULL)
1745 		error = put_nvlist(zc, nvp);
1746 	else
1747 		error = EFAULT;
1748 
1749 	nvlist_free(nvp);
1750 	return (error);
1751 }
1752 
1753 static int
1754 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1755 {
1756 	nvlist_t *nvp;
1757 	int error;
1758 	uint32_t uid;
1759 	uint32_t gid;
1760 	uint32_t *groups;
1761 	uint_t group_cnt;
1762 	cred_t	*usercred;
1763 
1764 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1765 	    &nvp)) != 0) {
1766 		return (error);
1767 	}
1768 
1769 	if ((error = nvlist_lookup_uint32(nvp,
1770 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
1771 		nvlist_free(nvp);
1772 		return (EPERM);
1773 	}
1774 
1775 	if ((error = nvlist_lookup_uint32(nvp,
1776 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
1777 		nvlist_free(nvp);
1778 		return (EPERM);
1779 	}
1780 
1781 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1782 	    &groups, &group_cnt)) != 0) {
1783 		nvlist_free(nvp);
1784 		return (EPERM);
1785 	}
1786 	usercred = cralloc();
1787 	if ((crsetugid(usercred, uid, gid) != 0) ||
1788 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1789 		nvlist_free(nvp);
1790 		crfree(usercred);
1791 		return (EPERM);
1792 	}
1793 	nvlist_free(nvp);
1794 	error = dsl_deleg_access(zc->zc_name,
1795 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1796 	crfree(usercred);
1797 	return (error);
1798 }
1799 
1800 /*
1801  * inputs:
1802  * zc_name		name of filesystem
1803  * zc_nvlist_src{_size}	nvlist of delegated permissions
1804  * zc_perm_action	allow/unallow flag
1805  *
1806  * outputs:		none
1807  */
1808 static int
1809 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1810 {
1811 	int error;
1812 	nvlist_t *fsaclnv = NULL;
1813 
1814 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1815 	    &fsaclnv)) != 0)
1816 		return (error);
1817 
1818 	/*
1819 	 * Verify nvlist is constructed correctly
1820 	 */
1821 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1822 		nvlist_free(fsaclnv);
1823 		return (EINVAL);
1824 	}
1825 
1826 	/*
1827 	 * If we don't have PRIV_SYS_MOUNT, then validate
1828 	 * that user is allowed to hand out each permission in
1829 	 * the nvlist(s)
1830 	 */
1831 
1832 	error = secpolicy_zfs(CRED());
1833 	if (error) {
1834 		if (zc->zc_perm_action == B_FALSE) {
1835 			error = dsl_deleg_can_allow(zc->zc_name,
1836 			    fsaclnv, CRED());
1837 		} else {
1838 			error = dsl_deleg_can_unallow(zc->zc_name,
1839 			    fsaclnv, CRED());
1840 		}
1841 	}
1842 
1843 	if (error == 0)
1844 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1845 
1846 	nvlist_free(fsaclnv);
1847 	return (error);
1848 }
1849 
1850 /*
1851  * inputs:
1852  * zc_name		name of filesystem
1853  *
1854  * outputs:
1855  * zc_nvlist_src{_size}	nvlist of delegated permissions
1856  */
1857 static int
1858 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1859 {
1860 	nvlist_t *nvp;
1861 	int error;
1862 
1863 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1864 		error = put_nvlist(zc, nvp);
1865 		nvlist_free(nvp);
1866 	}
1867 
1868 	return (error);
1869 }
1870 
1871 /*
1872  * inputs:
1873  * zc_name		name of volume
1874  *
1875  * outputs:		none
1876  */
1877 static int
1878 zfs_ioc_create_minor(zfs_cmd_t *zc)
1879 {
1880 	return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1881 }
1882 
1883 /*
1884  * inputs:
1885  * zc_name		name of volume
1886  *
1887  * outputs:		none
1888  */
1889 static int
1890 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1891 {
1892 	return (zvol_remove_minor(zc->zc_name));
1893 }
1894 
1895 /*
1896  * Search the vfs list for a specified resource.  Returns a pointer to it
1897  * or NULL if no suitable entry is found. The caller of this routine
1898  * is responsible for releasing the returned vfs pointer.
1899  */
1900 static vfs_t *
1901 zfs_get_vfs(const char *resource)
1902 {
1903 	struct vfs *vfsp;
1904 	struct vfs *vfs_found = NULL;
1905 
1906 	vfs_list_read_lock();
1907 	vfsp = rootvfs;
1908 	do {
1909 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1910 			VFS_HOLD(vfsp);
1911 			vfs_found = vfsp;
1912 			break;
1913 		}
1914 		vfsp = vfsp->vfs_next;
1915 	} while (vfsp != rootvfs);
1916 	vfs_list_unlock();
1917 	return (vfs_found);
1918 }
1919 
1920 /* ARGSUSED */
1921 static void
1922 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1923 {
1924 	zfs_creat_t *zct = arg;
1925 
1926 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
1927 }
1928 
1929 #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
1930 
1931 /*
1932  * inputs:
1933  * createprops		list of properties requested by creator
1934  * default_zplver	zpl version to use if unspecified in createprops
1935  * fuids_ok		fuids allowed in this version of the spa?
1936  * os			parent objset pointer (NULL if root fs)
1937  *
1938  * outputs:
1939  * zplprops	values for the zplprops we attach to the master node object
1940  * is_ci	true if requested file system will be purely case-insensitive
1941  *
1942  * Determine the settings for utf8only, normalization and
1943  * casesensitivity.  Specific values may have been requested by the
1944  * creator and/or we can inherit values from the parent dataset.  If
1945  * the file system is of too early a vintage, a creator can not
1946  * request settings for these properties, even if the requested
1947  * setting is the default value.  We don't actually want to create dsl
1948  * properties for these, so remove them from the source nvlist after
1949  * processing.
1950  */
1951 static int
1952 zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
1953     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
1954     boolean_t *is_ci)
1955 {
1956 	uint64_t zplver = default_zplver;
1957 	uint64_t sense = ZFS_PROP_UNDEFINED;
1958 	uint64_t norm = ZFS_PROP_UNDEFINED;
1959 	uint64_t u8 = ZFS_PROP_UNDEFINED;
1960 
1961 	ASSERT(zplprops != NULL);
1962 
1963 	/*
1964 	 * Pull out creator prop choices, if any.
1965 	 */
1966 	if (createprops) {
1967 		(void) nvlist_lookup_uint64(createprops,
1968 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
1969 		(void) nvlist_lookup_uint64(createprops,
1970 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
1971 		(void) nvlist_remove_all(createprops,
1972 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
1973 		(void) nvlist_lookup_uint64(createprops,
1974 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
1975 		(void) nvlist_remove_all(createprops,
1976 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1977 		(void) nvlist_lookup_uint64(createprops,
1978 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
1979 		(void) nvlist_remove_all(createprops,
1980 		    zfs_prop_to_name(ZFS_PROP_CASE));
1981 	}
1982 
1983 	/*
1984 	 * If the zpl version requested is whacky or the file system
1985 	 * or pool is version is too "young" to support normalization
1986 	 * and the creator tried to set a value for one of the props,
1987 	 * error out.
1988 	 */
1989 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
1990 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
1991 	    (zplver < ZPL_VERSION_NORMALIZATION &&
1992 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
1993 	    sense != ZFS_PROP_UNDEFINED)))
1994 		return (ENOTSUP);
1995 
1996 	/*
1997 	 * Put the version in the zplprops
1998 	 */
1999 	VERIFY(nvlist_add_uint64(zplprops,
2000 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2001 
2002 	if (norm == ZFS_PROP_UNDEFINED)
2003 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2004 	VERIFY(nvlist_add_uint64(zplprops,
2005 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2006 
2007 	/*
2008 	 * If we're normalizing, names must always be valid UTF-8 strings.
2009 	 */
2010 	if (norm)
2011 		u8 = 1;
2012 	if (u8 == ZFS_PROP_UNDEFINED)
2013 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2014 	VERIFY(nvlist_add_uint64(zplprops,
2015 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2016 
2017 	if (sense == ZFS_PROP_UNDEFINED)
2018 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2019 	VERIFY(nvlist_add_uint64(zplprops,
2020 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2021 
2022 	if (is_ci)
2023 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
2024 
2025 	return (0);
2026 }
2027 
2028 static int
2029 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2030     nvlist_t *zplprops, boolean_t *is_ci)
2031 {
2032 	boolean_t fuids_ok = B_TRUE;
2033 	uint64_t zplver = ZPL_VERSION;
2034 	objset_t *os = NULL;
2035 	char parentname[MAXNAMELEN];
2036 	char *cp;
2037 	int error;
2038 
2039 	(void) strlcpy(parentname, dataset, sizeof (parentname));
2040 	cp = strrchr(parentname, '/');
2041 	ASSERT(cp != NULL);
2042 	cp[0] = '\0';
2043 
2044 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2045 		zplver = ZPL_VERSION_FUID - 1;
2046 		fuids_ok = B_FALSE;
2047 	}
2048 
2049 	/*
2050 	 * Open parent object set so we can inherit zplprop values.
2051 	 */
2052 	if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
2053 	    DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
2054 		return (error);
2055 
2056 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2057 	    zplprops, is_ci);
2058 	dmu_objset_close(os);
2059 	return (error);
2060 }
2061 
2062 static int
2063 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2064     nvlist_t *zplprops, boolean_t *is_ci)
2065 {
2066 	boolean_t fuids_ok = B_TRUE;
2067 	uint64_t zplver = ZPL_VERSION;
2068 	int error;
2069 
2070 	if (spa_vers < SPA_VERSION_FUID) {
2071 		zplver = ZPL_VERSION_FUID - 1;
2072 		fuids_ok = B_FALSE;
2073 	}
2074 
2075 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2076 	    zplprops, is_ci);
2077 	return (error);
2078 }
2079 
2080 /*
2081  * inputs:
2082  * zc_objset_type	type of objset to create (fs vs zvol)
2083  * zc_name		name of new objset
2084  * zc_value		name of snapshot to clone from (may be empty)
2085  * zc_nvlist_src{_size}	nvlist of properties to apply
2086  *
2087  * outputs: none
2088  */
2089 static int
2090 zfs_ioc_create(zfs_cmd_t *zc)
2091 {
2092 	objset_t *clone;
2093 	int error = 0;
2094 	zfs_creat_t zct;
2095 	nvlist_t *nvprops = NULL;
2096 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2097 	dmu_objset_type_t type = zc->zc_objset_type;
2098 
2099 	switch (type) {
2100 
2101 	case DMU_OST_ZFS:
2102 		cbfunc = zfs_create_cb;
2103 		break;
2104 
2105 	case DMU_OST_ZVOL:
2106 		cbfunc = zvol_create_cb;
2107 		break;
2108 
2109 	default:
2110 		cbfunc = NULL;
2111 		break;
2112 	}
2113 	if (strchr(zc->zc_name, '@') ||
2114 	    strchr(zc->zc_name, '%'))
2115 		return (EINVAL);
2116 
2117 	if (zc->zc_nvlist_src != NULL &&
2118 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2119 	    &nvprops)) != 0)
2120 		return (error);
2121 
2122 	zct.zct_zplprops = NULL;
2123 	zct.zct_props = nvprops;
2124 
2125 	if (zc->zc_value[0] != '\0') {
2126 		/*
2127 		 * We're creating a clone of an existing snapshot.
2128 		 */
2129 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2130 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2131 			nvlist_free(nvprops);
2132 			return (EINVAL);
2133 		}
2134 
2135 		error = dmu_objset_open(zc->zc_value, type,
2136 		    DS_MODE_USER | DS_MODE_READONLY, &clone);
2137 		if (error) {
2138 			nvlist_free(nvprops);
2139 			return (error);
2140 		}
2141 
2142 		error = dmu_objset_create(zc->zc_name, type, clone, 0,
2143 		    NULL, NULL);
2144 		if (error) {
2145 			dmu_objset_close(clone);
2146 			nvlist_free(nvprops);
2147 			return (error);
2148 		}
2149 		dmu_objset_close(clone);
2150 	} else {
2151 		boolean_t is_insensitive = B_FALSE;
2152 
2153 		if (cbfunc == NULL) {
2154 			nvlist_free(nvprops);
2155 			return (EINVAL);
2156 		}
2157 
2158 		if (type == DMU_OST_ZVOL) {
2159 			uint64_t volsize, volblocksize;
2160 
2161 			if (nvprops == NULL ||
2162 			    nvlist_lookup_uint64(nvprops,
2163 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2164 			    &volsize) != 0) {
2165 				nvlist_free(nvprops);
2166 				return (EINVAL);
2167 			}
2168 
2169 			if ((error = nvlist_lookup_uint64(nvprops,
2170 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2171 			    &volblocksize)) != 0 && error != ENOENT) {
2172 				nvlist_free(nvprops);
2173 				return (EINVAL);
2174 			}
2175 
2176 			if (error != 0)
2177 				volblocksize = zfs_prop_default_numeric(
2178 				    ZFS_PROP_VOLBLOCKSIZE);
2179 
2180 			if ((error = zvol_check_volblocksize(
2181 			    volblocksize)) != 0 ||
2182 			    (error = zvol_check_volsize(volsize,
2183 			    volblocksize)) != 0) {
2184 				nvlist_free(nvprops);
2185 				return (error);
2186 			}
2187 		} else if (type == DMU_OST_ZFS) {
2188 			int error;
2189 
2190 			/*
2191 			 * We have to have normalization and
2192 			 * case-folding flags correct when we do the
2193 			 * file system creation, so go figure them out
2194 			 * now.
2195 			 */
2196 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2197 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2198 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
2199 			    zct.zct_zplprops, &is_insensitive);
2200 			if (error != 0) {
2201 				nvlist_free(nvprops);
2202 				nvlist_free(zct.zct_zplprops);
2203 				return (error);
2204 			}
2205 		}
2206 		error = dmu_objset_create(zc->zc_name, type, NULL,
2207 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2208 		nvlist_free(zct.zct_zplprops);
2209 	}
2210 
2211 	/*
2212 	 * It would be nice to do this atomically.
2213 	 */
2214 	if (error == 0) {
2215 		if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2216 			(void) dmu_objset_destroy(zc->zc_name);
2217 	}
2218 	nvlist_free(nvprops);
2219 	return (error);
2220 }
2221 
2222 struct snap_prop_arg {
2223 	nvlist_t *nvprops;
2224 	const char *snapname;
2225 };
2226 
2227 static int
2228 set_snap_props(char *name, void *arg)
2229 {
2230 	struct snap_prop_arg *snpa = arg;
2231 	int len = strlen(name) + strlen(snpa->snapname) + 2;
2232 	char *buf = kmem_alloc(len, KM_SLEEP);
2233 	int err;
2234 
2235 	(void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
2236 	err = zfs_set_prop_nvlist(buf, snpa->nvprops);
2237 	if (err)
2238 		(void) dmu_objset_destroy(buf);
2239 	kmem_free(buf, len);
2240 	return (err);
2241 }
2242 
2243 /*
2244  * inputs:
2245  * zc_name	name of filesystem
2246  * zc_value	short name of snapshot
2247  * zc_cookie	recursive flag
2248  *
2249  * outputs:	none
2250  */
2251 static int
2252 zfs_ioc_snapshot(zfs_cmd_t *zc)
2253 {
2254 	nvlist_t *nvprops = NULL;
2255 	int error;
2256 	boolean_t recursive = zc->zc_cookie;
2257 
2258 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2259 		return (EINVAL);
2260 
2261 	if (zc->zc_nvlist_src != NULL &&
2262 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2263 	    &nvprops)) != 0)
2264 		return (error);
2265 
2266 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
2267 
2268 	/*
2269 	 * It would be nice to do this atomically.
2270 	 */
2271 	if (error == 0) {
2272 		struct snap_prop_arg snpa;
2273 		snpa.nvprops = nvprops;
2274 		snpa.snapname = zc->zc_value;
2275 		if (recursive) {
2276 			error = dmu_objset_find(zc->zc_name,
2277 			    set_snap_props, &snpa, DS_FIND_CHILDREN);
2278 			if (error) {
2279 				(void) dmu_snapshots_destroy(zc->zc_name,
2280 				    zc->zc_value);
2281 			}
2282 		} else {
2283 			error = set_snap_props(zc->zc_name, &snpa);
2284 		}
2285 	}
2286 	nvlist_free(nvprops);
2287 	return (error);
2288 }
2289 
2290 int
2291 zfs_unmount_snap(char *name, void *arg)
2292 {
2293 	vfs_t *vfsp = NULL;
2294 
2295 	if (arg) {
2296 		char *snapname = arg;
2297 		int len = strlen(name) + strlen(snapname) + 2;
2298 		char *buf = kmem_alloc(len, KM_SLEEP);
2299 
2300 		(void) strcpy(buf, name);
2301 		(void) strcat(buf, "@");
2302 		(void) strcat(buf, snapname);
2303 		vfsp = zfs_get_vfs(buf);
2304 		kmem_free(buf, len);
2305 	} else if (strchr(name, '@')) {
2306 		vfsp = zfs_get_vfs(name);
2307 	}
2308 
2309 	if (vfsp) {
2310 		/*
2311 		 * Always force the unmount for snapshots.
2312 		 */
2313 		int flag = MS_FORCE;
2314 		int err;
2315 
2316 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2317 			VFS_RELE(vfsp);
2318 			return (err);
2319 		}
2320 		VFS_RELE(vfsp);
2321 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
2322 			return (err);
2323 	}
2324 	return (0);
2325 }
2326 
2327 /*
2328  * inputs:
2329  * zc_name	name of filesystem
2330  * zc_value	short name of snapshot
2331  *
2332  * outputs:	none
2333  */
2334 static int
2335 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2336 {
2337 	int err;
2338 
2339 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2340 		return (EINVAL);
2341 	err = dmu_objset_find(zc->zc_name,
2342 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2343 	if (err)
2344 		return (err);
2345 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2346 }
2347 
2348 /*
2349  * inputs:
2350  * zc_name		name of dataset to destroy
2351  * zc_objset_type	type of objset
2352  *
2353  * outputs:		none
2354  */
2355 static int
2356 zfs_ioc_destroy(zfs_cmd_t *zc)
2357 {
2358 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2359 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2360 		if (err)
2361 			return (err);
2362 	}
2363 
2364 	return (dmu_objset_destroy(zc->zc_name));
2365 }
2366 
2367 /*
2368  * inputs:
2369  * zc_name	name of dataset to rollback (to most recent snapshot)
2370  *
2371  * outputs:	none
2372  */
2373 static int
2374 zfs_ioc_rollback(zfs_cmd_t *zc)
2375 {
2376 	objset_t *os;
2377 	int error;
2378 	zfsvfs_t *zfsvfs = NULL;
2379 
2380 	/*
2381 	 * Get the zfsvfs for the receiving objset. There
2382 	 * won't be one if we're operating on a zvol, if the
2383 	 * objset doesn't exist yet, or is not mounted.
2384 	 */
2385 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
2386 	if (error)
2387 		return (error);
2388 
2389 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
2390 		mutex_enter(&os->os->os_user_ptr_lock);
2391 		zfsvfs = dmu_objset_get_user(os);
2392 		if (zfsvfs != NULL)
2393 			VFS_HOLD(zfsvfs->z_vfs);
2394 		mutex_exit(&os->os->os_user_ptr_lock);
2395 	}
2396 
2397 	if (zfsvfs != NULL) {
2398 		char *osname;
2399 		int mode;
2400 
2401 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2402 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
2403 		if (error == 0) {
2404 			int resume_err;
2405 
2406 			ASSERT(strcmp(osname, zc->zc_name) == 0);
2407 			error = dmu_objset_rollback(os);
2408 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2409 			error = error ? error : resume_err;
2410 		} else {
2411 			dmu_objset_close(os);
2412 		}
2413 		kmem_free(osname, MAXNAMELEN);
2414 		VFS_RELE(zfsvfs->z_vfs);
2415 	} else {
2416 		error = dmu_objset_rollback(os);
2417 	}
2418 	/* Note, the dmu_objset_rollback() releases the objset for us. */
2419 
2420 	return (error);
2421 }
2422 
2423 /*
2424  * inputs:
2425  * zc_name	old name of dataset
2426  * zc_value	new name of dataset
2427  * zc_cookie	recursive flag (only valid for snapshots)
2428  *
2429  * outputs:	none
2430  */
2431 static int
2432 zfs_ioc_rename(zfs_cmd_t *zc)
2433 {
2434 	boolean_t recursive = zc->zc_cookie & 1;
2435 
2436 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2437 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2438 	    strchr(zc->zc_value, '%'))
2439 		return (EINVAL);
2440 
2441 	/*
2442 	 * Unmount snapshot unless we're doing a recursive rename,
2443 	 * in which case the dataset code figures out which snapshots
2444 	 * to unmount.
2445 	 */
2446 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2447 	    zc->zc_objset_type == DMU_OST_ZFS) {
2448 		int err = zfs_unmount_snap(zc->zc_name, NULL);
2449 		if (err)
2450 			return (err);
2451 	}
2452 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2453 }
2454 
2455 static void
2456 clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
2457 {
2458 	zfs_cmd_t *zc;
2459 	nvpair_t *prop;
2460 
2461 	if (props == NULL)
2462 		return;
2463 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2464 	(void) strcpy(zc->zc_name, dataset);
2465 	for (prop = nvlist_next_nvpair(props, NULL); prop;
2466 	    prop = nvlist_next_nvpair(props, prop)) {
2467 		if (newprops != NULL &&
2468 		    nvlist_exists(newprops, nvpair_name(prop)))
2469 			continue;
2470 		(void) strcpy(zc->zc_value, nvpair_name(prop));
2471 		if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2472 			(void) zfs_ioc_inherit_prop(zc);
2473 	}
2474 	kmem_free(zc, sizeof (zfs_cmd_t));
2475 }
2476 
2477 /*
2478  * inputs:
2479  * zc_name		name of containing filesystem
2480  * zc_nvlist_src{_size}	nvlist of properties to apply
2481  * zc_value		name of snapshot to create
2482  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
2483  * zc_cookie		file descriptor to recv from
2484  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
2485  * zc_guid		force flag
2486  *
2487  * outputs:
2488  * zc_cookie		number of bytes read
2489  */
2490 static int
2491 zfs_ioc_recv(zfs_cmd_t *zc)
2492 {
2493 	file_t *fp;
2494 	objset_t *os;
2495 	dmu_recv_cookie_t drc;
2496 	zfsvfs_t *zfsvfs = NULL;
2497 	boolean_t force = (boolean_t)zc->zc_guid;
2498 	int error, fd;
2499 	offset_t off;
2500 	nvlist_t *props = NULL;
2501 	nvlist_t *origprops = NULL;
2502 	objset_t *origin = NULL;
2503 	char *tosnap;
2504 	char tofs[ZFS_MAXNAMELEN];
2505 
2506 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2507 	    strchr(zc->zc_value, '@') == NULL ||
2508 	    strchr(zc->zc_value, '%'))
2509 		return (EINVAL);
2510 
2511 	(void) strcpy(tofs, zc->zc_value);
2512 	tosnap = strchr(tofs, '@');
2513 	*tosnap = '\0';
2514 	tosnap++;
2515 
2516 	if (zc->zc_nvlist_src != NULL &&
2517 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2518 	    &props)) != 0)
2519 		return (error);
2520 
2521 	fd = zc->zc_cookie;
2522 	fp = getf(fd);
2523 	if (fp == NULL) {
2524 		nvlist_free(props);
2525 		return (EBADF);
2526 	}
2527 
2528 	if (dmu_objset_open(tofs, DMU_OST_ANY,
2529 	    DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2530 		/*
2531 		 * Try to get the zfsvfs for the receiving objset.
2532 		 * There won't be one if we're operating on a zvol,
2533 		 * if the objset doesn't exist yet, or is not mounted.
2534 		 */
2535 		mutex_enter(&os->os->os_user_ptr_lock);
2536 		if (zfsvfs = dmu_objset_get_user(os)) {
2537 			if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2538 				mutex_exit(&os->os->os_user_ptr_lock);
2539 				dmu_objset_close(os);
2540 				zfsvfs = NULL;
2541 				error = EBUSY;
2542 				goto out;
2543 			}
2544 			VFS_HOLD(zfsvfs->z_vfs);
2545 		}
2546 		mutex_exit(&os->os->os_user_ptr_lock);
2547 
2548 		/*
2549 		 * If new properties are supplied, they are to completely
2550 		 * replace the existing ones, so stash away the existing ones.
2551 		 */
2552 		if (props)
2553 			(void) dsl_prop_get_all(os, &origprops, TRUE);
2554 
2555 		dmu_objset_close(os);
2556 	}
2557 
2558 	if (zc->zc_string[0]) {
2559 		error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2560 		    DS_MODE_USER | DS_MODE_READONLY, &origin);
2561 		if (error)
2562 			goto out;
2563 	}
2564 
2565 	error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2566 	    force, origin, zfsvfs != NULL, &drc);
2567 	if (origin)
2568 		dmu_objset_close(origin);
2569 	if (error)
2570 		goto out;
2571 
2572 	/*
2573 	 * Reset properties.  We do this before we receive the stream
2574 	 * so that the properties are applied to the new data.
2575 	 */
2576 	if (props) {
2577 		clear_props(tofs, origprops, props);
2578 		/*
2579 		 * XXX - Note, this is all-or-nothing; should be best-effort.
2580 		 */
2581 		(void) zfs_set_prop_nvlist(tofs, props);
2582 	}
2583 
2584 	off = fp->f_offset;
2585 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
2586 
2587 	if (error == 0 && zfsvfs) {
2588 		char *osname;
2589 		int mode;
2590 
2591 		/* online recv */
2592 		osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2593 		error = zfs_suspend_fs(zfsvfs, osname, &mode);
2594 		if (error == 0) {
2595 			int resume_err;
2596 
2597 			error = dmu_recv_end(&drc);
2598 			resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2599 			error = error ? error : resume_err;
2600 		} else {
2601 			dmu_recv_abort_cleanup(&drc);
2602 		}
2603 		kmem_free(osname, MAXNAMELEN);
2604 	} else if (error == 0) {
2605 		error = dmu_recv_end(&drc);
2606 	}
2607 
2608 	zc->zc_cookie = off - fp->f_offset;
2609 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2610 		fp->f_offset = off;
2611 
2612 	/*
2613 	 * On error, restore the original props.
2614 	 */
2615 	if (error && props) {
2616 		clear_props(tofs, props, NULL);
2617 		(void) zfs_set_prop_nvlist(tofs, origprops);
2618 	}
2619 out:
2620 	if (zfsvfs) {
2621 		mutex_exit(&zfsvfs->z_online_recv_lock);
2622 		VFS_RELE(zfsvfs->z_vfs);
2623 	}
2624 	nvlist_free(props);
2625 	nvlist_free(origprops);
2626 	releasef(fd);
2627 	return (error);
2628 }
2629 
2630 /*
2631  * inputs:
2632  * zc_name	name of snapshot to send
2633  * zc_value	short name of incremental fromsnap (may be empty)
2634  * zc_cookie	file descriptor to send stream to
2635  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
2636  *
2637  * outputs: none
2638  */
2639 static int
2640 zfs_ioc_send(zfs_cmd_t *zc)
2641 {
2642 	objset_t *fromsnap = NULL;
2643 	objset_t *tosnap;
2644 	file_t *fp;
2645 	int error;
2646 	offset_t off;
2647 
2648 	error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2649 	    DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2650 	if (error)
2651 		return (error);
2652 
2653 	if (zc->zc_value[0] != '\0') {
2654 		char *buf;
2655 		char *cp;
2656 
2657 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2658 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
2659 		cp = strchr(buf, '@');
2660 		if (cp)
2661 			*(cp+1) = 0;
2662 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
2663 		error = dmu_objset_open(buf, DMU_OST_ANY,
2664 		    DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
2665 		kmem_free(buf, MAXPATHLEN);
2666 		if (error) {
2667 			dmu_objset_close(tosnap);
2668 			return (error);
2669 		}
2670 	}
2671 
2672 	fp = getf(zc->zc_cookie);
2673 	if (fp == NULL) {
2674 		dmu_objset_close(tosnap);
2675 		if (fromsnap)
2676 			dmu_objset_close(fromsnap);
2677 		return (EBADF);
2678 	}
2679 
2680 	off = fp->f_offset;
2681 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
2682 
2683 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
2684 		fp->f_offset = off;
2685 	releasef(zc->zc_cookie);
2686 	if (fromsnap)
2687 		dmu_objset_close(fromsnap);
2688 	dmu_objset_close(tosnap);
2689 	return (error);
2690 }
2691 
2692 static int
2693 zfs_ioc_inject_fault(zfs_cmd_t *zc)
2694 {
2695 	int id, error;
2696 
2697 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2698 	    &zc->zc_inject_record);
2699 
2700 	if (error == 0)
2701 		zc->zc_guid = (uint64_t)id;
2702 
2703 	return (error);
2704 }
2705 
2706 static int
2707 zfs_ioc_clear_fault(zfs_cmd_t *zc)
2708 {
2709 	return (zio_clear_fault((int)zc->zc_guid));
2710 }
2711 
2712 static int
2713 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2714 {
2715 	int id = (int)zc->zc_guid;
2716 	int error;
2717 
2718 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2719 	    &zc->zc_inject_record);
2720 
2721 	zc->zc_guid = id;
2722 
2723 	return (error);
2724 }
2725 
2726 static int
2727 zfs_ioc_error_log(zfs_cmd_t *zc)
2728 {
2729 	spa_t *spa;
2730 	int error;
2731 	size_t count = (size_t)zc->zc_nvlist_dst_size;
2732 
2733 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2734 		return (error);
2735 
2736 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2737 	    &count);
2738 	if (error == 0)
2739 		zc->zc_nvlist_dst_size = count;
2740 	else
2741 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2742 
2743 	spa_close(spa, FTAG);
2744 
2745 	return (error);
2746 }
2747 
2748 static int
2749 zfs_ioc_clear(zfs_cmd_t *zc)
2750 {
2751 	spa_t *spa;
2752 	vdev_t *vd;
2753 	int error;
2754 
2755 	/*
2756 	 * On zpool clear we also fix up missing slogs
2757 	 */
2758 	mutex_enter(&spa_namespace_lock);
2759 	spa = spa_lookup(zc->zc_name);
2760 	if (spa == NULL) {
2761 		mutex_exit(&spa_namespace_lock);
2762 		return (EIO);
2763 	}
2764 	if (spa->spa_log_state == SPA_LOG_MISSING) {
2765 		/* we need to let spa_open/spa_load clear the chains */
2766 		spa->spa_log_state = SPA_LOG_CLEAR;
2767 	}
2768 	mutex_exit(&spa_namespace_lock);
2769 
2770 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2771 		return (error);
2772 
2773 	spa_vdev_state_enter(spa);
2774 
2775 	if (zc->zc_guid == 0) {
2776 		vd = NULL;
2777 	} else {
2778 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
2779 		if (vd == NULL) {
2780 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
2781 			spa_close(spa, FTAG);
2782 			return (ENODEV);
2783 		}
2784 	}
2785 
2786 	vdev_clear(spa, vd);
2787 
2788 	(void) spa_vdev_state_exit(spa, NULL, 0);
2789 
2790 	/*
2791 	 * Resume any suspended I/Os.
2792 	 */
2793 	if (zio_resume(spa) != 0)
2794 		error = EIO;
2795 
2796 	spa_close(spa, FTAG);
2797 
2798 	return (error);
2799 }
2800 
2801 /*
2802  * inputs:
2803  * zc_name	name of filesystem
2804  * zc_value	name of origin snapshot
2805  *
2806  * outputs:	none
2807  */
2808 static int
2809 zfs_ioc_promote(zfs_cmd_t *zc)
2810 {
2811 	char *cp;
2812 
2813 	/*
2814 	 * We don't need to unmount *all* the origin fs's snapshots, but
2815 	 * it's easier.
2816 	 */
2817 	cp = strchr(zc->zc_value, '@');
2818 	if (cp)
2819 		*cp = '\0';
2820 	(void) dmu_objset_find(zc->zc_value,
2821 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2822 	return (dsl_dataset_promote(zc->zc_name));
2823 }
2824 
2825 /*
2826  * We don't want to have a hard dependency
2827  * against some special symbols in sharefs
2828  * nfs, and smbsrv.  Determine them if needed when
2829  * the first file system is shared.
2830  * Neither sharefs, nfs or smbsrv are unloadable modules.
2831  */
2832 int (*znfsexport_fs)(void *arg);
2833 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2834 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2835 
2836 int zfs_nfsshare_inited;
2837 int zfs_smbshare_inited;
2838 
2839 ddi_modhandle_t nfs_mod;
2840 ddi_modhandle_t sharefs_mod;
2841 ddi_modhandle_t smbsrv_mod;
2842 kmutex_t zfs_share_lock;
2843 
2844 static int
2845 zfs_init_sharefs()
2846 {
2847 	int error;
2848 
2849 	ASSERT(MUTEX_HELD(&zfs_share_lock));
2850 	/* Both NFS and SMB shares also require sharetab support. */
2851 	if (sharefs_mod == NULL && ((sharefs_mod =
2852 	    ddi_modopen("fs/sharefs",
2853 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
2854 		return (ENOSYS);
2855 	}
2856 	if (zshare_fs == NULL && ((zshare_fs =
2857 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2858 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2859 		return (ENOSYS);
2860 	}
2861 	return (0);
2862 }
2863 
2864 static int
2865 zfs_ioc_share(zfs_cmd_t *zc)
2866 {
2867 	int error;
2868 	int opcode;
2869 
2870 	switch (zc->zc_share.z_sharetype) {
2871 	case ZFS_SHARE_NFS:
2872 	case ZFS_UNSHARE_NFS:
2873 		if (zfs_nfsshare_inited == 0) {
2874 			mutex_enter(&zfs_share_lock);
2875 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2876 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2877 				mutex_exit(&zfs_share_lock);
2878 				return (ENOSYS);
2879 			}
2880 			if (znfsexport_fs == NULL &&
2881 			    ((znfsexport_fs = (int (*)(void *))
2882 			    ddi_modsym(nfs_mod,
2883 			    "nfs_export", &error)) == NULL)) {
2884 				mutex_exit(&zfs_share_lock);
2885 				return (ENOSYS);
2886 			}
2887 			error = zfs_init_sharefs();
2888 			if (error) {
2889 				mutex_exit(&zfs_share_lock);
2890 				return (ENOSYS);
2891 			}
2892 			zfs_nfsshare_inited = 1;
2893 			mutex_exit(&zfs_share_lock);
2894 		}
2895 		break;
2896 	case ZFS_SHARE_SMB:
2897 	case ZFS_UNSHARE_SMB:
2898 		if (zfs_smbshare_inited == 0) {
2899 			mutex_enter(&zfs_share_lock);
2900 			if (smbsrv_mod == NULL && ((smbsrv_mod =
2901 			    ddi_modopen("drv/smbsrv",
2902 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
2903 				mutex_exit(&zfs_share_lock);
2904 				return (ENOSYS);
2905 			}
2906 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2907 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2908 			    "smb_server_share", &error)) == NULL)) {
2909 				mutex_exit(&zfs_share_lock);
2910 				return (ENOSYS);
2911 			}
2912 			error = zfs_init_sharefs();
2913 			if (error) {
2914 				mutex_exit(&zfs_share_lock);
2915 				return (ENOSYS);
2916 			}
2917 			zfs_smbshare_inited = 1;
2918 			mutex_exit(&zfs_share_lock);
2919 		}
2920 		break;
2921 	default:
2922 		return (EINVAL);
2923 	}
2924 
2925 	switch (zc->zc_share.z_sharetype) {
2926 	case ZFS_SHARE_NFS:
2927 	case ZFS_UNSHARE_NFS:
2928 		if (error =
2929 		    znfsexport_fs((void *)
2930 		    (uintptr_t)zc->zc_share.z_exportdata))
2931 			return (error);
2932 		break;
2933 	case ZFS_SHARE_SMB:
2934 	case ZFS_UNSHARE_SMB:
2935 		if (error = zsmbexport_fs((void *)
2936 		    (uintptr_t)zc->zc_share.z_exportdata,
2937 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2938 		    B_TRUE: B_FALSE)) {
2939 			return (error);
2940 		}
2941 		break;
2942 	}
2943 
2944 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2945 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2946 	    SHAREFS_ADD : SHAREFS_REMOVE;
2947 
2948 	/*
2949 	 * Add or remove share from sharetab
2950 	 */
2951 	error = zshare_fs(opcode,
2952 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
2953 	    zc->zc_share.z_sharemax);
2954 
2955 	return (error);
2956 
2957 }
2958 
2959 ace_t full_access[] = {
2960 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
2961 };
2962 
2963 /*
2964  * Remove all ACL files in shares dir
2965  */
2966 static int
2967 zfs_smb_acl_purge(znode_t *dzp)
2968 {
2969 	zap_cursor_t	zc;
2970 	zap_attribute_t	zap;
2971 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2972 	int error;
2973 
2974 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
2975 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
2976 	    zap_cursor_advance(&zc)) {
2977 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
2978 		    NULL, 0)) != 0)
2979 			break;
2980 	}
2981 	zap_cursor_fini(&zc);
2982 	return (error);
2983 }
2984 
2985 static int
2986 zfs_ioc_smb_acl(zfs_cmd_t *zc)
2987 {
2988 	vnode_t *vp;
2989 	znode_t *dzp;
2990 	vnode_t *resourcevp = NULL;
2991 	znode_t *sharedir;
2992 	zfsvfs_t *zfsvfs;
2993 	nvlist_t *nvlist;
2994 	char *src, *target;
2995 	vattr_t vattr;
2996 	vsecattr_t vsec;
2997 	int error = 0;
2998 
2999 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
3000 	    NO_FOLLOW, NULL, &vp)) != 0)
3001 		return (error);
3002 
3003 	/* Now make sure mntpnt and dataset are ZFS */
3004 
3005 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
3006 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
3007 	    zc->zc_name) != 0)) {
3008 		VN_RELE(vp);
3009 		return (EINVAL);
3010 	}
3011 
3012 	dzp = VTOZ(vp);
3013 	zfsvfs = dzp->z_zfsvfs;
3014 	ZFS_ENTER(zfsvfs);
3015 
3016 	/*
3017 	 * Create share dir if its missing.
3018 	 */
3019 	mutex_enter(&zfsvfs->z_lock);
3020 	if (zfsvfs->z_shares_dir == 0) {
3021 		dmu_tx_t *tx;
3022 
3023 		tx = dmu_tx_create(zfsvfs->z_os);
3024 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
3025 		    ZFS_SHARES_DIR);
3026 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
3027 		error = dmu_tx_assign(tx, TXG_WAIT);
3028 		if (error) {
3029 			dmu_tx_abort(tx);
3030 		} else {
3031 			error = zfs_create_share_dir(zfsvfs, tx);
3032 			dmu_tx_commit(tx);
3033 		}
3034 		if (error) {
3035 			mutex_exit(&zfsvfs->z_lock);
3036 			VN_RELE(vp);
3037 			ZFS_EXIT(zfsvfs);
3038 			return (error);
3039 		}
3040 	}
3041 	mutex_exit(&zfsvfs->z_lock);
3042 
3043 	ASSERT(zfsvfs->z_shares_dir);
3044 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
3045 		VN_RELE(vp);
3046 		ZFS_EXIT(zfsvfs);
3047 		return (error);
3048 	}
3049 
3050 	switch (zc->zc_cookie) {
3051 	case ZFS_SMB_ACL_ADD:
3052 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
3053 		vattr.va_type = VREG;
3054 		vattr.va_mode = S_IFREG|0777;
3055 		vattr.va_uid = 0;
3056 		vattr.va_gid = 0;
3057 
3058 		vsec.vsa_mask = VSA_ACE;
3059 		vsec.vsa_aclentp = &full_access;
3060 		vsec.vsa_aclentsz = sizeof (full_access);
3061 		vsec.vsa_aclcnt = 1;
3062 
3063 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
3064 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
3065 		if (resourcevp)
3066 			VN_RELE(resourcevp);
3067 		break;
3068 
3069 	case ZFS_SMB_ACL_REMOVE:
3070 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
3071 		    NULL, 0);
3072 		break;
3073 
3074 	case ZFS_SMB_ACL_RENAME:
3075 		if ((error = get_nvlist(zc->zc_nvlist_src,
3076 		    zc->zc_nvlist_src_size, &nvlist)) != 0) {
3077 			VN_RELE(vp);
3078 			ZFS_EXIT(zfsvfs);
3079 			return (error);
3080 		}
3081 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
3082 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
3083 		    &target)) {
3084 			VN_RELE(vp);
3085 			VN_RELE(ZTOV(sharedir));
3086 			ZFS_EXIT(zfsvfs);
3087 			return (error);
3088 		}
3089 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
3090 		    kcred, NULL, 0);
3091 		nvlist_free(nvlist);
3092 		break;
3093 
3094 	case ZFS_SMB_ACL_PURGE:
3095 		error = zfs_smb_acl_purge(sharedir);
3096 		break;
3097 
3098 	default:
3099 		error = EINVAL;
3100 		break;
3101 	}
3102 
3103 	VN_RELE(vp);
3104 	VN_RELE(ZTOV(sharedir));
3105 
3106 	ZFS_EXIT(zfsvfs);
3107 
3108 	return (error);
3109 }
3110 
3111 /*
3112  * pool create, destroy, and export don't log the history as part of
3113  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
3114  * do the logging of those commands.
3115  */
3116 static zfs_ioc_vec_t zfs_ioc_vec[] = {
3117 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3118 	    B_FALSE },
3119 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
3120 	    B_FALSE },
3121 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3122 	    B_FALSE },
3123 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3124 	    B_FALSE },
3125 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
3126 	    B_FALSE },
3127 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
3128 	    B_FALSE },
3129 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
3130 	    B_FALSE },
3131 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3132 	    B_TRUE },
3133 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
3134 	    B_FALSE },
3135 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
3136 	    B_TRUE },
3137 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3138 	    B_FALSE },
3139 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3140 	    B_TRUE },
3141 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3142 	    B_TRUE },
3143 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
3144 	    B_FALSE },
3145 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3146 	    B_TRUE },
3147 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3148 	    B_TRUE },
3149 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
3150 	    B_TRUE },
3151 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3152 	    B_FALSE },
3153 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3154 	    B_FALSE },
3155 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3156 	    B_FALSE },
3157 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3158 	    B_FALSE },
3159 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
3160 	{ zfs_ioc_create_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
3161 	    B_FALSE },
3162 	{ zfs_ioc_remove_minor,	zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
3163 	    B_FALSE },
3164 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
3165 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
3166 	    B_TRUE},
3167 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
3168 	    B_TRUE },
3169 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
3170 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
3171 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
3172 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
3173 	    B_FALSE },
3174 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3175 	    B_FALSE },
3176 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3177 	    B_FALSE },
3178 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
3179 	    B_FALSE },
3180 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
3181 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
3182 	    B_TRUE },
3183 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy,	DATASET_NAME, B_TRUE,
3184 	    B_TRUE },
3185 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
3186 	    B_TRUE },
3187 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3188 	    B_FALSE },
3189 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
3190 	    B_FALSE },
3191 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
3192 	    B_TRUE },
3193 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
3194 	    B_FALSE },
3195 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
3196 	    B_TRUE },
3197 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3198 	    B_FALSE },
3199 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
3200 	    B_FALSE },
3201 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
3202 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
3203 	    B_TRUE },
3204 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
3205 	    B_FALSE }
3206 };
3207 
3208 int
3209 pool_status_check(const char *name, zfs_ioc_namecheck_t type)
3210 {
3211 	spa_t *spa;
3212 	char pool[ZFS_MAXNAMELEN];
3213 	int error;
3214 
3215 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
3216 
3217 	(void) strlcpy(pool, name, ZFS_MAXNAMELEN);
3218 	if (type == DATASET_NAME) {
3219 		char *p;
3220 
3221 		if ((p = strpbrk(pool, "/@")) != NULL)
3222 			*p = '\0';
3223 	}
3224 
3225 	error = spa_open(pool, &spa, FTAG);
3226 	if (error == 0) {
3227 		if (spa_suspended(spa))
3228 			error = EAGAIN;
3229 		spa_close(spa, FTAG);
3230 	}
3231 	return (error);
3232 }
3233 
3234 static int
3235 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
3236 {
3237 	zfs_cmd_t *zc;
3238 	uint_t vec;
3239 	int error, rc;
3240 
3241 	if (getminor(dev) != 0)
3242 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3243 
3244 	vec = cmd - ZFS_IOC;
3245 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3246 
3247 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3248 		return (EINVAL);
3249 
3250 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3251 
3252 	error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3253 
3254 	if (error == 0)
3255 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3256 
3257 	/*
3258 	 * Ensure that all pool/dataset names are valid before we pass down to
3259 	 * the lower layers.
3260 	 */
3261 	if (error == 0) {
3262 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3263 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
3264 		case POOL_NAME:
3265 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3266 				error = EINVAL;
3267 			if (zfs_ioc_vec[vec].zvec_pool_check)
3268 				error = pool_status_check(zc->zc_name,
3269 				    zfs_ioc_vec[vec].zvec_namecheck);
3270 			break;
3271 
3272 		case DATASET_NAME:
3273 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3274 				error = EINVAL;
3275 			if (zfs_ioc_vec[vec].zvec_pool_check)
3276 				error = pool_status_check(zc->zc_name,
3277 				    zfs_ioc_vec[vec].zvec_namecheck);
3278 			break;
3279 
3280 		case NO_NAME:
3281 			break;
3282 		}
3283 	}
3284 
3285 	if (error == 0)
3286 		error = zfs_ioc_vec[vec].zvec_func(zc);
3287 
3288 	rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
3289 	if (error == 0) {
3290 		error = rc;
3291 		if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
3292 			zfs_log_history(zc);
3293 	}
3294 
3295 	kmem_free(zc, sizeof (zfs_cmd_t));
3296 	return (error);
3297 }
3298 
3299 static int
3300 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3301 {
3302 	if (cmd != DDI_ATTACH)
3303 		return (DDI_FAILURE);
3304 
3305 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3306 	    DDI_PSEUDO, 0) == DDI_FAILURE)
3307 		return (DDI_FAILURE);
3308 
3309 	zfs_dip = dip;
3310 
3311 	ddi_report_dev(dip);
3312 
3313 	return (DDI_SUCCESS);
3314 }
3315 
3316 static int
3317 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3318 {
3319 	if (spa_busy() || zfs_busy() || zvol_busy())
3320 		return (DDI_FAILURE);
3321 
3322 	if (cmd != DDI_DETACH)
3323 		return (DDI_FAILURE);
3324 
3325 	zfs_dip = NULL;
3326 
3327 	ddi_prop_remove_all(dip);
3328 	ddi_remove_minor_node(dip, NULL);
3329 
3330 	return (DDI_SUCCESS);
3331 }
3332 
3333 /*ARGSUSED*/
3334 static int
3335 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3336 {
3337 	switch (infocmd) {
3338 	case DDI_INFO_DEVT2DEVINFO:
3339 		*result = zfs_dip;
3340 		return (DDI_SUCCESS);
3341 
3342 	case DDI_INFO_DEVT2INSTANCE:
3343 		*result = (void *)0;
3344 		return (DDI_SUCCESS);
3345 	}
3346 
3347 	return (DDI_FAILURE);
3348 }
3349 
3350 /*
3351  * OK, so this is a little weird.
3352  *
3353  * /dev/zfs is the control node, i.e. minor 0.
3354  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3355  *
3356  * /dev/zfs has basically nothing to do except serve up ioctls,
3357  * so most of the standard driver entry points are in zvol.c.
3358  */
3359 static struct cb_ops zfs_cb_ops = {
3360 	zvol_open,	/* open */
3361 	zvol_close,	/* close */
3362 	zvol_strategy,	/* strategy */
3363 	nodev,		/* print */
3364 	zvol_dump,	/* dump */
3365 	zvol_read,	/* read */
3366 	zvol_write,	/* write */
3367 	zfsdev_ioctl,	/* ioctl */
3368 	nodev,		/* devmap */
3369 	nodev,		/* mmap */
3370 	nodev,		/* segmap */
3371 	nochpoll,	/* poll */
3372 	ddi_prop_op,	/* prop_op */
3373 	NULL,		/* streamtab */
3374 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
3375 	CB_REV,		/* version */
3376 	nodev,		/* async read */
3377 	nodev,		/* async write */
3378 };
3379 
3380 static struct dev_ops zfs_dev_ops = {
3381 	DEVO_REV,	/* version */
3382 	0,		/* refcnt */
3383 	zfs_info,	/* info */
3384 	nulldev,	/* identify */
3385 	nulldev,	/* probe */
3386 	zfs_attach,	/* attach */
3387 	zfs_detach,	/* detach */
3388 	nodev,		/* reset */
3389 	&zfs_cb_ops,	/* driver operations */
3390 	NULL,		/* no bus operations */
3391 	NULL,		/* power */
3392 	ddi_quiesce_not_needed,	/* quiesce */
3393 };
3394 
3395 static struct modldrv zfs_modldrv = {
3396 	&mod_driverops,
3397 	"ZFS storage pool",
3398 	&zfs_dev_ops
3399 };
3400 
3401 static struct modlinkage modlinkage = {
3402 	MODREV_1,
3403 	(void *)&zfs_modlfs,
3404 	(void *)&zfs_modldrv,
3405 	NULL
3406 };
3407 
3408 
3409 uint_t zfs_fsyncer_key;
3410 extern uint_t rrw_tsd_key;
3411 
3412 int
3413 _init(void)
3414 {
3415 	int error;
3416 
3417 	spa_init(FREAD | FWRITE);
3418 	zfs_init();
3419 	zvol_init();
3420 
3421 	if ((error = mod_install(&modlinkage)) != 0) {
3422 		zvol_fini();
3423 		zfs_fini();
3424 		spa_fini();
3425 		return (error);
3426 	}
3427 
3428 	tsd_create(&zfs_fsyncer_key, NULL);
3429 	tsd_create(&rrw_tsd_key, NULL);
3430 
3431 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3432 	ASSERT(error == 0);
3433 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3434 
3435 	return (0);
3436 }
3437 
3438 int
3439 _fini(void)
3440 {
3441 	int error;
3442 
3443 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3444 		return (EBUSY);
3445 
3446 	if ((error = mod_remove(&modlinkage)) != 0)
3447 		return (error);
3448 
3449 	zvol_fini();
3450 	zfs_fini();
3451 	spa_fini();
3452 	if (zfs_nfsshare_inited)
3453 		(void) ddi_modclose(nfs_mod);
3454 	if (zfs_smbshare_inited)
3455 		(void) ddi_modclose(smbsrv_mod);
3456 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
3457 		(void) ddi_modclose(sharefs_mod);
3458 
3459 	tsd_destroy(&zfs_fsyncer_key);
3460 	ldi_ident_release(zfs_li);
3461 	zfs_li = NULL;
3462 	mutex_destroy(&zfs_share_lock);
3463 
3464 	return (error);
3465 }
3466 
3467 int
3468 _info(struct modinfo *modinfop)
3469 {
3470 	return (mod_info(&modlinkage, modinfop));
3471 }
3472