xref: /titanic_51/usr/src/uts/common/fs/autofs/auto_vfsops.c (revision 89b43686db1fe9681d80a7cf5662730cb9378cae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22134a1f4eSCasper H.S. Dik  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23*89b43686SBayard Bell  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/errno.h>
287c478bd9Sstevel@tonic-gate #include <sys/proc.h>
297c478bd9Sstevel@tonic-gate #include <sys/disp.h>
307c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
31aa59c4cbSrsb #include <sys/vfs_opreg.h>
327c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
337c478bd9Sstevel@tonic-gate #include <sys/uio.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
377c478bd9Sstevel@tonic-gate #include <sys/mount.h>
387c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
447c478bd9Sstevel@tonic-gate #include <rpc/types.h>
457c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
467c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
477c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
487c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
517c478bd9Sstevel@tonic-gate #include <sys/policy.h>
527c478bd9Sstevel@tonic-gate #include <sys/zone.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int autofs_init(int, char *);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static major_t autofs_major;
577c478bd9Sstevel@tonic-gate static minor_t autofs_minor;
587c478bd9Sstevel@tonic-gate 
59780213fbSevanl kmutex_t autofs_minor_lock;
607c478bd9Sstevel@tonic-gate zone_key_t autofs_key;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static mntopts_t auto_mntopts;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * The AUTOFS system call.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static struct sysent autofssysent = {
687c478bd9Sstevel@tonic-gate 	2,
697c478bd9Sstevel@tonic-gate 	SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
707c478bd9Sstevel@tonic-gate 	autofssys
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
747c478bd9Sstevel@tonic-gate 	&mod_syscallops,
757c478bd9Sstevel@tonic-gate 	"AUTOFS syscall",
767c478bd9Sstevel@tonic-gate 	&autofssysent
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
807c478bd9Sstevel@tonic-gate static struct modlsys  modlsys32 = {
817c478bd9Sstevel@tonic-gate 	&mod_syscallops32,
827c478bd9Sstevel@tonic-gate 	"AUTOFS syscall (32-bit)",
837c478bd9Sstevel@tonic-gate 	&autofssysent
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
887c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
897c478bd9Sstevel@tonic-gate 	"autofs",
907c478bd9Sstevel@tonic-gate 	autofs_init,
910fbb751dSJohn Levon 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_STATS|VSW_ZMOUNT,
927c478bd9Sstevel@tonic-gate 	&auto_mntopts
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
997c478bd9Sstevel@tonic-gate 	&mod_fsops, "filesystem for autofs", &vfw
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1037c478bd9Sstevel@tonic-gate 	MODREV_1,
10418b3347eSevanl 	&modlfs,
1057c478bd9Sstevel@tonic-gate 	&modlsys,
1067c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1077c478bd9Sstevel@tonic-gate 	&modlsys32,
1087c478bd9Sstevel@tonic-gate #endif
1097c478bd9Sstevel@tonic-gate 	NULL
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * This is the module initialization routine.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate int
1167c478bd9Sstevel@tonic-gate _init(void)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int
1227c478bd9Sstevel@tonic-gate _fini(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * Don't allow the autofs module to be unloaded for now.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	return (EBUSY);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int
1317c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int autofs_fstype;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * autofs VFS operations
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static int auto_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
1427c478bd9Sstevel@tonic-gate static int auto_unmount(vfs_t *, int, cred_t *);
1437c478bd9Sstevel@tonic-gate static int auto_root(vfs_t *, vnode_t **);
1447c478bd9Sstevel@tonic-gate static int auto_statvfs(vfs_t *, struct statvfs64 *);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Auto Mount options table
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static char *direct_cancel[] = { MNTOPT_INDIRECT, NULL };
1517c478bd9Sstevel@tonic-gate static char *indirect_cancel[] = { MNTOPT_DIRECT, NULL };
1527c478bd9Sstevel@tonic-gate static char *browse_cancel[] = { MNTOPT_NOBROWSE, NULL };
1537c478bd9Sstevel@tonic-gate static char *nobrowse_cancel[] = { MNTOPT_BROWSE, NULL };
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static mntopt_t mntopts[] = {
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  *	option name		cancel options	default arg	flags
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate 	{ MNTOPT_DIRECT,	direct_cancel,	NULL,		0,
1607c478bd9Sstevel@tonic-gate 		NULL },
1617c478bd9Sstevel@tonic-gate 	{ MNTOPT_INDIRECT,	indirect_cancel, NULL,		0,
1627c478bd9Sstevel@tonic-gate 		NULL },
1637c478bd9Sstevel@tonic-gate 	{ MNTOPT_IGNORE,	NULL,		NULL,
1647c478bd9Sstevel@tonic-gate 		MO_DEFAULT|MO_TAG,	NULL },
1657c478bd9Sstevel@tonic-gate 	{ "nest",		NULL,		NULL,		MO_TAG,
1667c478bd9Sstevel@tonic-gate 		NULL },
1677c478bd9Sstevel@tonic-gate 	{ MNTOPT_BROWSE,	browse_cancel,	NULL,		MO_TAG,
1687c478bd9Sstevel@tonic-gate 		NULL },
1697c478bd9Sstevel@tonic-gate 	{ MNTOPT_NOBROWSE,	nobrowse_cancel, NULL,		MO_TAG,
1707c478bd9Sstevel@tonic-gate 		NULL },
1717c478bd9Sstevel@tonic-gate 	{ MNTOPT_RESTRICT,	NULL,		NULL,		MO_TAG,
1727c478bd9Sstevel@tonic-gate 		NULL },
1737c478bd9Sstevel@tonic-gate };
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static mntopts_t auto_mntopts = {
1767c478bd9Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1777c478bd9Sstevel@tonic-gate 	mntopts
1787c478bd9Sstevel@tonic-gate };
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1817c478bd9Sstevel@tonic-gate static void
1827c478bd9Sstevel@tonic-gate autofs_zone_destructor(zoneid_t zoneid, void *arg)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	struct autofs_globals *fngp = arg;
1857c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (fngp == NULL)
1887c478bd9Sstevel@tonic-gate 		return;
1897c478bd9Sstevel@tonic-gate 	ASSERT(fngp->fng_fnnode_count == 1);
1907c478bd9Sstevel@tonic-gate 	ASSERT(fngp->fng_unmount_threads == 0);
191134a1f4eSCasper H.S. Dik 
192134a1f4eSCasper H.S. Dik 	if (fngp->fng_autofs_daemon_dh != NULL)
193134a1f4eSCasper H.S. Dik 		door_ki_rele(fngp->fng_autofs_daemon_dh);
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * vn_alloc() initialized the rootnode with a count of 1; we need to
1967c478bd9Sstevel@tonic-gate 	 * make this 0 to placate auto_freefnnode().
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	vp = fntovn(fngp->fng_rootfnnodep);
1997c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count == 1);
2007c478bd9Sstevel@tonic-gate 	vp->v_count--;
2017c478bd9Sstevel@tonic-gate 	auto_freefnnode(fngp->fng_rootfnnodep);
2027c478bd9Sstevel@tonic-gate 	mutex_destroy(&fngp->fng_unmount_threads_lock);
2037c478bd9Sstevel@tonic-gate 	kmem_free(fngp, sizeof (*fngp));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * rootfnnodep is allocated here.  Its sole purpose is to provide
2087c478bd9Sstevel@tonic-gate  * read/write locking for top level fnnodes.  This object is
2097c478bd9Sstevel@tonic-gate  * persistent and will not be deallocated until the zone is destroyed.
2107c478bd9Sstevel@tonic-gate  *
2117c478bd9Sstevel@tonic-gate  * The current zone is implied as the zone of interest, since we will be
2127c478bd9Sstevel@tonic-gate  * calling zthread_create() which must be called from the correct zone.
2137c478bd9Sstevel@tonic-gate  */
21439d3e169Sevanl struct autofs_globals *
2157c478bd9Sstevel@tonic-gate autofs_zone_init(void)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	char rootname[sizeof ("root_fnnode_zone_") + ZONEID_WIDTH];
2187c478bd9Sstevel@tonic-gate 	struct autofs_globals *fngp;
2197c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	fngp = kmem_zalloc(sizeof (*fngp), KM_SLEEP);
2227c478bd9Sstevel@tonic-gate 	(void) snprintf(rootname, sizeof (rootname), "root_fnnode_zone_%d",
2237c478bd9Sstevel@tonic-gate 	    zoneid);
2247c478bd9Sstevel@tonic-gate 	fngp->fng_rootfnnodep = auto_makefnnode(VNON, NULL, rootname, CRED(),
2257c478bd9Sstevel@tonic-gate 	    fngp);
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Don't need to hold fng_rootfnnodep as it's never really used for
2287c478bd9Sstevel@tonic-gate 	 * anything.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	fngp->fng_fnnode_count = 1;
2317c478bd9Sstevel@tonic-gate 	fngp->fng_printed_not_running_msg = 0;
2327c478bd9Sstevel@tonic-gate 	fngp->fng_zoneid = zoneid;
2337c478bd9Sstevel@tonic-gate 	mutex_init(&fngp->fng_unmount_threads_lock, NULL, MUTEX_DEFAULT,
2347c478bd9Sstevel@tonic-gate 	    NULL);
2357c478bd9Sstevel@tonic-gate 	fngp->fng_unmount_threads = 0;
2367c478bd9Sstevel@tonic-gate 
23739d3e169Sevanl 	mutex_init(&fngp->fng_autofs_daemon_lock, NULL, MUTEX_DEFAULT, NULL);
23839d3e169Sevanl 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Start the unmounter thread for this zone.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, auto_do_unmount, fngp, 0, minclsyspri);
2437c478bd9Sstevel@tonic-gate 	return (fngp);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate int
2477c478bd9Sstevel@tonic-gate autofs_init(int fstype, char *name)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t auto_vfsops_template[] = {
250aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = auto_mount },
251aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = auto_unmount },
252aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = auto_root },
253aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = auto_statvfs },
2547c478bd9Sstevel@tonic-gate 		NULL,			NULL
2557c478bd9Sstevel@tonic-gate 	};
2567c478bd9Sstevel@tonic-gate 	int error;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	autofs_fstype = fstype;
2597c478bd9Sstevel@tonic-gate 	ASSERT(autofs_fstype != 0);
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * Associate VFS ops vector with this fstype
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, auto_vfsops_template, NULL);
2647c478bd9Sstevel@tonic-gate 	if (error != 0) {
2657c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs_init: bad vfs ops template");
2667c478bd9Sstevel@tonic-gate 		return (error);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, auto_vnodeops_template, &auto_vnodeops);
2707c478bd9Sstevel@tonic-gate 	if (error != 0) {
2717c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2727c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs_init: bad vnode ops template");
2737c478bd9Sstevel@tonic-gate 		return (error);
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	mutex_init(&autofs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Assign unique major number for all autofs mounts
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	if ((autofs_major = getudev()) == (major_t)-1) {
2817c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
2827c478bd9Sstevel@tonic-gate 		    "autofs: autofs_init: can't get unique device number");
2837c478bd9Sstevel@tonic-gate 		mutex_destroy(&autofs_minor_lock);
2847c478bd9Sstevel@tonic-gate 		return (1);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * We'd like to be able to provide a constructor here, but we can't
2897c478bd9Sstevel@tonic-gate 	 * since it wants to zthread_create(), something it can't do in a ZSD
2907c478bd9Sstevel@tonic-gate 	 * constructor.
2917c478bd9Sstevel@tonic-gate 	 */
2927c478bd9Sstevel@tonic-gate 	zone_key_create(&autofs_key, NULL, NULL, autofs_zone_destructor);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static char *restropts[] = {
2987c478bd9Sstevel@tonic-gate 	RESTRICTED_MNTOPTS
2997c478bd9Sstevel@tonic-gate };
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * This routine adds those options to the option string `buf' which are
3037c478bd9Sstevel@tonic-gate  * forced by secpolicy_fs_mount.  If the automatic "security" options
3047c478bd9Sstevel@tonic-gate  * are set, the option string gets them added if they aren't already
3057c478bd9Sstevel@tonic-gate  * there.  We search the string with "strstr" and make sure that
3067c478bd9Sstevel@tonic-gate  * the string we find is bracketed with <start|",">MNTOPT<","|"\0">
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * This is one half of the option inheritence algorithm which
3097c478bd9Sstevel@tonic-gate  * implements the "restrict" option.  The other half is implemented
3107c478bd9Sstevel@tonic-gate  * in automountd; it takes its cue from the options we add here.
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate static int
3137c478bd9Sstevel@tonic-gate autofs_restrict_opts(struct vfs *vfsp, char *buf, size_t maxlen, size_t *curlen)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	int i;
3167c478bd9Sstevel@tonic-gate 	char *p;
3177c478bd9Sstevel@tonic-gate 	size_t len = *curlen - 1;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* Unrestricted */
3207c478bd9Sstevel@tonic-gate 	if (!vfs_optionisset(vfsp, restropts[0], NULL))
3217c478bd9Sstevel@tonic-gate 		return (0);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (restropts)/sizeof (restropts[0]); i++) {
3247c478bd9Sstevel@tonic-gate 		size_t olen = strlen(restropts[i]);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		/* Add "restrict" always and the others insofar set */
3277c478bd9Sstevel@tonic-gate 		if ((i == 0 || vfs_optionisset(vfsp, restropts[i], NULL)) &&
3287c478bd9Sstevel@tonic-gate 		    ((p = strstr(buf, restropts[i])) == NULL ||
3297c478bd9Sstevel@tonic-gate 		    !((p == buf || p[-1] == ',') &&
3307c478bd9Sstevel@tonic-gate 		    (p[olen] == '\0' || p[olen] == ',')))) {
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 			if (len + olen + 1 > maxlen)
3337c478bd9Sstevel@tonic-gate 				return (-1);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			if (*buf != '\0')
3367c478bd9Sstevel@tonic-gate 				buf[len++] = ',';
3377c478bd9Sstevel@tonic-gate 			(void) strcpy(&buf[len], restropts[i]);
3387c478bd9Sstevel@tonic-gate 			len += olen;
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	*curlen = len + 1;
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /* ARGSUSED */
3467c478bd9Sstevel@tonic-gate static int
3477c478bd9Sstevel@tonic-gate auto_mount(vfs_t *vfsp, vnode_t *vp, struct mounta *uap, cred_t *cr)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	int error;
3507c478bd9Sstevel@tonic-gate 	size_t len = 0;
35139d3e169Sevanl 	autofs_args args;
3527c478bd9Sstevel@tonic-gate 	fninfo_t *fnip = NULL;
3537c478bd9Sstevel@tonic-gate 	vnode_t *rootvp = NULL;
3547c478bd9Sstevel@tonic-gate 	fnnode_t *rootfnp = NULL;
3557c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
3567c478bd9Sstevel@tonic-gate 	char datalen = uap->datalen;
3577c478bd9Sstevel@tonic-gate 	dev_t autofs_dev;
3587c478bd9Sstevel@tonic-gate 	char strbuff[MAXPATHLEN + 1];
359af4c679fSSean McEnroe 	vnode_t *kkvp;
3607c478bd9Sstevel@tonic-gate 	struct autofs_globals *fngp;
3617c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mount: vfs %p vp %p\n", (void *)vfsp,
3647c478bd9Sstevel@tonic-gate 	    (void *)vp));
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
3677c478bd9Sstevel@tonic-gate 		return (EPERM);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (zone == global_zone) {
3707c478bd9Sstevel@tonic-gate 		zone_t *mntzone;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3737c478bd9Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
3747c478bd9Sstevel@tonic-gate 		zone_rele(mntzone);
3757c478bd9Sstevel@tonic-gate 		if (mntzone != zone) {
3767c478bd9Sstevel@tonic-gate 			return (EBUSY);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * Stop the mount from going any further if the zone is going away.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN)
3847c478bd9Sstevel@tonic-gate 		return (EBUSY);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * We need a lock to serialize this; minor_lock is as good as any.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	mutex_enter(&autofs_minor_lock);
3907c478bd9Sstevel@tonic-gate 	if ((fngp = zone_getspecific(autofs_key, zone)) == NULL) {
3917c478bd9Sstevel@tonic-gate 		fngp = autofs_zone_init();
3927c478bd9Sstevel@tonic-gate 		(void) zone_setspecific(autofs_key, zone, fngp);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	mutex_exit(&autofs_minor_lock);
3957c478bd9Sstevel@tonic-gate 	ASSERT(fngp != NULL);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * Get arguments
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE) {
4017c478bd9Sstevel@tonic-gate 		if (datalen != sizeof (args))
4027c478bd9Sstevel@tonic-gate 			return (EINVAL);
4037c478bd9Sstevel@tonic-gate 		error = kcopy(data, &args, sizeof (args));
4047c478bd9Sstevel@tonic-gate 	} else {
4057c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
4067c478bd9Sstevel@tonic-gate 			if (datalen != sizeof (args))
4077c478bd9Sstevel@tonic-gate 				return (EINVAL);
4087c478bd9Sstevel@tonic-gate 			error = copyin(data, &args, sizeof (args));
4097c478bd9Sstevel@tonic-gate 		} else {
4107c478bd9Sstevel@tonic-gate 			struct autofs_args32 args32;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			if (datalen != sizeof (args32))
4137c478bd9Sstevel@tonic-gate 				return (EINVAL);
4147c478bd9Sstevel@tonic-gate 			error = copyin(data, &args32, sizeof (args32));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			args.addr.maxlen = args32.addr.maxlen;
4177c478bd9Sstevel@tonic-gate 			args.addr.len = args32.addr.len;
4187c478bd9Sstevel@tonic-gate 			args.addr.buf = (char *)(uintptr_t)args32.addr.buf;
4197c478bd9Sstevel@tonic-gate 			args.path = (char *)(uintptr_t)args32.path;
4207c478bd9Sstevel@tonic-gate 			args.opts = (char *)(uintptr_t)args32.opts;
4217c478bd9Sstevel@tonic-gate 			args.map = (char *)(uintptr_t)args32.map;
4227c478bd9Sstevel@tonic-gate 			args.subdir = (char *)(uintptr_t)args32.subdir;
4237c478bd9Sstevel@tonic-gate 			args.key = (char *)(uintptr_t)args32.key;
4247c478bd9Sstevel@tonic-gate 			args.mount_to = args32.mount_to;
4257c478bd9Sstevel@tonic-gate 			args.rpc_to = args32.rpc_to;
4267c478bd9Sstevel@tonic-gate 			args.direct = args32.direct;
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	if (error)
4307c478bd9Sstevel@tonic-gate 		return (EFAULT);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * For a remount, only update mount information
4347c478bd9Sstevel@tonic-gate 	 * i.e. default mount options, map name, etc.
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
4377c478bd9Sstevel@tonic-gate 		fnip = vfstofni(vfsp);
4387c478bd9Sstevel@tonic-gate 		if (fnip == NULL)
4397c478bd9Sstevel@tonic-gate 			return (EINVAL);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		if (args.direct == 1)
4427c478bd9Sstevel@tonic-gate 			fnip->fi_flags |= MF_DIRECT;
4437c478bd9Sstevel@tonic-gate 		else
4447c478bd9Sstevel@tonic-gate 			fnip->fi_flags &= ~MF_DIRECT;
4457c478bd9Sstevel@tonic-gate 		fnip->fi_mount_to = args.mount_to;
4467c478bd9Sstevel@tonic-gate 		fnip->fi_rpc_to = args.rpc_to;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		/*
4497c478bd9Sstevel@tonic-gate 		 * Get default options
4507c478bd9Sstevel@tonic-gate 		 */
4517c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
4527c478bd9Sstevel@tonic-gate 			error = copystr(args.opts, strbuff, sizeof (strbuff),
4537c478bd9Sstevel@tonic-gate 			    &len);
4547c478bd9Sstevel@tonic-gate 		else
4557c478bd9Sstevel@tonic-gate 			error = copyinstr(args.opts, strbuff, sizeof (strbuff),
4567c478bd9Sstevel@tonic-gate 			    &len);
4577c478bd9Sstevel@tonic-gate 		if (error)
4587c478bd9Sstevel@tonic-gate 			return (EFAULT);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		if (autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len)
4617c478bd9Sstevel@tonic-gate 		    != 0) {
4627c478bd9Sstevel@tonic-gate 			return (EFAULT);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_opts, fnip->fi_optslen);
4667c478bd9Sstevel@tonic-gate 		fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
4677c478bd9Sstevel@tonic-gate 		fnip->fi_optslen = (int)len;
4687c478bd9Sstevel@tonic-gate 		bcopy(strbuff, fnip->fi_opts, len);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * Get context/map name
4727c478bd9Sstevel@tonic-gate 		 */
4737c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
4747c478bd9Sstevel@tonic-gate 			error = copystr(args.map, strbuff, sizeof (strbuff),
4757c478bd9Sstevel@tonic-gate 			    &len);
4767c478bd9Sstevel@tonic-gate 		else
4777c478bd9Sstevel@tonic-gate 			error = copyinstr(args.map, strbuff, sizeof (strbuff),
4787c478bd9Sstevel@tonic-gate 			    &len);
4797c478bd9Sstevel@tonic-gate 		if (error)
4807c478bd9Sstevel@tonic-gate 			return (EFAULT);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_map, fnip->fi_maplen);
4837c478bd9Sstevel@tonic-gate 		fnip->fi_map = kmem_alloc(len, KM_SLEEP);
4847c478bd9Sstevel@tonic-gate 		fnip->fi_maplen = (int)len;
4857c478bd9Sstevel@tonic-gate 		bcopy(strbuff, fnip->fi_map, len);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		return (0);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/*
4917c478bd9Sstevel@tonic-gate 	 * Allocate fninfo struct and attach it to vfs
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	fnip = kmem_zalloc(sizeof (*fnip), KM_SLEEP);
4947c478bd9Sstevel@tonic-gate 	fnip->fi_mountvfs = vfsp;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	fnip->fi_mount_to = args.mount_to;
4977c478bd9Sstevel@tonic-gate 	fnip->fi_rpc_to = args.rpc_to;
4987c478bd9Sstevel@tonic-gate 	fnip->fi_refcnt = 0;
4997c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = AUTOFS_BLOCKSIZE;
5007c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = autofs_fstype;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * Assign a unique device id to the mount
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	mutex_enter(&autofs_minor_lock);
5067c478bd9Sstevel@tonic-gate 	do {
5077c478bd9Sstevel@tonic-gate 		autofs_minor = (autofs_minor + 1) & L_MAXMIN32;
5087c478bd9Sstevel@tonic-gate 		autofs_dev = makedevice(autofs_major, autofs_minor);
5097c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(autofs_dev));
5107c478bd9Sstevel@tonic-gate 	mutex_exit(&autofs_minor_lock);
5117c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = autofs_dev;
5127c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, autofs_dev, autofs_fstype);
5137c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (void *)fnip;
5147c478bd9Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/*
5177c478bd9Sstevel@tonic-gate 	 * Get daemon address
5187c478bd9Sstevel@tonic-gate 	 */
5197c478bd9Sstevel@tonic-gate 	fnip->fi_addr.len = args.addr.len;
5207c478bd9Sstevel@tonic-gate 	fnip->fi_addr.maxlen = fnip->fi_addr.len;
5217c478bd9Sstevel@tonic-gate 	fnip->fi_addr.buf = kmem_alloc(args.addr.len, KM_SLEEP);
5227c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5237c478bd9Sstevel@tonic-gate 		error = kcopy(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
5247c478bd9Sstevel@tonic-gate 	else
5257c478bd9Sstevel@tonic-gate 		error = copyin(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
5267c478bd9Sstevel@tonic-gate 	if (error) {
5277c478bd9Sstevel@tonic-gate 		error = EFAULT;
5287c478bd9Sstevel@tonic-gate 		goto errout;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	fnip->fi_zoneid = getzoneid();
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Get path for mountpoint
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5367c478bd9Sstevel@tonic-gate 		error = copystr(args.path, strbuff, sizeof (strbuff), &len);
5377c478bd9Sstevel@tonic-gate 	else
5387c478bd9Sstevel@tonic-gate 		error = copyinstr(args.path, strbuff, sizeof (strbuff), &len);
5397c478bd9Sstevel@tonic-gate 	if (error) {
5407c478bd9Sstevel@tonic-gate 		error = EFAULT;
5417c478bd9Sstevel@tonic-gate 		goto errout;
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	fnip->fi_path = kmem_alloc(len, KM_SLEEP);
5447c478bd9Sstevel@tonic-gate 	fnip->fi_pathlen = (int)len;
5457c478bd9Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_path, len);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * Get default options
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5517c478bd9Sstevel@tonic-gate 		error = copystr(args.opts, strbuff, sizeof (strbuff), &len);
5527c478bd9Sstevel@tonic-gate 	else
5537c478bd9Sstevel@tonic-gate 		error = copyinstr(args.opts, strbuff, sizeof (strbuff), &len);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (error != 0 ||
5567c478bd9Sstevel@tonic-gate 	    autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len) != 0) {
5577c478bd9Sstevel@tonic-gate 		error = EFAULT;
5587c478bd9Sstevel@tonic-gate 		goto errout;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
5617c478bd9Sstevel@tonic-gate 	fnip->fi_optslen = (int)len;
5627c478bd9Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_opts, len);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * Get context/map name
5667c478bd9Sstevel@tonic-gate 	 */
5677c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5687c478bd9Sstevel@tonic-gate 		error = copystr(args.map, strbuff, sizeof (strbuff), &len);
5697c478bd9Sstevel@tonic-gate 	else
5707c478bd9Sstevel@tonic-gate 		error = copyinstr(args.map, strbuff, sizeof (strbuff), &len);
5717c478bd9Sstevel@tonic-gate 	if (error) {
5727c478bd9Sstevel@tonic-gate 		error = EFAULT;
5737c478bd9Sstevel@tonic-gate 		goto errout;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	fnip->fi_map = kmem_alloc(len, KM_SLEEP);
5767c478bd9Sstevel@tonic-gate 	fnip->fi_maplen = (int)len;
5777c478bd9Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_map, len);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * Get subdirectory within map
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5837c478bd9Sstevel@tonic-gate 		error = copystr(args.subdir, strbuff, sizeof (strbuff), &len);
5847c478bd9Sstevel@tonic-gate 	else
5857c478bd9Sstevel@tonic-gate 		error = copyinstr(args.subdir, strbuff, sizeof (strbuff), &len);
5867c478bd9Sstevel@tonic-gate 	if (error) {
5877c478bd9Sstevel@tonic-gate 		error = EFAULT;
5887c478bd9Sstevel@tonic-gate 		goto errout;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	fnip->fi_subdir = kmem_alloc(len, KM_SLEEP);
5917c478bd9Sstevel@tonic-gate 	fnip->fi_subdirlen = (int)len;
5927c478bd9Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_subdir, len);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * Get the key
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5987c478bd9Sstevel@tonic-gate 		error = copystr(args.key, strbuff, sizeof (strbuff), &len);
5997c478bd9Sstevel@tonic-gate 	else
6007c478bd9Sstevel@tonic-gate 		error = copyinstr(args.key, strbuff, sizeof (strbuff), &len);
6017c478bd9Sstevel@tonic-gate 	if (error) {
6027c478bd9Sstevel@tonic-gate 		error = EFAULT;
6037c478bd9Sstevel@tonic-gate 		goto errout;
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	fnip->fi_key = kmem_alloc(len, KM_SLEEP);
6067c478bd9Sstevel@tonic-gate 	fnip->fi_keylen = (int)len;
6077c478bd9Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_key, len);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * Is this a direct mount?
6117c478bd9Sstevel@tonic-gate 	 */
6127c478bd9Sstevel@tonic-gate 	if (args.direct == 1)
6137c478bd9Sstevel@tonic-gate 		fnip->fi_flags |= MF_DIRECT;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/*
6167c478bd9Sstevel@tonic-gate 	 * Setup netconfig.
6177c478bd9Sstevel@tonic-gate 	 * Can I pass in knconf as mount argument? what
6187c478bd9Sstevel@tonic-gate 	 * happens when the daemon gets restarted?
6197c478bd9Sstevel@tonic-gate 	 */
6207c478bd9Sstevel@tonic-gate 	if ((error = lookupname("/dev/ticotsord", UIO_SYSSPACE, FOLLOW,
621af4c679fSSean McEnroe 	    NULLVPP, &kkvp)) != 0) {
6227c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs: lookupname: %d", error);
6237c478bd9Sstevel@tonic-gate 		goto errout;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
626af4c679fSSean McEnroe 	fnip->fi_knconf.knc_rdev = kkvp->v_rdev;
6277c478bd9Sstevel@tonic-gate 	fnip->fi_knconf.knc_protofmly = NC_LOOPBACK;
6287c478bd9Sstevel@tonic-gate 	fnip->fi_knconf.knc_semantics = NC_TPI_COTS_ORD;
629af4c679fSSean McEnroe 	VN_RELE(kkvp);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * Make the root vnode
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	rootfnp = auto_makefnnode(VDIR, vfsp, fnip->fi_path, cr, fngp);
6357c478bd9Sstevel@tonic-gate 	if (rootfnp == NULL) {
6367c478bd9Sstevel@tonic-gate 		error = ENOMEM;
6377c478bd9Sstevel@tonic-gate 		goto errout;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	rootvp = fntovn(rootfnp);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	rootvp->v_flag |= VROOT;
6427c478bd9Sstevel@tonic-gate 	rootfnp->fn_mode = AUTOFS_MODE;
6437c478bd9Sstevel@tonic-gate 	rootfnp->fn_parent = rootfnp;
6447c478bd9Sstevel@tonic-gate 	/* account for ".." entry */
6457c478bd9Sstevel@tonic-gate 	rootfnp->fn_linkcnt = rootfnp->fn_size = 1;
6467c478bd9Sstevel@tonic-gate 	fnip->fi_rootvp = rootvp;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Add to list of top level AUTOFS' if it is being mounted by
6507c478bd9Sstevel@tonic-gate 	 * a user level process.
6517c478bd9Sstevel@tonic-gate 	 */
6527c478bd9Sstevel@tonic-gate 	if (!(uap->flags & MS_SYSSPACE)) {
6537c478bd9Sstevel@tonic-gate 		rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_WRITER);
6547c478bd9Sstevel@tonic-gate 		rootfnp->fn_parent = fngp->fng_rootfnnodep;
6557c478bd9Sstevel@tonic-gate 		rootfnp->fn_next = fngp->fng_rootfnnodep->fn_dirents;
6567c478bd9Sstevel@tonic-gate 		fngp->fng_rootfnnodep->fn_dirents = rootfnp;
6577c478bd9Sstevel@tonic-gate 		rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
6617c478bd9Sstevel@tonic-gate 	    (void *)vfsp, (void *)rootvp, (void *)fnip, error));
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	return (0);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate errout:
6667c478bd9Sstevel@tonic-gate 	ASSERT(fnip != NULL);
6677c478bd9Sstevel@tonic-gate 	ASSERT((uap->flags & MS_REMOUNT) == 0);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (fnip->fi_addr.buf != NULL)
6707c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
6717c478bd9Sstevel@tonic-gate 	if (fnip->fi_path != NULL)
6727c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_path, fnip->fi_pathlen);
6737c478bd9Sstevel@tonic-gate 	if (fnip->fi_opts != NULL)
6747c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_opts, fnip->fi_optslen);
6757c478bd9Sstevel@tonic-gate 	if (fnip->fi_map != NULL)
6767c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_map, fnip->fi_maplen);
6777c478bd9Sstevel@tonic-gate 	if (fnip->fi_subdir != NULL)
6787c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
6797c478bd9Sstevel@tonic-gate 	if (fnip->fi_key != NULL)
6807c478bd9Sstevel@tonic-gate 		kmem_free(fnip->fi_key, fnip->fi_keylen);
6817c478bd9Sstevel@tonic-gate 	kmem_free(fnip, sizeof (*fnip));
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
6847c478bd9Sstevel@tonic-gate 	    (void *)vfsp, (void *)rootvp, (void *)fnip, error));
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	return (error);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate /* ARGSUSED */
6907c478bd9Sstevel@tonic-gate static int
6917c478bd9Sstevel@tonic-gate auto_unmount(vfs_t *vfsp, int flag, cred_t *cr)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	fninfo_t *fnip;
6947c478bd9Sstevel@tonic-gate 	vnode_t *rvp;
6957c478bd9Sstevel@tonic-gate 	fnnode_t *rfnp, *fnp, *pfnp;
6967c478bd9Sstevel@tonic-gate 	fnnode_t *myrootfnnodep;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	fnip = vfstofni(vfsp);
6997c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_unmount vfsp %p fnip %p\n", (void *)vfsp,
7007c478bd9Sstevel@tonic-gate 	    (void *)fnip));
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
7037c478bd9Sstevel@tonic-gate 		return (EPERM);
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
7067c478bd9Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE)
7097c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(vfsp->vfs_vnodecovered));
7127c478bd9Sstevel@tonic-gate 	rvp = fnip->fi_rootvp;
7137c478bd9Sstevel@tonic-gate 	rfnp = vntofn(rvp);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (rvp->v_count > 1 || rfnp->fn_dirents != NULL)
7167c478bd9Sstevel@tonic-gate 		return (EBUSY);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * The root vnode is on the linked list of root fnnodes only if
7207c478bd9Sstevel@tonic-gate 	 * this was not a trigger node. Since we have no way of knowing,
7217c478bd9Sstevel@tonic-gate 	 * if we don't find it, then we assume it was a trigger node.
7227c478bd9Sstevel@tonic-gate 	 */
7237c478bd9Sstevel@tonic-gate 	myrootfnnodep = rfnp->fn_globals->fng_rootfnnodep;
7247c478bd9Sstevel@tonic-gate 	pfnp = NULL;
7257c478bd9Sstevel@tonic-gate 	rw_enter(&myrootfnnodep->fn_rwlock, RW_WRITER);
7267c478bd9Sstevel@tonic-gate 	fnp = myrootfnnodep->fn_dirents;
7277c478bd9Sstevel@tonic-gate 	while (fnp != NULL) {
7287c478bd9Sstevel@tonic-gate 		if (fnp == rfnp) {
7297c478bd9Sstevel@tonic-gate 			/*
7307c478bd9Sstevel@tonic-gate 			 * A check here is made to see if rvp is busy.  If
7317c478bd9Sstevel@tonic-gate 			 * so, return EBUSY.  Otherwise proceed with
7327c478bd9Sstevel@tonic-gate 			 * disconnecting it from the list.
7337c478bd9Sstevel@tonic-gate 			 */
7347c478bd9Sstevel@tonic-gate 			if (rvp->v_count > 1 || rfnp->fn_dirents != NULL) {
7357c478bd9Sstevel@tonic-gate 				rw_exit(&myrootfnnodep->fn_rwlock);
7367c478bd9Sstevel@tonic-gate 				return (EBUSY);
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			if (pfnp)
7397c478bd9Sstevel@tonic-gate 				pfnp->fn_next = fnp->fn_next;
7407c478bd9Sstevel@tonic-gate 			else
7417c478bd9Sstevel@tonic-gate 				myrootfnnodep->fn_dirents = fnp->fn_next;
7427c478bd9Sstevel@tonic-gate 			fnp->fn_next = NULL;
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		pfnp = fnp;
7467c478bd9Sstevel@tonic-gate 		fnp = fnp->fn_next;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	rw_exit(&myrootfnnodep->fn_rwlock);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	ASSERT(rvp->v_count == 1);
7517c478bd9Sstevel@tonic-gate 	ASSERT(rfnp->fn_size == 1);
7527c478bd9Sstevel@tonic-gate 	ASSERT(rfnp->fn_linkcnt == 1);
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * The following drops linkcnt to 0, therefore the disconnect is
7557c478bd9Sstevel@tonic-gate 	 * not attempted when auto_inactive() is called by
7567c478bd9Sstevel@tonic-gate 	 * vn_rele(). This is necessary because we have nothing to get
7577c478bd9Sstevel@tonic-gate 	 * disconnected from since we're the root of the filesystem. As a
7587c478bd9Sstevel@tonic-gate 	 * side effect the node is not freed, therefore I should free the
7597c478bd9Sstevel@tonic-gate 	 * node here.
7607c478bd9Sstevel@tonic-gate 	 *
7617c478bd9Sstevel@tonic-gate 	 * XXX - I really need to think of a better way of doing this.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	rfnp->fn_size--;
7647c478bd9Sstevel@tonic-gate 	rfnp->fn_linkcnt--;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/*
7677c478bd9Sstevel@tonic-gate 	 * release of last reference causes node
7687c478bd9Sstevel@tonic-gate 	 * to be freed
7697c478bd9Sstevel@tonic-gate 	 */
7707c478bd9Sstevel@tonic-gate 	VN_RELE(rvp);
7717c478bd9Sstevel@tonic-gate 	rfnp->fn_parent = NULL;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	auto_freefnnode(rfnp);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
7767c478bd9Sstevel@tonic-gate 	kmem_free(fnip->fi_path, fnip->fi_pathlen);
7777c478bd9Sstevel@tonic-gate 	kmem_free(fnip->fi_map, fnip->fi_maplen);
7787c478bd9Sstevel@tonic-gate 	kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
7797c478bd9Sstevel@tonic-gate 	kmem_free(fnip->fi_key, fnip->fi_keylen);
7807c478bd9Sstevel@tonic-gate 	kmem_free(fnip->fi_opts, fnip->fi_optslen);
7817c478bd9Sstevel@tonic-gate 	kmem_free(fnip, sizeof (*fnip));
7827c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_unmount: return=0\n"));
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	return (0);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  * find root of autofs
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate static int
7927c478bd9Sstevel@tonic-gate auto_root(vfs_t *vfsp, vnode_t **vpp)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	*vpp = (vnode_t *)vfstofni(vfsp)->fi_rootvp;
7957c478bd9Sstevel@tonic-gate 	VN_HOLD(*vpp);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_root: vfs %p, *vpp %p\n", (void *)vfsp,
7987c478bd9Sstevel@tonic-gate 	    (void *)*vpp));
7997c478bd9Sstevel@tonic-gate 	return (0);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * Get file system statistics.
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate static int
8067c478bd9Sstevel@tonic-gate auto_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	dev32_t d32;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_statvfs %p\n", (void *)vfsp));
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	bzero(sbp, sizeof (*sbp));
8137c478bd9Sstevel@tonic-gate 	sbp->f_bsize	= vfsp->vfs_bsize;
8147c478bd9Sstevel@tonic-gate 	sbp->f_frsize	= sbp->f_bsize;
8157c478bd9Sstevel@tonic-gate 	sbp->f_blocks	= (fsblkcnt64_t)0;
8167c478bd9Sstevel@tonic-gate 	sbp->f_bfree	= (fsblkcnt64_t)0;
8177c478bd9Sstevel@tonic-gate 	sbp->f_bavail	= (fsblkcnt64_t)0;
8187c478bd9Sstevel@tonic-gate 	sbp->f_files	= (fsfilcnt64_t)0;
8197c478bd9Sstevel@tonic-gate 	sbp->f_ffree	= (fsfilcnt64_t)0;
8207c478bd9Sstevel@tonic-gate 	sbp->f_favail	= (fsfilcnt64_t)0;
8217c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
8227c478bd9Sstevel@tonic-gate 	sbp->f_fsid	= d32;
8237c478bd9Sstevel@tonic-gate 	(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
8247c478bd9Sstevel@tonic-gate 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
8257c478bd9Sstevel@tonic-gate 	sbp->f_namemax = MAXNAMELEN;
8267c478bd9Sstevel@tonic-gate 	(void) strcpy(sbp->f_fstr, MNTTYPE_AUTOFS);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	return (0);
8297c478bd9Sstevel@tonic-gate }
830