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