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