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