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