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