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