xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_subr.c (revision 3901:279598fd6d51)
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
52170Sevanl  * Common Development and Distribution License (the "License").
62170Sevanl  * 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 /*
223391Ssemery  * Copyright 2007 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/kmem.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/proc.h>
320Sstevel@tonic-gate #include <sys/disp.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
340Sstevel@tonic-gate #include <sys/vnode.h>
350Sstevel@tonic-gate #include <sys/pathname.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/mount.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/systm.h>
410Sstevel@tonic-gate #include <sys/dirent.h>
420Sstevel@tonic-gate #include <fs/fs_subr.h>
430Sstevel@tonic-gate #include <sys/fs/autofs.h>
440Sstevel@tonic-gate #include <sys/callb.h>
450Sstevel@tonic-gate #include <sys/sysmacros.h>
460Sstevel@tonic-gate #include <sys/zone.h>
472170Sevanl #include <sys/door.h>
480Sstevel@tonic-gate #include <sys/fs/mntdata.h>
492170Sevanl #include <nfs/mount.h>
502170Sevanl #include <rpc/clnt.h>
512170Sevanl #include <rpcsvc/autofs_prot.h>
522170Sevanl #include <nfs/rnode.h>
532170Sevanl #include <sys/utsname.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Autofs and Zones:
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * Zones are delegated the responsibility of managing their own autofs mounts
590Sstevel@tonic-gate  * and maps.  Each zone runs its own copy of automountd, with its own timeouts,
600Sstevel@tonic-gate  * and other logically "global" parameters.  kRPC and virtualization in the
610Sstevel@tonic-gate  * loopback transport (tl) will prevent a zone from communicating with another
620Sstevel@tonic-gate  * zone's automountd.
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * Each zone has its own "rootfnnode" and associated tree of auto nodes.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * Each zone also has its own set of "unmounter" kernel threads; these are
670Sstevel@tonic-gate  * created and run within the zone's context (ie, they are created via
680Sstevel@tonic-gate  * zthread_create()).
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * Cross-zone mount triggers are disallowed.  There is a check in
710Sstevel@tonic-gate  * auto_trigger_mount() to this effect; EPERM is returned to indicate that the
720Sstevel@tonic-gate  * mount is not owned by the caller.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * autofssys() enables a caller in the global zone to clean up in-kernel (as
750Sstevel@tonic-gate  * well as regular) autofs mounts via the unmount_tree() mechanism.  This is
760Sstevel@tonic-gate  * routinely done when all mounts are removed as part of zone shutdown.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate #define	TYPICALMAXPATHLEN	64
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static kmutex_t autofs_nodeid_lock;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int auto_perform_link(fnnode_t *, struct linka *, cred_t *);
830Sstevel@tonic-gate static int auto_perform_actions(fninfo_t *, fnnode_t *,
840Sstevel@tonic-gate     action_list *, cred_t *);
850Sstevel@tonic-gate static int auto_getmntpnt(vnode_t *, char *, vnode_t **, cred_t *);
860Sstevel@tonic-gate static int auto_lookup_request(fninfo_t *, char *, struct linka *,
873391Ssemery     bool_t, bool_t *, cred_t *);
883391Ssemery static int auto_mount_request(fninfo_t *, char *, action_list **, cred_t *,
892170Sevanl     bool_t);
902170Sevanl 
912170Sevanl extern struct autofs_globals *autofs_zone_init(void);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Clears the MF_INPROG flag, and wakes up those threads sleeping on
950Sstevel@tonic-gate  * fn_cv_mount if MF_WAITING is set.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate void
980Sstevel@tonic-gate auto_unblock_others(
990Sstevel@tonic-gate 	fnnode_t *fnp,
1000Sstevel@tonic-gate 	uint_t operation)		/* either MF_INPROG or MF_LOOKUP */
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	ASSERT(operation & (MF_INPROG | MF_LOOKUP));
1030Sstevel@tonic-gate 	fnp->fn_flags &= ~operation;
1040Sstevel@tonic-gate 	if (fnp->fn_flags & MF_WAITING) {
1050Sstevel@tonic-gate 		fnp->fn_flags &= ~MF_WAITING;
1060Sstevel@tonic-gate 		cv_broadcast(&fnp->fn_cv_mount);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate int
1110Sstevel@tonic-gate auto_wait4mount(fnnode_t *fnp)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	int error;
1140Sstevel@tonic-gate 	k_sigset_t smask;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_wait4mount: fnp=%p\n", (void *)fnp));
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
1190Sstevel@tonic-gate 	while (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
1200Sstevel@tonic-gate 		/*
1210Sstevel@tonic-gate 		 * There is a mount or a lookup in progress.
1220Sstevel@tonic-gate 		 */
1230Sstevel@tonic-gate 		fnp->fn_flags |= MF_WAITING;
1240Sstevel@tonic-gate 		sigintr(&smask, 1);
1250Sstevel@tonic-gate 		if (!cv_wait_sig(&fnp->fn_cv_mount, &fnp->fn_lock)) {
1260Sstevel@tonic-gate 			/*
1270Sstevel@tonic-gate 			 * Decided not to wait for operation to
1280Sstevel@tonic-gate 			 * finish after all.
1290Sstevel@tonic-gate 			 */
1300Sstevel@tonic-gate 			sigunintr(&smask);
1310Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1320Sstevel@tonic-gate 			return (EINTR);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 		sigunintr(&smask);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 	error = fnp->fn_error;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (error == EINTR) {
1390Sstevel@tonic-gate 		/*
1400Sstevel@tonic-gate 		 * The thread doing the mount got interrupted, we need to
1410Sstevel@tonic-gate 		 * try again, by returning EAGAIN.
1420Sstevel@tonic-gate 		 */
1430Sstevel@tonic-gate 		error = EAGAIN;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_wait4mount: fnp=%p error=%d\n", (void *)fnp,
1480Sstevel@tonic-gate 	    error));
1490Sstevel@tonic-gate 	return (error);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate int
1530Sstevel@tonic-gate auto_lookup_aux(fnnode_t *fnp, char *name, cred_t *cred)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	struct fninfo *fnip;
1560Sstevel@tonic-gate 	struct linka link;
1570Sstevel@tonic-gate 	bool_t mountreq = FALSE;
1580Sstevel@tonic-gate 	int error = 0;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	fnip = vfstofni(fntovn(fnp)->v_vfsp);
1610Sstevel@tonic-gate 	bzero(&link, sizeof (link));
1623391Ssemery 	error = auto_lookup_request(fnip, name, &link, TRUE, &mountreq, cred);
1630Sstevel@tonic-gate 	if (!error) {
1642170Sevanl 		if (link.link != NULL || link.link != '\0') {
1650Sstevel@tonic-gate 			/*
1660Sstevel@tonic-gate 			 * This node should be a symlink
1670Sstevel@tonic-gate 			 */
1680Sstevel@tonic-gate 			error = auto_perform_link(fnp, &link, cred);
1690Sstevel@tonic-gate 		} else if (mountreq) {
1700Sstevel@tonic-gate 			/*
1710Sstevel@tonic-gate 			 * The automount daemon is requesting a mount,
1720Sstevel@tonic-gate 			 * implying this entry must be a wildcard match and
1730Sstevel@tonic-gate 			 * therefore in need of verification that the entry
1740Sstevel@tonic-gate 			 * exists on the server.
1750Sstevel@tonic-gate 			 */
1760Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
1770Sstevel@tonic-gate 			AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
1780Sstevel@tonic-gate 			fnp->fn_error = 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			/*
1810Sstevel@tonic-gate 			 * Unblock other lookup requests on this node,
1820Sstevel@tonic-gate 			 * this is needed to let the lookup generated by
1830Sstevel@tonic-gate 			 * the mount call to complete. The caveat is
1840Sstevel@tonic-gate 			 * other lookups on this node can also get by,
1850Sstevel@tonic-gate 			 * i.e., another lookup on this node that occurs
1860Sstevel@tonic-gate 			 * while this lookup is attempting the mount
1870Sstevel@tonic-gate 			 * would return a positive result no matter what.
1880Sstevel@tonic-gate 			 * Therefore two lookups on the this node could
1890Sstevel@tonic-gate 			 * potentially get disparate results.
1900Sstevel@tonic-gate 			 */
1910Sstevel@tonic-gate 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
1920Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1930Sstevel@tonic-gate 			/*
1940Sstevel@tonic-gate 			 * auto_new_mount_thread fires up a new thread which
1950Sstevel@tonic-gate 			 * calls automountd finishing up the work
1960Sstevel@tonic-gate 			 */
1970Sstevel@tonic-gate 			auto_new_mount_thread(fnp, name, cred);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 			/*
2000Sstevel@tonic-gate 			 * At this point, we are simply another thread
2010Sstevel@tonic-gate 			 * waiting for the mount to complete
2020Sstevel@tonic-gate 			 */
2030Sstevel@tonic-gate 			error = auto_wait4mount(fnp);
2040Sstevel@tonic-gate 			if (error == AUTOFS_SHUTDOWN)
2050Sstevel@tonic-gate 				error = ENOENT;
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2092170Sevanl 	if (link.link)
2102170Sevanl 		kmem_free(link.link, strlen(link.link) + 1);
2112170Sevanl 	if (link.dir)
2122170Sevanl 		kmem_free(link.dir, strlen(link.dir) + 1);
2130Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
2140Sstevel@tonic-gate 	fnp->fn_error = error;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * Notify threads waiting for lookup/mount that
2180Sstevel@tonic-gate 	 * it's done.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (mountreq) {
2210Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2220Sstevel@tonic-gate 	} else {
2230Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
2260Sstevel@tonic-gate 	return (error);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Starting point for thread to handle mount requests with automountd.
2310Sstevel@tonic-gate  * XXX auto_mount_thread() is not suspend-safe within the scope of
2320Sstevel@tonic-gate  * the present model defined for cpr to suspend the system. Calls
2330Sstevel@tonic-gate  * made by the auto_mount_thread() that have been identified to be unsafe
2340Sstevel@tonic-gate  * are (1) RPC client handle setup and client calls to automountd which
2350Sstevel@tonic-gate  * can block deep down in the RPC library, (2) kmem_alloc() calls with the
2360Sstevel@tonic-gate  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*(), and
2370Sstevel@tonic-gate  * lookuppnvp() calls which can result in over the wire calls to servers.
2380Sstevel@tonic-gate  * The thread should be completely reevaluated to make it suspend-safe in
2390Sstevel@tonic-gate  * case of future updates to the cpr model.
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate static void
2420Sstevel@tonic-gate auto_mount_thread(struct autofs_callargs *argsp)
2430Sstevel@tonic-gate {
2442170Sevanl 	struct fninfo 		*fnip;
2452170Sevanl 	fnnode_t 		*fnp;
2462170Sevanl 	vnode_t 		*vp;
2472170Sevanl 	char 			*name;
2482170Sevanl 	size_t 			namelen;
2492170Sevanl 	cred_t 			*cred;
2502170Sevanl 	action_list		*alp = NULL;
2512170Sevanl 	int 			error;
2522170Sevanl 	callb_cpr_t 		cprinfo;
2532170Sevanl 	kmutex_t 		auto_mount_thread_cpr_lock;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	mutex_init(&auto_mount_thread_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
2562170Sevanl 	CALLB_CPR_INIT(&cprinfo, &auto_mount_thread_cpr_lock,
2572170Sevanl 		callb_generic_cpr, "auto_mount_thread");
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	fnp = argsp->fnc_fnp;
2600Sstevel@tonic-gate 	vp = fntovn(fnp);
2610Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
2620Sstevel@tonic-gate 	name = argsp->fnc_name;
2630Sstevel@tonic-gate 	cred = argsp->fnc_cred;
2640Sstevel@tonic-gate 	ASSERT(crgetzoneid(argsp->fnc_cred) == fnip->fi_zoneid);
2650Sstevel@tonic-gate 
2663391Ssemery 	error = auto_mount_request(fnip, name, &alp, cred, TRUE);
2670Sstevel@tonic-gate 	if (!error)
2680Sstevel@tonic-gate 		error = auto_perform_actions(fnip, fnp, alp, cred);
2690Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
2700Sstevel@tonic-gate 	fnp->fn_error = error;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Notify threads waiting for mount that
2740Sstevel@tonic-gate 	 * it's done.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2770Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	VN_RELE(vp);
2800Sstevel@tonic-gate 	crfree(argsp->fnc_cred);
2810Sstevel@tonic-gate 	namelen = strlen(argsp->fnc_name) + 1;
2820Sstevel@tonic-gate 	kmem_free(argsp->fnc_name, namelen);
2830Sstevel@tonic-gate 	kmem_free(argsp, sizeof (*argsp));
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	mutex_enter(&auto_mount_thread_cpr_lock);
2860Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
2870Sstevel@tonic-gate 	mutex_destroy(&auto_mount_thread_cpr_lock);
2880Sstevel@tonic-gate 	zthread_exit();
2890Sstevel@tonic-gate 	/* NOTREACHED */
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static int autofs_thr_success = 0;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Creates new thread which calls auto_mount_thread which does
2960Sstevel@tonic-gate  * the bulk of the work calling automountd, via 'auto_perform_actions'.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate void
2990Sstevel@tonic-gate auto_new_mount_thread(fnnode_t *fnp, char *name, cred_t *cred)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct autofs_callargs *argsp;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	argsp = kmem_alloc(sizeof (*argsp), KM_SLEEP);
3040Sstevel@tonic-gate 	VN_HOLD(fntovn(fnp));
3050Sstevel@tonic-gate 	argsp->fnc_fnp = fnp;
3060Sstevel@tonic-gate 	argsp->fnc_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
3070Sstevel@tonic-gate 	(void) strcpy(argsp->fnc_name, name);
3080Sstevel@tonic-gate 	argsp->fnc_origin = curthread;
3090Sstevel@tonic-gate 	crhold(cred);
3100Sstevel@tonic-gate 	argsp->fnc_cred = cred;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, auto_mount_thread, argsp, 0,
3130Sstevel@tonic-gate 	    minclsyspri);
3140Sstevel@tonic-gate 	autofs_thr_success++;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3172170Sevanl 
3180Sstevel@tonic-gate int
3190Sstevel@tonic-gate auto_calldaemon(
3202170Sevanl 	zoneid_t 		zoneid,
3212170Sevanl 	int			which,
3222170Sevanl 	xdrproc_t		xarg_func,
3232170Sevanl 	void 			*argsp,
3242170Sevanl 	xdrproc_t		xresp_func,
3252170Sevanl 	void 			*resp,
3262170Sevanl 	int			reslen,
3272170Sevanl 	bool_t 			hard)	/* retry forever? */
3280Sstevel@tonic-gate {
3292170Sevanl 
3302170Sevanl 	int 			retry, error = 0;
3312170Sevanl 	k_sigset_t 		smask;
3322170Sevanl 	door_arg_t		door_args;
3332170Sevanl 	door_handle_t		dh;
3342170Sevanl 	XDR			xdrarg, xdrres;
3352170Sevanl 	struct autofs_globals 	*fngp = NULL;
3362170Sevanl 	void			*orig_resp = NULL;
3372170Sevanl 	int			orig_reslen = reslen;
3382170Sevanl 	autofs_door_args_t	*xdr_argsp;
3392170Sevanl 	int			xdr_len = 0;
3402170Sevanl 
3412170Sevanl 	/*
3422170Sevanl 	 * We know that the current thread is doing work on
3432170Sevanl 	 * behalf of its own zone, so it's ok to use
3442170Sevanl 	 * curproc->p_zone.
3452170Sevanl 	 */
3462170Sevanl 	ASSERT(zoneid == getzoneid());
3472170Sevanl 	if (zone_status_get(curproc->p_zone) >=
3482170Sevanl 			ZONE_IS_SHUTTING_DOWN) {
3492170Sevanl 		/*
3502170Sevanl 		 * There's no point in trying to talk to
3512170Sevanl 		 * automountd.  Plus, zone_shutdown() is
3522170Sevanl 		 * waiting for us.
3532170Sevanl 		 */
3542170Sevanl 		return (ECONNREFUSED);
3552170Sevanl 	}
3560Sstevel@tonic-gate 
3572223Sevanl 	if ((fngp = zone_getspecific(autofs_key, curproc->p_zone)) ==
3582170Sevanl 	    NULL) {
3592170Sevanl 		fngp = autofs_zone_init();
3602223Sevanl 		(void) zone_setspecific(autofs_key, curproc->p_zone, fngp);
3612170Sevanl 	}
3622170Sevanl 
3632170Sevanl 	ASSERT(fngp != NULL);
3642170Sevanl 
3652170Sevanl 	if (argsp != NULL && (xdr_len = xdr_sizeof(xarg_func, argsp)) == 0)
3662170Sevanl 		return (EINVAL);
3672170Sevanl 	xdr_argsp = kmem_zalloc(xdr_len + sizeof (*xdr_argsp), KM_SLEEP);
3682170Sevanl 	xdr_argsp->xdr_len = xdr_len;
3692170Sevanl 	xdr_argsp->cmd = which;
3702170Sevanl 
3712170Sevanl 	if (argsp) {
3722170Sevanl 		xdrmem_create(&xdrarg, (char *)&xdr_argsp->xdr_arg,
3732170Sevanl 			xdr_argsp->xdr_len, XDR_ENCODE);
3742170Sevanl 
3752170Sevanl 		if (!(*xarg_func)(&xdrarg, argsp)) {
3762170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
3772170Sevanl 			return (EINVAL);
3782170Sevanl 		}
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3822170Sevanl 	 * We're saving off the original pointer and length due to the
3832170Sevanl 	 * possibility that the results buffer returned by the door
3842170Sevanl 	 * upcall can be different then what we passed in. This is because
3852170Sevanl 	 * the door will allocate new memory if the results buffer passed
3862170Sevanl 	 * in isn't large enough to hold what we need to send back.
3872170Sevanl 	 * In this case we need to free the memory originally allocated
3882170Sevanl 	 * for that buffer.
3890Sstevel@tonic-gate 	 */
3902170Sevanl 	if (orig_reslen)
3912170Sevanl 		orig_resp = kmem_zalloc(orig_reslen, KM_SLEEP);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	do {
3942170Sevanl 		retry = 0;
3952170Sevanl 		mutex_enter(&fngp->fng_autofs_daemon_lock);
3962170Sevanl 		dh = fngp->fng_autofs_daemon_dh;
3972170Sevanl 		if (dh)
3982170Sevanl 			door_ki_hold(dh);
3992170Sevanl 		mutex_exit(&fngp->fng_autofs_daemon_lock);
4000Sstevel@tonic-gate 
4012170Sevanl 		if (dh == NULL) {
4022170Sevanl 			if (orig_resp)
4032170Sevanl 				kmem_free(orig_resp, orig_reslen);
4042170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
4052170Sevanl 			return (ENOENT);
4062170Sevanl 		}
4072170Sevanl 		door_args.data_ptr = (char *)xdr_argsp;
4082170Sevanl 		door_args.data_size = sizeof (*xdr_argsp) + xdr_argsp->xdr_len;
4092170Sevanl 		door_args.desc_ptr = NULL;
4102170Sevanl 		door_args.desc_num = 0;
4112170Sevanl 		door_args.rbuf = orig_resp ? (char *)orig_resp : NULL;
4122170Sevanl 		door_args.rsize = reslen;
4130Sstevel@tonic-gate 
4142170Sevanl 		sigintr(&smask, 1);
4152170Sevanl 		error = door_ki_upcall(dh, &door_args);
4160Sstevel@tonic-gate 		sigunintr(&smask);
4170Sstevel@tonic-gate 
4182170Sevanl 		door_ki_rele(dh);
4190Sstevel@tonic-gate 
4202170Sevanl 		if (!error) {
4212170Sevanl 			autofs_door_res_t *adr =
4222170Sevanl 				(autofs_door_res_t *)door_args.rbuf;
4232170Sevanl 			if (door_args.rbuf != NULL &&
4242170Sevanl 				(error = adr->res_status)) {
4252170Sevanl 				kmem_free(xdr_argsp,
4262170Sevanl 					xdr_len + sizeof (*xdr_argsp));
4272170Sevanl 				if (orig_resp)
4282170Sevanl 					kmem_free(orig_resp, orig_reslen);
4292170Sevanl 				return (error);
4302170Sevanl 			}
4312170Sevanl 			continue;
4322170Sevanl 		}
4332170Sevanl 		switch (error) {
4342170Sevanl 		case EINTR:
4350Sstevel@tonic-gate 			/*
4362170Sevanl 			 * interrupts should be handled properly by the
4372170Sevanl 			 * door upcall.
4382170Sevanl 			 *
4392170Sevanl 			 * We may have gotten EINTR for other reasons
4402170Sevanl 			 * like the door being revoked on us. Instead
4412170Sevanl 			 * of trying to extract this out of the door
4422170Sevanl 			 * handle, sleep and try again, if still
4432170Sevanl 			 * revoked we will get EBADF next time
4442170Sevanl 			 * through.
4450Sstevel@tonic-gate 			 */
4462170Sevanl 		case EAGAIN:    /* process may be forking */
4472170Sevanl 			/*
4482170Sevanl 			 * Back off for a bit
4492170Sevanl 			 */
4502170Sevanl 			delay(hz);
4512170Sevanl 			retry = 1;
4522170Sevanl 			break;
4532170Sevanl 		case EBADF:	/* Invalid door */
4542170Sevanl 		case EINVAL:    /* Not a door, wrong target */
4550Sstevel@tonic-gate 			/*
4562170Sevanl 			 * A fatal door error, if our failing door
4572170Sevanl 			 * handle is the current door handle, clean
4582170Sevanl 			 * up our state.
4590Sstevel@tonic-gate 			 */
4602170Sevanl 			mutex_enter(&fngp->fng_autofs_daemon_lock);
4612170Sevanl 			if (dh == fngp->fng_autofs_daemon_dh) {
4622170Sevanl 				door_ki_rele(fngp->fng_autofs_daemon_dh);
4632170Sevanl 				fngp->fng_autofs_daemon_dh = NULL;
4640Sstevel@tonic-gate 			}
4652170Sevanl 			mutex_exit(&fngp->fng_autofs_daemon_lock);
4662170Sevanl 			AUTOFS_DPRINT((5,
4672170Sevanl 				"auto_calldaemon error=%d\n", error));
4682170Sevanl 			if (hard) {
4692170Sevanl 				if (!fngp->fng_printed_not_running_msg) {
4702170Sevanl 				    fngp->fng_printed_not_running_msg = 1;
4712170Sevanl 				    zprintf(zoneid, "automountd not "\
4722170Sevanl 					"running, retrying\n");
4732170Sevanl 				}
4742170Sevanl 				delay(hz);
4752170Sevanl 				retry = 1;
4762170Sevanl 				break;
4772170Sevanl 			} else {
4782170Sevanl 				error = ECONNREFUSED;
4792170Sevanl 				kmem_free(xdr_argsp,
4802170Sevanl 					xdr_len + sizeof (*xdr_argsp));
4812170Sevanl 				if (orig_resp)
4822170Sevanl 					kmem_free(orig_resp, orig_reslen);
4832170Sevanl 				return (error);
4842170Sevanl 			}
4852170Sevanl 		default:	/* Unknown must be fatal */
4860Sstevel@tonic-gate 			error = ENOENT;
4872170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
4882170Sevanl 			if (orig_resp)
4892170Sevanl 				kmem_free(orig_resp, orig_reslen);
4902170Sevanl 			return (error);
4910Sstevel@tonic-gate 		}
4922170Sevanl 	} while (retry);
4930Sstevel@tonic-gate 
4942170Sevanl 	if (fngp->fng_printed_not_running_msg == 1) {
4952170Sevanl 		fngp->fng_printed_not_running_msg = 0;
4962170Sevanl 		zprintf(zoneid, "automountd OK\n");
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4992170Sevanl 	if (orig_resp && orig_reslen) {
5002170Sevanl 		autofs_door_res_t	*door_resp;
5012170Sevanl 		door_resp =
5022170Sevanl 			(autofs_door_res_t *)door_args.rbuf;
5032170Sevanl 		if ((void *)door_args.rbuf != orig_resp)
5042170Sevanl 			kmem_free(orig_resp, orig_reslen);
5052170Sevanl 		xdrmem_create(&xdrres, (char *)&door_resp->xdr_res,
5062170Sevanl 			door_resp->xdr_len, XDR_DECODE);
5072170Sevanl 		if (!((*xresp_func)(&xdrres, resp)))
5082170Sevanl 			error = EINVAL;
5092170Sevanl 		kmem_free(door_args.rbuf, door_args.rsize);
5102170Sevanl 	}
5112170Sevanl 	kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
5120Sstevel@tonic-gate 	return (error);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate static int
5162170Sevanl auto_null_request(fninfo_t *fnip, bool_t hard)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	int error;
5192170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tauto_null_request\n"));
5220Sstevel@tonic-gate 
5232170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
5242170Sevanl 		NULLPROC,
5252170Sevanl 		xdr_void,
5262170Sevanl 		NULL,
5272170Sevanl 		xdr_void,
5282170Sevanl 		NULL,
5292170Sevanl 		0,
5302170Sevanl 		hard);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tauto_null_request: error=%d\n", error));
5330Sstevel@tonic-gate 	return (error);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate static int
5370Sstevel@tonic-gate auto_lookup_request(
5380Sstevel@tonic-gate 	fninfo_t *fnip,
5390Sstevel@tonic-gate 	char *key,
5400Sstevel@tonic-gate 	struct linka *lnp,
5410Sstevel@tonic-gate 	bool_t hard,
5423391Ssemery 	bool_t *mountreq,
5433391Ssemery 	cred_t *cred)
5440Sstevel@tonic-gate {
5452170Sevanl 	int 				error;
5462170Sevanl 	struct autofs_globals 		*fngp;
5472170Sevanl 	struct autofs_lookupargs	reqst;
5482170Sevanl 	autofs_lookupres 		*resp;
5492170Sevanl 	struct linka 			*p;
5500Sstevel@tonic-gate 
5512170Sevanl 
5522170Sevanl 	AUTOFS_DPRINT((4, "auto_lookup_equest: path=%s name=%s\n",
5530Sstevel@tonic-gate 	    fnip->fi_path, key));
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
5562170Sevanl 
5572170Sevanl 	reqst.map = fnip->fi_map;
5582170Sevanl 	reqst.path = fnip->fi_path;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (fnip->fi_flags & MF_DIRECT)
5612170Sevanl 		reqst.name = fnip->fi_key;
5620Sstevel@tonic-gate 	else
5632170Sevanl 		reqst.name = key;
5642170Sevanl 	AUTOFS_DPRINT((4, "auto_lookup_request: using key=%s\n", reqst.name));
5650Sstevel@tonic-gate 
5662170Sevanl 	reqst.subdir = fnip->fi_subdir;
5672170Sevanl 	reqst.opts = fnip->fi_opts;
5682170Sevanl 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
5693391Ssemery 	reqst.uid = crgetuid(cred);
5702170Sevanl 
5712170Sevanl 	resp = kmem_zalloc(sizeof (*resp), KM_SLEEP);
5720Sstevel@tonic-gate 
5732170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
5742170Sevanl 		AUTOFS_LOOKUP,
5752170Sevanl 		xdr_autofs_lookupargs,
5762170Sevanl 		&reqst,
5772170Sevanl 		xdr_autofs_lookupres,
5782170Sevanl 		(void *)resp,
5792170Sevanl 		sizeof (autofs_lookupres),
5802170Sevanl 		hard);
5812170Sevanl 
5822170Sevanl 
5832170Sevanl 	if (error) {
5842170Sevanl 		xdr_free(xdr_autofs_lookupres, (char *)resp);
5852170Sevanl 		kmem_free(resp, sizeof (*resp));
5862170Sevanl 		return (error);
5872170Sevanl 	}
5882170Sevanl 
5890Sstevel@tonic-gate 	if (!error) {
5902170Sevanl 		fngp->fng_verbose = resp->lu_verbose;
5912170Sevanl 		switch (resp->lu_res) {
5920Sstevel@tonic-gate 		case AUTOFS_OK:
5932170Sevanl 			switch (resp->lu_type.action) {
5940Sstevel@tonic-gate 			case AUTOFS_MOUNT_RQ:
5950Sstevel@tonic-gate 				lnp->link = NULL;
5960Sstevel@tonic-gate 				lnp->dir = NULL;
5970Sstevel@tonic-gate 				*mountreq = TRUE;
5980Sstevel@tonic-gate 				break;
5990Sstevel@tonic-gate 			case AUTOFS_LINK_RQ:
6000Sstevel@tonic-gate 				p =
6012170Sevanl 				&resp->lu_type.lookup_result_type_u.lt_linka;
6020Sstevel@tonic-gate 				lnp->dir = kmem_alloc(strlen(p->dir) + 1,
6032170Sevanl 					KM_SLEEP);
6040Sstevel@tonic-gate 				(void) strcpy(lnp->dir, p->dir);
6050Sstevel@tonic-gate 				lnp->link = kmem_alloc(strlen(p->link) + 1,
6062170Sevanl 					KM_SLEEP);
6070Sstevel@tonic-gate 				(void) strcpy(lnp->link, p->link);
6080Sstevel@tonic-gate 				break;
6090Sstevel@tonic-gate 			case AUTOFS_NONE:
6100Sstevel@tonic-gate 				lnp->link = NULL;
6110Sstevel@tonic-gate 				lnp->dir = NULL;
6120Sstevel@tonic-gate 				break;
6130Sstevel@tonic-gate 			default:
6142170Sevanl 				auto_log(fngp->fng_verbose,
6152170Sevanl 					fngp->fng_zoneid, CE_WARN,
6160Sstevel@tonic-gate 				    "auto_lookup_request: bad action type %d",
6172170Sevanl 				    resp->lu_res);
6180Sstevel@tonic-gate 				error = ENOENT;
6190Sstevel@tonic-gate 			}
6200Sstevel@tonic-gate 			break;
6210Sstevel@tonic-gate 		case AUTOFS_NOENT:
6220Sstevel@tonic-gate 			error = ENOENT;
6230Sstevel@tonic-gate 			break;
6240Sstevel@tonic-gate 		default:
6250Sstevel@tonic-gate 			error = ENOENT;
6262170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
6270Sstevel@tonic-gate 			    "auto_lookup_request: unknown result: %d",
6282170Sevanl 			    resp->lu_res);
6290Sstevel@tonic-gate 			break;
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate done:
6332170Sevanl 	xdr_free(xdr_autofs_lookupres, (char *)resp);
6342170Sevanl 	kmem_free(resp, sizeof (*resp));
6350Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_lookup_request: path=%s name=%s error=%d\n",
6360Sstevel@tonic-gate 	    fnip->fi_path, key, error));
6370Sstevel@tonic-gate 	return (error);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate static int
6410Sstevel@tonic-gate auto_mount_request(
6420Sstevel@tonic-gate 	fninfo_t *fnip,
6430Sstevel@tonic-gate 	char *key,
6440Sstevel@tonic-gate 	action_list **alpp,
6453391Ssemery 	cred_t *cred,
6460Sstevel@tonic-gate 	bool_t hard)
6470Sstevel@tonic-gate {
6482170Sevanl 	int 			error;
6492170Sevanl 	struct autofs_globals 	*fngp;
6502170Sevanl 	autofs_lookupargs 	reqst;
6512170Sevanl 	autofs_mountres		*xdrres = NULL;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mount_request: path=%s name=%s\n",
6540Sstevel@tonic-gate 	    fnip->fi_path, key));
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
6572170Sevanl 	reqst.map = fnip->fi_map;
6582170Sevanl 	reqst.path = fnip->fi_path;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if (fnip->fi_flags & MF_DIRECT)
6612170Sevanl 		reqst.name = fnip->fi_key;
6620Sstevel@tonic-gate 	else
6632170Sevanl 		reqst.name = key;
6642170Sevanl 
6652170Sevanl 	AUTOFS_DPRINT((4, "auto_mount_request: using key=%s\n", reqst.name));
6660Sstevel@tonic-gate 
6672170Sevanl 	reqst.subdir = fnip->fi_subdir;
6682170Sevanl 	reqst.opts = fnip->fi_opts;
6692170Sevanl 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
6703391Ssemery 	reqst.uid = crgetuid(cred);
6712170Sevanl 
6722170Sevanl 	xdrres = kmem_zalloc(sizeof (*xdrres), KM_SLEEP);
6730Sstevel@tonic-gate 
6742170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
6752170Sevanl 		AUTOFS_MNTINFO,
6762170Sevanl 		xdr_autofs_lookupargs,
6772170Sevanl 		&reqst,
6782170Sevanl 		xdr_autofs_mountres,
6792170Sevanl 		(void *)xdrres,
6802170Sevanl 		sizeof (autofs_mountres),
6812170Sevanl 		hard);
6822170Sevanl 
6830Sstevel@tonic-gate 	if (!error) {
6842170Sevanl 		fngp->fng_verbose = xdrres->mr_verbose;
6852170Sevanl 		switch (xdrres->mr_type.status) {
6860Sstevel@tonic-gate 		case AUTOFS_ACTION:
6870Sstevel@tonic-gate 			error = 0;
6880Sstevel@tonic-gate 			/*
6890Sstevel@tonic-gate 			 * Save the action list since it is used by
6900Sstevel@tonic-gate 			 * the caller. We NULL the action list pointer
6910Sstevel@tonic-gate 			 * in 'result' so that xdr_free() will not free
6920Sstevel@tonic-gate 			 * the list.
6930Sstevel@tonic-gate 			 */
6942170Sevanl 			*alpp = xdrres->mr_type.mount_result_type_u.list;
6952170Sevanl 			xdrres->mr_type.mount_result_type_u.list = NULL;
6960Sstevel@tonic-gate 			break;
6970Sstevel@tonic-gate 		case AUTOFS_DONE:
6982170Sevanl 			error = xdrres->mr_type.mount_result_type_u.error;
6990Sstevel@tonic-gate 			break;
7000Sstevel@tonic-gate 		default:
7010Sstevel@tonic-gate 			error = ENOENT;
7022170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
7030Sstevel@tonic-gate 			    "auto_mount_request: unknown status %d",
7042170Sevanl 			    xdrres->mr_type.status);
7050Sstevel@tonic-gate 			break;
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7092170Sevanl 	xdr_free(xdr_autofs_mountres, (char *)xdrres);
7102170Sevanl 	kmem_free(xdrres, sizeof (*xdrres));
7112170Sevanl 
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount_request: path=%s name=%s error=%d\n",
7140Sstevel@tonic-gate 	    fnip->fi_path, key, error));
7150Sstevel@tonic-gate 	return (error);
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate static int
7200Sstevel@tonic-gate auto_send_unmount_request(
7210Sstevel@tonic-gate 	fninfo_t *fnip,
7220Sstevel@tonic-gate 	umntrequest *ul,
7230Sstevel@tonic-gate 	bool_t hard)
7240Sstevel@tonic-gate {
7252170Sevanl 	int 	error;
7262170Sevanl 	umntres	xdrres;
7272170Sevanl 
7282170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tauto_send_unmount_request: fstype=%s "
7310Sstevel@tonic-gate 			" mntpnt=%s\n", ul->fstype, ul->mntpnt));
7320Sstevel@tonic-gate 
7332170Sevanl 	bzero(&xdrres, sizeof (umntres));
7342170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
7352170Sevanl 		AUTOFS_UNMOUNT,
7362170Sevanl 		xdr_umntrequest,
7372170Sevanl 		(void *)ul,
7382170Sevanl 		xdr_umntres,
7392170Sevanl 		(void *)&xdrres,
7402170Sevanl 		sizeof (umntres),
7412170Sevanl 		hard);
7420Sstevel@tonic-gate 
7432395Sevanl 	if (!error)
7442395Sevanl 		error = xdrres.status;
7452395Sevanl 
7460Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tauto_send_unmount_request: error=%d\n", error));
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	return (error);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate static int
7520Sstevel@tonic-gate auto_perform_link(fnnode_t *fnp, struct linka *linkp, cred_t *cred)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	vnode_t *vp;
7550Sstevel@tonic-gate 	size_t len;
7560Sstevel@tonic-gate 	char *tmp;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_perform_link: fnp=%p dir=%s link=%s\n",
7590Sstevel@tonic-gate 	    (void *)fnp, linkp->dir, linkp->link));
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	len = strlen(linkp->link) + 1;		/* include '\0' */
7620Sstevel@tonic-gate 	tmp = kmem_zalloc(len, KM_SLEEP);
7630Sstevel@tonic-gate 	(void) kcopy(linkp->link, tmp, len);
7640Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
7650Sstevel@tonic-gate 	fnp->fn_symlink = tmp;
7660Sstevel@tonic-gate 	fnp->fn_symlinklen = (uint_t)len;
7670Sstevel@tonic-gate 	fnp->fn_flags |= MF_THISUID_MATCH_RQD;
7680Sstevel@tonic-gate 	crhold(cred);
7690Sstevel@tonic-gate 	fnp->fn_cred = cred;
7700Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	vp = fntovn(fnp);
7730Sstevel@tonic-gate 	vp->v_type = VLNK;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	return (0);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate static boolean_t
7792170Sevanl auto_invalid_autofs(fninfo_t *dfnip, fnnode_t *dfnp, action_list *p)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	struct mounta *m;
7820Sstevel@tonic-gate 	struct autofs_args *argsp;
7830Sstevel@tonic-gate 	vnode_t *dvp;
7840Sstevel@tonic-gate 	char buff[AUTOFS_MAXPATHLEN];
7850Sstevel@tonic-gate 	size_t len;
7860Sstevel@tonic-gate 	struct autofs_globals *fngp;
7870Sstevel@tonic-gate 	fngp = dfnp->fn_globals;
7880Sstevel@tonic-gate 	dvp = fntovn(dfnp);
7892170Sevanl 
7900Sstevel@tonic-gate 	m = &p->action.action_list_entry_u.mounta;
7910Sstevel@tonic-gate 	/*
7920Sstevel@tonic-gate 	 * Make sure we aren't geting passed NULL values or a "dir" that
7930Sstevel@tonic-gate 	 * isn't "." and doesn't begin with "./".
7940Sstevel@tonic-gate 	 *
7950Sstevel@tonic-gate 	 * We also only want to perform autofs mounts, so make sure
7960Sstevel@tonic-gate 	 * no-one is trying to trick us into doing anything else.
7970Sstevel@tonic-gate 	 */
798*3901Skr143551 	if (m->dir[0] != '.' ||
7990Sstevel@tonic-gate 	    (m->dir[1] != '/' && m->dir[1] != '\0') ||
800*3901Skr143551 	    strcmp(m->fstype, "autofs") != 0 ||
801*3901Skr143551 	    m->datalen != sizeof (struct autofs_args) ||
8020Sstevel@tonic-gate 	    m->optptr == NULL)
8030Sstevel@tonic-gate 		return (B_TRUE);
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * We also don't like ".."s in the pathname.  Symlinks are
8060Sstevel@tonic-gate 	 * handled by the fact that we'll use NOFOLLOW when we do
8070Sstevel@tonic-gate 	 * lookup()s.
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	if (strstr(m->dir, "/../") != NULL ||
8100Sstevel@tonic-gate 	    (len = strlen(m->dir)) > sizeof ("/..") - 1 &&
8110Sstevel@tonic-gate 	    m->dir[len] == '.' && m->dir[len - 1] == '.' &&
8120Sstevel@tonic-gate 	    m->dir[len - 2] == '/')
8130Sstevel@tonic-gate 		return (B_TRUE);
8140Sstevel@tonic-gate 	argsp = (struct autofs_args *)m->dataptr;
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * We don't want NULL values here either.
8170Sstevel@tonic-gate 	 */
8180Sstevel@tonic-gate 	if (argsp->addr.buf == NULL || argsp->path == NULL ||
8190Sstevel@tonic-gate 	    argsp->opts == NULL || argsp->map == NULL || argsp->subdir == NULL)
8200Sstevel@tonic-gate 		return (B_TRUE);
8210Sstevel@tonic-gate 	/*
8220Sstevel@tonic-gate 	 * We know what the claimed pathname *should* look like:
8230Sstevel@tonic-gate 	 *
8240Sstevel@tonic-gate 	 * If the parent (dfnp) is a mount point (VROOT), then
8250Sstevel@tonic-gate 	 * the path should be (dfnip->fi_path + m->dir).
8260Sstevel@tonic-gate 	 *
8270Sstevel@tonic-gate 	 * Else, we know we're only two levels deep, so we use
8280Sstevel@tonic-gate 	 * (dfnip->fi_path + dfnp->fn_name + m->dir).
8290Sstevel@tonic-gate 	 *
8300Sstevel@tonic-gate 	 * Furthermore, "." only makes sense if dfnp is a
8310Sstevel@tonic-gate 	 * trigger node.
8320Sstevel@tonic-gate 	 *
8330Sstevel@tonic-gate 	 * At this point it seems like the passed-in path is
8340Sstevel@tonic-gate 	 * redundant.
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	if (dvp->v_flag & VROOT) {
8370Sstevel@tonic-gate 		if (m->dir[1] == '\0' && !(dfnp->fn_flags & MF_TRIGGER))
8380Sstevel@tonic-gate 			return (B_TRUE);
8390Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s%s",
8400Sstevel@tonic-gate 		    dfnip->fi_path, m->dir + 1);
8410Sstevel@tonic-gate 	} else {
8420Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s/%s%s",
8430Sstevel@tonic-gate 		    dfnip->fi_path, dfnp->fn_name, m->dir + 1);
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 	if (strcmp(argsp->path, buff) != 0) {
8462170Sevanl 		auto_log(fngp->fng_verbose, fngp->fng_zoneid,
8472170Sevanl 		    CE_WARN, "autofs: expected path of '%s', "
8480Sstevel@tonic-gate 		    "got '%s' instead.", buff, argsp->path);
8490Sstevel@tonic-gate 		return (B_TRUE);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 	return (B_FALSE); /* looks OK */
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8542170Sevanl /*
8552170Sevanl  * auto_invalid_action will validate the action_list received.  If all is good
856*3901Skr143551  * this function returns FALSE, if there is a problem it returned TRUE.
857*3901Skr143551  * Based on the mount type, autofs or nfs, it dispatchs to auto_invalid_autofs
858*3901Skr143551  * or validates the fstype is nfs.  For nfs mounts, the spec, dir, datalen
859*3901Skr143551  * etc should all either be good here, or have already been checked.
8602170Sevanl  */
861*3901Skr143551 
8622170Sevanl static boolean_t
8632170Sevanl auto_invalid_action(fninfo_t *dfnip, fnnode_t *dfnp, action_list *alistpp)
8642170Sevanl {
865*3901Skr143551 	struct mounta	*m;
8662170Sevanl 
8672170Sevanl 	/*
8682170Sevanl 	 * Before we go any further, this better be a mount request.
8692170Sevanl 	 */
8702170Sevanl 	if (alistpp->action.action != AUTOFS_MOUNT_RQ)
8712170Sevanl 		return (B_TRUE);
872*3901Skr143551 
873*3901Skr143551 	m = &alistpp->action.action_list_entry_u.mounta;
874*3901Skr143551 	/*
875*3901Skr143551 	 * Make sure we aren't geting passed NULL values or a "dir" that
876*3901Skr143551 	 * isn't "." and doesn't begin with "./".
877*3901Skr143551 	 *
878*3901Skr143551 	 * We also only want to perform autofs mounts, so make sure
879*3901Skr143551 	 * no-one is trying to trick us into doing anything else.
880*3901Skr143551 	 */
881*3901Skr143551 	if (m->spec == NULL || m->dir == NULL ||
882*3901Skr143551 	    m->fstype == NULL || m->dataptr == NULL)
883*3901Skr143551 		return (B_TRUE);
8842170Sevanl 
885*3901Skr143551 	/*
886*3901Skr143551 	 * Dispatch to the appropriate validation routine based
887*3901Skr143551 	 * on the filesystem type.
888*3901Skr143551 	 */
889*3901Skr143551 	if (strncmp(alistpp->action.action_list_entry_u.mounta.fstype,
890*3901Skr143551 		"autofs", 6) == 0) {
891*3901Skr143551 		return (auto_invalid_autofs(dfnip, dfnp, alistpp));
892*3901Skr143551 	} else if (strncmp(alistpp->action.action_list_entry_u.mounta.fstype,
893*3901Skr143551 		"nfs", 3) == 0) {
894*3901Skr143551 		return (B_FALSE);
895*3901Skr143551 	}
896*3901Skr143551 	return (B_TRUE);
8972170Sevanl }
8982170Sevanl 
8990Sstevel@tonic-gate static int
9000Sstevel@tonic-gate auto_perform_actions(
9010Sstevel@tonic-gate 	fninfo_t *dfnip,
9020Sstevel@tonic-gate 	fnnode_t *dfnp,
9030Sstevel@tonic-gate 	action_list *alp,
9040Sstevel@tonic-gate 	cred_t *cred)	/* Credentials of the caller */
9050Sstevel@tonic-gate {
9062170Sevanl 
9070Sstevel@tonic-gate 	action_list *p;
9082170Sevanl 	struct mounta		*m, margs;
9092170Sevanl 	struct autofs_args 		*argsp;
9102170Sevanl 	int 			error, success = 0;
9112170Sevanl 	vnode_t 		*mvp, *dvp, *newvp;
9122170Sevanl 	fnnode_t 		*newfnp, *mfnp;
9132170Sevanl 	int 			auto_mount = 0;
9142170Sevanl 	int 			save_triggers = 0;
9152170Sevanl 	int 			update_times = 0;
9162170Sevanl 	char 			*mntpnt;
9172170Sevanl 	char 			buff[AUTOFS_MAXPATHLEN];
9182170Sevanl 	timestruc_t 		now;
9192170Sevanl 	struct autofs_globals 	*fngp;
9202170Sevanl 	cred_t 			*zcred;
9210Sstevel@tonic-gate 
9222170Sevanl 	AUTOFS_DPRINT((4, "auto_perform_actions: alp=%p\n",
9232170Sevanl 		(void *)alp));
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	fngp = dfnp->fn_globals;
9260Sstevel@tonic-gate 	dvp = fntovn(dfnp);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * As automountd running in a zone may be compromised, and this may be
9300Sstevel@tonic-gate 	 * an attack, we can't trust everything passed in by automountd, and we
9310Sstevel@tonic-gate 	 * need to do argument verification.  We'll issue a warning and drop
9320Sstevel@tonic-gate 	 * the request if it doesn't seem right.
9330Sstevel@tonic-gate 	 */
9342170Sevanl 
9350Sstevel@tonic-gate 	for (p = alp; p != NULL; p = p->next) {
9360Sstevel@tonic-gate 		if (auto_invalid_action(dfnip, dfnp, p)) {
9370Sstevel@tonic-gate 			/*
9380Sstevel@tonic-gate 			 * This warning should be sent to the global zone,
9390Sstevel@tonic-gate 			 * since presumably the zone administrator is the same
9400Sstevel@tonic-gate 			 * as the attacker.
9410Sstevel@tonic-gate 			 */
9420Sstevel@tonic-gate 			cmn_err(CE_WARN, "autofs: invalid action list received "
9430Sstevel@tonic-gate 			    "by automountd in zone %s.",
9440Sstevel@tonic-gate 			    curproc->p_zone->zone_name);
9450Sstevel@tonic-gate 			/*
9460Sstevel@tonic-gate 			 * This conversation is over.
9470Sstevel@tonic-gate 			 */
9480Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
949*3901Skr143551 			kmem_free(alp, sizeof (*alp));
9500Sstevel@tonic-gate 			return (EINVAL);
9510Sstevel@tonic-gate 		}
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	zcred = zone_get_kcred(getzoneid());
9550Sstevel@tonic-gate 	ASSERT(zcred != NULL);
9560Sstevel@tonic-gate 
957*3901Skr143551 	/*
958*3901Skr143551 	 * Clear MF_MOUNTPOINT, if the NFS mount is required and
959*3901Skr143551 	 * completes successfully, we will then set MF_MOUNTPOINT.
960*3901Skr143551 	 */
961*3901Skr143551 	mutex_enter(&dfnp->fn_lock);
962*3901Skr143551 	if (dfnp->fn_flags & MF_MOUNTPOINT) {
963*3901Skr143551 		AUTOFS_DPRINT((10, "autofs: clearing mountpoint "
964*3901Skr143551 			"flag on %s.", dfnp->fn_name));
965*3901Skr143551 		ASSERT(dfnp->fn_dirents == NULL);
966*3901Skr143551 		ASSERT(dfnp->fn_trigger == NULL);
967*3901Skr143551 	}
968*3901Skr143551 	dfnp->fn_flags &= ~MF_MOUNTPOINT;
969*3901Skr143551 	mutex_exit(&dfnp->fn_lock);
970*3901Skr143551 
971*3901Skr143551 
9720Sstevel@tonic-gate 	if (vn_mountedvfs(dvp) != NULL) {
9730Sstevel@tonic-gate 		/*
9740Sstevel@tonic-gate 		 * The daemon successfully mounted a filesystem
9750Sstevel@tonic-gate 		 * on the AUTOFS root node.
9760Sstevel@tonic-gate 		 */
9770Sstevel@tonic-gate 		mutex_enter(&dfnp->fn_lock);
9780Sstevel@tonic-gate 		dfnp->fn_flags |= MF_MOUNTPOINT;
9790Sstevel@tonic-gate 		ASSERT(dfnp->fn_dirents == NULL);
9800Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
9810Sstevel@tonic-gate 		success++;
9820Sstevel@tonic-gate 	} else {
9830Sstevel@tonic-gate 		/*
9840Sstevel@tonic-gate 		 * Clear MF_MOUNTPOINT.
9850Sstevel@tonic-gate 		 */
9860Sstevel@tonic-gate 		mutex_enter(&dfnp->fn_lock);
9870Sstevel@tonic-gate 		if (dfnp->fn_flags & MF_MOUNTPOINT) {
9880Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "autofs: clearing mountpoint "
9892170Sevanl 				"flag on %s.", dfnp->fn_name));
9900Sstevel@tonic-gate 			ASSERT(dfnp->fn_dirents == NULL);
9910Sstevel@tonic-gate 			ASSERT(dfnp->fn_trigger == NULL);
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 		dfnp->fn_flags &= ~MF_MOUNTPOINT;
9940Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	for (p = alp; p != NULL; p = p->next) {
9982170Sevanl 
9990Sstevel@tonic-gate 		vfs_t *vfsp;	/* dummy argument */
10000Sstevel@tonic-gate 		vfs_t *mvfsp;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		auto_mount = 0;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 		m = &p->action.action_list_entry_u.mounta;
10050Sstevel@tonic-gate 		argsp = (struct autofs_args *)m->dataptr;
1006*3901Skr143551 		if (strncmp(m->fstype, "nfs", 3) == 0) {
1007*3901Skr143551 			if (vn_mountedvfs(dvp) != NULL) {
1008*3901Skr143551 				/*
1009*3901Skr143551 				 * Its possible that the filesystem is
1010*3901Skr143551 				 * already mounted if nfs because we are
1011*3901Skr143551 				 * being called from unmount_tree when
1012*3901Skr143551 				 * if failed to unmount a node from a
1013*3901Skr143551 				 * trigger point, and is remounting again and
1014*3901Skr143551 				 * this particular filesystem was not
1015*3901Skr143551 				 * unmounted.
1016*3901Skr143551 				 */
1017*3901Skr143551 				mutex_enter(&dfnp->fn_lock);
1018*3901Skr143551 				dfnp->fn_flags |= MF_MOUNTPOINT;
1019*3901Skr143551 				mutex_exit(&dfnp->fn_lock);
1020*3901Skr143551 			} else {
1021*3901Skr143551 				m->flags |= MS_SYSSPACE;
1022*3901Skr143551 				VN_HOLD(dvp);
1023*3901Skr143551 				error = domount(NULL, m, dvp, zcred, &vfsp);
1024*3901Skr143551 				if (!error) {
1025*3901Skr143551 					VFS_RELE(vfsp);
1026*3901Skr143551 					mutex_enter(&dfnp->fn_lock);
1027*3901Skr143551 					dfnp->fn_flags |= MF_MOUNTPOINT;
1028*3901Skr143551 					ASSERT(dfnp->fn_dirents == NULL);
1029*3901Skr143551 					mutex_exit(&dfnp->fn_lock);
1030*3901Skr143551 					success++;
1031*3901Skr143551 				}
1032*3901Skr143551 				VN_RELE(dvp);
1033*3901Skr143551 			}
1034*3901Skr143551 		} else {
1035*3901Skr143551 			ASSERT(strcmp(m->fstype, "autofs") == 0);
10360Sstevel@tonic-gate 			/*
1037*3901Skr143551 			 * use the parent directory's timeout since it's the
1038*3901Skr143551 			 * one specified/inherited by automount.
1039*3901Skr143551 			 */
1040*3901Skr143551 			argsp->mount_to = dfnip->fi_mount_to;
1041*3901Skr143551 			/*
1042*3901Skr143551 			 * The mountpoint is relative, and it is guaranteed to
1043*3901Skr143551 			 * begin with "."
1044*3901Skr143551 			 *
10450Sstevel@tonic-gate 			 */
1046*3901Skr143551 			ASSERT(m->dir[0] == '.');
1047*3901Skr143551 			if (m->dir[0] == '.' && m->dir[1] == '\0') {
1048*3901Skr143551 				/*
1049*3901Skr143551 				 * mounting on the trigger node
1050*3901Skr143551 				 */
1051*3901Skr143551 				mvp = dvp;
1052*3901Skr143551 				VN_HOLD(mvp);
1053*3901Skr143551 				goto mount;
1054*3901Skr143551 			}
1055*3901Skr143551 			/*
1056*3901Skr143551 			 * ignore "./" in front of mountpoint
1057*3901Skr143551 			 */
1058*3901Skr143551 			ASSERT(m->dir[1] == '/');
1059*3901Skr143551 			mntpnt = m->dir + 2;
10600Sstevel@tonic-gate 
1061*3901Skr143551 			AUTOFS_DPRINT((10, "\tdfnip->fi_path=%s\n",
1062*3901Skr143551 				dfnip->fi_path));
1063*3901Skr143551 			AUTOFS_DPRINT((10, "\tdfnip->fi_flags=%x\n",
1064*3901Skr143551 				dfnip->fi_flags));
1065*3901Skr143551 			AUTOFS_DPRINT((10, "\tmntpnt=%s\n", mntpnt));
10660Sstevel@tonic-gate 
1067*3901Skr143551 			if (dfnip->fi_flags & MF_DIRECT) {
1068*3901Skr143551 				AUTOFS_DPRINT((10, "\tDIRECT\n"));
1069*3901Skr143551 				(void) sprintf(buff, "%s/%s", dfnip->fi_path,
1070*3901Skr143551 					mntpnt);
1071*3901Skr143551 			} else {
1072*3901Skr143551 				AUTOFS_DPRINT((10, "\tINDIRECT\n"));
1073*3901Skr143551 				(void) sprintf(buff, "%s/%s/%s",
1074*3901Skr143551 					dfnip->fi_path,
1075*3901Skr143551 					dfnp->fn_name, mntpnt);
1076*3901Skr143551 			}
10770Sstevel@tonic-gate 
1078*3901Skr143551 			if (vn_mountedvfs(dvp) == NULL) {
10790Sstevel@tonic-gate 				/*
1080*3901Skr143551 				 * Daemon didn't mount anything on the root
1081*3901Skr143551 				 * We have to create the mountpoint if it
1082*3901Skr143551 				 * doesn't exist already
1083*3901Skr143551 				 *
1084*3901Skr143551 				 * We use the caller's credentials in case a
1085*3901Skr143551 				 * UID-match is required
1086*3901Skr143551 				 * (MF_THISUID_MATCH_RQD).
10870Sstevel@tonic-gate 				 */
1088*3901Skr143551 				rw_enter(&dfnp->fn_rwlock, RW_WRITER);
1089*3901Skr143551 				error = auto_search(dfnp, mntpnt, &mfnp, cred);
1090*3901Skr143551 				if (error == 0) {
1091*3901Skr143551 					/*
1092*3901Skr143551 					 * AUTOFS mountpoint exists
1093*3901Skr143551 					 */
1094*3901Skr143551 					if (vn_mountedvfs(fntovn(mfnp))
1095*3901Skr143551 						!= NULL) {
1096*3901Skr143551 						cmn_err(CE_PANIC,
1097*3901Skr143551 							"auto_perform_actions:"
1098*3901Skr143551 							" mfnp=%p covered",
1099*3901Skr143551 							(void *)mfnp);
1100*3901Skr143551 					}
1101*3901Skr143551 				} else {
1102*3901Skr143551 					/*
1103*3901Skr143551 					 * Create AUTOFS mountpoint
1104*3901Skr143551 					 */
1105*3901Skr143551 					ASSERT((dfnp->fn_flags &
1106*3901Skr143551 						MF_MOUNTPOINT) == 0);
1107*3901Skr143551 					error = auto_enter(dfnp, mntpnt,
1108*3901Skr143551 						&mfnp, cred);
1109*3901Skr143551 					ASSERT(mfnp->fn_linkcnt == 1);
1110*3901Skr143551 					mfnp->fn_linkcnt++;
1111*3901Skr143551 				}
1112*3901Skr143551 				if (!error)
1113*3901Skr143551 					update_times = 1;
1114*3901Skr143551 				rw_exit(&dfnp->fn_rwlock);
1115*3901Skr143551 				ASSERT(error != EEXIST);
1116*3901Skr143551 				if (!error) {
1117*3901Skr143551 					/*
1118*3901Skr143551 					 * mfnp is already held.
1119*3901Skr143551 					 */
1120*3901Skr143551 					mvp = fntovn(mfnp);
1121*3901Skr143551 				} else {
1122*3901Skr143551 					auto_log(fngp->fng_verbose,
1123*3901Skr143551 						fngp->fng_zoneid,
1124*3901Skr143551 						CE_WARN, "autofs: mount of %s "
1125*3901Skr143551 						"failed - can't create"
1126*3901Skr143551 						" mountpoint.", buff);
1127*3901Skr143551 					continue;
11280Sstevel@tonic-gate 				}
11290Sstevel@tonic-gate 			} else {
11300Sstevel@tonic-gate 				/*
1131*3901Skr143551 				 * Find mountpoint in VFS mounted here. If not
1132*3901Skr143551 				 * found, fail the submount, though the overall
1133*3901Skr143551 				 * mount has succeeded since the root is
1134*3901Skr143551 				 * mounted.
11350Sstevel@tonic-gate 				 */
1136*3901Skr143551 				if (error = auto_getmntpnt(dvp, mntpnt, &mvp,
1137*3901Skr143551 					kcred)) {
1138*3901Skr143551 					auto_log(fngp->fng_verbose,
1139*3901Skr143551 						fngp->fng_zoneid,
1140*3901Skr143551 						CE_WARN, "autofs: mount of %s "
1141*3901Skr143551 						"failed - mountpoint doesn't"
1142*3901Skr143551 						" exist.", buff);
11432170Sevanl 					continue;
1144*3901Skr143551 				}
1145*3901Skr143551 				if (mvp->v_type == VLNK) {
1146*3901Skr143551 					auto_log(fngp->fng_verbose,
1147*3901Skr143551 						fngp->fng_zoneid,
1148*3901Skr143551 						CE_WARN, "autofs: %s symbolic "
1149*3901Skr143551 						"link: not a valid mountpoint "
1150*3901Skr143551 						"- mount failed", buff);
1151*3901Skr143551 					VN_RELE(mvp);
1152*3901Skr143551 					error = ENOENT;
1153*3901Skr143551 					continue;
1154*3901Skr143551 				}
11550Sstevel@tonic-gate 			}
11560Sstevel@tonic-gate mount:
1157*3901Skr143551 			m->flags |= MS_SYSSPACE | MS_OPTIONSTR;
11582170Sevanl 
1159*3901Skr143551 			/*
1160*3901Skr143551 			 * Copy mounta struct here so we can substitute a
1161*3901Skr143551 			 * buffer that is large enough to hold the returned
1162*3901Skr143551 			 * option string, if that string is longer than the
1163*3901Skr143551 			 * input option string.
1164*3901Skr143551 			 * This can happen if there are default options enabled
1165*3901Skr143551 			 * that were not in the input option string.
1166*3901Skr143551 			 */
1167*3901Skr143551 			bcopy(m, &margs, sizeof (*m));
1168*3901Skr143551 			margs.optptr = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1169*3901Skr143551 			margs.optlen = MAX_MNTOPT_STR;
1170*3901Skr143551 			(void) strcpy(margs.optptr, m->optptr);
1171*3901Skr143551 			margs.dir = argsp->path;
11720Sstevel@tonic-gate 
1173*3901Skr143551 			/*
1174*3901Skr143551 			 * We use the zone's kcred because we don't want the
1175*3901Skr143551 			 * zone to be able to thus do something it wouldn't
1176*3901Skr143551 			 * normally be able to.
1177*3901Skr143551 			 */
1178*3901Skr143551 			error = domount(NULL, &margs, mvp, zcred, &vfsp);
1179*3901Skr143551 			kmem_free(margs.optptr, MAX_MNTOPT_STR);
1180*3901Skr143551 			if (error != 0) {
1181*3901Skr143551 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1182*3901Skr143551 					CE_WARN,
1183*3901Skr143551 					"autofs: domount of %s failed "
1184*3901Skr143551 					"error=%d", buff, error);
11850Sstevel@tonic-gate 				VN_RELE(mvp);
11860Sstevel@tonic-gate 				continue;
11870Sstevel@tonic-gate 			}
1188*3901Skr143551 			VFS_RELE(vfsp);
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 			/*
1191*3901Skr143551 			 * If mountpoint is an AUTOFS node, then I'm going to
1192*3901Skr143551 			 * flag it that the Filesystem mounted on top was
1193*3901Skr143551 			 * mounted in the kernel so that the unmount can be
1194*3901Skr143551 			 * done inside the kernel as well.
1195*3901Skr143551 			 * I don't care to flag non-AUTOFS mountpoints when an
1196*3901Skr143551 			 * AUTOFS in-kernel mount was done on top, because the
1197*3901Skr143551 			 * unmount routine already knows that such case was
1198*3901Skr143551 			 * done in the kernel.
11990Sstevel@tonic-gate 			 */
1200*3901Skr143551 			if (vfs_matchops(dvp->v_vfsp,
1201*3901Skr143551 				vfs_getops(mvp->v_vfsp))) {
1202*3901Skr143551 				mfnp = vntofn(mvp);
1203*3901Skr143551 				mutex_enter(&mfnp->fn_lock);
1204*3901Skr143551 				mfnp->fn_flags |= MF_IK_MOUNT;
1205*3901Skr143551 				mutex_exit(&mfnp->fn_lock);
1206*3901Skr143551 			}
1207*3901Skr143551 
1208*3901Skr143551 			(void) vn_vfswlock_wait(mvp);
1209*3901Skr143551 			mvfsp = vn_mountedvfs(mvp);
1210*3901Skr143551 			if (mvfsp != NULL) {
1211*3901Skr143551 				vfs_lock_wait(mvfsp);
1212*3901Skr143551 				vn_vfsunlock(mvp);
1213*3901Skr143551 				error = VFS_ROOT(mvfsp, &newvp);
1214*3901Skr143551 				vfs_unlock(mvfsp);
1215*3901Skr143551 				if (error) {
1216*3901Skr143551 					/*
1217*3901Skr143551 					 * We've dropped the locks, so let's
1218*3901Skr143551 					 * get the mounted vfs again in case
1219*3901Skr143551 					 * it changed.
1220*3901Skr143551 					 */
1221*3901Skr143551 					(void) vn_vfswlock_wait(mvp);
1222*3901Skr143551 					mvfsp = vn_mountedvfs(mvp);
1223*3901Skr143551 					if (mvfsp != NULL) {
1224*3901Skr143551 						error = dounmount(mvfsp, 0,
1225*3901Skr143551 							CRED());
1226*3901Skr143551 						if (error) {
1227*3901Skr143551 							cmn_err(CE_WARN,
1228*3901Skr143551 								"autofs: could"
1229*3901Skr143551 								" not unmount"
1230*3901Skr143551 								" vfs=%p",
1231*3901Skr143551 								(void *)mvfsp);
1232*3901Skr143551 						}
1233*3901Skr143551 					} else
1234*3901Skr143551 						vn_vfsunlock(mvp);
1235*3901Skr143551 					VN_RELE(mvp);
1236*3901Skr143551 					continue;
1237*3901Skr143551 				}
1238*3901Skr143551 			} else {
1239*3901Skr143551 				vn_vfsunlock(mvp);
1240*3901Skr143551 				VN_RELE(mvp);
1241*3901Skr143551 				continue;
1242*3901Skr143551 			}
1243*3901Skr143551 
1244*3901Skr143551 			auto_mount = vfs_matchops(dvp->v_vfsp,
1245*3901Skr143551 				vfs_getops(newvp->v_vfsp));
1246*3901Skr143551 			newfnp = vntofn(newvp);
1247*3901Skr143551 			newfnp->fn_parent = dfnp;
1248*3901Skr143551 
12490Sstevel@tonic-gate 			/*
1250*3901Skr143551 			 * At this time we want to save the AUTOFS filesystem
1251*3901Skr143551 			 * as a trigger node. (We only do this if the mount
1252*3901Skr143551 			 * occurred on a node different from the root.
1253*3901Skr143551 			 * We look at the trigger nodes during
1254*3901Skr143551 			 * the automatic unmounting to make sure we remove them
1255*3901Skr143551 			 * as a unit and remount them as a unit if the
1256*3901Skr143551 			 * filesystem mounted at the root could not be
1257*3901Skr143551 			 * unmounted.
12580Sstevel@tonic-gate 			 */
1259*3901Skr143551 			if (auto_mount && (error == 0) && (mvp != dvp)) {
1260*3901Skr143551 				save_triggers++;
1261*3901Skr143551 				/*
1262*3901Skr143551 				 * Add AUTOFS mount to hierarchy
1263*3901Skr143551 				 */
1264*3901Skr143551 				newfnp->fn_flags |= MF_TRIGGER;
1265*3901Skr143551 				rw_enter(&newfnp->fn_rwlock, RW_WRITER);
1266*3901Skr143551 				newfnp->fn_next = dfnp->fn_trigger;
1267*3901Skr143551 				rw_exit(&newfnp->fn_rwlock);
1268*3901Skr143551 				rw_enter(&dfnp->fn_rwlock, RW_WRITER);
1269*3901Skr143551 				dfnp->fn_trigger = newfnp;
1270*3901Skr143551 				rw_exit(&dfnp->fn_rwlock);
1271*3901Skr143551 				/*
1272*3901Skr143551 				 * Don't VN_RELE(newvp) here since dfnp now
1273*3901Skr143551 				 * holds reference to it as its trigger node.
1274*3901Skr143551 				 */
12752170Sevanl 				AUTOFS_DPRINT((10,
1276*3901Skr143551 					"\tadding trigger %s to %s\n",
1277*3901Skr143551 				newfnp->fn_name, dfnp->fn_name));
1278*3901Skr143551 				AUTOFS_DPRINT((10, "\tfirst trigger is %s\n",
1279*3901Skr143551 					dfnp->fn_trigger->fn_name));
1280*3901Skr143551 				if (newfnp->fn_next != NULL)
1281*3901Skr143551 					AUTOFS_DPRINT((10,
1282*3901Skr143551 						"\tnext trigger is %s\n",
1283*3901Skr143551 						newfnp->fn_next->fn_name));
1284*3901Skr143551 				else
1285*3901Skr143551 					AUTOFS_DPRINT((10,
1286*3901Skr143551 						"\tno next trigger\n"));
1287*3901Skr143551 			} else
1288*3901Skr143551 				VN_RELE(newvp);
12890Sstevel@tonic-gate 
1290*3901Skr143551 			if (!error)
1291*3901Skr143551 				success++;
12920Sstevel@tonic-gate 
1293*3901Skr143551 			if (update_times) {
1294*3901Skr143551 				gethrestime(&now);
1295*3901Skr143551 				dfnp->fn_atime = dfnp->fn_mtime = now;
1296*3901Skr143551 			}
1297*3901Skr143551 
1298*3901Skr143551 			VN_RELE(mvp);
12990Sstevel@tonic-gate 		}
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	if (save_triggers) {
13030Sstevel@tonic-gate 		/*
13040Sstevel@tonic-gate 		 * Make sure the parent can't be freed while it has triggers.
13050Sstevel@tonic-gate 		 */
13060Sstevel@tonic-gate 		VN_HOLD(dvp);
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	crfree(zcred);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate done:
13120Sstevel@tonic-gate 	/*
13130Sstevel@tonic-gate 	 * Return failure if daemon didn't mount anything, and all
13140Sstevel@tonic-gate 	 * kernel mounts attempted failed.
13150Sstevel@tonic-gate 	 */
13160Sstevel@tonic-gate 	error = success ? 0 : ENOENT;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	if (alp != NULL) {
13190Sstevel@tonic-gate 		if ((error == 0) && save_triggers) {
13200Sstevel@tonic-gate 			/*
13210Sstevel@tonic-gate 			 * Save action_list information, so that we can use it
13220Sstevel@tonic-gate 			 * when it comes time to remount the trigger nodes
13230Sstevel@tonic-gate 			 * The action list is freed when the directory node
13240Sstevel@tonic-gate 			 * containing the reference to it is unmounted in
13250Sstevel@tonic-gate 			 * unmount_tree().
13260Sstevel@tonic-gate 			 */
13270Sstevel@tonic-gate 			mutex_enter(&dfnp->fn_lock);
13280Sstevel@tonic-gate 			ASSERT(dfnp->fn_alp == NULL);
13290Sstevel@tonic-gate 			dfnp->fn_alp = alp;
13300Sstevel@tonic-gate 			mutex_exit(&dfnp->fn_lock);
13310Sstevel@tonic-gate 		} else {
13320Sstevel@tonic-gate 			/*
13330Sstevel@tonic-gate 			 * free the action list now,
13340Sstevel@tonic-gate 			 */
13350Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
1336*3901Skr143551 			kmem_free(alp, sizeof (*alp));
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_perform_actions: error=%d\n", error));
13400Sstevel@tonic-gate 	return (error);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate fnnode_t *
13440Sstevel@tonic-gate auto_makefnnode(
13450Sstevel@tonic-gate 	vtype_t type,
13460Sstevel@tonic-gate 	vfs_t *vfsp,
13470Sstevel@tonic-gate 	char *name,
13480Sstevel@tonic-gate 	cred_t *cred,
13490Sstevel@tonic-gate 	struct autofs_globals *fngp)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	fnnode_t *fnp;
13520Sstevel@tonic-gate 	vnode_t *vp;
13530Sstevel@tonic-gate 	char *tmpname;
13540Sstevel@tonic-gate 	timestruc_t now;
13550Sstevel@tonic-gate 	/*
13560Sstevel@tonic-gate 	 * autofs uses odd inode numbers
13570Sstevel@tonic-gate 	 * automountd uses even inode numbers
13580Sstevel@tonic-gate 	 *
13590Sstevel@tonic-gate 	 * To preserve the age-old semantics that inum+devid is unique across
13600Sstevel@tonic-gate 	 * the system, this variable must be global across zones.
13610Sstevel@tonic-gate 	 */
13620Sstevel@tonic-gate 	static ino_t nodeid = 3;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	fnp = kmem_zalloc(sizeof (*fnp), KM_SLEEP);
13650Sstevel@tonic-gate 	fnp->fn_vnode = vn_alloc(KM_SLEEP);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	vp = fntovn(fnp);
13680Sstevel@tonic-gate 	tmpname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
13690Sstevel@tonic-gate 	(void) strcpy(tmpname, name);
13700Sstevel@tonic-gate 	fnp->fn_name = &tmpname[0];
13710Sstevel@tonic-gate 	fnp->fn_namelen = (int)strlen(tmpname) + 1;	/* include '\0' */
13720Sstevel@tonic-gate 	fnp->fn_uid = crgetuid(cred);
13730Sstevel@tonic-gate 	fnp->fn_gid = crgetgid(cred);
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * ".." is added in auto_enter and auto_mount.
13760Sstevel@tonic-gate 	 * "." is added in auto_mkdir and auto_mount.
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	/*
13790Sstevel@tonic-gate 	 * Note that fn_size and fn_linkcnt are already 0 since
13800Sstevel@tonic-gate 	 * we used kmem_zalloc to allocated fnp
13810Sstevel@tonic-gate 	 */
13820Sstevel@tonic-gate 	fnp->fn_mode = AUTOFS_MODE;
13830Sstevel@tonic-gate 	gethrestime(&now);
13840Sstevel@tonic-gate 	fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now;
13850Sstevel@tonic-gate 	fnp->fn_ref_time = now.tv_sec;
13860Sstevel@tonic-gate 	mutex_enter(&autofs_nodeid_lock);
13870Sstevel@tonic-gate 	fnp->fn_nodeid = nodeid;
13880Sstevel@tonic-gate 	nodeid += 2;
13890Sstevel@tonic-gate 	fnp->fn_globals = fngp;
13900Sstevel@tonic-gate 	fngp->fng_fnnode_count++;
13910Sstevel@tonic-gate 	mutex_exit(&autofs_nodeid_lock);
13920Sstevel@tonic-gate 	vn_setops(vp, auto_vnodeops);
13930Sstevel@tonic-gate 	vp->v_type = type;
13940Sstevel@tonic-gate 	vp->v_data = (void *)fnp;
13950Sstevel@tonic-gate 	vp->v_vfsp = vfsp;
13960Sstevel@tonic-gate 	mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL);
13970Sstevel@tonic-gate 	rw_init(&fnp->fn_rwlock, NULL, RW_DEFAULT, NULL);
13980Sstevel@tonic-gate 	cv_init(&fnp->fn_cv_mount, NULL, CV_DEFAULT, NULL);
13990Sstevel@tonic-gate 	vn_exists(vp);
14000Sstevel@tonic-gate 	return (fnp);
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate void
14050Sstevel@tonic-gate auto_freefnnode(fnnode_t *fnp)
14060Sstevel@tonic-gate {
14070Sstevel@tonic-gate 	vnode_t *vp = fntovn(fnp);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_freefnnode: fnp=%p\n", (void *)fnp));
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	ASSERT(fnp->fn_linkcnt == 0);
14120Sstevel@tonic-gate 	ASSERT(vp->v_count == 0);
14130Sstevel@tonic-gate 	ASSERT(fnp->fn_dirents == NULL);
14140Sstevel@tonic-gate 	ASSERT(fnp->fn_parent == NULL);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	vn_invalid(vp);
14170Sstevel@tonic-gate 	kmem_free(fnp->fn_name, fnp->fn_namelen);
14180Sstevel@tonic-gate 	if (fnp->fn_symlink) {
14190Sstevel@tonic-gate 		ASSERT(fnp->fn_flags & MF_THISUID_MATCH_RQD);
14200Sstevel@tonic-gate 		kmem_free(fnp->fn_symlink, fnp->fn_symlinklen);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 	if (fnp->fn_cred)
14230Sstevel@tonic-gate 		crfree(fnp->fn_cred);
14240Sstevel@tonic-gate 	mutex_destroy(&fnp->fn_lock);
14250Sstevel@tonic-gate 	rw_destroy(&fnp->fn_rwlock);
14260Sstevel@tonic-gate 	cv_destroy(&fnp->fn_cv_mount);
14270Sstevel@tonic-gate 	vn_free(vp);
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	mutex_enter(&autofs_nodeid_lock);
14300Sstevel@tonic-gate 	fnp->fn_globals->fng_fnnode_count--;
14310Sstevel@tonic-gate 	mutex_exit(&autofs_nodeid_lock);
14320Sstevel@tonic-gate 	kmem_free(fnp, sizeof (*fnp));
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate void
14360Sstevel@tonic-gate auto_disconnect(
14370Sstevel@tonic-gate 	fnnode_t *dfnp,
14380Sstevel@tonic-gate 	fnnode_t *fnp)
14390Sstevel@tonic-gate {
14400Sstevel@tonic-gate 	fnnode_t *tmp, **fnpp;
14410Sstevel@tonic-gate 	vnode_t *vp = fntovn(fnp);
14420Sstevel@tonic-gate 	timestruc_t now;
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	AUTOFS_DPRINT((4,
14450Sstevel@tonic-gate 	    "auto_disconnect: dfnp=%p fnp=%p linkcnt=%d\n v_count=%d",
14460Sstevel@tonic-gate 	    (void *)dfnp, (void *)fnp, fnp->fn_linkcnt, vp->v_count));
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
14490Sstevel@tonic-gate 	ASSERT(fnp->fn_linkcnt == 1);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (vn_mountedvfs(vp) != NULL) {
14520Sstevel@tonic-gate 		cmn_err(CE_PANIC, "auto_disconnect: vp %p mounted on",
14530Sstevel@tonic-gate 		    (void *)vp);
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	/*
14570Sstevel@tonic-gate 	 * Decrement by 1 because we're removing the entry in dfnp.
14580Sstevel@tonic-gate 	 */
14590Sstevel@tonic-gate 	fnp->fn_linkcnt--;
14600Sstevel@tonic-gate 	fnp->fn_size--;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/*
14630Sstevel@tonic-gate 	 * only changed while holding parent's (dfnp) rw_lock
14640Sstevel@tonic-gate 	 */
14650Sstevel@tonic-gate 	fnp->fn_parent = NULL;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	fnpp = &dfnp->fn_dirents;
14680Sstevel@tonic-gate 	for (;;) {
14690Sstevel@tonic-gate 		tmp = *fnpp;
14700Sstevel@tonic-gate 		if (tmp == NULL) {
14710Sstevel@tonic-gate 			cmn_err(CE_PANIC,
14720Sstevel@tonic-gate 			    "auto_disconnect: %p not in %p dirent list",
14730Sstevel@tonic-gate 			    (void *)fnp, (void *)dfnp);
14740Sstevel@tonic-gate 		}
14750Sstevel@tonic-gate 		if (tmp == fnp) {
14760Sstevel@tonic-gate 			*fnpp = tmp->fn_next; 	/* remove it from the list */
14770Sstevel@tonic-gate 			ASSERT(vp->v_count == 0);
14780Sstevel@tonic-gate 			/* child had a pointer to parent ".." */
14790Sstevel@tonic-gate 			dfnp->fn_linkcnt--;
14800Sstevel@tonic-gate 			dfnp->fn_size--;
14810Sstevel@tonic-gate 			break;
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 		fnpp = &tmp->fn_next;
14840Sstevel@tonic-gate 	}
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
14870Sstevel@tonic-gate 	gethrestime(&now);
14880Sstevel@tonic-gate 	fnp->fn_atime = fnp->fn_mtime = now;
14890Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_disconnect: done\n"));
14920Sstevel@tonic-gate }
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate int
14950Sstevel@tonic-gate auto_enter(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
14960Sstevel@tonic-gate {
14970Sstevel@tonic-gate 	struct fnnode *cfnp, **spp;
14980Sstevel@tonic-gate 	vnode_t *dvp = fntovn(dfnp);
14990Sstevel@tonic-gate 	ushort_t offset = 0;
15000Sstevel@tonic-gate 	ushort_t diff;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_enter: dfnp=%p, name=%s ", (void *)dfnp, name));
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	cfnp = dfnp->fn_dirents;
15070Sstevel@tonic-gate 	if (cfnp == NULL) {
15080Sstevel@tonic-gate 		/*
15090Sstevel@tonic-gate 		 * offset = 0 for '.' and offset = 1 for '..'
15100Sstevel@tonic-gate 		 */
15110Sstevel@tonic-gate 		spp = &dfnp->fn_dirents;
15120Sstevel@tonic-gate 		offset = 2;
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	for (; cfnp; cfnp = cfnp->fn_next) {
15160Sstevel@tonic-gate 		if (strcmp(cfnp->fn_name, name) == 0) {
15170Sstevel@tonic-gate 			mutex_enter(&cfnp->fn_lock);
15180Sstevel@tonic-gate 			if (cfnp->fn_flags & MF_THISUID_MATCH_RQD) {
15190Sstevel@tonic-gate 				/*
15200Sstevel@tonic-gate 				 * "thisuser" kind of node, need to
15210Sstevel@tonic-gate 				 * match CREDs as well
15220Sstevel@tonic-gate 				 */
15230Sstevel@tonic-gate 				mutex_exit(&cfnp->fn_lock);
15240Sstevel@tonic-gate 				if (crcmp(cfnp->fn_cred, cred) == 0)
15250Sstevel@tonic-gate 					return (EEXIST);
15260Sstevel@tonic-gate 			} else {
15270Sstevel@tonic-gate 				mutex_exit(&cfnp->fn_lock);
15280Sstevel@tonic-gate 				return (EEXIST);
15290Sstevel@tonic-gate 			}
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		if (cfnp->fn_next != NULL) {
15330Sstevel@tonic-gate 			diff = (ushort_t)
15340Sstevel@tonic-gate 			    (cfnp->fn_next->fn_offset - cfnp->fn_offset);
15350Sstevel@tonic-gate 			ASSERT(diff != 0);
15360Sstevel@tonic-gate 			if (diff > 1 && offset == 0) {
15370Sstevel@tonic-gate 				offset = (ushort_t)cfnp->fn_offset + 1;
15380Sstevel@tonic-gate 				spp = &cfnp->fn_next;
15390Sstevel@tonic-gate 			}
15400Sstevel@tonic-gate 		} else if (offset == 0) {
15410Sstevel@tonic-gate 			offset = (ushort_t)cfnp->fn_offset + 1;
15420Sstevel@tonic-gate 			spp = &cfnp->fn_next;
15430Sstevel@tonic-gate 		}
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	*fnpp = auto_makefnnode(VDIR, dvp->v_vfsp, name, cred,
15470Sstevel@tonic-gate 	    dfnp->fn_globals);
15480Sstevel@tonic-gate 	if (*fnpp == NULL)
15490Sstevel@tonic-gate 		return (ENOMEM);
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	/*
15520Sstevel@tonic-gate 	 * I don't hold the mutex on fnpp because I created it, and
15530Sstevel@tonic-gate 	 * I'm already holding the writers lock for it's parent
15540Sstevel@tonic-gate 	 * directory, therefore nobody can reference it without me first
15550Sstevel@tonic-gate 	 * releasing the writers lock.
15560Sstevel@tonic-gate 	 */
15570Sstevel@tonic-gate 	(*fnpp)->fn_offset = offset;
15580Sstevel@tonic-gate 	(*fnpp)->fn_next = *spp;
15590Sstevel@tonic-gate 	*spp = *fnpp;
15600Sstevel@tonic-gate 	(*fnpp)->fn_parent = dfnp;
15610Sstevel@tonic-gate 	(*fnpp)->fn_linkcnt++;	/* parent now holds reference to entry */
15620Sstevel@tonic-gate 	(*fnpp)->fn_size++;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	/*
15650Sstevel@tonic-gate 	 * dfnp->fn_linkcnt and dfnp->fn_size protected by dfnp->rw_lock
15660Sstevel@tonic-gate 	 */
15670Sstevel@tonic-gate 	dfnp->fn_linkcnt++;	/* child now holds reference to parent '..' */
15680Sstevel@tonic-gate 	dfnp->fn_size++;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	dfnp->fn_ref_time = gethrestime_sec();
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "*fnpp=%p\n", (void *)*fnpp));
15730Sstevel@tonic-gate 	return (0);
15740Sstevel@tonic-gate }
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate int
15770Sstevel@tonic-gate auto_search(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
15780Sstevel@tonic-gate {
15790Sstevel@tonic-gate 	vnode_t *dvp;
15800Sstevel@tonic-gate 	fnnode_t *p;
15810Sstevel@tonic-gate 	int error = ENOENT, match = 0;
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_search: dfnp=%p, name=%s...\n",
15840Sstevel@tonic-gate 	    (void *)dfnp, name));
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	dvp = fntovn(dfnp);
15870Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
15880Sstevel@tonic-gate 		cmn_err(CE_PANIC, "auto_search: dvp=%p not a directory",
15890Sstevel@tonic-gate 		    (void *)dvp);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dfnp->fn_rwlock));
15930Sstevel@tonic-gate 	for (p = dfnp->fn_dirents; p != NULL; p = p->fn_next) {
15940Sstevel@tonic-gate 		if (strcmp(p->fn_name, name) == 0) {
15950Sstevel@tonic-gate 			mutex_enter(&p->fn_lock);
15960Sstevel@tonic-gate 			if (p->fn_flags & MF_THISUID_MATCH_RQD) {
15970Sstevel@tonic-gate 				/*
15980Sstevel@tonic-gate 				 * "thisuser" kind of node
15990Sstevel@tonic-gate 				 * Need to match CREDs as well
16000Sstevel@tonic-gate 				 */
16010Sstevel@tonic-gate 				mutex_exit(&p->fn_lock);
16020Sstevel@tonic-gate 				match = crcmp(p->fn_cred, cred) == 0;
16030Sstevel@tonic-gate 			} else {
16040Sstevel@tonic-gate 				/*
16050Sstevel@tonic-gate 				 * No need to check CRED
16060Sstevel@tonic-gate 				 */
16070Sstevel@tonic-gate 				mutex_exit(&p->fn_lock);
16080Sstevel@tonic-gate 				match = 1;
16090Sstevel@tonic-gate 			}
16100Sstevel@tonic-gate 		}
16110Sstevel@tonic-gate 		if (match) {
16120Sstevel@tonic-gate 			error = 0;
16130Sstevel@tonic-gate 			if (fnpp) {
16140Sstevel@tonic-gate 				*fnpp = p;
16150Sstevel@tonic-gate 				VN_HOLD(fntovn(*fnpp));
16160Sstevel@tonic-gate 			}
16170Sstevel@tonic-gate 			break;
16180Sstevel@tonic-gate 		}
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_search: error=%d\n", error));
16220Sstevel@tonic-gate 	return (error);
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate /*
16260Sstevel@tonic-gate  * If dvp is mounted on, get path's vnode in the mounted on
16270Sstevel@tonic-gate  * filesystem.  Path is relative to dvp, ie "./path".
16280Sstevel@tonic-gate  * If successful, *mvp points to a the held mountpoint vnode.
16290Sstevel@tonic-gate  */
16300Sstevel@tonic-gate /* ARGSUSED */
16310Sstevel@tonic-gate static int
16320Sstevel@tonic-gate auto_getmntpnt(
16330Sstevel@tonic-gate 	vnode_t *dvp,
16340Sstevel@tonic-gate 	char *path,
16350Sstevel@tonic-gate 	vnode_t **mvpp,		/* vnode for mountpoint */
16360Sstevel@tonic-gate 	cred_t *cred)
16370Sstevel@tonic-gate {
16380Sstevel@tonic-gate 	int error = 0;
16390Sstevel@tonic-gate 	vnode_t *newvp;
16400Sstevel@tonic-gate 	char namebuf[TYPICALMAXPATHLEN];
16410Sstevel@tonic-gate 	struct pathname lookpn;
16420Sstevel@tonic-gate 	vfs_t *vfsp;
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_getmntpnt: path=%s\n", path));
16450Sstevel@tonic-gate 
16461153Snr123932 	if (error = vn_vfsrlock_wait(dvp))
16470Sstevel@tonic-gate 		return (error);
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	/*
16500Sstevel@tonic-gate 	 * Now that we have the vfswlock, check to see if dvp
16510Sstevel@tonic-gate 	 * is still mounted on.  If not, then just bail out as
16520Sstevel@tonic-gate 	 * there is no need to remount the triggers since the
16530Sstevel@tonic-gate 	 * higher level mount point has gotten unmounted.
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	vfsp = vn_mountedvfs(dvp);
16560Sstevel@tonic-gate 	if (vfsp == NULL) {
16570Sstevel@tonic-gate 		vn_vfsunlock(dvp);
16580Sstevel@tonic-gate 		error = EBUSY;
16590Sstevel@tonic-gate 		goto done;
16600Sstevel@tonic-gate 	}
16610Sstevel@tonic-gate 	/*
16620Sstevel@tonic-gate 	 * Since mounted on, lookup "path" in the new filesystem,
16630Sstevel@tonic-gate 	 * it is important that we do the filesystem jump here to
16640Sstevel@tonic-gate 	 * avoid lookuppn() calling auto_lookup on dvp and deadlock.
16650Sstevel@tonic-gate 	 */
16661153Snr123932 	error = VFS_ROOT(vfsp, &newvp);
16670Sstevel@tonic-gate 	vn_vfsunlock(dvp);
16680Sstevel@tonic-gate 	if (error)
16690Sstevel@tonic-gate 		goto done;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	/*
16720Sstevel@tonic-gate 	 * We do a VN_HOLD on newvp just in case the first call to
16730Sstevel@tonic-gate 	 * lookuppnvp() fails with ENAMETOOLONG.  We should still have a
16740Sstevel@tonic-gate 	 * reference to this vnode for the second call to lookuppnvp().
16750Sstevel@tonic-gate 	 */
16760Sstevel@tonic-gate 	VN_HOLD(newvp);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	/*
16790Sstevel@tonic-gate 	 * Now create the pathname struct so we can make use of lookuppnvp,
16800Sstevel@tonic-gate 	 * and pn_getcomponent.
16810Sstevel@tonic-gate 	 * This code is similar to lookupname() in fs/lookup.c.
16820Sstevel@tonic-gate 	 */
16830Sstevel@tonic-gate 	error = pn_get_buf(path, UIO_SYSSPACE, &lookpn,
16840Sstevel@tonic-gate 		namebuf, sizeof (namebuf));
16850Sstevel@tonic-gate 	if (error == 0) {
16860Sstevel@tonic-gate 		error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
16870Sstevel@tonic-gate 		    mvpp, rootdir, newvp, cred);
16880Sstevel@tonic-gate 	} else
16890Sstevel@tonic-gate 		VN_RELE(newvp);
16900Sstevel@tonic-gate 	if (error == ENAMETOOLONG) {
16910Sstevel@tonic-gate 		/*
16920Sstevel@tonic-gate 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
16930Sstevel@tonic-gate 		 * newvp is VN_RELE'd by this call to lookuppnvp.
16940Sstevel@tonic-gate 		 *
16950Sstevel@tonic-gate 		 * Using 'rootdir' in a zone's context is OK here: we already
16960Sstevel@tonic-gate 		 * ascertained that there are no '..'s in the path, and we're
16970Sstevel@tonic-gate 		 * not following symlinks.
16980Sstevel@tonic-gate 		 */
16990Sstevel@tonic-gate 		if ((error = pn_get(path, UIO_SYSSPACE, &lookpn)) == 0) {
17000Sstevel@tonic-gate 			error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
17010Sstevel@tonic-gate 			    mvpp, rootdir, newvp, cred);
17020Sstevel@tonic-gate 			pn_free(&lookpn);
17030Sstevel@tonic-gate 		} else
17040Sstevel@tonic-gate 			VN_RELE(newvp);
17050Sstevel@tonic-gate 	} else {
17060Sstevel@tonic-gate 		/*
17070Sstevel@tonic-gate 		 * Need to release newvp here since we held it.
17080Sstevel@tonic-gate 		 */
17090Sstevel@tonic-gate 		VN_RELE(newvp);
17100Sstevel@tonic-gate 	}
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate done:
17130Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_getmntpnt: path=%s *mvpp=%p error=%d\n",
17140Sstevel@tonic-gate 	    path, (void *)*mvpp, error));
17150Sstevel@tonic-gate 	return (error);
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate #define	DEEPER(x) (((x)->fn_dirents != NULL) || \
17190Sstevel@tonic-gate 			(vn_mountedvfs(fntovn((x)))) != NULL)
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate /*
17220Sstevel@tonic-gate  * The caller, should have already VN_RELE'd its reference to the
17230Sstevel@tonic-gate  * root vnode of this filesystem.
17240Sstevel@tonic-gate  */
17250Sstevel@tonic-gate static int
17260Sstevel@tonic-gate auto_inkernel_unmount(vfs_t *vfsp)
17270Sstevel@tonic-gate {
17280Sstevel@tonic-gate 	vnode_t *cvp = vfsp->vfs_vnodecovered;
17290Sstevel@tonic-gate 	int error;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	AUTOFS_DPRINT((4,
17320Sstevel@tonic-gate 	    "auto_inkernel_unmount: devid=%lx mntpnt(%p) count %u\n",
17330Sstevel@tonic-gate 	    vfsp->vfs_dev, (void *)cvp, cvp->v_count));
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(cvp));
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	/*
17380Sstevel@tonic-gate 	 * Perform the unmount
17390Sstevel@tonic-gate 	 * The mountpoint has already been locked by the caller.
17400Sstevel@tonic-gate 	 */
17410Sstevel@tonic-gate 	error = dounmount(vfsp, 0, kcred);
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_inkernel_unmount: exit count %u\n",
17440Sstevel@tonic-gate 	    cvp->v_count));
17450Sstevel@tonic-gate 	return (error);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate /*
17490Sstevel@tonic-gate  * unmounts trigger nodes in the kernel.
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate static void
17520Sstevel@tonic-gate unmount_triggers(fnnode_t *fnp, action_list **alp)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	fnnode_t *tp, *next;
17550Sstevel@tonic-gate 	int error = 0;
17560Sstevel@tonic-gate 	vfs_t *vfsp;
17570Sstevel@tonic-gate 	vnode_t *tvp;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "unmount_triggers: fnp=%p\n", (void *)fnp));
17600Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	*alp = fnp->fn_alp;
17630Sstevel@tonic-gate 	next = fnp->fn_trigger;
17640Sstevel@tonic-gate 	while ((tp = next) != NULL) {
17650Sstevel@tonic-gate 		tvp = fntovn(tp);
17660Sstevel@tonic-gate 		ASSERT(tvp->v_count >= 2);
17670Sstevel@tonic-gate 		next = tp->fn_next;
17680Sstevel@tonic-gate 		/*
17690Sstevel@tonic-gate 		 * drop writer's lock since the unmount will end up
17700Sstevel@tonic-gate 		 * disconnecting this node from fnp and needs to acquire
17710Sstevel@tonic-gate 		 * the writer's lock again.
17720Sstevel@tonic-gate 		 * next has at least a reference count >= 2 since it's
17730Sstevel@tonic-gate 		 * a trigger node, therefore can not be accidentally freed
17740Sstevel@tonic-gate 		 * by a VN_RELE
17750Sstevel@tonic-gate 		 */
17760Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 		vfsp = tvp->v_vfsp;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		/*
17810Sstevel@tonic-gate 		 * Its parent was holding a reference to it, since this
17820Sstevel@tonic-gate 		 * is a trigger vnode.
17830Sstevel@tonic-gate 		 */
17840Sstevel@tonic-gate 		VN_RELE(tvp);
17850Sstevel@tonic-gate 		if (error = auto_inkernel_unmount(vfsp)) {
17860Sstevel@tonic-gate 			cmn_err(CE_PANIC, "unmount_triggers: "
17870Sstevel@tonic-gate 			    "unmount of vp=%p failed error=%d",
17880Sstevel@tonic-gate 			    (void *)tvp, error);
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 		/*
17910Sstevel@tonic-gate 		 * reacquire writer's lock
17920Sstevel@tonic-gate 		 */
17930Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	/*
17970Sstevel@tonic-gate 	 * We were holding a reference to our parent.  Drop that.
17980Sstevel@tonic-gate 	 */
17990Sstevel@tonic-gate 	VN_RELE(fntovn(fnp));
18000Sstevel@tonic-gate 	fnp->fn_trigger = NULL;
18010Sstevel@tonic-gate 	fnp->fn_alp = NULL;
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "unmount_triggers: finished\n"));
18040Sstevel@tonic-gate }
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate /*
18070Sstevel@tonic-gate  * This routine locks the mountpoint of every trigger node if they're
18080Sstevel@tonic-gate  * not busy, or returns EBUSY if any node is busy. If a trigger node should
18090Sstevel@tonic-gate  * be unmounted first, then it sets nfnp to point to it, otherwise nfnp
18100Sstevel@tonic-gate  * points to NULL.
18110Sstevel@tonic-gate  */
18120Sstevel@tonic-gate static int
18130Sstevel@tonic-gate triggers_busy(fnnode_t *fnp, fnnode_t **nfnp)
18140Sstevel@tonic-gate {
18150Sstevel@tonic-gate 	int error = 0, done;
18160Sstevel@tonic-gate 	int lck_error = 0;
18170Sstevel@tonic-gate 	fnnode_t *tp, *t1p;
18180Sstevel@tonic-gate 	vfs_t *vfsp;
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	*nfnp = NULL;
18230Sstevel@tonic-gate 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
18240Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\ttrigger: %s\n", tp->fn_name));
18250Sstevel@tonic-gate 		vfsp = fntovn(tp)->v_vfsp;
18260Sstevel@tonic-gate 		error = 0;
18270Sstevel@tonic-gate 		/*
18280Sstevel@tonic-gate 		 * The vn_vfsunlock will be done in auto_inkernel_unmount.
18290Sstevel@tonic-gate 		 */
18300Sstevel@tonic-gate 		lck_error = vn_vfswlock(vfsp->vfs_vnodecovered);
18310Sstevel@tonic-gate 		if (lck_error == 0) {
18320Sstevel@tonic-gate 			mutex_enter(&tp->fn_lock);
18330Sstevel@tonic-gate 			ASSERT((tp->fn_flags & MF_LOOKUP) == 0);
18340Sstevel@tonic-gate 			if (tp->fn_flags & MF_INPROG) {
18350Sstevel@tonic-gate 				/*
18360Sstevel@tonic-gate 				 * a mount is in progress
18370Sstevel@tonic-gate 				 */
18380Sstevel@tonic-gate 				error = EBUSY;
18390Sstevel@tonic-gate 			}
18400Sstevel@tonic-gate 			mutex_exit(&tp->fn_lock);
18410Sstevel@tonic-gate 		}
18420Sstevel@tonic-gate 		if (lck_error || error || DEEPER(tp) ||
18430Sstevel@tonic-gate 		    ((fntovn(tp))->v_count) > 2) {
18440Sstevel@tonic-gate 			/*
18450Sstevel@tonic-gate 			 * couldn't lock it because it's busy,
18460Sstevel@tonic-gate 			 * It is mounted on or has dirents?
18470Sstevel@tonic-gate 			 * If reference count is greater than two, then
18480Sstevel@tonic-gate 			 * somebody else is holding a reference to this vnode.
18490Sstevel@tonic-gate 			 * One reference is for the mountpoint, and the second
18500Sstevel@tonic-gate 			 * is for the trigger node.
18510Sstevel@tonic-gate 			 */
18520Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\ttrigger busy\n"));
18530Sstevel@tonic-gate 			if ((lck_error == 0) && (error == 0)) {
18540Sstevel@tonic-gate 				*nfnp = tp;
18550Sstevel@tonic-gate 				/*
18560Sstevel@tonic-gate 				 * The matching VN_RELE is done in
18570Sstevel@tonic-gate 				 * unmount_tree().
18580Sstevel@tonic-gate 				 */
18590Sstevel@tonic-gate 				VN_HOLD(fntovn(*nfnp));
18600Sstevel@tonic-gate 			}
18610Sstevel@tonic-gate 			/*
18620Sstevel@tonic-gate 			 * Unlock previously locked mountpoints
18630Sstevel@tonic-gate 			 */
18640Sstevel@tonic-gate 			for (done = 0, t1p = fnp->fn_trigger; !done;
18650Sstevel@tonic-gate 			    t1p = t1p->fn_next) {
18660Sstevel@tonic-gate 				/*
18670Sstevel@tonic-gate 				 * Unlock all nodes previously
18680Sstevel@tonic-gate 				 * locked. All nodes up to 'tp'
18690Sstevel@tonic-gate 				 * were successfully locked. If 'lck_err' is
18700Sstevel@tonic-gate 				 * set, then 'tp' was not locked, and thus
18710Sstevel@tonic-gate 				 * should not be unlocked. If
18720Sstevel@tonic-gate 				 * 'lck_err' is not set, then 'tp' was
18730Sstevel@tonic-gate 				 * successfully locked, and it should
18740Sstevel@tonic-gate 				 * be unlocked.
18750Sstevel@tonic-gate 				 */
18760Sstevel@tonic-gate 				if (t1p != tp || !lck_error) {
18770Sstevel@tonic-gate 					vfsp = fntovn(t1p)->v_vfsp;
18780Sstevel@tonic-gate 					vn_vfsunlock(vfsp->vfs_vnodecovered);
18790Sstevel@tonic-gate 				}
18800Sstevel@tonic-gate 				done = (t1p == tp);
18810Sstevel@tonic-gate 			}
18820Sstevel@tonic-gate 			error = EBUSY;
18830Sstevel@tonic-gate 			break;
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "triggers_busy: error=%d\n", error));
18880Sstevel@tonic-gate 	return (error);
18890Sstevel@tonic-gate }
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate /*
18920Sstevel@tonic-gate  * Unlock previously locked trigger nodes.
18930Sstevel@tonic-gate  */
18940Sstevel@tonic-gate static int
18950Sstevel@tonic-gate triggers_unlock(fnnode_t *fnp)
18960Sstevel@tonic-gate {
18970Sstevel@tonic-gate 	fnnode_t *tp;
18980Sstevel@tonic-gate 	vfs_t *vfsp;
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
19030Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tunlock trigger: %s\n", tp->fn_name));
19040Sstevel@tonic-gate 		vfsp = fntovn(tp)->v_vfsp;
19050Sstevel@tonic-gate 		vn_vfsunlock(vfsp->vfs_vnodecovered);
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	return (0);
19090Sstevel@tonic-gate }
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate /*
19120Sstevel@tonic-gate  * It is the caller's responsibility to grab the VVFSLOCK.
19130Sstevel@tonic-gate  * Releases the VVFSLOCK upon return.
19140Sstevel@tonic-gate  */
19150Sstevel@tonic-gate static int
19160Sstevel@tonic-gate unmount_node(vnode_t *cvp, int force)
19170Sstevel@tonic-gate {
19180Sstevel@tonic-gate 	int error = 0;
19190Sstevel@tonic-gate 	fnnode_t *cfnp;
19200Sstevel@tonic-gate 	vfs_t *vfsp;
19210Sstevel@tonic-gate 	umntrequest ul;
19220Sstevel@tonic-gate 	fninfo_t *fnip;
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tunmount_node cvp=%p\n", (void *)cvp));
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(cvp));
19270Sstevel@tonic-gate 	cfnp = vntofn(cvp);
19280Sstevel@tonic-gate 	vfsp = vn_mountedvfs(cvp);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	if (force || cfnp->fn_flags & MF_IK_MOUNT) {
19310Sstevel@tonic-gate 		/*
19320Sstevel@tonic-gate 		 * Mount was performed in the kernel, so
19330Sstevel@tonic-gate 		 * do an in-kernel unmount. auto_inkernel_unmount()
19340Sstevel@tonic-gate 		 * will vn_vfsunlock(cvp).
19350Sstevel@tonic-gate 		 */
19360Sstevel@tonic-gate 		error = auto_inkernel_unmount(vfsp);
19370Sstevel@tonic-gate 	} else {
19380Sstevel@tonic-gate 		zone_t *zone = NULL;
19390Sstevel@tonic-gate 		refstr_t *mntpt, *resource;
19400Sstevel@tonic-gate 		size_t mntoptslen;
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 		/*
19430Sstevel@tonic-gate 		 * Get the mnttab information of the node
19440Sstevel@tonic-gate 		 * and ask the daemon to unmount it.
19450Sstevel@tonic-gate 		 */
19460Sstevel@tonic-gate 		bzero(&ul, sizeof (ul));
19470Sstevel@tonic-gate 		mntfs_getmntopts(vfsp, &ul.mntopts, &mntoptslen);
19480Sstevel@tonic-gate 		if (ul.mntopts == NULL) {
19492170Sevanl 			auto_log(cfnp->fn_globals->fng_verbose,
19502170Sevanl 				cfnp->fn_globals->fng_zoneid,
19512170Sevanl 				CE_WARN, "unmount_node: "
19520Sstevel@tonic-gate 			    "no memory");
19530Sstevel@tonic-gate 			vn_vfsunlock(cvp);
19540Sstevel@tonic-gate 			error = ENOMEM;
19550Sstevel@tonic-gate 			goto done;
19560Sstevel@tonic-gate 		}
19570Sstevel@tonic-gate 		if (mntoptslen > AUTOFS_MAXOPTSLEN)
19580Sstevel@tonic-gate 			ul.mntopts[AUTOFS_MAXOPTSLEN - 1] = '\0';
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 		mntpt = vfs_getmntpoint(vfsp);
19610Sstevel@tonic-gate 		ul.mntpnt = (char *)refstr_value(mntpt);
19620Sstevel@tonic-gate 		resource = vfs_getresource(vfsp);
19630Sstevel@tonic-gate 		ul.mntresource = (char *)refstr_value(resource);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 		fnip = vfstofni(cvp->v_vfsp);
19660Sstevel@tonic-gate 		ul.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		/*
19690Sstevel@tonic-gate 		 * Since a zone'd automountd's view of the autofs mount points
19700Sstevel@tonic-gate 		 * differs from those in the kernel, we need to make sure we
19710Sstevel@tonic-gate 		 * give it consistent mount points.
19720Sstevel@tonic-gate 		 */
19730Sstevel@tonic-gate 		ASSERT(fnip->fi_zoneid == getzoneid());
19740Sstevel@tonic-gate 		zone = curproc->p_zone;
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 		if (fnip->fi_zoneid != GLOBAL_ZONEID) {
19770Sstevel@tonic-gate 			if (ZONE_PATH_VISIBLE(ul.mntpnt, zone)) {
19780Sstevel@tonic-gate 				ul.mntpnt =
19790Sstevel@tonic-gate 				    ZONE_PATH_TRANSLATE(ul.mntpnt, zone);
19800Sstevel@tonic-gate 			}
19810Sstevel@tonic-gate 			if (ZONE_PATH_VISIBLE(ul.mntresource, zone)) {
19820Sstevel@tonic-gate 				ul.mntresource =
19830Sstevel@tonic-gate 				    ZONE_PATH_TRANSLATE(ul.mntresource, zone);
19840Sstevel@tonic-gate 			}
19850Sstevel@tonic-gate 		}
19862170Sevanl 
19870Sstevel@tonic-gate 		ul.fstype = vfssw[vfsp->vfs_fstype].vsw_name;
19880Sstevel@tonic-gate 		vn_vfsunlock(cvp);
19890Sstevel@tonic-gate 
19902170Sevanl 		error = auto_send_unmount_request(fnip, &ul, FALSE);
19910Sstevel@tonic-gate 		kmem_free(ul.mntopts, mntoptslen);
19920Sstevel@tonic-gate 		refstr_rele(mntpt);
19930Sstevel@tonic-gate 		refstr_rele(resource);
19940Sstevel@tonic-gate 	}
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate done:
19970Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tunmount_node cvp=%p error=%d\n", (void *)cvp,
19980Sstevel@tonic-gate 	    error));
19990Sstevel@tonic-gate 	return (error);
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate /*
20030Sstevel@tonic-gate  * vp is the "root" of the AUTOFS filesystem.
20040Sstevel@tonic-gate  * return EBUSY if any thread is holding a reference to this vnode
20050Sstevel@tonic-gate  * other than us.
20060Sstevel@tonic-gate  */
20070Sstevel@tonic-gate static int
20080Sstevel@tonic-gate check_auto_node(vnode_t *vp)
20090Sstevel@tonic-gate {
20100Sstevel@tonic-gate 	fnnode_t *fnp;
20110Sstevel@tonic-gate 	int error = 0;
20120Sstevel@tonic-gate 	/*
20130Sstevel@tonic-gate 	 * number of references to expect for
20140Sstevel@tonic-gate 	 * a non-busy vnode.
20150Sstevel@tonic-gate 	 */
20160Sstevel@tonic-gate 	uint_t count;
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tcheck_auto_node vp=%p ", (void *)vp));
20190Sstevel@tonic-gate 	fnp = vntofn(vp);
20200Sstevel@tonic-gate 	ASSERT(fnp->fn_flags & MF_INPROG);
20210Sstevel@tonic-gate 	ASSERT((fnp->fn_flags & MF_LOOKUP) == 0);
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	count = 1;		/* we are holding a reference to vp */
20240Sstevel@tonic-gate 	if (fnp->fn_flags & MF_TRIGGER) {
20250Sstevel@tonic-gate 		/*
20260Sstevel@tonic-gate 		 * parent holds a pointer to us (trigger)
20270Sstevel@tonic-gate 		 */
20280Sstevel@tonic-gate 		count++;
20290Sstevel@tonic-gate 	}
20300Sstevel@tonic-gate 	if (fnp->fn_trigger != NULL) {
20310Sstevel@tonic-gate 		/*
20320Sstevel@tonic-gate 		 * The trigger nodes have a hold on us.
20330Sstevel@tonic-gate 		 */
20340Sstevel@tonic-gate 		count++;
20350Sstevel@tonic-gate 	}
20360Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
20370Sstevel@tonic-gate 	if (vp->v_flag & VROOT)
20380Sstevel@tonic-gate 		count++;
20390Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
20400Sstevel@tonic-gate 	AUTOFS_DPRINT((10, "\tcount=%u ", vp->v_count));
20410Sstevel@tonic-gate 	if (vp->v_count > count)
20420Sstevel@tonic-gate 		error = EBUSY;
20430Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tcheck_auto_node error=%d ", error));
20460Sstevel@tonic-gate 	return (error);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate /*
20500Sstevel@tonic-gate  * rootvp is the root of the AUTOFS filesystem.
20510Sstevel@tonic-gate  * If rootvp is busy (v_count > 1) returns EBUSY.
20520Sstevel@tonic-gate  * else removes every vnode under this tree.
20530Sstevel@tonic-gate  * ASSUMPTION: Assumes that the only node which can be busy is
20540Sstevel@tonic-gate  * the root vnode. This filesystem better be two levels deep only,
20550Sstevel@tonic-gate  * the root and its immediate subdirs.
20560Sstevel@tonic-gate  * The daemon will "AUTOFS direct-mount" only one level below the root.
20570Sstevel@tonic-gate  */
20580Sstevel@tonic-gate static int
20590Sstevel@tonic-gate unmount_autofs(vnode_t *rootvp)
20600Sstevel@tonic-gate {
20610Sstevel@tonic-gate 	fnnode_t *fnp, *rootfnp, *nfnp;
20620Sstevel@tonic-gate 	int error;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tunmount_autofs rootvp=%p ", (void *)rootvp));
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	error = check_auto_node(rootvp);
20670Sstevel@tonic-gate 	if (error == 0) {
20680Sstevel@tonic-gate 		/*
20690Sstevel@tonic-gate 		 * Remove all its immediate subdirectories.
20700Sstevel@tonic-gate 		 */
20710Sstevel@tonic-gate 		rootfnp = vntofn(rootvp);
20720Sstevel@tonic-gate 		rw_enter(&rootfnp->fn_rwlock, RW_WRITER);
20730Sstevel@tonic-gate 		nfnp = NULL;	/* lint clean */
20740Sstevel@tonic-gate 		for (fnp = rootfnp->fn_dirents; fnp != NULL; fnp = nfnp) {
20750Sstevel@tonic-gate 			ASSERT(fntovn(fnp)->v_count == 0);
20760Sstevel@tonic-gate 			ASSERT(fnp->fn_dirents == NULL);
20770Sstevel@tonic-gate 			ASSERT(fnp->fn_linkcnt == 2);
20780Sstevel@tonic-gate 			fnp->fn_linkcnt--;
20790Sstevel@tonic-gate 			auto_disconnect(rootfnp, fnp);
20800Sstevel@tonic-gate 			nfnp = fnp->fn_next;
20810Sstevel@tonic-gate 			auto_freefnnode(fnp);
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate 		rw_exit(&rootfnp->fn_rwlock);
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tunmount_autofs error=%d ", error));
20860Sstevel@tonic-gate 	return (error);
20870Sstevel@tonic-gate }
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate /*
20900Sstevel@tonic-gate  * max number of unmount threads running
20910Sstevel@tonic-gate  */
20920Sstevel@tonic-gate static int autofs_unmount_threads = 5;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate /*
20950Sstevel@tonic-gate  * XXX unmount_tree() is not suspend-safe within the scope of
20960Sstevel@tonic-gate  * the present model defined for cpr to suspend the system. Calls made
20970Sstevel@tonic-gate  * by the unmount_tree() that have been identified to be unsafe are
20980Sstevel@tonic-gate  * (1) RPC client handle setup and client calls to automountd which can
20990Sstevel@tonic-gate  * block deep down in the RPC library, (2) kmem_alloc() calls with the
21000Sstevel@tonic-gate  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*() and
21010Sstevel@tonic-gate  * VOP_*() calls which can result in over the wire calls to servers.
21020Sstevel@tonic-gate  * The thread should be completely reevaluated to make it suspend-safe in
21030Sstevel@tonic-gate  * case of future updates to the cpr model.
21040Sstevel@tonic-gate  */
21050Sstevel@tonic-gate void
21060Sstevel@tonic-gate unmount_tree(struct autofs_globals *fngp, int force)
21070Sstevel@tonic-gate {
21080Sstevel@tonic-gate 	vnode_t *vp, *newvp;
21090Sstevel@tonic-gate 	vfs_t *vfsp;
21100Sstevel@tonic-gate 	fnnode_t *fnp, *nfnp, *pfnp;
21110Sstevel@tonic-gate 	action_list *alp;
21120Sstevel@tonic-gate 	int error, ilocked_it = 0;
21130Sstevel@tonic-gate 	fninfo_t *fnip;
21140Sstevel@tonic-gate 	time_t ref_time;
21150Sstevel@tonic-gate 	int autofs_busy_root, unmount_as_unit, unmount_done = 0;
21160Sstevel@tonic-gate 	timestruc_t now;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	callb_cpr_t cprinfo;
21190Sstevel@tonic-gate 	kmutex_t unmount_tree_cpr_lock;
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	mutex_init(&unmount_tree_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
21220Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &unmount_tree_cpr_lock, callb_generic_cpr,
21230Sstevel@tonic-gate 		"unmount_tree");
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	/*
21260Sstevel@tonic-gate 	 * Got to release lock before attempting unmount in case
21270Sstevel@tonic-gate 	 * it hangs.
21280Sstevel@tonic-gate 	 */
21290Sstevel@tonic-gate 	rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_READER);
21300Sstevel@tonic-gate 	if ((fnp = fngp->fng_rootfnnodep->fn_dirents) == NULL) {
21310Sstevel@tonic-gate 		ASSERT(fngp->fng_fnnode_count == 1);
21320Sstevel@tonic-gate 		/*
21330Sstevel@tonic-gate 		 * no autofs mounted, done.
21340Sstevel@tonic-gate 		 */
21350Sstevel@tonic-gate 		rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
21360Sstevel@tonic-gate 		goto done;
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 	VN_HOLD(fntovn(fnp));
21390Sstevel@tonic-gate 	rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 	vp = fntovn(fnp);
21420Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
21430Sstevel@tonic-gate 	/*
21440Sstevel@tonic-gate 	 * autofssys() will be calling in from the global zone and doing
21450Sstevel@tonic-gate 	 * work on the behalf of the given zone, hence we can't always assert
21460Sstevel@tonic-gate 	 * that we have the right credentials, nor that the caller is always in
21470Sstevel@tonic-gate 	 * the correct zone.
21480Sstevel@tonic-gate 	 *
21490Sstevel@tonic-gate 	 * We do, however, know that if this is a "forced unmount" operation
21500Sstevel@tonic-gate 	 * (which autofssys() does), then we won't go down to the krpc layers,
21510Sstevel@tonic-gate 	 * so we don't need to fudge with the credentials.
21520Sstevel@tonic-gate 	 */
21530Sstevel@tonic-gate 	ASSERT(force || fnip->fi_zoneid == getzoneid());
21542170Sevanl 	if (!force && auto_null_request(fnip, FALSE) != 0) {
21550Sstevel@tonic-gate 		/*
21560Sstevel@tonic-gate 		 * automountd not running in this zone,
21570Sstevel@tonic-gate 		 * don't attempt unmounting this round.
21580Sstevel@tonic-gate 		 */
21590Sstevel@tonic-gate 		VN_RELE(vp);
21600Sstevel@tonic-gate 		goto done;
21610Sstevel@tonic-gate 	}
21620Sstevel@tonic-gate 	/* reference time for this unmount round */
21630Sstevel@tonic-gate 	ref_time = gethrestime_sec();
21640Sstevel@tonic-gate 	/*
21650Sstevel@tonic-gate 	 * If this an autofssys() call, we need to make sure we don't skip
21660Sstevel@tonic-gate 	 * nodes because we think we saw them recently.
21670Sstevel@tonic-gate 	 */
21680Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
21690Sstevel@tonic-gate 	if (force && fnp->fn_unmount_ref_time >= ref_time)
21700Sstevel@tonic-gate 		ref_time = fnp->fn_unmount_ref_time + 1;
21710Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "unmount_tree (ID=%ld)\n", ref_time));
21740Sstevel@tonic-gate top:
21750Sstevel@tonic-gate 	AUTOFS_DPRINT((10, "unmount_tree: %s\n", fnp->fn_name));
21760Sstevel@tonic-gate 	ASSERT(fnp);
21770Sstevel@tonic-gate 	vp = fntovn(fnp);
21780Sstevel@tonic-gate 	if (vp->v_type == VLNK) {
21790Sstevel@tonic-gate 		/*
21800Sstevel@tonic-gate 		 * can't unmount symbolic links
21810Sstevel@tonic-gate 		 */
21820Sstevel@tonic-gate 		goto next;
21830Sstevel@tonic-gate 	}
21840Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
21850Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
21860Sstevel@tonic-gate 	error = 0;
21870Sstevel@tonic-gate 	autofs_busy_root = unmount_as_unit = 0;
21880Sstevel@tonic-gate 	alp = NULL;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	ilocked_it = 0;
21910Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
21920Sstevel@tonic-gate 	if (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
21930Sstevel@tonic-gate 		/*
21940Sstevel@tonic-gate 		 * Either a mount, lookup or another unmount of this
21950Sstevel@tonic-gate 		 * subtree is in progress, don't attempt to unmount at
21960Sstevel@tonic-gate 		 * this time.
21970Sstevel@tonic-gate 		 */
21980Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
21990Sstevel@tonic-gate 		error = EBUSY;
22000Sstevel@tonic-gate 		goto next;
22010Sstevel@tonic-gate 	}
22020Sstevel@tonic-gate 	if (fnp->fn_unmount_ref_time >= ref_time) {
22030Sstevel@tonic-gate 		/*
22040Sstevel@tonic-gate 		 * Already been here, try next node.
22050Sstevel@tonic-gate 		 */
22060Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
22070Sstevel@tonic-gate 		error = EBUSY;
22080Sstevel@tonic-gate 		goto next;
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate 	fnp->fn_unmount_ref_time = ref_time;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	/*
22130Sstevel@tonic-gate 	 * If forced operation ignore timeout values
22140Sstevel@tonic-gate 	 */
22150Sstevel@tonic-gate 	if (!force && fnp->fn_ref_time + fnip->fi_mount_to >
22160Sstevel@tonic-gate 	    gethrestime_sec()) {
22170Sstevel@tonic-gate 		/*
22180Sstevel@tonic-gate 		 * Node has been referenced recently, try the
22190Sstevel@tonic-gate 		 * unmount of its children if any.
22200Sstevel@tonic-gate 		 */
22210Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
22220Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "fn_ref_time within range\n"));
22230Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
22240Sstevel@tonic-gate 		if (fnp->fn_dirents) {
22250Sstevel@tonic-gate 			/*
22260Sstevel@tonic-gate 			 * Has subdirectory, attempt their
22270Sstevel@tonic-gate 			 * unmount first
22280Sstevel@tonic-gate 			 */
22290Sstevel@tonic-gate 			nfnp = fnp->fn_dirents;
22300Sstevel@tonic-gate 			VN_HOLD(fntovn(nfnp));
22310Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 			VN_RELE(vp);
22340Sstevel@tonic-gate 			fnp = nfnp;
22350Sstevel@tonic-gate 			goto top;
22360Sstevel@tonic-gate 		}
22370Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
22380Sstevel@tonic-gate 		/*
22390Sstevel@tonic-gate 		 * No children, try next node.
22400Sstevel@tonic-gate 		 */
22410Sstevel@tonic-gate 		error = EBUSY;
22420Sstevel@tonic-gate 		goto next;
22430Sstevel@tonic-gate 	}
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
22460Sstevel@tonic-gate 	fnp->fn_error = 0;
22470Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
22480Sstevel@tonic-gate 	ilocked_it = 1;
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	rw_enter(&fnp->fn_rwlock, RW_WRITER);
22510Sstevel@tonic-gate 	if (fnp->fn_trigger != NULL) {
22520Sstevel@tonic-gate 		unmount_as_unit = 1;
22530Sstevel@tonic-gate 		if ((vn_mountedvfs(vp) == NULL) && (check_auto_node(vp))) {
22540Sstevel@tonic-gate 			/*
22550Sstevel@tonic-gate 			 * AUTOFS mountpoint is busy, there's
22560Sstevel@tonic-gate 			 * no point trying to unmount. Fall through
22570Sstevel@tonic-gate 			 * to attempt to unmount subtrees rooted
22580Sstevel@tonic-gate 			 * at a possible trigger node, but remember
22590Sstevel@tonic-gate 			 * not to unmount this tree.
22600Sstevel@tonic-gate 			 */
22610Sstevel@tonic-gate 			autofs_busy_root = 1;
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 		if (triggers_busy(fnp, &nfnp)) {
22650Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
22660Sstevel@tonic-gate 			if (nfnp == NULL) {
22670Sstevel@tonic-gate 				error = EBUSY;
22680Sstevel@tonic-gate 				goto next;
22690Sstevel@tonic-gate 			}
22700Sstevel@tonic-gate 			/*
22710Sstevel@tonic-gate 			 * nfnp is busy, try to unmount it first
22720Sstevel@tonic-gate 			 */
22730Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
22740Sstevel@tonic-gate 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
22750Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
22760Sstevel@tonic-gate 			VN_RELE(vp);
22770Sstevel@tonic-gate 			ASSERT(fntovn(nfnp)->v_count > 1);
22780Sstevel@tonic-gate 			fnp = nfnp;
22790Sstevel@tonic-gate 			goto top;
22800Sstevel@tonic-gate 		}
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 		/*
22830Sstevel@tonic-gate 		 * At this point, we know all trigger nodes are locked,
22840Sstevel@tonic-gate 		 * and they're not busy or mounted on.
22850Sstevel@tonic-gate 		 */
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 		if (autofs_busy_root) {
22880Sstevel@tonic-gate 			/*
22890Sstevel@tonic-gate 			 * Got to unlock the the trigger nodes since
22900Sstevel@tonic-gate 			 * I'm not really going to unmount the filesystem.
22910Sstevel@tonic-gate 			 */
22920Sstevel@tonic-gate 			(void) triggers_unlock(fnp);
22930Sstevel@tonic-gate 		} else {
22940Sstevel@tonic-gate 			/*
22950Sstevel@tonic-gate 			 * Attempt to unmount all the trigger nodes,
22960Sstevel@tonic-gate 			 * save the action_list in case we need to
22972170Sevanl 			 * remount them later. The action_list will be
22980Sstevel@tonic-gate 			 * freed later if there was no need to remount the
22990Sstevel@tonic-gate 			 * trigger nodes.
23000Sstevel@tonic-gate 			 */
23010Sstevel@tonic-gate 			unmount_triggers(fnp, &alp);
23020Sstevel@tonic-gate 		}
23030Sstevel@tonic-gate 	}
23040Sstevel@tonic-gate 	rw_exit(&fnp->fn_rwlock);
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 	if (autofs_busy_root)
23070Sstevel@tonic-gate 		goto next;
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 	(void) vn_vfswlock_wait(vp);
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
23120Sstevel@tonic-gate 	if (vfsp != NULL) {
23130Sstevel@tonic-gate 		/*
23140Sstevel@tonic-gate 		 * Node is mounted on.
23150Sstevel@tonic-gate 		 */
23160Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tNode is mounted on\n"));
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 		/*
23190Sstevel@tonic-gate 		 * Deal with /xfn/host/jurassic alikes here...
23200Sstevel@tonic-gate 		 */
23210Sstevel@tonic-gate 		if (vfs_matchops(vfsp, vfs_getops(vp->v_vfsp))) {
23220Sstevel@tonic-gate 			/*
23230Sstevel@tonic-gate 			 * If the filesystem mounted here is AUTOFS, and it
23240Sstevel@tonic-gate 			 * is busy, try to unmount the tree rooted on it
23250Sstevel@tonic-gate 			 * first. We know this call to VFS_ROOT is safe to
23260Sstevel@tonic-gate 			 * call while holding VVFSLOCK, since it resolves
23270Sstevel@tonic-gate 			 * to a call to auto_root().
23280Sstevel@tonic-gate 			 */
23290Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\t\tAUTOFS mounted here\n"));
23300Sstevel@tonic-gate 			if (VFS_ROOT(vfsp, &newvp)) {
23310Sstevel@tonic-gate 				cmn_err(CE_PANIC,
23320Sstevel@tonic-gate 				    "unmount_tree: VFS_ROOT(vfs=%p) failed",
23330Sstevel@tonic-gate 				    (void *)vfsp);
23340Sstevel@tonic-gate 			}
23350Sstevel@tonic-gate 			nfnp = vntofn(newvp);
23360Sstevel@tonic-gate 			if (DEEPER(nfnp)) {
23370Sstevel@tonic-gate 				vn_vfsunlock(vp);
23380Sstevel@tonic-gate 				mutex_enter(&fnp->fn_lock);
23390Sstevel@tonic-gate 				AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
23400Sstevel@tonic-gate 				mutex_exit(&fnp->fn_lock);
23410Sstevel@tonic-gate 				VN_RELE(vp);
23420Sstevel@tonic-gate 				fnp = nfnp;
23430Sstevel@tonic-gate 				goto top;
23440Sstevel@tonic-gate 			}
23450Sstevel@tonic-gate 			/*
23460Sstevel@tonic-gate 			 * Fall through to unmount this filesystem
23470Sstevel@tonic-gate 			 */
23480Sstevel@tonic-gate 			VN_RELE(newvp);
23490Sstevel@tonic-gate 		}
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 		/*
23520Sstevel@tonic-gate 		 * vn_vfsunlock(vp) is done inside unmount_node()
23530Sstevel@tonic-gate 		 */
23540Sstevel@tonic-gate 		error = unmount_node(vp, force);
23550Sstevel@tonic-gate 		if (error == ECONNRESET) {
23560Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tConnection dropped\n"));
23570Sstevel@tonic-gate 			if (vn_mountedvfs(vp) == NULL) {
23580Sstevel@tonic-gate 				/*
23590Sstevel@tonic-gate 				 * The filesystem was unmounted before the
23600Sstevel@tonic-gate 				 * daemon died. Unfortunately we can not
23610Sstevel@tonic-gate 				 * determine whether all the cleanup work was
23620Sstevel@tonic-gate 				 * successfully finished (i.e. update mnttab,
23630Sstevel@tonic-gate 				 * or notify NFS server of the unmount).
23640Sstevel@tonic-gate 				 * We should not retry the operation since the
23650Sstevel@tonic-gate 				 * filesystem has already been unmounted, and
23660Sstevel@tonic-gate 				 * may have already been removed from mnttab,
23670Sstevel@tonic-gate 				 * in such case the devid/rdevid we send to
23680Sstevel@tonic-gate 				 * the daemon will not be matched. So we have
23692170Sevanl 				 * to be content with the partial unmount.
23700Sstevel@tonic-gate 				 * Since the mountpoint is no longer covered, we
23710Sstevel@tonic-gate 				 * clear the error condition.
23720Sstevel@tonic-gate 				 */
23730Sstevel@tonic-gate 				error = 0;
23742170Sevanl 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
23752170Sevanl 					CE_WARN,
23760Sstevel@tonic-gate 				    "unmount_tree: automountd connection "
23770Sstevel@tonic-gate 				    "dropped");
23780Sstevel@tonic-gate 				if (fnip->fi_flags & MF_DIRECT) {
23792170Sevanl 					auto_log(fngp->fng_verbose,
23802170Sevanl 						fngp->fng_zoneid, CE_WARN,
23812170Sevanl 						"unmount_tree: "
23820Sstevel@tonic-gate 					    "%s successfully unmounted - "
23830Sstevel@tonic-gate 					    "do not remount triggers",
23840Sstevel@tonic-gate 					    fnip->fi_path);
23850Sstevel@tonic-gate 				} else {
23862170Sevanl 					auto_log(fngp->fng_verbose,
23872170Sevanl 						fngp->fng_zoneid, CE_WARN,
23882170Sevanl 						"unmount_tree: "
23890Sstevel@tonic-gate 					    "%s/%s successfully unmounted - "
23900Sstevel@tonic-gate 					    "do not remount triggers",
23910Sstevel@tonic-gate 					    fnip->fi_path, fnp->fn_name);
23920Sstevel@tonic-gate 				}
23930Sstevel@tonic-gate 			}
23940Sstevel@tonic-gate 		}
23950Sstevel@tonic-gate 	} else {
23960Sstevel@tonic-gate 		vn_vfsunlock(vp);
23970Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tNode is AUTOFS\n"));
23980Sstevel@tonic-gate 		if (unmount_as_unit) {
23990Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tunmount as unit\n"));
24000Sstevel@tonic-gate 			error = unmount_autofs(vp);
24010Sstevel@tonic-gate 		} else {
24020Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tunmount one at a time\n"));
24030Sstevel@tonic-gate 			rw_enter(&fnp->fn_rwlock, RW_READER);
24040Sstevel@tonic-gate 			if (fnp->fn_dirents != NULL) {
24050Sstevel@tonic-gate 				/*
24060Sstevel@tonic-gate 				 * Has subdirectory, attempt their
24070Sstevel@tonic-gate 				 * unmount first
24080Sstevel@tonic-gate 				 */
24090Sstevel@tonic-gate 				nfnp = fnp->fn_dirents;
24100Sstevel@tonic-gate 				VN_HOLD(fntovn(nfnp));
24110Sstevel@tonic-gate 				rw_exit(&fnp->fn_rwlock);
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 				mutex_enter(&fnp->fn_lock);
24140Sstevel@tonic-gate 				AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
24150Sstevel@tonic-gate 				mutex_exit(&fnp->fn_lock);
24160Sstevel@tonic-gate 				VN_RELE(vp);
24170Sstevel@tonic-gate 				fnp = nfnp;
24180Sstevel@tonic-gate 				goto top;
24190Sstevel@tonic-gate 			}
24200Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
24210Sstevel@tonic-gate 			goto next;
24220Sstevel@tonic-gate 		}
24230Sstevel@tonic-gate 	}
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	if (error) {
24260Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tUnmount failed\n"));
24270Sstevel@tonic-gate 		if (alp != NULL) {
24280Sstevel@tonic-gate 			/*
24290Sstevel@tonic-gate 			 * Unmount failed, got to remount triggers.
24300Sstevel@tonic-gate 			 */
24310Sstevel@tonic-gate 			ASSERT((fnp->fn_flags & MF_THISUID_MATCH_RQD) == 0);
24322170Sevanl 			error = auto_perform_actions(fnip, fnp, alp,
24332170Sevanl 				CRED());
24340Sstevel@tonic-gate 			if (error) {
24352170Sevanl 				auto_log(fngp->fng_verbose,
24362170Sevanl 					fngp->fng_zoneid, CE_WARN,
24372170Sevanl 					"autofs: can't remount "
24380Sstevel@tonic-gate 				    "triggers fnp=%p error=%d", (void *)fnp,
24390Sstevel@tonic-gate 				    error);
24400Sstevel@tonic-gate 				error = 0;
24410Sstevel@tonic-gate 				/*
24420Sstevel@tonic-gate 				 * The action list should have been
24432170Sevanl 				 * free'd by auto_perform_actions
24440Sstevel@tonic-gate 				 * since an error occured
24450Sstevel@tonic-gate 				 */
24460Sstevel@tonic-gate 				alp = NULL;
24472170Sevanl 
24480Sstevel@tonic-gate 			}
24490Sstevel@tonic-gate 		}
24500Sstevel@tonic-gate 	} else {
24510Sstevel@tonic-gate 		/*
24520Sstevel@tonic-gate 		 * The unmount succeeded, which will cause this node to
24530Sstevel@tonic-gate 		 * be removed from its parent if its an indirect mount,
24540Sstevel@tonic-gate 		 * therefore update the parent's atime and mtime now.
24550Sstevel@tonic-gate 		 * I don't update them in auto_disconnect() because I
24560Sstevel@tonic-gate 		 * don't want atime and mtime changing every time a
24570Sstevel@tonic-gate 		 * lookup goes to the daemon and creates a new node.
24580Sstevel@tonic-gate 		 */
24590Sstevel@tonic-gate 		unmount_done = 1;
24600Sstevel@tonic-gate 		if ((fnip->fi_flags & MF_DIRECT) == 0) {
24610Sstevel@tonic-gate 			gethrestime(&now);
24620Sstevel@tonic-gate 			if (fnp->fn_parent == fngp->fng_rootfnnodep)
24630Sstevel@tonic-gate 				fnp->fn_atime = fnp->fn_mtime = now;
24640Sstevel@tonic-gate 			else
24650Sstevel@tonic-gate 				fnp->fn_parent->fn_atime =
24660Sstevel@tonic-gate 					fnp->fn_parent->fn_mtime = now;
24670Sstevel@tonic-gate 		}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 		/*
24700Sstevel@tonic-gate 		 * Free the action list here
24710Sstevel@tonic-gate 		 */
24720Sstevel@tonic-gate 		if (alp != NULL) {
24730Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
2474*3901Skr143551 			kmem_free(alp, sizeof (*alp));
24750Sstevel@tonic-gate 			alp = NULL;
24760Sstevel@tonic-gate 		}
24770Sstevel@tonic-gate 	}
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	fnp->fn_ref_time = gethrestime_sec();
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate next:
24820Sstevel@tonic-gate 	/*
24830Sstevel@tonic-gate 	 * Obtain parent's readers lock before grabbing
24840Sstevel@tonic-gate 	 * reference to next sibling.
24850Sstevel@tonic-gate 	 * XXX Note that nodes in the top level list (mounted
24860Sstevel@tonic-gate 	 * in user space not by the daemon in the kernel) parent is itself,
24870Sstevel@tonic-gate 	 * therefore grabbing the lock makes no sense, but doesn't
24880Sstevel@tonic-gate 	 * hurt either.
24890Sstevel@tonic-gate 	 */
24900Sstevel@tonic-gate 	pfnp = fnp->fn_parent;
24910Sstevel@tonic-gate 	ASSERT(pfnp != NULL);
24920Sstevel@tonic-gate 	rw_enter(&pfnp->fn_rwlock, RW_READER);
24930Sstevel@tonic-gate 	if ((nfnp = fnp->fn_next) != NULL)
24940Sstevel@tonic-gate 		VN_HOLD(fntovn(nfnp));
24950Sstevel@tonic-gate 	rw_exit(&pfnp->fn_rwlock);
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	if (ilocked_it) {
24980Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
24990Sstevel@tonic-gate 		if (unmount_done) {
25000Sstevel@tonic-gate 			/*
25010Sstevel@tonic-gate 			 * Other threads may be waiting for this unmount to
25020Sstevel@tonic-gate 			 * finish. We must let it know that in order to
25030Sstevel@tonic-gate 			 * proceed, it must trigger the mount itself.
25040Sstevel@tonic-gate 			 */
25050Sstevel@tonic-gate 			fnp->fn_flags &= ~MF_IK_MOUNT;
25060Sstevel@tonic-gate 			if (fnp->fn_flags & MF_WAITING)
25070Sstevel@tonic-gate 				fnp->fn_error = EAGAIN;
25080Sstevel@tonic-gate 			unmount_done = 0;
25090Sstevel@tonic-gate 		}
25100Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
25110Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
25120Sstevel@tonic-gate 		ilocked_it = 0;
25130Sstevel@tonic-gate 	}
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	if (nfnp != NULL) {
25160Sstevel@tonic-gate 		VN_RELE(vp);
25170Sstevel@tonic-gate 		fnp = nfnp;
25180Sstevel@tonic-gate 		/*
25190Sstevel@tonic-gate 		 * Unmount next element
25200Sstevel@tonic-gate 		 */
25210Sstevel@tonic-gate 		goto top;
25220Sstevel@tonic-gate 	}
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	/*
25250Sstevel@tonic-gate 	 * We don't want to unmount rootfnnodep, so the check is made here
25260Sstevel@tonic-gate 	 */
25270Sstevel@tonic-gate 	ASSERT(pfnp != fnp);
25280Sstevel@tonic-gate 	if (pfnp != fngp->fng_rootfnnodep) {
25290Sstevel@tonic-gate 		/*
25300Sstevel@tonic-gate 		 * Now attempt to unmount my parent
25310Sstevel@tonic-gate 		 */
25320Sstevel@tonic-gate 		VN_HOLD(fntovn(pfnp));
25330Sstevel@tonic-gate 		VN_RELE(vp);
25340Sstevel@tonic-gate 		fnp = pfnp;
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 		goto top;
25370Sstevel@tonic-gate 	}
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	VN_RELE(vp);
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	/*
25420Sstevel@tonic-gate 	 * At this point we've walked the entire tree and attempted to unmount
25430Sstevel@tonic-gate 	 * as much as we can one level at a time.
25440Sstevel@tonic-gate 	 */
25450Sstevel@tonic-gate done:
25460Sstevel@tonic-gate 	mutex_enter(&unmount_tree_cpr_lock);
25470Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
25480Sstevel@tonic-gate 	mutex_destroy(&unmount_tree_cpr_lock);
25490Sstevel@tonic-gate }
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate static void
25520Sstevel@tonic-gate unmount_zone_tree(struct autofs_globals *fngp)
25530Sstevel@tonic-gate {
25540Sstevel@tonic-gate 	unmount_tree(fngp, 0);
25550Sstevel@tonic-gate 	mutex_enter(&fngp->fng_unmount_threads_lock);
25560Sstevel@tonic-gate 	fngp->fng_unmount_threads--;
25570Sstevel@tonic-gate 	mutex_exit(&fngp->fng_unmount_threads_lock);
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "unmount_tree done. Thread exiting.\n"));
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	zthread_exit();
25620Sstevel@tonic-gate 	/* NOTREACHED */
25630Sstevel@tonic-gate }
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate static int autofs_unmount_thread_timer = 120;	/* in seconds */
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate void
25680Sstevel@tonic-gate auto_do_unmount(struct autofs_globals *fngp)
25690Sstevel@tonic-gate {
25700Sstevel@tonic-gate 	callb_cpr_t cprinfo;
25710Sstevel@tonic-gate 	clock_t timeleft;
25720Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &fngp->fng_unmount_threads_lock,
25750Sstevel@tonic-gate 		callb_generic_cpr, "auto_do_unmount");
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	for (;;) {	/* forever */
25780Sstevel@tonic-gate 		mutex_enter(&fngp->fng_unmount_threads_lock);
25790Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
25800Sstevel@tonic-gate newthread:
25810Sstevel@tonic-gate 		mutex_exit(&fngp->fng_unmount_threads_lock);
25820Sstevel@tonic-gate 		timeleft = zone_status_timedwait(zone, lbolt +
25830Sstevel@tonic-gate 		    autofs_unmount_thread_timer * hz, ZONE_IS_SHUTTING_DOWN);
25840Sstevel@tonic-gate 		mutex_enter(&fngp->fng_unmount_threads_lock);
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 		if (timeleft != -1) {	/* didn't time out */
25870Sstevel@tonic-gate 			ASSERT(zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN);
25880Sstevel@tonic-gate 			/*
25890Sstevel@tonic-gate 			 * zone is exiting... don't create any new threads.
25900Sstevel@tonic-gate 			 * fng_unmount_threads_lock is released implicitly by
25910Sstevel@tonic-gate 			 * the below.
25920Sstevel@tonic-gate 			 */
25930Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo,
25940Sstevel@tonic-gate 				&fngp->fng_unmount_threads_lock);
25950Sstevel@tonic-gate 			CALLB_CPR_EXIT(&cprinfo);
25960Sstevel@tonic-gate 			zthread_exit();
25970Sstevel@tonic-gate 			/* NOTREACHED */
25980Sstevel@tonic-gate 		}
25990Sstevel@tonic-gate 		if (fngp->fng_unmount_threads < autofs_unmount_threads) {
26000Sstevel@tonic-gate 			fngp->fng_unmount_threads++;
26010Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo,
26020Sstevel@tonic-gate 				&fngp->fng_unmount_threads_lock);
26030Sstevel@tonic-gate 			mutex_exit(&fngp->fng_unmount_threads_lock);
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 			(void) zthread_create(NULL, 0, unmount_zone_tree, fngp,
26060Sstevel@tonic-gate 			    0, minclsyspri);
26070Sstevel@tonic-gate 		} else
26080Sstevel@tonic-gate 			goto newthread;
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate 	/* NOTREACHED */
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate /*
26140Sstevel@tonic-gate  * Is nobrowse specified in option string?
26150Sstevel@tonic-gate  * opts should be a null ('\0') terminated string.
26160Sstevel@tonic-gate  * Returns non-zero if nobrowse has been specified.
26170Sstevel@tonic-gate  */
26180Sstevel@tonic-gate int
26190Sstevel@tonic-gate auto_nobrowse_option(char *opts)
26200Sstevel@tonic-gate {
26210Sstevel@tonic-gate 	char *buf;
26220Sstevel@tonic-gate 	char *p;
26230Sstevel@tonic-gate 	char *t;
26240Sstevel@tonic-gate 	int nobrowse = 0;
26250Sstevel@tonic-gate 	int last_opt = 0;
26260Sstevel@tonic-gate 	size_t len;
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	len = strlen(opts) + 1;
26290Sstevel@tonic-gate 	p = buf = kmem_alloc(len, KM_SLEEP);
26300Sstevel@tonic-gate 	(void) strcpy(buf, opts);
26310Sstevel@tonic-gate 	do {
26320Sstevel@tonic-gate 		if (t = strchr(p, ','))
26330Sstevel@tonic-gate 			*t++ = '\0';
26340Sstevel@tonic-gate 		else
26350Sstevel@tonic-gate 			last_opt++;
26360Sstevel@tonic-gate 		if (strcmp(p, MNTOPT_NOBROWSE) == 0)
26370Sstevel@tonic-gate 			nobrowse = 1;
26380Sstevel@tonic-gate 		else if (strcmp(p, MNTOPT_BROWSE) == 0)
26390Sstevel@tonic-gate 			nobrowse = 0;
26400Sstevel@tonic-gate 		p = t;
26410Sstevel@tonic-gate 	} while (!last_opt);
26420Sstevel@tonic-gate 	kmem_free(buf, len);
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	return (nobrowse);
26450Sstevel@tonic-gate }
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate /*
26480Sstevel@tonic-gate  * used to log warnings only if automountd is running
26490Sstevel@tonic-gate  * with verbose mode set
26500Sstevel@tonic-gate  */
26512170Sevanl 
26520Sstevel@tonic-gate void
26532170Sevanl auto_log(int verbose, zoneid_t zoneid, int level, const char *fmt, ...)
26540Sstevel@tonic-gate {
26552170Sevanl 	va_list	args;
26560Sstevel@tonic-gate 
26572170Sevanl 	if (verbose) {
26580Sstevel@tonic-gate 		va_start(args, fmt);
26592170Sevanl 		vzcmn_err(zoneid, level, fmt, args);
26600Sstevel@tonic-gate 		va_end(args);
26610Sstevel@tonic-gate 	}
26620Sstevel@tonic-gate }
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate #ifdef DEBUG
26650Sstevel@tonic-gate static int autofs_debug = 0;
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate /*
26680Sstevel@tonic-gate  * Utilities used by both client and server
26690Sstevel@tonic-gate  * Standard levels:
26700Sstevel@tonic-gate  * 0) no debugging
26710Sstevel@tonic-gate  * 1) hard failures
26720Sstevel@tonic-gate  * 2) soft failures
26730Sstevel@tonic-gate  * 3) current test software
26740Sstevel@tonic-gate  * 4) main procedure entry points
26750Sstevel@tonic-gate  * 5) main procedure exit points
26760Sstevel@tonic-gate  * 6) utility procedure entry points
26770Sstevel@tonic-gate  * 7) utility procedure exit points
26780Sstevel@tonic-gate  * 8) obscure procedure entry points
26790Sstevel@tonic-gate  * 9) obscure procedure exit points
26800Sstevel@tonic-gate  * 10) random stuff
26810Sstevel@tonic-gate  * 11) all <= 1
26820Sstevel@tonic-gate  * 12) all <= 2
26830Sstevel@tonic-gate  * 13) all <= 3
26840Sstevel@tonic-gate  * ...
26850Sstevel@tonic-gate  */
26860Sstevel@tonic-gate /* PRINTFLIKE2 */
26870Sstevel@tonic-gate void
26880Sstevel@tonic-gate auto_dprint(int level, const char *fmt, ...)
26890Sstevel@tonic-gate {
26900Sstevel@tonic-gate 	va_list args;
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	if (autofs_debug == level ||
26930Sstevel@tonic-gate 	    (autofs_debug > 10 && (autofs_debug - 10) >= level)) {
26940Sstevel@tonic-gate 		va_start(args, fmt);
26950Sstevel@tonic-gate 		(void) vprintf(fmt, args);
26960Sstevel@tonic-gate 		va_end(args);
26970Sstevel@tonic-gate 	}
26980Sstevel@tonic-gate }
26990Sstevel@tonic-gate #endif /* DEBUG */
2700