xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_ioctl.c (revision cf2859fccb5a7f36f44f1a01bea8aad0d8c0f80c)
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 {
2293 			type = zfs_prop_get_type(prop);
2294 		}
2295 
2296 		VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2297 
2298 		switch (type) {
2299 		case PROP_TYPE_STRING:
2300 			VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2301 			break;
2302 		case PROP_TYPE_NUMBER:
2303 		case PROP_TYPE_INDEX:
2304 			VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2305 			break;
2306 		default:
2307 			nvlist_free(dummy);
2308 			return (EINVAL);
2309 		}
2310 
2311 		pair = nvlist_next_nvpair(dummy, NULL);
2312 		err = zfs_prop_set_special(zc->zc_name, source, pair);
2313 		nvlist_free(dummy);
2314 		if (err != -1)
2315 			return (err); /* special property already handled */
2316 	} else {
2317 		/*
2318 		 * Only check this in the non-received case. We want to allow
2319 		 * 'inherit -S' to revert non-inheritable properties like quota
2320 		 * and reservation to the received or default values even though
2321 		 * they are not considered inheritable.
2322 		 */
2323 		if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2324 			return (EINVAL);
2325 	}
2326 
2327 	/* the property name has been validated by zfs_secpolicy_inherit() */
2328 	return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2329 }
2330 
2331 static int
2332 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2333 {
2334 	nvlist_t *props;
2335 	spa_t *spa;
2336 	int error;
2337 	nvpair_t *pair;
2338 
2339 	if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2340 	    zc->zc_iflags, &props))
2341 		return (error);
2342 
2343 	/*
2344 	 * If the only property is the configfile, then just do a spa_lookup()
2345 	 * to handle the faulted case.
2346 	 */
2347 	pair = nvlist_next_nvpair(props, NULL);
2348 	if (pair != NULL && strcmp(nvpair_name(pair),
2349 	    zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2350 	    nvlist_next_nvpair(props, pair) == NULL) {
2351 		mutex_enter(&spa_namespace_lock);
2352 		if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2353 			spa_configfile_set(spa, props, B_FALSE);
2354 			spa_config_sync(spa, B_FALSE, B_TRUE);
2355 		}
2356 		mutex_exit(&spa_namespace_lock);
2357 		if (spa != NULL) {
2358 			nvlist_free(props);
2359 			return (0);
2360 		}
2361 	}
2362 
2363 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2364 		nvlist_free(props);
2365 		return (error);
2366 	}
2367 
2368 	error = spa_prop_set(spa, props);
2369 
2370 	nvlist_free(props);
2371 	spa_close(spa, FTAG);
2372 
2373 	return (error);
2374 }
2375 
2376 static int
2377 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2378 {
2379 	spa_t *spa;
2380 	int error;
2381 	nvlist_t *nvp = NULL;
2382 
2383 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2384 		/*
2385 		 * If the pool is faulted, there may be properties we can still
2386 		 * get (such as altroot and cachefile), so attempt to get them
2387 		 * anyway.
2388 		 */
2389 		mutex_enter(&spa_namespace_lock);
2390 		if ((spa = spa_lookup(zc->zc_name)) != NULL)
2391 			error = spa_prop_get(spa, &nvp);
2392 		mutex_exit(&spa_namespace_lock);
2393 	} else {
2394 		error = spa_prop_get(spa, &nvp);
2395 		spa_close(spa, FTAG);
2396 	}
2397 
2398 	if (error == 0 && zc->zc_nvlist_dst != NULL)
2399 		error = put_nvlist(zc, nvp);
2400 	else
2401 		error = EFAULT;
2402 
2403 	nvlist_free(nvp);
2404 	return (error);
2405 }
2406 
2407 static int
2408 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
2409 {
2410 	nvlist_t *nvp;
2411 	int error;
2412 	uint32_t uid;
2413 	uint32_t gid;
2414 	uint32_t *groups;
2415 	uint_t group_cnt;
2416 	cred_t	*usercred;
2417 
2418 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2419 	    zc->zc_iflags, &nvp)) != 0) {
2420 		return (error);
2421 	}
2422 
2423 	if ((error = nvlist_lookup_uint32(nvp,
2424 	    ZFS_DELEG_PERM_UID, &uid)) != 0) {
2425 		nvlist_free(nvp);
2426 		return (EPERM);
2427 	}
2428 
2429 	if ((error = nvlist_lookup_uint32(nvp,
2430 	    ZFS_DELEG_PERM_GID, &gid)) != 0) {
2431 		nvlist_free(nvp);
2432 		return (EPERM);
2433 	}
2434 
2435 	if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
2436 	    &groups, &group_cnt)) != 0) {
2437 		nvlist_free(nvp);
2438 		return (EPERM);
2439 	}
2440 	usercred = cralloc();
2441 	if ((crsetugid(usercred, uid, gid) != 0) ||
2442 	    (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
2443 		nvlist_free(nvp);
2444 		crfree(usercred);
2445 		return (EPERM);
2446 	}
2447 	nvlist_free(nvp);
2448 	error = dsl_deleg_access(zc->zc_name,
2449 	    zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
2450 	crfree(usercred);
2451 	return (error);
2452 }
2453 
2454 /*
2455  * inputs:
2456  * zc_name		name of filesystem
2457  * zc_nvlist_src{_size}	nvlist of delegated permissions
2458  * zc_perm_action	allow/unallow flag
2459  *
2460  * outputs:		none
2461  */
2462 static int
2463 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2464 {
2465 	int error;
2466 	nvlist_t *fsaclnv = NULL;
2467 
2468 	if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2469 	    zc->zc_iflags, &fsaclnv)) != 0)
2470 		return (error);
2471 
2472 	/*
2473 	 * Verify nvlist is constructed correctly
2474 	 */
2475 	if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2476 		nvlist_free(fsaclnv);
2477 		return (EINVAL);
2478 	}
2479 
2480 	/*
2481 	 * If we don't have PRIV_SYS_MOUNT, then validate
2482 	 * that user is allowed to hand out each permission in
2483 	 * the nvlist(s)
2484 	 */
2485 
2486 	error = secpolicy_zfs(CRED());
2487 	if (error) {
2488 		if (zc->zc_perm_action == B_FALSE) {
2489 			error = dsl_deleg_can_allow(zc->zc_name,
2490 			    fsaclnv, CRED());
2491 		} else {
2492 			error = dsl_deleg_can_unallow(zc->zc_name,
2493 			    fsaclnv, CRED());
2494 		}
2495 	}
2496 
2497 	if (error == 0)
2498 		error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2499 
2500 	nvlist_free(fsaclnv);
2501 	return (error);
2502 }
2503 
2504 /*
2505  * inputs:
2506  * zc_name		name of filesystem
2507  *
2508  * outputs:
2509  * zc_nvlist_src{_size}	nvlist of delegated permissions
2510  */
2511 static int
2512 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2513 {
2514 	nvlist_t *nvp;
2515 	int error;
2516 
2517 	if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2518 		error = put_nvlist(zc, nvp);
2519 		nvlist_free(nvp);
2520 	}
2521 
2522 	return (error);
2523 }
2524 
2525 /*
2526  * Search the vfs list for a specified resource.  Returns a pointer to it
2527  * or NULL if no suitable entry is found. The caller of this routine
2528  * is responsible for releasing the returned vfs pointer.
2529  */
2530 static vfs_t *
2531 zfs_get_vfs(const char *resource)
2532 {
2533 	struct vfs *vfsp;
2534 	struct vfs *vfs_found = NULL;
2535 
2536 	vfs_list_read_lock();
2537 	vfsp = rootvfs;
2538 	do {
2539 		if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2540 			VFS_HOLD(vfsp);
2541 			vfs_found = vfsp;
2542 			break;
2543 		}
2544 		vfsp = vfsp->vfs_next;
2545 	} while (vfsp != rootvfs);
2546 	vfs_list_unlock();
2547 	return (vfs_found);
2548 }
2549 
2550 /* ARGSUSED */
2551 static void
2552 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2553 {
2554 	zfs_creat_t *zct = arg;
2555 
2556 	zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2557 }
2558 
2559 #define	ZFS_PROP_UNDEFINED	((uint64_t)-1)
2560 
2561 /*
2562  * inputs:
2563  * createprops		list of properties requested by creator
2564  * default_zplver	zpl version to use if unspecified in createprops
2565  * fuids_ok		fuids allowed in this version of the spa?
2566  * os			parent objset pointer (NULL if root fs)
2567  *
2568  * outputs:
2569  * zplprops	values for the zplprops we attach to the master node object
2570  * is_ci	true if requested file system will be purely case-insensitive
2571  *
2572  * Determine the settings for utf8only, normalization and
2573  * casesensitivity.  Specific values may have been requested by the
2574  * creator and/or we can inherit values from the parent dataset.  If
2575  * the file system is of too early a vintage, a creator can not
2576  * request settings for these properties, even if the requested
2577  * setting is the default value.  We don't actually want to create dsl
2578  * properties for these, so remove them from the source nvlist after
2579  * processing.
2580  */
2581 static int
2582 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2583     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
2584     boolean_t *is_ci)
2585 {
2586 	uint64_t sense = ZFS_PROP_UNDEFINED;
2587 	uint64_t norm = ZFS_PROP_UNDEFINED;
2588 	uint64_t u8 = ZFS_PROP_UNDEFINED;
2589 
2590 	ASSERT(zplprops != NULL);
2591 
2592 	/*
2593 	 * Pull out creator prop choices, if any.
2594 	 */
2595 	if (createprops) {
2596 		(void) nvlist_lookup_uint64(createprops,
2597 		    zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2598 		(void) nvlist_lookup_uint64(createprops,
2599 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2600 		(void) nvlist_remove_all(createprops,
2601 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2602 		(void) nvlist_lookup_uint64(createprops,
2603 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2604 		(void) nvlist_remove_all(createprops,
2605 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2606 		(void) nvlist_lookup_uint64(createprops,
2607 		    zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2608 		(void) nvlist_remove_all(createprops,
2609 		    zfs_prop_to_name(ZFS_PROP_CASE));
2610 	}
2611 
2612 	/*
2613 	 * If the zpl version requested is whacky or the file system
2614 	 * or pool is version is too "young" to support normalization
2615 	 * and the creator tried to set a value for one of the props,
2616 	 * error out.
2617 	 */
2618 	if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2619 	    (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2620 	    (zplver < ZPL_VERSION_NORMALIZATION &&
2621 	    (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2622 	    sense != ZFS_PROP_UNDEFINED)))
2623 		return (ENOTSUP);
2624 
2625 	/*
2626 	 * Put the version in the zplprops
2627 	 */
2628 	VERIFY(nvlist_add_uint64(zplprops,
2629 	    zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2630 
2631 	if (norm == ZFS_PROP_UNDEFINED)
2632 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2633 	VERIFY(nvlist_add_uint64(zplprops,
2634 	    zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2635 
2636 	/*
2637 	 * If we're normalizing, names must always be valid UTF-8 strings.
2638 	 */
2639 	if (norm)
2640 		u8 = 1;
2641 	if (u8 == ZFS_PROP_UNDEFINED)
2642 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2643 	VERIFY(nvlist_add_uint64(zplprops,
2644 	    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2645 
2646 	if (sense == ZFS_PROP_UNDEFINED)
2647 		VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2648 	VERIFY(nvlist_add_uint64(zplprops,
2649 	    zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2650 
2651 	if (is_ci)
2652 		*is_ci = (sense == ZFS_CASE_INSENSITIVE);
2653 
2654 	return (0);
2655 }
2656 
2657 static int
2658 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2659     nvlist_t *zplprops, boolean_t *is_ci)
2660 {
2661 	boolean_t fuids_ok = B_TRUE;
2662 	uint64_t zplver = ZPL_VERSION;
2663 	objset_t *os = NULL;
2664 	char parentname[MAXNAMELEN];
2665 	char *cp;
2666 	int error;
2667 
2668 	(void) strlcpy(parentname, dataset, sizeof (parentname));
2669 	cp = strrchr(parentname, '/');
2670 	ASSERT(cp != NULL);
2671 	cp[0] = '\0';
2672 
2673 	if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
2674 		zplver = ZPL_VERSION_USERSPACE - 1;
2675 	if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2676 		zplver = ZPL_VERSION_FUID - 1;
2677 		fuids_ok = B_FALSE;
2678 	}
2679 
2680 	/*
2681 	 * Open parent object set so we can inherit zplprop values.
2682 	 */
2683 	if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2684 		return (error);
2685 
2686 	error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2687 	    zplprops, is_ci);
2688 	dmu_objset_rele(os, FTAG);
2689 	return (error);
2690 }
2691 
2692 static int
2693 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2694     nvlist_t *zplprops, boolean_t *is_ci)
2695 {
2696 	boolean_t fuids_ok = B_TRUE;
2697 	uint64_t zplver = ZPL_VERSION;
2698 	int error;
2699 
2700 	if (spa_vers < SPA_VERSION_FUID) {
2701 		zplver = ZPL_VERSION_FUID - 1;
2702 		fuids_ok = B_FALSE;
2703 	}
2704 
2705 	error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2706 	    zplprops, is_ci);
2707 	return (error);
2708 }
2709 
2710 /*
2711  * inputs:
2712  * zc_objset_type	type of objset to create (fs vs zvol)
2713  * zc_name		name of new objset
2714  * zc_value		name of snapshot to clone from (may be empty)
2715  * zc_nvlist_src{_size}	nvlist of properties to apply
2716  *
2717  * outputs: none
2718  */
2719 static int
2720 zfs_ioc_create(zfs_cmd_t *zc)
2721 {
2722 	objset_t *clone;
2723 	int error = 0;
2724 	zfs_creat_t zct;
2725 	nvlist_t *nvprops = NULL;
2726 	void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2727 	dmu_objset_type_t type = zc->zc_objset_type;
2728 
2729 	switch (type) {
2730 
2731 	case DMU_OST_ZFS:
2732 		cbfunc = zfs_create_cb;
2733 		break;
2734 
2735 	case DMU_OST_ZVOL:
2736 		cbfunc = zvol_create_cb;
2737 		break;
2738 
2739 	default:
2740 		cbfunc = NULL;
2741 		break;
2742 	}
2743 	if (strchr(zc->zc_name, '@') ||
2744 	    strchr(zc->zc_name, '%'))
2745 		return (EINVAL);
2746 
2747 	if (zc->zc_nvlist_src != NULL &&
2748 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2749 	    zc->zc_iflags, &nvprops)) != 0)
2750 		return (error);
2751 
2752 	zct.zct_zplprops = NULL;
2753 	zct.zct_props = nvprops;
2754 
2755 	if (zc->zc_value[0] != '\0') {
2756 		/*
2757 		 * We're creating a clone of an existing snapshot.
2758 		 */
2759 		zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2760 		if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2761 			nvlist_free(nvprops);
2762 			return (EINVAL);
2763 		}
2764 
2765 		error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2766 		if (error) {
2767 			nvlist_free(nvprops);
2768 			return (error);
2769 		}
2770 
2771 		error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2772 		dmu_objset_rele(clone, FTAG);
2773 		if (error) {
2774 			nvlist_free(nvprops);
2775 			return (error);
2776 		}
2777 	} else {
2778 		boolean_t is_insensitive = B_FALSE;
2779 
2780 		if (cbfunc == NULL) {
2781 			nvlist_free(nvprops);
2782 			return (EINVAL);
2783 		}
2784 
2785 		if (type == DMU_OST_ZVOL) {
2786 			uint64_t volsize, volblocksize;
2787 
2788 			if (nvprops == NULL ||
2789 			    nvlist_lookup_uint64(nvprops,
2790 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2791 			    &volsize) != 0) {
2792 				nvlist_free(nvprops);
2793 				return (EINVAL);
2794 			}
2795 
2796 			if ((error = nvlist_lookup_uint64(nvprops,
2797 			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2798 			    &volblocksize)) != 0 && error != ENOENT) {
2799 				nvlist_free(nvprops);
2800 				return (EINVAL);
2801 			}
2802 
2803 			if (error != 0)
2804 				volblocksize = zfs_prop_default_numeric(
2805 				    ZFS_PROP_VOLBLOCKSIZE);
2806 
2807 			if ((error = zvol_check_volblocksize(
2808 			    volblocksize)) != 0 ||
2809 			    (error = zvol_check_volsize(volsize,
2810 			    volblocksize)) != 0) {
2811 				nvlist_free(nvprops);
2812 				return (error);
2813 			}
2814 		} else if (type == DMU_OST_ZFS) {
2815 			int error;
2816 
2817 			/*
2818 			 * We have to have normalization and
2819 			 * case-folding flags correct when we do the
2820 			 * file system creation, so go figure them out
2821 			 * now.
2822 			 */
2823 			VERIFY(nvlist_alloc(&zct.zct_zplprops,
2824 			    NV_UNIQUE_NAME, KM_SLEEP) == 0);
2825 			error = zfs_fill_zplprops(zc->zc_name, nvprops,
2826 			    zct.zct_zplprops, &is_insensitive);
2827 			if (error != 0) {
2828 				nvlist_free(nvprops);
2829 				nvlist_free(zct.zct_zplprops);
2830 				return (error);
2831 			}
2832 		}
2833 		error = dmu_objset_create(zc->zc_name, type,
2834 		    is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2835 		nvlist_free(zct.zct_zplprops);
2836 	}
2837 
2838 	/*
2839 	 * It would be nice to do this atomically.
2840 	 */
2841 	if (error == 0) {
2842 		error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
2843 		    nvprops, NULL);
2844 		if (error != 0)
2845 			(void) dmu_objset_destroy(zc->zc_name, B_FALSE);
2846 	}
2847 	nvlist_free(nvprops);
2848 	return (error);
2849 }
2850 
2851 /*
2852  * inputs:
2853  * zc_name	name of filesystem
2854  * zc_value	short name of snapshot
2855  * zc_cookie	recursive flag
2856  * zc_nvlist_src[_size] property list
2857  *
2858  * outputs:
2859  * zc_value	short snapname (i.e. part after the '@')
2860  */
2861 static int
2862 zfs_ioc_snapshot(zfs_cmd_t *zc)
2863 {
2864 	nvlist_t *nvprops = NULL;
2865 	int error;
2866 	boolean_t recursive = zc->zc_cookie;
2867 
2868 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2869 		return (EINVAL);
2870 
2871 	if (zc->zc_nvlist_src != NULL &&
2872 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2873 	    zc->zc_iflags, &nvprops)) != 0)
2874 		return (error);
2875 
2876 	error = zfs_check_userprops(zc->zc_name, nvprops);
2877 	if (error)
2878 		goto out;
2879 
2880 	if (!nvlist_empty(nvprops) &&
2881 	    zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
2882 		error = ENOTSUP;
2883 		goto out;
2884 	}
2885 
2886 	error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
2887 	    nvprops, recursive);
2888 
2889 out:
2890 	nvlist_free(nvprops);
2891 	return (error);
2892 }
2893 
2894 int
2895 zfs_unmount_snap(const char *name, void *arg)
2896 {
2897 	vfs_t *vfsp = NULL;
2898 
2899 	if (arg) {
2900 		char *snapname = arg;
2901 		char *fullname = kmem_asprintf("%s@%s", name, snapname);
2902 		vfsp = zfs_get_vfs(fullname);
2903 		strfree(fullname);
2904 	} else if (strchr(name, '@')) {
2905 		vfsp = zfs_get_vfs(name);
2906 	}
2907 
2908 	if (vfsp) {
2909 		/*
2910 		 * Always force the unmount for snapshots.
2911 		 */
2912 		int flag = MS_FORCE;
2913 		int err;
2914 
2915 		if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2916 			VFS_RELE(vfsp);
2917 			return (err);
2918 		}
2919 		VFS_RELE(vfsp);
2920 		if ((err = dounmount(vfsp, flag, kcred)) != 0)
2921 			return (err);
2922 	}
2923 	return (0);
2924 }
2925 
2926 /*
2927  * inputs:
2928  * zc_name		name of filesystem
2929  * zc_value		short name of snapshot
2930  * zc_defer_destroy	mark for deferred destroy
2931  *
2932  * outputs:	none
2933  */
2934 static int
2935 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2936 {
2937 	int err;
2938 
2939 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2940 		return (EINVAL);
2941 	err = dmu_objset_find(zc->zc_name,
2942 	    zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2943 	if (err)
2944 		return (err);
2945 	return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
2946 	    zc->zc_defer_destroy));
2947 }
2948 
2949 /*
2950  * inputs:
2951  * zc_name		name of dataset to destroy
2952  * zc_objset_type	type of objset
2953  * zc_defer_destroy	mark for deferred destroy
2954  *
2955  * outputs:		none
2956  */
2957 static int
2958 zfs_ioc_destroy(zfs_cmd_t *zc)
2959 {
2960 	int err;
2961 	if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2962 		err = zfs_unmount_snap(zc->zc_name, NULL);
2963 		if (err)
2964 			return (err);
2965 	}
2966 
2967 	err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
2968 	if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
2969 		(void) zvol_remove_minor(zc->zc_name);
2970 	return (err);
2971 }
2972 
2973 /*
2974  * inputs:
2975  * zc_name	name of dataset to rollback (to most recent snapshot)
2976  *
2977  * outputs:	none
2978  */
2979 static int
2980 zfs_ioc_rollback(zfs_cmd_t *zc)
2981 {
2982 	dsl_dataset_t *ds, *clone;
2983 	int error;
2984 	zfsvfs_t *zfsvfs;
2985 	char *clone_name;
2986 
2987 	error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
2988 	if (error)
2989 		return (error);
2990 
2991 	/* must not be a snapshot */
2992 	if (dsl_dataset_is_snapshot(ds)) {
2993 		dsl_dataset_rele(ds, FTAG);
2994 		return (EINVAL);
2995 	}
2996 
2997 	/* must have a most recent snapshot */
2998 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
2999 		dsl_dataset_rele(ds, FTAG);
3000 		return (EINVAL);
3001 	}
3002 
3003 	/*
3004 	 * Create clone of most recent snapshot.
3005 	 */
3006 	clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3007 	error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3008 	if (error)
3009 		goto out;
3010 
3011 	error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3012 	if (error)
3013 		goto out;
3014 
3015 	/*
3016 	 * Do clone swap.
3017 	 */
3018 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3019 		error = zfs_suspend_fs(zfsvfs);
3020 		if (error == 0) {
3021 			int resume_err;
3022 
3023 			if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3024 				error = dsl_dataset_clone_swap(clone, ds,
3025 				    B_TRUE);
3026 				dsl_dataset_disown(ds, FTAG);
3027 				ds = NULL;
3028 			} else {
3029 				error = EBUSY;
3030 			}
3031 			resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3032 			error = error ? error : resume_err;
3033 		}
3034 		VFS_RELE(zfsvfs->z_vfs);
3035 	} else {
3036 		if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3037 			error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3038 			dsl_dataset_disown(ds, FTAG);
3039 			ds = NULL;
3040 		} else {
3041 			error = EBUSY;
3042 		}
3043 	}
3044 
3045 	/*
3046 	 * Destroy clone (which also closes it).
3047 	 */
3048 	(void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3049 
3050 out:
3051 	strfree(clone_name);
3052 	if (ds)
3053 		dsl_dataset_rele(ds, FTAG);
3054 	return (error);
3055 }
3056 
3057 /*
3058  * inputs:
3059  * zc_name	old name of dataset
3060  * zc_value	new name of dataset
3061  * zc_cookie	recursive flag (only valid for snapshots)
3062  *
3063  * outputs:	none
3064  */
3065 static int
3066 zfs_ioc_rename(zfs_cmd_t *zc)
3067 {
3068 	boolean_t recursive = zc->zc_cookie & 1;
3069 
3070 	zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3071 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3072 	    strchr(zc->zc_value, '%'))
3073 		return (EINVAL);
3074 
3075 	/*
3076 	 * Unmount snapshot unless we're doing a recursive rename,
3077 	 * in which case the dataset code figures out which snapshots
3078 	 * to unmount.
3079 	 */
3080 	if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3081 	    zc->zc_objset_type == DMU_OST_ZFS) {
3082 		int err = zfs_unmount_snap(zc->zc_name, NULL);
3083 		if (err)
3084 			return (err);
3085 	}
3086 	if (zc->zc_objset_type == DMU_OST_ZVOL)
3087 		(void) zvol_remove_minor(zc->zc_name);
3088 	return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3089 }
3090 
3091 static int
3092 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3093 {
3094 	const char *propname = nvpair_name(pair);
3095 	boolean_t issnap = (strchr(dsname, '@') != NULL);
3096 	zfs_prop_t prop = zfs_name_to_prop(propname);
3097 	uint64_t intval;
3098 	int err;
3099 
3100 	if (prop == ZPROP_INVAL) {
3101 		if (zfs_prop_user(propname)) {
3102 			if (err = zfs_secpolicy_write_perms(dsname,
3103 			    ZFS_DELEG_PERM_USERPROP, cr))
3104 				return (err);
3105 			return (0);
3106 		}
3107 
3108 		if (!issnap && zfs_prop_userquota(propname)) {
3109 			const char *perm = NULL;
3110 			const char *uq_prefix =
3111 			    zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3112 			const char *gq_prefix =
3113 			    zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3114 
3115 			if (strncmp(propname, uq_prefix,
3116 			    strlen(uq_prefix)) == 0) {
3117 				perm = ZFS_DELEG_PERM_USERQUOTA;
3118 			} else if (strncmp(propname, gq_prefix,
3119 			    strlen(gq_prefix)) == 0) {
3120 				perm = ZFS_DELEG_PERM_GROUPQUOTA;
3121 			} else {
3122 				/* USERUSED and GROUPUSED are read-only */
3123 				return (EINVAL);
3124 			}
3125 
3126 			if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3127 				return (err);
3128 			return (0);
3129 		}
3130 
3131 		return (EINVAL);
3132 	}
3133 
3134 	if (issnap)
3135 		return (EINVAL);
3136 
3137 	if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3138 		/*
3139 		 * dsl_prop_get_all_impl() returns properties in this
3140 		 * format.
3141 		 */
3142 		nvlist_t *attrs;
3143 		VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3144 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3145 		    &pair) == 0);
3146 	}
3147 
3148 	/*
3149 	 * Check that this value is valid for this pool version
3150 	 */
3151 	switch (prop) {
3152 	case ZFS_PROP_COMPRESSION:
3153 		/*
3154 		 * If the user specified gzip compression, make sure
3155 		 * the SPA supports it. We ignore any errors here since
3156 		 * we'll catch them later.
3157 		 */
3158 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3159 		    nvpair_value_uint64(pair, &intval) == 0) {
3160 			if (intval >= ZIO_COMPRESS_GZIP_1 &&
3161 			    intval <= ZIO_COMPRESS_GZIP_9 &&
3162 			    zfs_earlier_version(dsname,
3163 			    SPA_VERSION_GZIP_COMPRESSION)) {
3164 				return (ENOTSUP);
3165 			}
3166 
3167 			if (intval == ZIO_COMPRESS_ZLE &&
3168 			    zfs_earlier_version(dsname,
3169 			    SPA_VERSION_ZLE_COMPRESSION))
3170 				return (ENOTSUP);
3171 
3172 			/*
3173 			 * If this is a bootable dataset then
3174 			 * verify that the compression algorithm
3175 			 * is supported for booting. We must return
3176 			 * something other than ENOTSUP since it
3177 			 * implies a downrev pool version.
3178 			 */
3179 			if (zfs_is_bootfs(dsname) &&
3180 			    !BOOTFS_COMPRESS_VALID(intval)) {
3181 				return (ERANGE);
3182 			}
3183 		}
3184 		break;
3185 
3186 	case ZFS_PROP_COPIES:
3187 		if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3188 			return (ENOTSUP);
3189 		break;
3190 
3191 	case ZFS_PROP_DEDUP:
3192 		if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3193 			return (ENOTSUP);
3194 		break;
3195 
3196 	case ZFS_PROP_SHARESMB:
3197 		if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3198 			return (ENOTSUP);
3199 		break;
3200 
3201 	case ZFS_PROP_ACLINHERIT:
3202 		if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3203 		    nvpair_value_uint64(pair, &intval) == 0) {
3204 			if (intval == ZFS_ACL_PASSTHROUGH_X &&
3205 			    zfs_earlier_version(dsname,
3206 			    SPA_VERSION_PASSTHROUGH_X))
3207 				return (ENOTSUP);
3208 		}
3209 		break;
3210 	}
3211 
3212 	return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3213 }
3214 
3215 /*
3216  * Removes properties from the given props list that fail permission checks
3217  * needed to clear them and to restore them in case of a receive error. For each
3218  * property, make sure we have both set and inherit permissions.
3219  *
3220  * Returns the first error encountered if any permission checks fail. If the
3221  * caller provides a non-NULL errlist, it also gives the complete list of names
3222  * of all the properties that failed a permission check along with the
3223  * corresponding error numbers. The caller is responsible for freeing the
3224  * returned errlist.
3225  *
3226  * If every property checks out successfully, zero is returned and the list
3227  * pointed at by errlist is NULL.
3228  */
3229 static int
3230 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3231 {
3232 	zfs_cmd_t *zc;
3233 	nvpair_t *pair, *next_pair;
3234 	nvlist_t *errors;
3235 	int err, rv = 0;
3236 
3237 	if (props == NULL)
3238 		return (0);
3239 
3240 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3241 
3242 	zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3243 	(void) strcpy(zc->zc_name, dataset);
3244 	pair = nvlist_next_nvpair(props, NULL);
3245 	while (pair != NULL) {
3246 		next_pair = nvlist_next_nvpair(props, pair);
3247 
3248 		(void) strcpy(zc->zc_value, nvpair_name(pair));
3249 		if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3250 		    (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3251 			VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3252 			VERIFY(nvlist_add_int32(errors,
3253 			    zc->zc_value, err) == 0);
3254 		}
3255 		pair = next_pair;
3256 	}
3257 	kmem_free(zc, sizeof (zfs_cmd_t));
3258 
3259 	if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3260 		nvlist_free(errors);
3261 		errors = NULL;
3262 	} else {
3263 		VERIFY(nvpair_value_int32(pair, &rv) == 0);
3264 	}
3265 
3266 	if (errlist == NULL)
3267 		nvlist_free(errors);
3268 	else
3269 		*errlist = errors;
3270 
3271 	return (rv);
3272 }
3273 
3274 static boolean_t
3275 propval_equals(nvpair_t *p1, nvpair_t *p2)
3276 {
3277 	if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3278 		/* dsl_prop_get_all_impl() format */
3279 		nvlist_t *attrs;
3280 		VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3281 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3282 		    &p1) == 0);
3283 	}
3284 
3285 	if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3286 		nvlist_t *attrs;
3287 		VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3288 		VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3289 		    &p2) == 0);
3290 	}
3291 
3292 	if (nvpair_type(p1) != nvpair_type(p2))
3293 		return (B_FALSE);
3294 
3295 	if (nvpair_type(p1) == DATA_TYPE_STRING) {
3296 		char *valstr1, *valstr2;
3297 
3298 		VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3299 		VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3300 		return (strcmp(valstr1, valstr2) == 0);
3301 	} else {
3302 		uint64_t intval1, intval2;
3303 
3304 		VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3305 		VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3306 		return (intval1 == intval2);
3307 	}
3308 }
3309 
3310 /*
3311  * Remove properties from props if they are not going to change (as determined
3312  * by comparison with origprops). Remove them from origprops as well, since we
3313  * do not need to clear or restore properties that won't change.
3314  */
3315 static void
3316 props_reduce(nvlist_t *props, nvlist_t *origprops)
3317 {
3318 	nvpair_t *pair, *next_pair;
3319 
3320 	if (origprops == NULL)
3321 		return; /* all props need to be received */
3322 
3323 	pair = nvlist_next_nvpair(props, NULL);
3324 	while (pair != NULL) {
3325 		const char *propname = nvpair_name(pair);
3326 		nvpair_t *match;
3327 
3328 		next_pair = nvlist_next_nvpair(props, pair);
3329 
3330 		if ((nvlist_lookup_nvpair(origprops, propname,
3331 		    &match) != 0) || !propval_equals(pair, match))
3332 			goto next; /* need to set received value */
3333 
3334 		/* don't clear the existing received value */
3335 		(void) nvlist_remove_nvpair(origprops, match);
3336 		/* don't bother receiving the property */
3337 		(void) nvlist_remove_nvpair(props, pair);
3338 next:
3339 		pair = next_pair;
3340 	}
3341 }
3342 
3343 #ifdef	DEBUG
3344 static boolean_t zfs_ioc_recv_inject_err;
3345 #endif
3346 
3347 /*
3348  * inputs:
3349  * zc_name		name of containing filesystem
3350  * zc_nvlist_src{_size}	nvlist of properties to apply
3351  * zc_value		name of snapshot to create
3352  * zc_string		name of clone origin (if DRR_FLAG_CLONE)
3353  * zc_cookie		file descriptor to recv from
3354  * zc_begin_record	the BEGIN record of the stream (not byteswapped)
3355  * zc_guid		force flag
3356  *
3357  * outputs:
3358  * zc_cookie		number of bytes read
3359  * zc_nvlist_dst{_size} error for each unapplied received property
3360  * zc_obj		zprop_errflags_t
3361  */
3362 static int
3363 zfs_ioc_recv(zfs_cmd_t *zc)
3364 {
3365 	file_t *fp;
3366 	objset_t *os;
3367 	dmu_recv_cookie_t drc;
3368 	boolean_t force = (boolean_t)zc->zc_guid;
3369 	int fd;
3370 	int error = 0;
3371 	int props_error = 0;
3372 	nvlist_t *errors;
3373 	offset_t off;
3374 	nvlist_t *props = NULL; /* sent properties */
3375 	nvlist_t *origprops = NULL; /* existing properties */
3376 	objset_t *origin = NULL;
3377 	char *tosnap;
3378 	char tofs[ZFS_MAXNAMELEN];
3379 	boolean_t first_recvd_props = B_FALSE;
3380 
3381 	if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3382 	    strchr(zc->zc_value, '@') == NULL ||
3383 	    strchr(zc->zc_value, '%'))
3384 		return (EINVAL);
3385 
3386 	(void) strcpy(tofs, zc->zc_value);
3387 	tosnap = strchr(tofs, '@');
3388 	*tosnap++ = '\0';
3389 
3390 	if (zc->zc_nvlist_src != NULL &&
3391 	    (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3392 	    zc->zc_iflags, &props)) != 0)
3393 		return (error);
3394 
3395 	fd = zc->zc_cookie;
3396 	fp = getf(fd);
3397 	if (fp == NULL) {
3398 		nvlist_free(props);
3399 		return (EBADF);
3400 	}
3401 
3402 	VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3403 
3404 	if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3405 		if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3406 		    !dsl_prop_get_hasrecvd(os)) {
3407 			first_recvd_props = B_TRUE;
3408 		}
3409 
3410 		/*
3411 		 * If new received properties are supplied, they are to
3412 		 * completely replace the existing received properties, so stash
3413 		 * away the existing ones.
3414 		 */
3415 		if (dsl_prop_get_received(os, &origprops) == 0) {
3416 			nvlist_t *errlist = NULL;
3417 			/*
3418 			 * Don't bother writing a property if its value won't
3419 			 * change (and avoid the unnecessary security checks).
3420 			 *
3421 			 * The first receive after SPA_VERSION_RECVD_PROPS is a
3422 			 * special case where we blow away all local properties
3423 			 * regardless.
3424 			 */
3425 			if (!first_recvd_props)
3426 				props_reduce(props, origprops);
3427 			if (zfs_check_clearable(tofs, origprops,
3428 			    &errlist) != 0)
3429 				(void) nvlist_merge(errors, errlist, 0);
3430 			nvlist_free(errlist);
3431 		}
3432 
3433 		dmu_objset_rele(os, FTAG);
3434 	}
3435 
3436 	if (zc->zc_string[0]) {
3437 		error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3438 		if (error)
3439 			goto out;
3440 	}
3441 
3442 	error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3443 	    &zc->zc_begin_record, force, origin, &drc);
3444 	if (origin)
3445 		dmu_objset_rele(origin, FTAG);
3446 	if (error)
3447 		goto out;
3448 
3449 	/*
3450 	 * Set properties before we receive the stream so that they are applied
3451 	 * to the new data. Note that we must call dmu_recv_stream() if
3452 	 * dmu_recv_begin() succeeds.
3453 	 */
3454 	if (props) {
3455 		nvlist_t *errlist;
3456 
3457 		if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3458 			if (drc.drc_newfs) {
3459 				if (spa_version(os->os_spa) >=
3460 				    SPA_VERSION_RECVD_PROPS)
3461 					first_recvd_props = B_TRUE;
3462 			} else if (origprops != NULL) {
3463 				if (clear_received_props(os, tofs, origprops,
3464 				    first_recvd_props ? NULL : props) != 0)
3465 					zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3466 			} else {
3467 				zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3468 			}
3469 			dsl_prop_set_hasrecvd(os);
3470 		} else if (!drc.drc_newfs) {
3471 			zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3472 		}
3473 
3474 		(void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3475 		    props, &errlist);
3476 		(void) nvlist_merge(errors, errlist, 0);
3477 		nvlist_free(errlist);
3478 	}
3479 
3480 	if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3481 		/*
3482 		 * Caller made zc->zc_nvlist_dst less than the minimum expected
3483 		 * size or supplied an invalid address.
3484 		 */
3485 		props_error = EINVAL;
3486 	}
3487 
3488 	off = fp->f_offset;
3489 	error = dmu_recv_stream(&drc, fp->f_vnode, &off);
3490 
3491 	if (error == 0) {
3492 		zfsvfs_t *zfsvfs = NULL;
3493 
3494 		if (getzfsvfs(tofs, &zfsvfs) == 0) {
3495 			/* online recv */
3496 			int end_err;
3497 
3498 			error = zfs_suspend_fs(zfsvfs);
3499 			/*
3500 			 * If the suspend fails, then the recv_end will
3501 			 * likely also fail, and clean up after itself.
3502 			 */
3503 			end_err = dmu_recv_end(&drc);
3504 			if (error == 0) {
3505 				int resume_err =
3506 				    zfs_resume_fs(zfsvfs, tofs);
3507 				error = error ? error : resume_err;
3508 			}
3509 			error = error ? error : end_err;
3510 			VFS_RELE(zfsvfs->z_vfs);
3511 		} else {
3512 			error = dmu_recv_end(&drc);
3513 		}
3514 	}
3515 
3516 	zc->zc_cookie = off - fp->f_offset;
3517 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3518 		fp->f_offset = off;
3519 
3520 #ifdef	DEBUG
3521 	if (zfs_ioc_recv_inject_err) {
3522 		zfs_ioc_recv_inject_err = B_FALSE;
3523 		error = 1;
3524 	}
3525 #endif
3526 	/*
3527 	 * On error, restore the original props.
3528 	 */
3529 	if (error && props) {
3530 		if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3531 			if (clear_received_props(os, tofs, props, NULL) != 0) {
3532 				/*
3533 				 * We failed to clear the received properties.
3534 				 * Since we may have left a $recvd value on the
3535 				 * system, we can't clear the $hasrecvd flag.
3536 				 */
3537 				zc->zc_obj |= ZPROP_ERR_NORESTORE;
3538 			} else if (first_recvd_props) {
3539 				dsl_prop_unset_hasrecvd(os);
3540 			}
3541 			dmu_objset_rele(os, FTAG);
3542 		} else if (!drc.drc_newfs) {
3543 			/* We failed to clear the received properties. */
3544 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
3545 		}
3546 
3547 		if (origprops == NULL && !drc.drc_newfs) {
3548 			/* We failed to stash the original properties. */
3549 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
3550 		}
3551 
3552 		/*
3553 		 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3554 		 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3555 		 * explictly if we're restoring local properties cleared in the
3556 		 * first new-style receive.
3557 		 */
3558 		if (origprops != NULL &&
3559 		    zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3560 		    ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3561 		    origprops, NULL) != 0) {
3562 			/*
3563 			 * We stashed the original properties but failed to
3564 			 * restore them.
3565 			 */
3566 			zc->zc_obj |= ZPROP_ERR_NORESTORE;
3567 		}
3568 	}
3569 out:
3570 	nvlist_free(props);
3571 	nvlist_free(origprops);
3572 	nvlist_free(errors);
3573 	releasef(fd);
3574 
3575 	if (error == 0)
3576 		error = props_error;
3577 
3578 	return (error);
3579 }
3580 
3581 /*
3582  * inputs:
3583  * zc_name	name of snapshot to send
3584  * zc_value	short name of incremental fromsnap (may be empty)
3585  * zc_cookie	file descriptor to send stream to
3586  * zc_obj	fromorigin flag (mutually exclusive with zc_value)
3587  *
3588  * outputs: none
3589  */
3590 static int
3591 zfs_ioc_send(zfs_cmd_t *zc)
3592 {
3593 	objset_t *fromsnap = NULL;
3594 	objset_t *tosnap;
3595 	file_t *fp;
3596 	int error;
3597 	offset_t off;
3598 
3599 	error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
3600 	if (error)
3601 		return (error);
3602 
3603 	if (zc->zc_value[0] != '\0') {
3604 		char *buf;
3605 		char *cp;
3606 
3607 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3608 		(void) strncpy(buf, zc->zc_name, MAXPATHLEN);
3609 		cp = strchr(buf, '@');
3610 		if (cp)
3611 			*(cp+1) = 0;
3612 		(void) strncat(buf, zc->zc_value, MAXPATHLEN);
3613 		error = dmu_objset_hold(buf, FTAG, &fromsnap);
3614 		kmem_free(buf, MAXPATHLEN);
3615 		if (error) {
3616 			dmu_objset_rele(tosnap, FTAG);
3617 			return (error);
3618 		}
3619 	}
3620 
3621 	fp = getf(zc->zc_cookie);
3622 	if (fp == NULL) {
3623 		dmu_objset_rele(tosnap, FTAG);
3624 		if (fromsnap)
3625 			dmu_objset_rele(fromsnap, FTAG);
3626 		return (EBADF);
3627 	}
3628 
3629 	off = fp->f_offset;
3630 	error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3631 
3632 	if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3633 		fp->f_offset = off;
3634 	releasef(zc->zc_cookie);
3635 	if (fromsnap)
3636 		dmu_objset_rele(fromsnap, FTAG);
3637 	dmu_objset_rele(tosnap, FTAG);
3638 	return (error);
3639 }
3640 
3641 static int
3642 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3643 {
3644 	int id, error;
3645 
3646 	error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3647 	    &zc->zc_inject_record);
3648 
3649 	if (error == 0)
3650 		zc->zc_guid = (uint64_t)id;
3651 
3652 	return (error);
3653 }
3654 
3655 static int
3656 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3657 {
3658 	return (zio_clear_fault((int)zc->zc_guid));
3659 }
3660 
3661 static int
3662 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3663 {
3664 	int id = (int)zc->zc_guid;
3665 	int error;
3666 
3667 	error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3668 	    &zc->zc_inject_record);
3669 
3670 	zc->zc_guid = id;
3671 
3672 	return (error);
3673 }
3674 
3675 static int
3676 zfs_ioc_error_log(zfs_cmd_t *zc)
3677 {
3678 	spa_t *spa;
3679 	int error;
3680 	size_t count = (size_t)zc->zc_nvlist_dst_size;
3681 
3682 	if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3683 		return (error);
3684 
3685 	error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3686 	    &count);
3687 	if (error == 0)
3688 		zc->zc_nvlist_dst_size = count;
3689 	else
3690 		zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3691 
3692 	spa_close(spa, FTAG);
3693 
3694 	return (error);
3695 }
3696 
3697 static int
3698 zfs_ioc_clear(zfs_cmd_t *zc)
3699 {
3700 	spa_t *spa;
3701 	vdev_t *vd;
3702 	int error;
3703 
3704 	/*
3705 	 * On zpool clear we also fix up missing slogs
3706 	 */
3707 	mutex_enter(&spa_namespace_lock);
3708 	spa = spa_lookup(zc->zc_name);
3709 	if (spa == NULL) {
3710 		mutex_exit(&spa_namespace_lock);
3711 		return (EIO);
3712 	}
3713 	if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3714 		/* we need to let spa_open/spa_load clear the chains */
3715 		spa_set_log_state(spa, SPA_LOG_CLEAR);
3716 	}
3717 	spa->spa_last_open_failed = 0;
3718 	mutex_exit(&spa_namespace_lock);
3719 
3720 	if (zc->zc_cookie == ZPOOL_NO_REWIND) {
3721 		error = spa_open(zc->zc_name, &spa, FTAG);
3722 	} else {
3723 		nvlist_t *policy;
3724 		nvlist_t *config = NULL;
3725 
3726 		if (zc->zc_nvlist_src == NULL)
3727 			return (EINVAL);
3728 
3729 		if ((error = get_nvlist(zc->zc_nvlist_src,
3730 		    zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3731 			error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3732 			    policy, &config);
3733 			if (config != NULL) {
3734 				(void) put_nvlist(zc, config);
3735 				nvlist_free(config);
3736 			}
3737 			nvlist_free(policy);
3738 		}
3739 	}
3740 
3741 	if (error)
3742 		return (error);
3743 
3744 	spa_vdev_state_enter(spa, SCL_NONE);
3745 
3746 	if (zc->zc_guid == 0) {
3747 		vd = NULL;
3748 	} else {
3749 		vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3750 		if (vd == NULL) {
3751 			(void) spa_vdev_state_exit(spa, NULL, ENODEV);
3752 			spa_close(spa, FTAG);
3753 			return (ENODEV);
3754 		}
3755 	}
3756 
3757 	vdev_clear(spa, vd);
3758 
3759 	(void) spa_vdev_state_exit(spa, NULL, 0);
3760 
3761 	/*
3762 	 * Resume any suspended I/Os.
3763 	 */
3764 	if (zio_resume(spa) != 0)
3765 		error = EIO;
3766 
3767 	spa_close(spa, FTAG);
3768 
3769 	return (error);
3770 }
3771 
3772 /*
3773  * inputs:
3774  * zc_name	name of filesystem
3775  * zc_value	name of origin snapshot
3776  *
3777  * outputs:
3778  * zc_string	name of conflicting snapshot, if there is one
3779  */
3780 static int
3781 zfs_ioc_promote(zfs_cmd_t *zc)
3782 {
3783 	char *cp;
3784 
3785 	/*
3786 	 * We don't need to unmount *all* the origin fs's snapshots, but
3787 	 * it's easier.
3788 	 */
3789 	cp = strchr(zc->zc_value, '@');
3790 	if (cp)
3791 		*cp = '\0';
3792 	(void) dmu_objset_find(zc->zc_value,
3793 	    zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
3794 	return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
3795 }
3796 
3797 /*
3798  * Retrieve a single {user|group}{used|quota}@... property.
3799  *
3800  * inputs:
3801  * zc_name	name of filesystem
3802  * zc_objset_type zfs_userquota_prop_t
3803  * zc_value	domain name (eg. "S-1-234-567-89")
3804  * zc_guid	RID/UID/GID
3805  *
3806  * outputs:
3807  * zc_cookie	property value
3808  */
3809 static int
3810 zfs_ioc_userspace_one(zfs_cmd_t *zc)
3811 {
3812 	zfsvfs_t *zfsvfs;
3813 	int error;
3814 
3815 	if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
3816 		return (EINVAL);
3817 
3818 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
3819 	if (error)
3820 		return (error);
3821 
3822 	error = zfs_userspace_one(zfsvfs,
3823 	    zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
3824 	zfsvfs_rele(zfsvfs, FTAG);
3825 
3826 	return (error);
3827 }
3828 
3829 /*
3830  * inputs:
3831  * zc_name		name of filesystem
3832  * zc_cookie		zap cursor
3833  * zc_objset_type	zfs_userquota_prop_t
3834  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
3835  *
3836  * outputs:
3837  * zc_nvlist_dst[_size]	data buffer (array of zfs_useracct_t)
3838  * zc_cookie	zap cursor
3839  */
3840 static int
3841 zfs_ioc_userspace_many(zfs_cmd_t *zc)
3842 {
3843 	zfsvfs_t *zfsvfs;
3844 	int error;
3845 
3846 	error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs);
3847 	if (error)
3848 		return (error);
3849 
3850 	int bufsize = zc->zc_nvlist_dst_size;
3851 	void *buf = kmem_alloc(bufsize, KM_SLEEP);
3852 
3853 	error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
3854 	    buf, &zc->zc_nvlist_dst_size);
3855 
3856 	if (error == 0) {
3857 		error = xcopyout(buf,
3858 		    (void *)(uintptr_t)zc->zc_nvlist_dst,
3859 		    zc->zc_nvlist_dst_size);
3860 	}
3861 	kmem_free(buf, bufsize);
3862 	zfsvfs_rele(zfsvfs, FTAG);
3863 
3864 	return (error);
3865 }
3866 
3867 /*
3868  * inputs:
3869  * zc_name		name of filesystem
3870  *
3871  * outputs:
3872  * none
3873  */
3874 static int
3875 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
3876 {
3877 	objset_t *os;
3878 	int error = 0;
3879 	zfsvfs_t *zfsvfs;
3880 
3881 	if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3882 		if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
3883 			/*
3884 			 * If userused is not enabled, it may be because the
3885 			 * objset needs to be closed & reopened (to grow the
3886 			 * objset_phys_t).  Suspend/resume the fs will do that.
3887 			 */
3888 			error = zfs_suspend_fs(zfsvfs);
3889 			if (error == 0)
3890 				error = zfs_resume_fs(zfsvfs, zc->zc_name);
3891 		}
3892 		if (error == 0)
3893 			error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
3894 		VFS_RELE(zfsvfs->z_vfs);
3895 	} else {
3896 		/* XXX kind of reading contents without owning */
3897 		error = dmu_objset_hold(zc->zc_name, FTAG, &os);
3898 		if (error)
3899 			return (error);
3900 
3901 		error = dmu_objset_userspace_upgrade(os);
3902 		dmu_objset_rele(os, FTAG);
3903 	}
3904 
3905 	return (error);
3906 }
3907 
3908 /*
3909  * We don't want to have a hard dependency
3910  * against some special symbols in sharefs
3911  * nfs, and smbsrv.  Determine them if needed when
3912  * the first file system is shared.
3913  * Neither sharefs, nfs or smbsrv are unloadable modules.
3914  */
3915 int (*znfsexport_fs)(void *arg);
3916 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
3917 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
3918 
3919 int zfs_nfsshare_inited;
3920 int zfs_smbshare_inited;
3921 
3922 ddi_modhandle_t nfs_mod;
3923 ddi_modhandle_t sharefs_mod;
3924 ddi_modhandle_t smbsrv_mod;
3925 kmutex_t zfs_share_lock;
3926 
3927 static int
3928 zfs_init_sharefs()
3929 {
3930 	int error;
3931 
3932 	ASSERT(MUTEX_HELD(&zfs_share_lock));
3933 	/* Both NFS and SMB shares also require sharetab support. */
3934 	if (sharefs_mod == NULL && ((sharefs_mod =
3935 	    ddi_modopen("fs/sharefs",
3936 	    KRTLD_MODE_FIRST, &error)) == NULL)) {
3937 		return (ENOSYS);
3938 	}
3939 	if (zshare_fs == NULL && ((zshare_fs =
3940 	    (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
3941 	    ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
3942 		return (ENOSYS);
3943 	}
3944 	return (0);
3945 }
3946 
3947 static int
3948 zfs_ioc_share(zfs_cmd_t *zc)
3949 {
3950 	int error;
3951 	int opcode;
3952 
3953 	switch (zc->zc_share.z_sharetype) {
3954 	case ZFS_SHARE_NFS:
3955 	case ZFS_UNSHARE_NFS:
3956 		if (zfs_nfsshare_inited == 0) {
3957 			mutex_enter(&zfs_share_lock);
3958 			if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
3959 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
3960 				mutex_exit(&zfs_share_lock);
3961 				return (ENOSYS);
3962 			}
3963 			if (znfsexport_fs == NULL &&
3964 			    ((znfsexport_fs = (int (*)(void *))
3965 			    ddi_modsym(nfs_mod,
3966 			    "nfs_export", &error)) == NULL)) {
3967 				mutex_exit(&zfs_share_lock);
3968 				return (ENOSYS);
3969 			}
3970 			error = zfs_init_sharefs();
3971 			if (error) {
3972 				mutex_exit(&zfs_share_lock);
3973 				return (ENOSYS);
3974 			}
3975 			zfs_nfsshare_inited = 1;
3976 			mutex_exit(&zfs_share_lock);
3977 		}
3978 		break;
3979 	case ZFS_SHARE_SMB:
3980 	case ZFS_UNSHARE_SMB:
3981 		if (zfs_smbshare_inited == 0) {
3982 			mutex_enter(&zfs_share_lock);
3983 			if (smbsrv_mod == NULL && ((smbsrv_mod =
3984 			    ddi_modopen("drv/smbsrv",
3985 			    KRTLD_MODE_FIRST, &error)) == NULL)) {
3986 				mutex_exit(&zfs_share_lock);
3987 				return (ENOSYS);
3988 			}
3989 			if (zsmbexport_fs == NULL && ((zsmbexport_fs =
3990 			    (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
3991 			    "smb_server_share", &error)) == NULL)) {
3992 				mutex_exit(&zfs_share_lock);
3993 				return (ENOSYS);
3994 			}
3995 			error = zfs_init_sharefs();
3996 			if (error) {
3997 				mutex_exit(&zfs_share_lock);
3998 				return (ENOSYS);
3999 			}
4000 			zfs_smbshare_inited = 1;
4001 			mutex_exit(&zfs_share_lock);
4002 		}
4003 		break;
4004 	default:
4005 		return (EINVAL);
4006 	}
4007 
4008 	switch (zc->zc_share.z_sharetype) {
4009 	case ZFS_SHARE_NFS:
4010 	case ZFS_UNSHARE_NFS:
4011 		if (error =
4012 		    znfsexport_fs((void *)
4013 		    (uintptr_t)zc->zc_share.z_exportdata))
4014 			return (error);
4015 		break;
4016 	case ZFS_SHARE_SMB:
4017 	case ZFS_UNSHARE_SMB:
4018 		if (error = zsmbexport_fs((void *)
4019 		    (uintptr_t)zc->zc_share.z_exportdata,
4020 		    zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4021 		    B_TRUE: B_FALSE)) {
4022 			return (error);
4023 		}
4024 		break;
4025 	}
4026 
4027 	opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4028 	    zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4029 	    SHAREFS_ADD : SHAREFS_REMOVE;
4030 
4031 	/*
4032 	 * Add or remove share from sharetab
4033 	 */
4034 	error = zshare_fs(opcode,
4035 	    (void *)(uintptr_t)zc->zc_share.z_sharedata,
4036 	    zc->zc_share.z_sharemax);
4037 
4038 	return (error);
4039 
4040 }
4041 
4042 ace_t full_access[] = {
4043 	{(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4044 };
4045 
4046 /*
4047  * Remove all ACL files in shares dir
4048  */
4049 static int
4050 zfs_smb_acl_purge(znode_t *dzp)
4051 {
4052 	zap_cursor_t	zc;
4053 	zap_attribute_t	zap;
4054 	zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4055 	int error;
4056 
4057 	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4058 	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4059 	    zap_cursor_advance(&zc)) {
4060 		if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4061 		    NULL, 0)) != 0)
4062 			break;
4063 	}
4064 	zap_cursor_fini(&zc);
4065 	return (error);
4066 }
4067 
4068 static int
4069 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4070 {
4071 	vnode_t *vp;
4072 	znode_t *dzp;
4073 	vnode_t *resourcevp = NULL;
4074 	znode_t *sharedir;
4075 	zfsvfs_t *zfsvfs;
4076 	nvlist_t *nvlist;
4077 	char *src, *target;
4078 	vattr_t vattr;
4079 	vsecattr_t vsec;
4080 	int error = 0;
4081 
4082 	if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4083 	    NO_FOLLOW, NULL, &vp)) != 0)
4084 		return (error);
4085 
4086 	/* Now make sure mntpnt and dataset are ZFS */
4087 
4088 	if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4089 	    (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4090 	    zc->zc_name) != 0)) {
4091 		VN_RELE(vp);
4092 		return (EINVAL);
4093 	}
4094 
4095 	dzp = VTOZ(vp);
4096 	zfsvfs = dzp->z_zfsvfs;
4097 	ZFS_ENTER(zfsvfs);
4098 
4099 	/*
4100 	 * Create share dir if its missing.
4101 	 */
4102 	mutex_enter(&zfsvfs->z_lock);
4103 	if (zfsvfs->z_shares_dir == 0) {
4104 		dmu_tx_t *tx;
4105 
4106 		tx = dmu_tx_create(zfsvfs->z_os);
4107 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4108 		    ZFS_SHARES_DIR);
4109 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4110 		error = dmu_tx_assign(tx, TXG_WAIT);
4111 		if (error) {
4112 			dmu_tx_abort(tx);
4113 		} else {
4114 			error = zfs_create_share_dir(zfsvfs, tx);
4115 			dmu_tx_commit(tx);
4116 		}
4117 		if (error) {
4118 			mutex_exit(&zfsvfs->z_lock);
4119 			VN_RELE(vp);
4120 			ZFS_EXIT(zfsvfs);
4121 			return (error);
4122 		}
4123 	}
4124 	mutex_exit(&zfsvfs->z_lock);
4125 
4126 	ASSERT(zfsvfs->z_shares_dir);
4127 	if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4128 		VN_RELE(vp);
4129 		ZFS_EXIT(zfsvfs);
4130 		return (error);
4131 	}
4132 
4133 	switch (zc->zc_cookie) {
4134 	case ZFS_SMB_ACL_ADD:
4135 		vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4136 		vattr.va_type = VREG;
4137 		vattr.va_mode = S_IFREG|0777;
4138 		vattr.va_uid = 0;
4139 		vattr.va_gid = 0;
4140 
4141 		vsec.vsa_mask = VSA_ACE;
4142 		vsec.vsa_aclentp = &full_access;
4143 		vsec.vsa_aclentsz = sizeof (full_access);
4144 		vsec.vsa_aclcnt = 1;
4145 
4146 		error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4147 		    &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4148 		if (resourcevp)
4149 			VN_RELE(resourcevp);
4150 		break;
4151 
4152 	case ZFS_SMB_ACL_REMOVE:
4153 		error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4154 		    NULL, 0);
4155 		break;
4156 
4157 	case ZFS_SMB_ACL_RENAME:
4158 		if ((error = get_nvlist(zc->zc_nvlist_src,
4159 		    zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4160 			VN_RELE(vp);
4161 			ZFS_EXIT(zfsvfs);
4162 			return (error);
4163 		}
4164 		if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4165 		    nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4166 		    &target)) {
4167 			VN_RELE(vp);
4168 			VN_RELE(ZTOV(sharedir));
4169 			ZFS_EXIT(zfsvfs);
4170 			nvlist_free(nvlist);
4171 			return (error);
4172 		}
4173 		error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4174 		    kcred, NULL, 0);
4175 		nvlist_free(nvlist);
4176 		break;
4177 
4178 	case ZFS_SMB_ACL_PURGE:
4179 		error = zfs_smb_acl_purge(sharedir);
4180 		break;
4181 
4182 	default:
4183 		error = EINVAL;
4184 		break;
4185 	}
4186 
4187 	VN_RELE(vp);
4188 	VN_RELE(ZTOV(sharedir));
4189 
4190 	ZFS_EXIT(zfsvfs);
4191 
4192 	return (error);
4193 }
4194 
4195 /*
4196  * inputs:
4197  * zc_name	name of filesystem
4198  * zc_value	short name of snap
4199  * zc_string	user-supplied tag for this reference
4200  * zc_cookie	recursive flag
4201  * zc_temphold	set if hold is temporary
4202  *
4203  * outputs:		none
4204  */
4205 static int
4206 zfs_ioc_hold(zfs_cmd_t *zc)
4207 {
4208 	boolean_t recursive = zc->zc_cookie;
4209 
4210 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4211 		return (EINVAL);
4212 
4213 	return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4214 	    zc->zc_string, recursive, zc->zc_temphold));
4215 }
4216 
4217 /*
4218  * inputs:
4219  * zc_name	name of dataset from which we're releasing a user reference
4220  * zc_value	short name of snap
4221  * zc_string	user-supplied tag for this reference
4222  * zc_cookie	recursive flag
4223  *
4224  * outputs:		none
4225  */
4226 static int
4227 zfs_ioc_release(zfs_cmd_t *zc)
4228 {
4229 	boolean_t recursive = zc->zc_cookie;
4230 
4231 	if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4232 		return (EINVAL);
4233 
4234 	return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4235 	    zc->zc_string, recursive));
4236 }
4237 
4238 /*
4239  * inputs:
4240  * zc_name		name of filesystem
4241  *
4242  * outputs:
4243  * zc_nvlist_src{_size}	nvlist of snapshot holds
4244  */
4245 static int
4246 zfs_ioc_get_holds(zfs_cmd_t *zc)
4247 {
4248 	nvlist_t *nvp;
4249 	int error;
4250 
4251 	if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4252 		error = put_nvlist(zc, nvp);
4253 		nvlist_free(nvp);
4254 	}
4255 
4256 	return (error);
4257 }
4258 
4259 /*
4260  * pool create, destroy, and export don't log the history as part of
4261  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4262  * do the logging of those commands.
4263  */
4264 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4265 	{ zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4266 	    B_FALSE },
4267 	{ zfs_ioc_pool_destroy,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4268 	    B_FALSE },
4269 	{ zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4270 	    B_FALSE },
4271 	{ zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4272 	    B_FALSE },
4273 	{ zfs_ioc_pool_configs,	zfs_secpolicy_none, NO_NAME, B_FALSE,
4274 	    B_FALSE },
4275 	{ zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4276 	    B_FALSE },
4277 	{ zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4278 	    B_FALSE },
4279 	{ zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4280 	    B_TRUE },
4281 	{ zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4282 	    B_FALSE },
4283 	{ zfs_ioc_pool_upgrade,	zfs_secpolicy_config, POOL_NAME, B_TRUE,
4284 	    B_TRUE },
4285 	{ zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4286 	    B_FALSE },
4287 	{ zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4288 	    B_TRUE },
4289 	{ zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4290 	    B_TRUE },
4291 	{ zfs_ioc_vdev_set_state, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
4292 	    B_FALSE },
4293 	{ zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4294 	    B_TRUE },
4295 	{ zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4296 	    B_TRUE },
4297 	{ zfs_ioc_vdev_setpath,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4298 	    B_TRUE },
4299 	{ zfs_ioc_vdev_setfru,	zfs_secpolicy_config, POOL_NAME, B_FALSE,
4300 	    B_TRUE },
4301 	{ zfs_ioc_objset_stats,	zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4302 	    B_TRUE },
4303 	{ zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4304 	    B_FALSE },
4305 	{ zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4306 	    B_TRUE },
4307 	{ zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4308 	    B_TRUE },
4309 	{ zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
4310 	{ zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
4311 	{ zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4312 	    B_TRUE},
4313 	{ zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4314 	    B_TRUE },
4315 	{ zfs_ioc_rename, zfs_secpolicy_rename,	DATASET_NAME, B_TRUE, B_TRUE },
4316 	{ zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
4317 	{ zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
4318 	{ zfs_ioc_inject_fault,	zfs_secpolicy_inject, NO_NAME, B_FALSE,
4319 	    B_FALSE },
4320 	{ zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4321 	    B_FALSE },
4322 	{ zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4323 	    B_FALSE },
4324 	{ zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4325 	    B_FALSE },
4326 	{ zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
4327 	{ zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4328 	    B_TRUE },
4329 	{ zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4330 	    B_TRUE, B_TRUE },
4331 	{ zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4332 	    B_TRUE },
4333 	{ zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4334 	    B_FALSE },
4335 	{ zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4336 	    B_TRUE },
4337 	{ zfs_ioc_pool_set_props, zfs_secpolicy_config,	POOL_NAME, B_TRUE,
4338 	    B_TRUE },
4339 	{ zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4340 	    B_FALSE },
4341 	{ zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4342 	    B_TRUE },
4343 	{ zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4344 	    B_FALSE },
4345 	{ zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
4346 	    B_FALSE },
4347 	{ zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
4348 	{ zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4349 	    B_TRUE },
4350 	{ zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4351 	    B_FALSE },
4352 	{ zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
4353 	    DATASET_NAME, B_FALSE, B_FALSE },
4354 	{ zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
4355 	    DATASET_NAME, B_FALSE, B_FALSE },
4356 	{ zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4357 	    DATASET_NAME, B_FALSE, B_TRUE },
4358 	{ zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
4359 	{ zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4360 	    B_TRUE },
4361 	{ zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4362 	    B_TRUE },
4363 	{ zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4364 	    B_FALSE },
4365 	{ zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4366 	    B_TRUE }
4367 };
4368 
4369 int
4370 pool_status_check(const char *name, zfs_ioc_namecheck_t type)
4371 {
4372 	spa_t *spa;
4373 	int error;
4374 
4375 	ASSERT(type == POOL_NAME || type == DATASET_NAME);
4376 
4377 	error = spa_open(name, &spa, FTAG);
4378 	if (error == 0) {
4379 		if (spa_suspended(spa))
4380 			error = EAGAIN;
4381 		spa_close(spa, FTAG);
4382 	}
4383 	return (error);
4384 }
4385 
4386 static int
4387 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4388 {
4389 	zfs_cmd_t *zc;
4390 	uint_t vec;
4391 	int error, rc;
4392 
4393 	if (getminor(dev) != 0)
4394 		return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4395 
4396 	vec = cmd - ZFS_IOC;
4397 	ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4398 
4399 	if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4400 		return (EINVAL);
4401 
4402 	zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4403 
4404 	error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4405 
4406 	if ((error == 0) && !(flag & FKIOCTL))
4407 		error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4408 
4409 	/*
4410 	 * Ensure that all pool/dataset names are valid before we pass down to
4411 	 * the lower layers.
4412 	 */
4413 	if (error == 0) {
4414 		zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4415 		zc->zc_iflags = flag & FKIOCTL;
4416 		switch (zfs_ioc_vec[vec].zvec_namecheck) {
4417 		case POOL_NAME:
4418 			if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4419 				error = EINVAL;
4420 			if (zfs_ioc_vec[vec].zvec_pool_check)
4421 				error = pool_status_check(zc->zc_name,
4422 				    zfs_ioc_vec[vec].zvec_namecheck);
4423 			break;
4424 
4425 		case DATASET_NAME:
4426 			if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4427 				error = EINVAL;
4428 			if (zfs_ioc_vec[vec].zvec_pool_check)
4429 				error = pool_status_check(zc->zc_name,
4430 				    zfs_ioc_vec[vec].zvec_namecheck);
4431 			break;
4432 
4433 		case NO_NAME:
4434 			break;
4435 		}
4436 	}
4437 
4438 	if (error == 0)
4439 		error = zfs_ioc_vec[vec].zvec_func(zc);
4440 
4441 	rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
4442 	if (error == 0) {
4443 		error = rc;
4444 		if (zfs_ioc_vec[vec].zvec_his_log)
4445 			zfs_log_history(zc);
4446 	}
4447 
4448 	kmem_free(zc, sizeof (zfs_cmd_t));
4449 	return (error);
4450 }
4451 
4452 static int
4453 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4454 {
4455 	if (cmd != DDI_ATTACH)
4456 		return (DDI_FAILURE);
4457 
4458 	if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4459 	    DDI_PSEUDO, 0) == DDI_FAILURE)
4460 		return (DDI_FAILURE);
4461 
4462 	zfs_dip = dip;
4463 
4464 	ddi_report_dev(dip);
4465 
4466 	return (DDI_SUCCESS);
4467 }
4468 
4469 static int
4470 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4471 {
4472 	if (spa_busy() || zfs_busy() || zvol_busy())
4473 		return (DDI_FAILURE);
4474 
4475 	if (cmd != DDI_DETACH)
4476 		return (DDI_FAILURE);
4477 
4478 	zfs_dip = NULL;
4479 
4480 	ddi_prop_remove_all(dip);
4481 	ddi_remove_minor_node(dip, NULL);
4482 
4483 	return (DDI_SUCCESS);
4484 }
4485 
4486 /*ARGSUSED*/
4487 static int
4488 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4489 {
4490 	switch (infocmd) {
4491 	case DDI_INFO_DEVT2DEVINFO:
4492 		*result = zfs_dip;
4493 		return (DDI_SUCCESS);
4494 
4495 	case DDI_INFO_DEVT2INSTANCE:
4496 		*result = (void *)0;
4497 		return (DDI_SUCCESS);
4498 	}
4499 
4500 	return (DDI_FAILURE);
4501 }
4502 
4503 /*
4504  * OK, so this is a little weird.
4505  *
4506  * /dev/zfs is the control node, i.e. minor 0.
4507  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
4508  *
4509  * /dev/zfs has basically nothing to do except serve up ioctls,
4510  * so most of the standard driver entry points are in zvol.c.
4511  */
4512 static struct cb_ops zfs_cb_ops = {
4513 	zvol_open,	/* open */
4514 	zvol_close,	/* close */
4515 	zvol_strategy,	/* strategy */
4516 	nodev,		/* print */
4517 	zvol_dump,	/* dump */
4518 	zvol_read,	/* read */
4519 	zvol_write,	/* write */
4520 	zfsdev_ioctl,	/* ioctl */
4521 	nodev,		/* devmap */
4522 	nodev,		/* mmap */
4523 	nodev,		/* segmap */
4524 	nochpoll,	/* poll */
4525 	ddi_prop_op,	/* prop_op */
4526 	NULL,		/* streamtab */
4527 	D_NEW | D_MP | D_64BIT,		/* Driver compatibility flag */
4528 	CB_REV,		/* version */
4529 	nodev,		/* async read */
4530 	nodev,		/* async write */
4531 };
4532 
4533 static struct dev_ops zfs_dev_ops = {
4534 	DEVO_REV,	/* version */
4535 	0,		/* refcnt */
4536 	zfs_info,	/* info */
4537 	nulldev,	/* identify */
4538 	nulldev,	/* probe */
4539 	zfs_attach,	/* attach */
4540 	zfs_detach,	/* detach */
4541 	nodev,		/* reset */
4542 	&zfs_cb_ops,	/* driver operations */
4543 	NULL,		/* no bus operations */
4544 	NULL,		/* power */
4545 	ddi_quiesce_not_needed,	/* quiesce */
4546 };
4547 
4548 static struct modldrv zfs_modldrv = {
4549 	&mod_driverops,
4550 	"ZFS storage pool",
4551 	&zfs_dev_ops
4552 };
4553 
4554 static struct modlinkage modlinkage = {
4555 	MODREV_1,
4556 	(void *)&zfs_modlfs,
4557 	(void *)&zfs_modldrv,
4558 	NULL
4559 };
4560 
4561 
4562 uint_t zfs_fsyncer_key;
4563 extern uint_t rrw_tsd_key;
4564 
4565 int
4566 _init(void)
4567 {
4568 	int error;
4569 
4570 	spa_init(FREAD | FWRITE);
4571 	zfs_init();
4572 	zvol_init();
4573 
4574 	if ((error = mod_install(&modlinkage)) != 0) {
4575 		zvol_fini();
4576 		zfs_fini();
4577 		spa_fini();
4578 		return (error);
4579 	}
4580 
4581 	tsd_create(&zfs_fsyncer_key, NULL);
4582 	tsd_create(&rrw_tsd_key, NULL);
4583 
4584 	error = ldi_ident_from_mod(&modlinkage, &zfs_li);
4585 	ASSERT(error == 0);
4586 	mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
4587 
4588 	return (0);
4589 }
4590 
4591 int
4592 _fini(void)
4593 {
4594 	int error;
4595 
4596 	if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
4597 		return (EBUSY);
4598 
4599 	if ((error = mod_remove(&modlinkage)) != 0)
4600 		return (error);
4601 
4602 	zvol_fini();
4603 	zfs_fini();
4604 	spa_fini();
4605 	if (zfs_nfsshare_inited)
4606 		(void) ddi_modclose(nfs_mod);
4607 	if (zfs_smbshare_inited)
4608 		(void) ddi_modclose(smbsrv_mod);
4609 	if (zfs_nfsshare_inited || zfs_smbshare_inited)
4610 		(void) ddi_modclose(sharefs_mod);
4611 
4612 	tsd_destroy(&zfs_fsyncer_key);
4613 	ldi_ident_release(zfs_li);
4614 	zfs_li = NULL;
4615 	mutex_destroy(&zfs_share_lock);
4616 
4617 	return (error);
4618 }
4619 
4620 int
4621 _info(struct modinfo *modinfop)
4622 {
4623 	return (mod_info(&modlinkage, modinfop));
4624 }
4625