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