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