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