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