xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_vfsops.c (revision 1488:196daa2cf3db)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1488Srsb  * Common Development and Distribution License (the "License").
6*1488Srsb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1488Srsb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/proc.h>
310Sstevel@tonic-gate #include <sys/disp.h>
320Sstevel@tonic-gate #include <sys/vfs.h>
330Sstevel@tonic-gate #include <sys/vnode.h>
340Sstevel@tonic-gate #include <sys/uio.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/statvfs.h>
380Sstevel@tonic-gate #include <sys/mount.h>
390Sstevel@tonic-gate #include <sys/tiuser.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/mkdev.h>
430Sstevel@tonic-gate #include <sys/systm.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/pathname.h>
460Sstevel@tonic-gate #include <rpc/types.h>
470Sstevel@tonic-gate #include <rpc/auth.h>
480Sstevel@tonic-gate #include <rpc/clnt.h>
490Sstevel@tonic-gate #include <sys/dnlc.h>
500Sstevel@tonic-gate #include <fs/fs_subr.h>
510Sstevel@tonic-gate #include <sys/fs/autofs.h>
520Sstevel@tonic-gate #include <rpcsvc/autofs_prot.h>
530Sstevel@tonic-gate #include <sys/note.h>
540Sstevel@tonic-gate #include <sys/modctl.h>
550Sstevel@tonic-gate #include <sys/mntent.h>
560Sstevel@tonic-gate #include <sys/policy.h>
570Sstevel@tonic-gate #include <sys/zone.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static int autofs_init(int, char *);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static major_t autofs_major;
620Sstevel@tonic-gate static minor_t autofs_minor;
630Sstevel@tonic-gate static kmutex_t autofs_minor_lock;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate zone_key_t autofs_key;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static mntopts_t auto_mntopts;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * The AUTOFS system call.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate static struct sysent autofssysent = {
730Sstevel@tonic-gate 	2,
740Sstevel@tonic-gate 	SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
750Sstevel@tonic-gate 	autofssys
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static struct modlsys modlsys = {
790Sstevel@tonic-gate 	&mod_syscallops,
800Sstevel@tonic-gate 	"AUTOFS syscall",
810Sstevel@tonic-gate 	&autofssysent
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
850Sstevel@tonic-gate static struct modlsys  modlsys32 = {
860Sstevel@tonic-gate 	&mod_syscallops32,
870Sstevel@tonic-gate 	"AUTOFS syscall (32-bit)",
880Sstevel@tonic-gate 	&autofssysent
890Sstevel@tonic-gate };
900Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static vfsdef_t vfw = {
930Sstevel@tonic-gate 	VFSDEF_VERSION,
940Sstevel@tonic-gate 	"autofs",
950Sstevel@tonic-gate 	autofs_init,
96*1488Srsb 	VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_STATS,
970Sstevel@tonic-gate 	&auto_mntopts
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Module linkage information for the kernel.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static struct modlfs modlfs = {
1040Sstevel@tonic-gate 	&mod_fsops, "filesystem for autofs", &vfw
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static struct modlinkage modlinkage = {
1080Sstevel@tonic-gate 	MODREV_1,
1090Sstevel@tonic-gate 	&modlsys,
1100Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1110Sstevel@tonic-gate 	&modlsys32,
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate 	&modlfs,
1140Sstevel@tonic-gate 	NULL
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * There are not enough stubs for rpcmod so we must force load it
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate char _depends_on[] = "strmod/rpcmod misc/rpcsec fs/mntfs";
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * This is the module initialization routine.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate int
1260Sstevel@tonic-gate _init(void)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate int
1320Sstevel@tonic-gate _fini(void)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * Don't allow the autofs module to be unloaded for now.
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	return (EBUSY);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate int
1410Sstevel@tonic-gate _info(struct modinfo *modinfop)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static int autofs_fstype;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * autofs VFS operations
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate static int auto_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
1520Sstevel@tonic-gate static int auto_unmount(vfs_t *, int, cred_t *);
1530Sstevel@tonic-gate static int auto_root(vfs_t *, vnode_t **);
1540Sstevel@tonic-gate static int auto_statvfs(vfs_t *, struct statvfs64 *);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * Auto Mount options table
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static char *direct_cancel[] = { MNTOPT_INDIRECT, NULL };
1610Sstevel@tonic-gate static char *indirect_cancel[] = { MNTOPT_DIRECT, NULL };
1620Sstevel@tonic-gate static char *browse_cancel[] = { MNTOPT_NOBROWSE, NULL };
1630Sstevel@tonic-gate static char *nobrowse_cancel[] = { MNTOPT_BROWSE, NULL };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static mntopt_t mntopts[] = {
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  *	option name		cancel options	default arg	flags
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate 	{ MNTOPT_DIRECT,	direct_cancel,	NULL,		0,
1700Sstevel@tonic-gate 		NULL },
1710Sstevel@tonic-gate 	{ MNTOPT_INDIRECT,	indirect_cancel, NULL,		0,
1720Sstevel@tonic-gate 		NULL },
1730Sstevel@tonic-gate 	{ MNTOPT_IGNORE,	NULL,		NULL,
1740Sstevel@tonic-gate 		MO_DEFAULT|MO_TAG,	NULL },
1750Sstevel@tonic-gate 	{ "nest",		NULL,		NULL,		MO_TAG,
1760Sstevel@tonic-gate 		NULL },
1770Sstevel@tonic-gate 	{ MNTOPT_BROWSE,	browse_cancel,	NULL,		MO_TAG,
1780Sstevel@tonic-gate 		NULL },
1790Sstevel@tonic-gate 	{ MNTOPT_NOBROWSE,	nobrowse_cancel, NULL,		MO_TAG,
1800Sstevel@tonic-gate 		NULL },
1810Sstevel@tonic-gate 	{ MNTOPT_RESTRICT,	NULL,		NULL,		MO_TAG,
1820Sstevel@tonic-gate 		NULL },
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static mntopts_t auto_mntopts = {
1860Sstevel@tonic-gate 	sizeof (mntopts) / sizeof (mntopt_t),
1870Sstevel@tonic-gate 	mntopts
1880Sstevel@tonic-gate };
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*ARGSUSED*/
1910Sstevel@tonic-gate static void
1920Sstevel@tonic-gate autofs_zone_destructor(zoneid_t zoneid, void *arg)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	struct autofs_globals *fngp = arg;
1950Sstevel@tonic-gate 	vnode_t *vp;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (fngp == NULL)
1980Sstevel@tonic-gate 		return;
1990Sstevel@tonic-gate 	ASSERT(fngp->fng_fnnode_count == 1);
2000Sstevel@tonic-gate 	ASSERT(fngp->fng_unmount_threads == 0);
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * vn_alloc() initialized the rootnode with a count of 1; we need to
2030Sstevel@tonic-gate 	 * make this 0 to placate auto_freefnnode().
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	vp = fntovn(fngp->fng_rootfnnodep);
2060Sstevel@tonic-gate 	ASSERT(vp->v_count == 1);
2070Sstevel@tonic-gate 	vp->v_count--;
2080Sstevel@tonic-gate 	auto_freefnnode(fngp->fng_rootfnnodep);
2090Sstevel@tonic-gate 	mutex_destroy(&fngp->fng_unmount_threads_lock);
2100Sstevel@tonic-gate 	kmem_free(fngp, sizeof (*fngp));
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * rootfnnodep is allocated here.  Its sole purpose is to provide
2150Sstevel@tonic-gate  * read/write locking for top level fnnodes.  This object is
2160Sstevel@tonic-gate  * persistent and will not be deallocated until the zone is destroyed.
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  * The current zone is implied as the zone of interest, since we will be
2190Sstevel@tonic-gate  * calling zthread_create() which must be called from the correct zone.
2200Sstevel@tonic-gate  */
2210Sstevel@tonic-gate static struct autofs_globals *
2220Sstevel@tonic-gate autofs_zone_init(void)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	char rootname[sizeof ("root_fnnode_zone_") + ZONEID_WIDTH];
2250Sstevel@tonic-gate 	struct autofs_globals *fngp;
2260Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	fngp = kmem_zalloc(sizeof (*fngp), KM_SLEEP);
2290Sstevel@tonic-gate 	(void) snprintf(rootname, sizeof (rootname), "root_fnnode_zone_%d",
2300Sstevel@tonic-gate 	    zoneid);
2310Sstevel@tonic-gate 	fngp->fng_rootfnnodep = auto_makefnnode(VNON, NULL, rootname, CRED(),
2320Sstevel@tonic-gate 	    fngp);
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * Don't need to hold fng_rootfnnodep as it's never really used for
2350Sstevel@tonic-gate 	 * anything.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	fngp->fng_fnnode_count = 1;
2380Sstevel@tonic-gate 	fngp->fng_printed_not_running_msg = 0;
2390Sstevel@tonic-gate 	fngp->fng_zoneid = zoneid;
2400Sstevel@tonic-gate 	mutex_init(&fngp->fng_unmount_threads_lock, NULL, MUTEX_DEFAULT,
2410Sstevel@tonic-gate 	    NULL);
2420Sstevel@tonic-gate 	fngp->fng_unmount_threads = 0;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * Start the unmounter thread for this zone.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, auto_do_unmount, fngp, 0, minclsyspri);
2480Sstevel@tonic-gate 	return (fngp);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate int
2520Sstevel@tonic-gate autofs_init(int fstype, char *name)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	static const fs_operation_def_t auto_vfsops_template[] = {
2550Sstevel@tonic-gate 		VFSNAME_MOUNT, auto_mount,
2560Sstevel@tonic-gate 		VFSNAME_UNMOUNT, auto_unmount,
2570Sstevel@tonic-gate 		VFSNAME_ROOT, auto_root,
2580Sstevel@tonic-gate 		VFSNAME_STATVFS, auto_statvfs,
2590Sstevel@tonic-gate 		NULL, NULL
2600Sstevel@tonic-gate 	};
2610Sstevel@tonic-gate 	int error;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	autofs_fstype = fstype;
2640Sstevel@tonic-gate 	ASSERT(autofs_fstype != 0);
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * Associate VFS ops vector with this fstype
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	error = vfs_setfsops(fstype, auto_vfsops_template, NULL);
2690Sstevel@tonic-gate 	if (error != 0) {
2700Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs_init: bad vfs ops template");
2710Sstevel@tonic-gate 		return (error);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	error = vn_make_ops(name, auto_vnodeops_template, &auto_vnodeops);
2750Sstevel@tonic-gate 	if (error != 0) {
2760Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
2770Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs_init: bad vnode ops template");
2780Sstevel@tonic-gate 		return (error);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	mutex_init(&autofs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * Assign unique major number for all autofs mounts
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	if ((autofs_major = getudev()) == (major_t)-1) {
2860Sstevel@tonic-gate 		cmn_err(CE_WARN,
2870Sstevel@tonic-gate 		    "autofs: autofs_init: can't get unique device number");
2880Sstevel@tonic-gate 		mutex_destroy(&autofs_minor_lock);
2890Sstevel@tonic-gate 		return (1);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	 * We'd like to be able to provide a constructor here, but we can't
2940Sstevel@tonic-gate 	 * since it wants to zthread_create(), something it can't do in a ZSD
2950Sstevel@tonic-gate 	 * constructor.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	zone_key_create(&autofs_key, NULL, NULL, autofs_zone_destructor);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (0);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate static char *restropts[] = {
3030Sstevel@tonic-gate 	RESTRICTED_MNTOPTS
3040Sstevel@tonic-gate };
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * This routine adds those options to the option string `buf' which are
3080Sstevel@tonic-gate  * forced by secpolicy_fs_mount.  If the automatic "security" options
3090Sstevel@tonic-gate  * are set, the option string gets them added if they aren't already
3100Sstevel@tonic-gate  * there.  We search the string with "strstr" and make sure that
3110Sstevel@tonic-gate  * the string we find is bracketed with <start|",">MNTOPT<","|"\0">
3120Sstevel@tonic-gate  *
3130Sstevel@tonic-gate  * This is one half of the option inheritence algorithm which
3140Sstevel@tonic-gate  * implements the "restrict" option.  The other half is implemented
3150Sstevel@tonic-gate  * in automountd; it takes its cue from the options we add here.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static int
3180Sstevel@tonic-gate autofs_restrict_opts(struct vfs *vfsp, char *buf, size_t maxlen, size_t *curlen)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	int i;
3210Sstevel@tonic-gate 	char *p;
3220Sstevel@tonic-gate 	size_t len = *curlen - 1;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/* Unrestricted */
3250Sstevel@tonic-gate 	if (!vfs_optionisset(vfsp, restropts[0], NULL))
3260Sstevel@tonic-gate 		return (0);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	for (i = 0; i < sizeof (restropts)/sizeof (restropts[0]); i++) {
3290Sstevel@tonic-gate 		size_t olen = strlen(restropts[i]);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		/* Add "restrict" always and the others insofar set */
3320Sstevel@tonic-gate 		if ((i == 0 || vfs_optionisset(vfsp, restropts[i], NULL)) &&
3330Sstevel@tonic-gate 		    ((p = strstr(buf, restropts[i])) == NULL ||
3340Sstevel@tonic-gate 		    !((p == buf || p[-1] == ',') &&
3350Sstevel@tonic-gate 		    (p[olen] == '\0' || p[olen] == ',')))) {
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 			if (len + olen + 1 > maxlen)
3380Sstevel@tonic-gate 				return (-1);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 			if (*buf != '\0')
3410Sstevel@tonic-gate 				buf[len++] = ',';
3420Sstevel@tonic-gate 			(void) strcpy(&buf[len], restropts[i]);
3430Sstevel@tonic-gate 			len += olen;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	*curlen = len + 1;
3470Sstevel@tonic-gate 	return (0);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /* ARGSUSED */
3510Sstevel@tonic-gate static int
3520Sstevel@tonic-gate auto_mount(vfs_t *vfsp, vnode_t *vp, struct mounta *uap, cred_t *cr)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	int error;
3550Sstevel@tonic-gate 	size_t len = 0;
3560Sstevel@tonic-gate 	struct autofs_args args;
3570Sstevel@tonic-gate 	fninfo_t *fnip = NULL;
3580Sstevel@tonic-gate 	vnode_t *rootvp = NULL;
3590Sstevel@tonic-gate 	fnnode_t *rootfnp = NULL;
3600Sstevel@tonic-gate 	char *data = uap->dataptr;
3610Sstevel@tonic-gate 	char datalen = uap->datalen;
3620Sstevel@tonic-gate 	dev_t autofs_dev;
3630Sstevel@tonic-gate 	char strbuff[MAXPATHLEN+1];
3640Sstevel@tonic-gate 	vnode_t *kvp;
3650Sstevel@tonic-gate 	struct autofs_globals *fngp;
3660Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mount: vfs %p vp %p\n", (void *)vfsp,
3690Sstevel@tonic-gate 	    (void *)vp));
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
3720Sstevel@tonic-gate 		return (EPERM);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (zone == global_zone) {
3750Sstevel@tonic-gate 		zone_t *mntzone;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
3780Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
3790Sstevel@tonic-gate 		zone_rele(mntzone);
3800Sstevel@tonic-gate 		if (mntzone != zone) {
3810Sstevel@tonic-gate 			return (EBUSY);
3820Sstevel@tonic-gate 		}
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * Stop the mount from going any further if the zone is going away.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN)
3890Sstevel@tonic-gate 		return (EBUSY);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * We need a lock to serialize this; minor_lock is as good as any.
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 	mutex_enter(&autofs_minor_lock);
3950Sstevel@tonic-gate 	if ((fngp = zone_getspecific(autofs_key, zone)) == NULL) {
3960Sstevel@tonic-gate 		fngp = autofs_zone_init();
3970Sstevel@tonic-gate 		(void) zone_setspecific(autofs_key, zone, fngp);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	mutex_exit(&autofs_minor_lock);
4000Sstevel@tonic-gate 	ASSERT(fngp != NULL);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * Get arguments
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE) {
4060Sstevel@tonic-gate 		if (datalen != sizeof (args))
4070Sstevel@tonic-gate 			return (EINVAL);
4080Sstevel@tonic-gate 		error = kcopy(data, &args, sizeof (args));
4090Sstevel@tonic-gate 	} else {
4100Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
4110Sstevel@tonic-gate 			if (datalen != sizeof (args))
4120Sstevel@tonic-gate 				return (EINVAL);
4130Sstevel@tonic-gate 			error = copyin(data, &args, sizeof (args));
4140Sstevel@tonic-gate 		} else {
4150Sstevel@tonic-gate 			struct autofs_args32 args32;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 			if (datalen != sizeof (args32))
4180Sstevel@tonic-gate 				return (EINVAL);
4190Sstevel@tonic-gate 			error = copyin(data, &args32, sizeof (args32));
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 			args.addr.maxlen = args32.addr.maxlen;
4220Sstevel@tonic-gate 			args.addr.len = args32.addr.len;
4230Sstevel@tonic-gate 			args.addr.buf = (char *)(uintptr_t)args32.addr.buf;
4240Sstevel@tonic-gate 			args.path = (char *)(uintptr_t)args32.path;
4250Sstevel@tonic-gate 			args.opts = (char *)(uintptr_t)args32.opts;
4260Sstevel@tonic-gate 			args.map = (char *)(uintptr_t)args32.map;
4270Sstevel@tonic-gate 			args.subdir = (char *)(uintptr_t)args32.subdir;
4280Sstevel@tonic-gate 			args.key = (char *)(uintptr_t)args32.key;
4290Sstevel@tonic-gate 			args.mount_to = args32.mount_to;
4300Sstevel@tonic-gate 			args.rpc_to = args32.rpc_to;
4310Sstevel@tonic-gate 			args.direct = args32.direct;
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	if (error)
4350Sstevel@tonic-gate 		return (EFAULT);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
4380Sstevel@tonic-gate 	 * For a remount, only update mount information
4390Sstevel@tonic-gate 	 * i.e. default mount options, map name, etc.
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
4420Sstevel@tonic-gate 		fnip = vfstofni(vfsp);
4430Sstevel@tonic-gate 		if (fnip == NULL)
4440Sstevel@tonic-gate 			return (EINVAL);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		if (args.direct == 1)
4470Sstevel@tonic-gate 			fnip->fi_flags |= MF_DIRECT;
4480Sstevel@tonic-gate 		else
4490Sstevel@tonic-gate 			fnip->fi_flags &= ~MF_DIRECT;
4500Sstevel@tonic-gate 		fnip->fi_mount_to = args.mount_to;
4510Sstevel@tonic-gate 		fnip->fi_rpc_to = args.rpc_to;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 		/*
4540Sstevel@tonic-gate 		 * Get default options
4550Sstevel@tonic-gate 		 */
4560Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
4570Sstevel@tonic-gate 			error = copystr(args.opts, strbuff, sizeof (strbuff),
4580Sstevel@tonic-gate 			    &len);
4590Sstevel@tonic-gate 		else
4600Sstevel@tonic-gate 			error = copyinstr(args.opts, strbuff, sizeof (strbuff),
4610Sstevel@tonic-gate 			    &len);
4620Sstevel@tonic-gate 		if (error)
4630Sstevel@tonic-gate 			return (EFAULT);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		if (autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len)
4660Sstevel@tonic-gate 		    != 0) {
4670Sstevel@tonic-gate 			return (EFAULT);
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		kmem_free(fnip->fi_opts, fnip->fi_optslen);
4710Sstevel@tonic-gate 		fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
4720Sstevel@tonic-gate 		fnip->fi_optslen = (int)len;
4730Sstevel@tonic-gate 		bcopy(strbuff, fnip->fi_opts, len);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * Get context/map name
4770Sstevel@tonic-gate 		 */
4780Sstevel@tonic-gate 		if (uap->flags & MS_SYSSPACE)
4790Sstevel@tonic-gate 			error = copystr(args.map, strbuff, sizeof (strbuff),
4800Sstevel@tonic-gate 			    &len);
4810Sstevel@tonic-gate 		else
4820Sstevel@tonic-gate 			error = copyinstr(args.map, strbuff, sizeof (strbuff),
4830Sstevel@tonic-gate 			    &len);
4840Sstevel@tonic-gate 		if (error)
4850Sstevel@tonic-gate 			return (EFAULT);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		kmem_free(fnip->fi_map, fnip->fi_maplen);
4880Sstevel@tonic-gate 		fnip->fi_map = kmem_alloc(len, KM_SLEEP);
4890Sstevel@tonic-gate 		fnip->fi_maplen = (int)len;
4900Sstevel@tonic-gate 		bcopy(strbuff, fnip->fi_map, len);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		return (0);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * Allocate fninfo struct and attach it to vfs
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 	fnip = kmem_zalloc(sizeof (*fnip), KM_SLEEP);
4990Sstevel@tonic-gate 	fnip->fi_mountvfs = vfsp;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	fnip->fi_mount_to = args.mount_to;
5020Sstevel@tonic-gate 	fnip->fi_rpc_to = args.rpc_to;
5030Sstevel@tonic-gate 	fnip->fi_refcnt = 0;
5040Sstevel@tonic-gate 	vfsp->vfs_bsize = AUTOFS_BLOCKSIZE;
5050Sstevel@tonic-gate 	vfsp->vfs_fstype = autofs_fstype;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Assign a unique device id to the mount
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	mutex_enter(&autofs_minor_lock);
5110Sstevel@tonic-gate 	do {
5120Sstevel@tonic-gate 		autofs_minor = (autofs_minor + 1) & L_MAXMIN32;
5130Sstevel@tonic-gate 		autofs_dev = makedevice(autofs_major, autofs_minor);
5140Sstevel@tonic-gate 	} while (vfs_devismounted(autofs_dev));
5150Sstevel@tonic-gate 	mutex_exit(&autofs_minor_lock);
5160Sstevel@tonic-gate 	vfsp->vfs_dev = autofs_dev;
5170Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, autofs_dev, autofs_fstype);
5180Sstevel@tonic-gate 	vfsp->vfs_data = (void *)fnip;
5190Sstevel@tonic-gate 	vfsp->vfs_bcount = 0;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * Get daemon address
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	fnip->fi_addr.len = args.addr.len;
5250Sstevel@tonic-gate 	fnip->fi_addr.maxlen = fnip->fi_addr.len;
5260Sstevel@tonic-gate 	fnip->fi_addr.buf = kmem_alloc(args.addr.len, KM_SLEEP);
5270Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5280Sstevel@tonic-gate 		error = kcopy(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
5290Sstevel@tonic-gate 	else
5300Sstevel@tonic-gate 		error = copyin(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
5310Sstevel@tonic-gate 	if (error) {
5320Sstevel@tonic-gate 		error = EFAULT;
5330Sstevel@tonic-gate 		goto errout;
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	fnip->fi_zoneid = getzoneid();
5370Sstevel@tonic-gate 	/*
5380Sstevel@tonic-gate 	 * Get path for mountpoint
5390Sstevel@tonic-gate 	 */
5400Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5410Sstevel@tonic-gate 		error = copystr(args.path, strbuff, sizeof (strbuff), &len);
5420Sstevel@tonic-gate 	else
5430Sstevel@tonic-gate 		error = copyinstr(args.path, strbuff, sizeof (strbuff), &len);
5440Sstevel@tonic-gate 	if (error) {
5450Sstevel@tonic-gate 		error = EFAULT;
5460Sstevel@tonic-gate 		goto errout;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	fnip->fi_path = kmem_alloc(len, KM_SLEEP);
5490Sstevel@tonic-gate 	fnip->fi_pathlen = (int)len;
5500Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_path, len);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * Get default options
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5560Sstevel@tonic-gate 		error = copystr(args.opts, strbuff, sizeof (strbuff), &len);
5570Sstevel@tonic-gate 	else
5580Sstevel@tonic-gate 		error = copyinstr(args.opts, strbuff, sizeof (strbuff), &len);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (error != 0 ||
5610Sstevel@tonic-gate 	    autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len) != 0) {
5620Sstevel@tonic-gate 		error = EFAULT;
5630Sstevel@tonic-gate 		goto errout;
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
5660Sstevel@tonic-gate 	fnip->fi_optslen = (int)len;
5670Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_opts, len);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Get context/map name
5710Sstevel@tonic-gate 	 */
5720Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5730Sstevel@tonic-gate 		error = copystr(args.map, strbuff, sizeof (strbuff), &len);
5740Sstevel@tonic-gate 	else
5750Sstevel@tonic-gate 		error = copyinstr(args.map, strbuff, sizeof (strbuff), &len);
5760Sstevel@tonic-gate 	if (error) {
5770Sstevel@tonic-gate 		error = EFAULT;
5780Sstevel@tonic-gate 		goto errout;
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	fnip->fi_map = kmem_alloc(len, KM_SLEEP);
5810Sstevel@tonic-gate 	fnip->fi_maplen = (int)len;
5820Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_map, len);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/*
5850Sstevel@tonic-gate 	 * Get subdirectory within map
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
5880Sstevel@tonic-gate 		error = copystr(args.subdir, strbuff, sizeof (strbuff), &len);
5890Sstevel@tonic-gate 	else
5900Sstevel@tonic-gate 		error = copyinstr(args.subdir, strbuff, sizeof (strbuff), &len);
5910Sstevel@tonic-gate 	if (error) {
5920Sstevel@tonic-gate 		error = EFAULT;
5930Sstevel@tonic-gate 		goto errout;
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 	fnip->fi_subdir = kmem_alloc(len, KM_SLEEP);
5960Sstevel@tonic-gate 	fnip->fi_subdirlen = (int)len;
5970Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_subdir, len);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * Get the key
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 	if (uap->flags & MS_SYSSPACE)
6030Sstevel@tonic-gate 		error = copystr(args.key, strbuff, sizeof (strbuff), &len);
6040Sstevel@tonic-gate 	else
6050Sstevel@tonic-gate 		error = copyinstr(args.key, strbuff, sizeof (strbuff), &len);
6060Sstevel@tonic-gate 	if (error) {
6070Sstevel@tonic-gate 		error = EFAULT;
6080Sstevel@tonic-gate 		goto errout;
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	fnip->fi_key = kmem_alloc(len, KM_SLEEP);
6110Sstevel@tonic-gate 	fnip->fi_keylen = (int)len;
6120Sstevel@tonic-gate 	bcopy(strbuff, fnip->fi_key, len);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * Is this a direct mount?
6160Sstevel@tonic-gate 	 */
6170Sstevel@tonic-gate 	if (args.direct == 1)
6180Sstevel@tonic-gate 		fnip->fi_flags |= MF_DIRECT;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Setup netconfig.
6220Sstevel@tonic-gate 	 * Can I pass in knconf as mount argument? what
6230Sstevel@tonic-gate 	 * happens when the daemon gets restarted?
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 	if ((error = lookupname("/dev/ticotsord", UIO_SYSSPACE, FOLLOW,
6260Sstevel@tonic-gate 	    NULLVPP, &kvp)) != 0) {
6270Sstevel@tonic-gate 		cmn_err(CE_WARN, "autofs: lookupname: %d", error);
6280Sstevel@tonic-gate 		goto errout;
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	fnip->fi_knconf.knc_rdev = kvp->v_rdev;
6320Sstevel@tonic-gate 	fnip->fi_knconf.knc_protofmly = NC_LOOPBACK;
6330Sstevel@tonic-gate 	fnip->fi_knconf.knc_semantics = NC_TPI_COTS_ORD;
6340Sstevel@tonic-gate 	VN_RELE(kvp);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * Make the root vnode
6380Sstevel@tonic-gate 	 */
6390Sstevel@tonic-gate 	rootfnp = auto_makefnnode(VDIR, vfsp, fnip->fi_path, cr, fngp);
6400Sstevel@tonic-gate 	if (rootfnp == NULL) {
6410Sstevel@tonic-gate 		error = ENOMEM;
6420Sstevel@tonic-gate 		goto errout;
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 	rootvp = fntovn(rootfnp);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	rootvp->v_flag |= VROOT;
6470Sstevel@tonic-gate 	rootfnp->fn_mode = AUTOFS_MODE;
6480Sstevel@tonic-gate 	rootfnp->fn_parent = rootfnp;
6490Sstevel@tonic-gate 	/* account for ".." entry */
6500Sstevel@tonic-gate 	rootfnp->fn_linkcnt = rootfnp->fn_size = 1;
6510Sstevel@tonic-gate 	fnip->fi_rootvp = rootvp;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * Add to list of top level AUTOFS' if it is being mounted by
6550Sstevel@tonic-gate 	 * a user level process.
6560Sstevel@tonic-gate 	 */
6570Sstevel@tonic-gate 	if (!(uap->flags & MS_SYSSPACE)) {
6580Sstevel@tonic-gate 		rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_WRITER);
6590Sstevel@tonic-gate 		rootfnp->fn_parent = fngp->fng_rootfnnodep;
6600Sstevel@tonic-gate 		rootfnp->fn_next = fngp->fng_rootfnnodep->fn_dirents;
6610Sstevel@tonic-gate 		fngp->fng_rootfnnodep->fn_dirents = rootfnp;
6620Sstevel@tonic-gate 		rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
6660Sstevel@tonic-gate 	    (void *)vfsp, (void *)rootvp, (void *)fnip, error));
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	return (0);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate errout:
6710Sstevel@tonic-gate 	ASSERT(fnip != NULL);
6720Sstevel@tonic-gate 	ASSERT((uap->flags & MS_REMOUNT) == 0);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (fnip->fi_addr.buf != NULL)
6750Sstevel@tonic-gate 		kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
6760Sstevel@tonic-gate 	if (fnip->fi_path != NULL)
6770Sstevel@tonic-gate 		kmem_free(fnip->fi_path, fnip->fi_pathlen);
6780Sstevel@tonic-gate 	if (fnip->fi_opts != NULL)
6790Sstevel@tonic-gate 		kmem_free(fnip->fi_opts, fnip->fi_optslen);
6800Sstevel@tonic-gate 	if (fnip->fi_map != NULL)
6810Sstevel@tonic-gate 		kmem_free(fnip->fi_map, fnip->fi_maplen);
6820Sstevel@tonic-gate 	if (fnip->fi_subdir != NULL)
6830Sstevel@tonic-gate 		kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
6840Sstevel@tonic-gate 	if (fnip->fi_key != NULL)
6850Sstevel@tonic-gate 		kmem_free(fnip->fi_key, fnip->fi_keylen);
6860Sstevel@tonic-gate 	kmem_free(fnip, sizeof (*fnip));
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
6890Sstevel@tonic-gate 	    (void *)vfsp, (void *)rootvp, (void *)fnip, error));
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	return (error);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /* ARGSUSED */
6950Sstevel@tonic-gate static int
6960Sstevel@tonic-gate auto_unmount(vfs_t *vfsp, int flag, cred_t *cr)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate 	fninfo_t *fnip;
6990Sstevel@tonic-gate 	vnode_t *rvp;
7000Sstevel@tonic-gate 	fnnode_t *rfnp, *fnp, *pfnp;
7010Sstevel@tonic-gate 	fnnode_t *myrootfnnodep;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	fnip = vfstofni(vfsp);
7040Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_unmount vfsp %p fnip %p\n", (void *)vfsp,
7050Sstevel@tonic-gate 			(void *)fnip));
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
7080Sstevel@tonic-gate 		return (EPERM);
7090Sstevel@tonic-gate 	/*
7100Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
7110Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	if (flag & MS_FORCE)
7140Sstevel@tonic-gate 		return (ENOTSUP);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(vfsp->vfs_vnodecovered));
7170Sstevel@tonic-gate 	rvp = fnip->fi_rootvp;
7180Sstevel@tonic-gate 	rfnp = vntofn(rvp);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	if (rvp->v_count > 1 || rfnp->fn_dirents != NULL)
7210Sstevel@tonic-gate 		return (EBUSY);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/*
7240Sstevel@tonic-gate 	 * The root vnode is on the linked list of root fnnodes only if
7250Sstevel@tonic-gate 	 * this was not a trigger node. Since we have no way of knowing,
7260Sstevel@tonic-gate 	 * if we don't find it, then we assume it was a trigger node.
7270Sstevel@tonic-gate 	 */
7280Sstevel@tonic-gate 	myrootfnnodep = rfnp->fn_globals->fng_rootfnnodep;
7290Sstevel@tonic-gate 	pfnp = NULL;
7300Sstevel@tonic-gate 	rw_enter(&myrootfnnodep->fn_rwlock, RW_WRITER);
7310Sstevel@tonic-gate 	fnp = myrootfnnodep->fn_dirents;
7320Sstevel@tonic-gate 	while (fnp != NULL) {
7330Sstevel@tonic-gate 		if (fnp == rfnp) {
7340Sstevel@tonic-gate 			/*
7350Sstevel@tonic-gate 			 * A check here is made to see if rvp is busy.  If
7360Sstevel@tonic-gate 			 * so, return EBUSY.  Otherwise proceed with
7370Sstevel@tonic-gate 			 * disconnecting it from the list.
7380Sstevel@tonic-gate 			 */
7390Sstevel@tonic-gate 			if (rvp->v_count > 1 || rfnp->fn_dirents != NULL) {
7400Sstevel@tonic-gate 				rw_exit(&myrootfnnodep->fn_rwlock);
7410Sstevel@tonic-gate 				return (EBUSY);
7420Sstevel@tonic-gate 			}
7430Sstevel@tonic-gate 			if (pfnp)
7440Sstevel@tonic-gate 				pfnp->fn_next = fnp->fn_next;
7450Sstevel@tonic-gate 			else
7460Sstevel@tonic-gate 				myrootfnnodep->fn_dirents = fnp->fn_next;
7470Sstevel@tonic-gate 			fnp->fn_next = NULL;
7480Sstevel@tonic-gate 			break;
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 		pfnp = fnp;
7510Sstevel@tonic-gate 		fnp = fnp->fn_next;
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 	rw_exit(&myrootfnnodep->fn_rwlock);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	ASSERT(rvp->v_count == 1);
7560Sstevel@tonic-gate 	ASSERT(rfnp->fn_size == 1);
7570Sstevel@tonic-gate 	ASSERT(rfnp->fn_linkcnt == 1);
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * The following drops linkcnt to 0, therefore the disconnect is
7600Sstevel@tonic-gate 	 * not attempted when auto_inactive() is called by
7610Sstevel@tonic-gate 	 * vn_rele(). This is necessary because we have nothing to get
7620Sstevel@tonic-gate 	 * disconnected from since we're the root of the filesystem. As a
7630Sstevel@tonic-gate 	 * side effect the node is not freed, therefore I should free the
7640Sstevel@tonic-gate 	 * node here.
7650Sstevel@tonic-gate 	 *
7660Sstevel@tonic-gate 	 * XXX - I really need to think of a better way of doing this.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	rfnp->fn_size--;
7690Sstevel@tonic-gate 	rfnp->fn_linkcnt--;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * release of last reference causes node
7730Sstevel@tonic-gate 	 * to be freed
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	VN_RELE(rvp);
7760Sstevel@tonic-gate 	rfnp->fn_parent = NULL;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	auto_freefnnode(rfnp);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
7810Sstevel@tonic-gate 	kmem_free(fnip->fi_path, fnip->fi_pathlen);
7820Sstevel@tonic-gate 	kmem_free(fnip->fi_map, fnip->fi_maplen);
7830Sstevel@tonic-gate 	kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
7840Sstevel@tonic-gate 	kmem_free(fnip->fi_key, fnip->fi_keylen);
7850Sstevel@tonic-gate 	kmem_free(fnip->fi_opts, fnip->fi_optslen);
7860Sstevel@tonic-gate 	kmem_free(fnip, sizeof (*fnip));
7870Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_unmount: return=0\n"));
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	return (0);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate /*
7940Sstevel@tonic-gate  * find root of autofs
7950Sstevel@tonic-gate  */
7960Sstevel@tonic-gate static int
7970Sstevel@tonic-gate auto_root(vfs_t *vfsp, vnode_t **vpp)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	*vpp = (vnode_t *)vfstofni(vfsp)->fi_rootvp;
8000Sstevel@tonic-gate 	VN_HOLD(*vpp);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_root: vfs %p, *vpp %p\n", (void *)vfsp,
8030Sstevel@tonic-gate 	    (void *)*vpp));
8040Sstevel@tonic-gate 	return (0);
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate /*
8080Sstevel@tonic-gate  * Get file system statistics.
8090Sstevel@tonic-gate  */
8100Sstevel@tonic-gate static int
8110Sstevel@tonic-gate auto_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	dev32_t d32;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_statvfs %p\n", (void *)vfsp));
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	bzero(sbp, sizeof (*sbp));
8180Sstevel@tonic-gate 	sbp->f_bsize	= vfsp->vfs_bsize;
8190Sstevel@tonic-gate 	sbp->f_frsize	= sbp->f_bsize;
8200Sstevel@tonic-gate 	sbp->f_blocks	= (fsblkcnt64_t)0;
8210Sstevel@tonic-gate 	sbp->f_bfree	= (fsblkcnt64_t)0;
8220Sstevel@tonic-gate 	sbp->f_bavail	= (fsblkcnt64_t)0;
8230Sstevel@tonic-gate 	sbp->f_files	= (fsfilcnt64_t)0;
8240Sstevel@tonic-gate 	sbp->f_ffree	= (fsfilcnt64_t)0;
8250Sstevel@tonic-gate 	sbp->f_favail	= (fsfilcnt64_t)0;
8260Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
8270Sstevel@tonic-gate 	sbp->f_fsid	= d32;
8280Sstevel@tonic-gate 	(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
8290Sstevel@tonic-gate 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
8300Sstevel@tonic-gate 	sbp->f_namemax = MAXNAMELEN;
8310Sstevel@tonic-gate 	(void) strcpy(sbp->f_fstr, MNTTYPE_AUTOFS);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	return (0);
8340Sstevel@tonic-gate }
835