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