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