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