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