xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_subr.c (revision 12883:5c6de9ad51b9)
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  */
215891Sraf 
220Sstevel@tonic-gate /*
2312184SJan.Kryl@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/kmem.h>
280Sstevel@tonic-gate #include <sys/errno.h>
290Sstevel@tonic-gate #include <sys/proc.h>
300Sstevel@tonic-gate #include <sys/disp.h>
310Sstevel@tonic-gate #include <sys/vfs.h>
320Sstevel@tonic-gate #include <sys/vnode.h>
330Sstevel@tonic-gate #include <sys/pathname.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/mount.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/debug.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/dirent.h>
400Sstevel@tonic-gate #include <fs/fs_subr.h>
410Sstevel@tonic-gate #include <sys/fs/autofs.h>
420Sstevel@tonic-gate #include <sys/callb.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
440Sstevel@tonic-gate #include <sys/zone.h>
452170Sevanl #include <sys/door.h>
460Sstevel@tonic-gate #include <sys/fs/mntdata.h>
472170Sevanl #include <nfs/mount.h>
482170Sevanl #include <rpc/clnt.h>
492170Sevanl #include <rpcsvc/autofs_prot.h>
502170Sevanl #include <nfs/rnode.h>
512170Sevanl #include <sys/utsname.h>
525891Sraf #include <sys/schedctl.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Autofs and Zones:
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * Zones are delegated the responsibility of managing their own autofs mounts
580Sstevel@tonic-gate  * and maps.  Each zone runs its own copy of automountd, with its own timeouts,
590Sstevel@tonic-gate  * and other logically "global" parameters.  kRPC and virtualization in the
600Sstevel@tonic-gate  * loopback transport (tl) will prevent a zone from communicating with another
610Sstevel@tonic-gate  * zone's automountd.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * Each zone has its own "rootfnnode" and associated tree of auto nodes.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * Each zone also has its own set of "unmounter" kernel threads; these are
660Sstevel@tonic-gate  * created and run within the zone's context (ie, they are created via
670Sstevel@tonic-gate  * zthread_create()).
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Cross-zone mount triggers are disallowed.  There is a check in
700Sstevel@tonic-gate  * auto_trigger_mount() to this effect; EPERM is returned to indicate that the
710Sstevel@tonic-gate  * mount is not owned by the caller.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * autofssys() enables a caller in the global zone to clean up in-kernel (as
740Sstevel@tonic-gate  * well as regular) autofs mounts via the unmount_tree() mechanism.  This is
750Sstevel@tonic-gate  * routinely done when all mounts are removed as part of zone shutdown.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate #define	TYPICALMAXPATHLEN	64
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static kmutex_t autofs_nodeid_lock;
800Sstevel@tonic-gate 
8112184SJan.Kryl@Sun.COM /* max number of unmount threads running */
8212184SJan.Kryl@Sun.COM static int autofs_unmount_threads = 5;
8312184SJan.Kryl@Sun.COM static int autofs_unmount_thread_timer = 120;	/* in seconds */
8412184SJan.Kryl@Sun.COM 
850Sstevel@tonic-gate static int auto_perform_link(fnnode_t *, struct linka *, cred_t *);
860Sstevel@tonic-gate static int auto_perform_actions(fninfo_t *, fnnode_t *,
870Sstevel@tonic-gate     action_list *, cred_t *);
880Sstevel@tonic-gate static int auto_getmntpnt(vnode_t *, char *, vnode_t **, cred_t *);
890Sstevel@tonic-gate static int auto_lookup_request(fninfo_t *, char *, struct linka *,
903391Ssemery     bool_t, bool_t *, cred_t *);
913391Ssemery static int auto_mount_request(fninfo_t *, char *, action_list **, cred_t *,
922170Sevanl     bool_t);
932170Sevanl 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * Clears the MF_INPROG flag, and wakes up those threads sleeping on
960Sstevel@tonic-gate  * fn_cv_mount if MF_WAITING is set.
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate void
auto_unblock_others(fnnode_t * fnp,uint_t operation)990Sstevel@tonic-gate auto_unblock_others(
1000Sstevel@tonic-gate 	fnnode_t *fnp,
1010Sstevel@tonic-gate 	uint_t operation)		/* either MF_INPROG or MF_LOOKUP */
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	ASSERT(operation & (MF_INPROG | MF_LOOKUP));
1040Sstevel@tonic-gate 	fnp->fn_flags &= ~operation;
1050Sstevel@tonic-gate 	if (fnp->fn_flags & MF_WAITING) {
1060Sstevel@tonic-gate 		fnp->fn_flags &= ~MF_WAITING;
1070Sstevel@tonic-gate 		cv_broadcast(&fnp->fn_cv_mount);
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate int
auto_wait4mount(fnnode_t * fnp)1120Sstevel@tonic-gate auto_wait4mount(fnnode_t *fnp)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	int error;
1150Sstevel@tonic-gate 	k_sigset_t smask;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_wait4mount: fnp=%p\n", (void *)fnp));
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
1200Sstevel@tonic-gate 	while (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
1210Sstevel@tonic-gate 		/*
1220Sstevel@tonic-gate 		 * There is a mount or a lookup in progress.
1230Sstevel@tonic-gate 		 */
1240Sstevel@tonic-gate 		fnp->fn_flags |= MF_WAITING;
1250Sstevel@tonic-gate 		sigintr(&smask, 1);
1260Sstevel@tonic-gate 		if (!cv_wait_sig(&fnp->fn_cv_mount, &fnp->fn_lock)) {
1270Sstevel@tonic-gate 			/*
1280Sstevel@tonic-gate 			 * Decided not to wait for operation to
1290Sstevel@tonic-gate 			 * finish after all.
1300Sstevel@tonic-gate 			 */
1310Sstevel@tonic-gate 			sigunintr(&smask);
1320Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1330Sstevel@tonic-gate 			return (EINTR);
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 		sigunintr(&smask);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	error = fnp->fn_error;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (error == EINTR) {
1400Sstevel@tonic-gate 		/*
1410Sstevel@tonic-gate 		 * The thread doing the mount got interrupted, we need to
1420Sstevel@tonic-gate 		 * try again, by returning EAGAIN.
1430Sstevel@tonic-gate 		 */
1440Sstevel@tonic-gate 		error = EAGAIN;
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_wait4mount: fnp=%p error=%d\n", (void *)fnp,
1490Sstevel@tonic-gate 	    error));
1500Sstevel@tonic-gate 	return (error);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
auto_lookup_aux(fnnode_t * fnp,char * name,cred_t * cred)1540Sstevel@tonic-gate auto_lookup_aux(fnnode_t *fnp, char *name, cred_t *cred)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	struct fninfo *fnip;
1570Sstevel@tonic-gate 	struct linka link;
1580Sstevel@tonic-gate 	bool_t mountreq = FALSE;
1590Sstevel@tonic-gate 	int error = 0;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	fnip = vfstofni(fntovn(fnp)->v_vfsp);
1620Sstevel@tonic-gate 	bzero(&link, sizeof (link));
1633391Ssemery 	error = auto_lookup_request(fnip, name, &link, TRUE, &mountreq, cred);
1640Sstevel@tonic-gate 	if (!error) {
1652170Sevanl 		if (link.link != NULL || link.link != '\0') {
1660Sstevel@tonic-gate 			/*
1670Sstevel@tonic-gate 			 * This node should be a symlink
1680Sstevel@tonic-gate 			 */
1690Sstevel@tonic-gate 			error = auto_perform_link(fnp, &link, cred);
1700Sstevel@tonic-gate 		} else if (mountreq) {
1710Sstevel@tonic-gate 			/*
1720Sstevel@tonic-gate 			 * The automount daemon is requesting a mount,
1730Sstevel@tonic-gate 			 * implying this entry must be a wildcard match and
1740Sstevel@tonic-gate 			 * therefore in need of verification that the entry
1750Sstevel@tonic-gate 			 * exists on the server.
1760Sstevel@tonic-gate 			 */
1770Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
1780Sstevel@tonic-gate 			AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
1790Sstevel@tonic-gate 			fnp->fn_error = 0;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 			/*
1820Sstevel@tonic-gate 			 * Unblock other lookup requests on this node,
1830Sstevel@tonic-gate 			 * this is needed to let the lookup generated by
1840Sstevel@tonic-gate 			 * the mount call to complete. The caveat is
1850Sstevel@tonic-gate 			 * other lookups on this node can also get by,
1860Sstevel@tonic-gate 			 * i.e., another lookup on this node that occurs
1870Sstevel@tonic-gate 			 * while this lookup is attempting the mount
1880Sstevel@tonic-gate 			 * would return a positive result no matter what.
1890Sstevel@tonic-gate 			 * Therefore two lookups on the this node could
1900Sstevel@tonic-gate 			 * potentially get disparate results.
1910Sstevel@tonic-gate 			 */
1920Sstevel@tonic-gate 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
1930Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1940Sstevel@tonic-gate 			/*
1950Sstevel@tonic-gate 			 * auto_new_mount_thread fires up a new thread which
1960Sstevel@tonic-gate 			 * calls automountd finishing up the work
1970Sstevel@tonic-gate 			 */
1980Sstevel@tonic-gate 			auto_new_mount_thread(fnp, name, cred);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 			/*
2010Sstevel@tonic-gate 			 * At this point, we are simply another thread
2020Sstevel@tonic-gate 			 * waiting for the mount to complete
2030Sstevel@tonic-gate 			 */
2040Sstevel@tonic-gate 			error = auto_wait4mount(fnp);
2050Sstevel@tonic-gate 			if (error == AUTOFS_SHUTDOWN)
2060Sstevel@tonic-gate 				error = ENOENT;
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2102170Sevanl 	if (link.link)
2112170Sevanl 		kmem_free(link.link, strlen(link.link) + 1);
2122170Sevanl 	if (link.dir)
2132170Sevanl 		kmem_free(link.dir, strlen(link.dir) + 1);
2140Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
2150Sstevel@tonic-gate 	fnp->fn_error = error;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * Notify threads waiting for lookup/mount that
2190Sstevel@tonic-gate 	 * it's done.
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	if (mountreq) {
2220Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2230Sstevel@tonic-gate 	} else {
2240Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
2270Sstevel@tonic-gate 	return (error);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Starting point for thread to handle mount requests with automountd.
2320Sstevel@tonic-gate  * XXX auto_mount_thread() is not suspend-safe within the scope of
2330Sstevel@tonic-gate  * the present model defined for cpr to suspend the system. Calls
2340Sstevel@tonic-gate  * made by the auto_mount_thread() that have been identified to be unsafe
2350Sstevel@tonic-gate  * are (1) RPC client handle setup and client calls to automountd which
2360Sstevel@tonic-gate  * can block deep down in the RPC library, (2) kmem_alloc() calls with the
2370Sstevel@tonic-gate  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*(), and
2380Sstevel@tonic-gate  * lookuppnvp() calls which can result in over the wire calls to servers.
2390Sstevel@tonic-gate  * The thread should be completely reevaluated to make it suspend-safe in
2400Sstevel@tonic-gate  * case of future updates to the cpr model.
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate static void
auto_mount_thread(struct autofs_callargs * argsp)2430Sstevel@tonic-gate auto_mount_thread(struct autofs_callargs *argsp)
2440Sstevel@tonic-gate {
2452170Sevanl 	struct fninfo 		*fnip;
2462170Sevanl 	fnnode_t 		*fnp;
2472170Sevanl 	vnode_t 		*vp;
2482170Sevanl 	char 			*name;
2492170Sevanl 	size_t 			namelen;
2502170Sevanl 	cred_t 			*cred;
2512170Sevanl 	action_list		*alp = NULL;
2522170Sevanl 	int 			error;
2532170Sevanl 	callb_cpr_t 		cprinfo;
2542170Sevanl 	kmutex_t 		auto_mount_thread_cpr_lock;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	mutex_init(&auto_mount_thread_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
2572170Sevanl 	CALLB_CPR_INIT(&cprinfo, &auto_mount_thread_cpr_lock,
2586046Srmesta 	    callb_generic_cpr, "auto_mount_thread");
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	fnp = argsp->fnc_fnp;
2610Sstevel@tonic-gate 	vp = fntovn(fnp);
2620Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
2630Sstevel@tonic-gate 	name = argsp->fnc_name;
2640Sstevel@tonic-gate 	cred = argsp->fnc_cred;
2650Sstevel@tonic-gate 	ASSERT(crgetzoneid(argsp->fnc_cred) == fnip->fi_zoneid);
2660Sstevel@tonic-gate 
2673391Ssemery 	error = auto_mount_request(fnip, name, &alp, cred, TRUE);
2680Sstevel@tonic-gate 	if (!error)
2690Sstevel@tonic-gate 		error = auto_perform_actions(fnip, fnp, alp, cred);
2700Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
2710Sstevel@tonic-gate 	fnp->fn_error = error;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/*
2740Sstevel@tonic-gate 	 * Notify threads waiting for mount that
2750Sstevel@tonic-gate 	 * it's done.
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2780Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	VN_RELE(vp);
2810Sstevel@tonic-gate 	crfree(argsp->fnc_cred);
2820Sstevel@tonic-gate 	namelen = strlen(argsp->fnc_name) + 1;
2830Sstevel@tonic-gate 	kmem_free(argsp->fnc_name, namelen);
2840Sstevel@tonic-gate 	kmem_free(argsp, sizeof (*argsp));
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	mutex_enter(&auto_mount_thread_cpr_lock);
2870Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
2880Sstevel@tonic-gate 	mutex_destroy(&auto_mount_thread_cpr_lock);
2890Sstevel@tonic-gate 	zthread_exit();
2900Sstevel@tonic-gate 	/* NOTREACHED */
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static int autofs_thr_success = 0;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * Creates new thread which calls auto_mount_thread which does
2970Sstevel@tonic-gate  * the bulk of the work calling automountd, via 'auto_perform_actions'.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate void
auto_new_mount_thread(fnnode_t * fnp,char * name,cred_t * cred)3000Sstevel@tonic-gate auto_new_mount_thread(fnnode_t *fnp, char *name, cred_t *cred)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	struct autofs_callargs *argsp;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	argsp = kmem_alloc(sizeof (*argsp), KM_SLEEP);
3050Sstevel@tonic-gate 	VN_HOLD(fntovn(fnp));
3060Sstevel@tonic-gate 	argsp->fnc_fnp = fnp;
3070Sstevel@tonic-gate 	argsp->fnc_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
3080Sstevel@tonic-gate 	(void) strcpy(argsp->fnc_name, name);
3090Sstevel@tonic-gate 	argsp->fnc_origin = curthread;
3100Sstevel@tonic-gate 	crhold(cred);
3110Sstevel@tonic-gate 	argsp->fnc_cred = cred;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, auto_mount_thread, argsp, 0,
3140Sstevel@tonic-gate 	    minclsyspri);
3150Sstevel@tonic-gate 	autofs_thr_success++;
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3186046Srmesta #define	DOOR_BUF_ALIGN		(1024*1024)
3196046Srmesta #define	DOOR_BUF_MULTIPLIER	3
3206046Srmesta #define	DOOR_BUF_DEFAULT_SZ	(DOOR_BUF_MULTIPLIER * DOOR_BUF_ALIGN)
3216046Srmesta int	doorbuf_defsz = DOOR_BUF_DEFAULT_SZ;
3226046Srmesta 
3236046Srmesta /*ARGSUSED*/
3240Sstevel@tonic-gate int
auto_calldaemon(zoneid_t zoneid,int which,xdrproc_t xarg_func,void * argsp,xdrproc_t xresp_func,void * resp,int reslen,bool_t hard)3250Sstevel@tonic-gate auto_calldaemon(
3262170Sevanl 	zoneid_t 		zoneid,
3272170Sevanl 	int			which,
3282170Sevanl 	xdrproc_t		xarg_func,
3292170Sevanl 	void 			*argsp,
3302170Sevanl 	xdrproc_t		xresp_func,
3312170Sevanl 	void 			*resp,
3322170Sevanl 	int			reslen,
3332170Sevanl 	bool_t 			hard)	/* retry forever? */
3340Sstevel@tonic-gate {
3356046Srmesta 	int			 retry;
3366046Srmesta 	int			 error = 0;
3376046Srmesta 	k_sigset_t		 smask;
3386046Srmesta 	door_arg_t		 door_args;
3396046Srmesta 	door_handle_t		 dh;
3406046Srmesta 	XDR			 xdrarg;
3416046Srmesta 	XDR			 xdrres;
3422170Sevanl 	struct autofs_globals 	*fngp = NULL;
3436046Srmesta 	void			*orp = NULL;
3446046Srmesta 	int			 orl;
3456060Srmesta 	int			 rlen = 0;	/* MUST be initialized */
3462170Sevanl 	autofs_door_args_t	*xdr_argsp;
3476046Srmesta 	int			 xdr_len = 0;
3486046Srmesta 	int			 printed_not_running_msg = 0;
3494092Sevanl 	klwp_t			*lwp = ttolwp(curthread);
3502170Sevanl 
3512170Sevanl 	/*
3522170Sevanl 	 * We know that the current thread is doing work on
3532170Sevanl 	 * behalf of its own zone, so it's ok to use
3542170Sevanl 	 * curproc->p_zone.
3552170Sevanl 	 */
3562170Sevanl 	ASSERT(zoneid == getzoneid());
3576046Srmesta 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) {
3582170Sevanl 		/*
3592170Sevanl 		 * There's no point in trying to talk to
3602170Sevanl 		 * automountd.  Plus, zone_shutdown() is
3612170Sevanl 		 * waiting for us.
3622170Sevanl 		 */
3632170Sevanl 		return (ECONNREFUSED);
3642170Sevanl 	}
3650Sstevel@tonic-gate 
3664092Sevanl 	do {
3674092Sevanl 		retry = 0;
3684092Sevanl 		mutex_enter(&autofs_minor_lock);
3694092Sevanl 		fngp = zone_getspecific(autofs_key, curproc->p_zone);
3704092Sevanl 		mutex_exit(&autofs_minor_lock);
3714092Sevanl 		if (fngp == NULL) {
3724092Sevanl 			if (hard) {
3734092Sevanl 				AUTOFS_DPRINT((5,
3744092Sevanl 				    "auto_calldaemon: "\
3754092Sevanl 				    "failed to get door handle\n"));
3764092Sevanl 				if (!printed_not_running_msg) {
3774092Sevanl 					printed_not_running_msg = 1;
3784092Sevanl 					zprintf(zoneid, "automountd not "\
3794092Sevanl 					    "running, retrying\n");
3804092Sevanl 				}
3814092Sevanl 				delay(hz);
3824092Sevanl 				retry = 1;
3834092Sevanl 			} else {
3844092Sevanl 				/*
3854092Sevanl 				 * There is no global data so no door.
3864092Sevanl 				 * There's no point in attempting to talk
3874092Sevanl 				 * to automountd if we can't get the door
3884092Sevanl 				 * handle.
3894092Sevanl 				 */
3904092Sevanl 				return (ECONNREFUSED);
3914092Sevanl 			}
3924092Sevanl 		}
3934092Sevanl 	} while (retry);
3944092Sevanl 
3954092Sevanl 	if (printed_not_running_msg) {
3964092Sevanl 		fngp->fng_printed_not_running_msg = printed_not_running_msg;
3972170Sevanl 	}
3982170Sevanl 
3992170Sevanl 	ASSERT(fngp != NULL);
4002170Sevanl 
4012170Sevanl 	if (argsp != NULL && (xdr_len = xdr_sizeof(xarg_func, argsp)) == 0)
4022170Sevanl 		return (EINVAL);
4032170Sevanl 	xdr_argsp = kmem_zalloc(xdr_len + sizeof (*xdr_argsp), KM_SLEEP);
4042170Sevanl 	xdr_argsp->xdr_len = xdr_len;
4052170Sevanl 	xdr_argsp->cmd = which;
4062170Sevanl 
4072170Sevanl 	if (argsp) {
4082170Sevanl 		xdrmem_create(&xdrarg, (char *)&xdr_argsp->xdr_arg,
4096046Srmesta 		    xdr_argsp->xdr_len, XDR_ENCODE);
4102170Sevanl 
4112170Sevanl 		if (!(*xarg_func)(&xdrarg, argsp)) {
4122170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
4132170Sevanl 			return (EINVAL);
4142170Sevanl 		}
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4182170Sevanl 	 * We're saving off the original pointer and length due to the
4192170Sevanl 	 * possibility that the results buffer returned by the door
4202170Sevanl 	 * upcall can be different then what we passed in. This is because
4212170Sevanl 	 * the door will allocate new memory if the results buffer passed
4222170Sevanl 	 * in isn't large enough to hold what we need to send back.
4232170Sevanl 	 * In this case we need to free the memory originally allocated
4242170Sevanl 	 * for that buffer.
4250Sstevel@tonic-gate 	 */
4266046Srmesta 	if (resp)
4276046Srmesta 		rlen = xdr_sizeof(xresp_func, resp);
4286046Srmesta 	orl = (rlen == 0) ? doorbuf_defsz : MAX(rlen, doorbuf_defsz);
4296046Srmesta 	orp = kmem_zalloc(orl, KM_SLEEP);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	do {
4322170Sevanl 		retry = 0;
4332170Sevanl 		mutex_enter(&fngp->fng_autofs_daemon_lock);
4342170Sevanl 		dh = fngp->fng_autofs_daemon_dh;
4352170Sevanl 		if (dh)
4362170Sevanl 			door_ki_hold(dh);
4372170Sevanl 		mutex_exit(&fngp->fng_autofs_daemon_lock);
4380Sstevel@tonic-gate 
4392170Sevanl 		if (dh == NULL) {
4406046Srmesta 			if (orp)
4416046Srmesta 				kmem_free(orp, orl);
4422170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
4432170Sevanl 			return (ENOENT);
4442170Sevanl 		}
4452170Sevanl 		door_args.data_ptr = (char *)xdr_argsp;
4462170Sevanl 		door_args.data_size = sizeof (*xdr_argsp) + xdr_argsp->xdr_len;
4472170Sevanl 		door_args.desc_ptr = NULL;
4482170Sevanl 		door_args.desc_num = 0;
4496046Srmesta 		door_args.rbuf = orp ? (char *)orp : NULL;
4506046Srmesta 		door_args.rsize = orl;
4510Sstevel@tonic-gate 
4522170Sevanl 		sigintr(&smask, 1);
4536997Sjwadams 		error =
4546997Sjwadams 		    door_ki_upcall_limited(dh, &door_args, NULL, SIZE_MAX, 0);
4550Sstevel@tonic-gate 		sigunintr(&smask);
4560Sstevel@tonic-gate 
4572170Sevanl 		door_ki_rele(dh);
4580Sstevel@tonic-gate 
4596046Srmesta 		/*
4606046Srmesta 		 * Handle daemon errors
4616046Srmesta 		 */
4622170Sevanl 		if (!error) {
4636046Srmesta 			/*
4646046Srmesta 			 * Upcall successful. Let's check for soft errors
4656046Srmesta 			 * from the daemon. We only recover from overflow
4666046Srmesta 			 * type scenarios. Any other errors, we return to
4676046Srmesta 			 * the caller.
4686046Srmesta 			 */
4692170Sevanl 			autofs_door_res_t *adr =
4706046Srmesta 			    (autofs_door_res_t *)door_args.rbuf;
4716046Srmesta 
4726046Srmesta 			if (door_args.rbuf != NULL) {
4736046Srmesta 				int	 nl;
4746046Srmesta 
4756046Srmesta 				switch (error = adr->res_status) {
4766046Srmesta 				case 0:	/* no error; continue */
4776046Srmesta 					break;
4786046Srmesta 
4796046Srmesta 				case EOVERFLOW:
4806046Srmesta 					/*
4816046Srmesta 					 * orig landing buf not big enough.
4826046Srmesta 					 * xdr_len in XDR_BYTES_PER_UNIT
4836046Srmesta 					 */
4846046Srmesta 					if ((nl = adr->xdr_len) > 0 &&
4856046Srmesta 					    (btopr(nl) < freemem/64)) {
4866046Srmesta 						if (orp)
4876046Srmesta 							kmem_free(orp, orl);
4886046Srmesta 						orp = kmem_zalloc(nl, KM_SLEEP);
4896046Srmesta 						orl = nl;
4906046Srmesta 						retry = 1;
4916046Srmesta 						break;
4926046Srmesta 					}
4936046Srmesta 					/*FALLTHROUGH*/
4946046Srmesta 
4956046Srmesta 				default:
4966046Srmesta 					kmem_free(xdr_argsp,
4976046Srmesta 					    xdr_len + sizeof (*xdr_argsp));
4986046Srmesta 					if (orp)
4996046Srmesta 						kmem_free(orp, orl);
5006046Srmesta 					return (error);
5016046Srmesta 				}
5022170Sevanl 			}
5032170Sevanl 			continue;
5042170Sevanl 		}
5056046Srmesta 
5066046Srmesta 		/*
5076046Srmesta 		 * no daemon errors; now process door/comm errors (if any)
5086046Srmesta 		 */
5092170Sevanl 		switch (error) {
5102170Sevanl 		case EINTR:
5110Sstevel@tonic-gate 			/*
5122170Sevanl 			 * interrupts should be handled properly by the
5134092Sevanl 			 * door upcall. If the door doesn't handle the
5144092Sevanl 			 * interupt completely then we need to bail out.
5154092Sevanl 			 */
5164092Sevanl 			if (lwp && (ISSIG(curthread,
5174092Sevanl 			    JUSTLOOKING) || MUSTRETURN(curproc, curthread))) {
5184092Sevanl 				if (ISSIG(curthread, FORREAL) ||
5194092Sevanl 				    lwp->lwp_sysabort ||
5204092Sevanl 				    MUSTRETURN(curproc, curthread)) {
5214092Sevanl 					lwp->lwp_sysabort = 0;
5224092Sevanl 					return (EINTR);
5234092Sevanl 				}
5244092Sevanl 			}
5254092Sevanl 			/*
5262170Sevanl 			 * We may have gotten EINTR for other reasons
5272170Sevanl 			 * like the door being revoked on us. Instead
5282170Sevanl 			 * of trying to extract this out of the door
5292170Sevanl 			 * handle, sleep and try again, if still
5302170Sevanl 			 * revoked we will get EBADF next time
5312170Sevanl 			 * through.
5325891Sraf 			 *
5335891Sraf 			 * If we have a pending cancellation and we don't
5345891Sraf 			 * have cancellation disabled, we will get EINTR
5355891Sraf 			 * forever, no matter how many times we retry,
5365891Sraf 			 * so just get out now if this is the case.
5370Sstevel@tonic-gate 			 */
5385891Sraf 			if (schedctl_cancel_pending())
5395891Sraf 				break;
5404092Sevanl 			/* FALLTHROUGH */
5412170Sevanl 		case EAGAIN:    /* process may be forking */
5422170Sevanl 			/*
5432170Sevanl 			 * Back off for a bit
5442170Sevanl 			 */
5452170Sevanl 			delay(hz);
5462170Sevanl 			retry = 1;
5472170Sevanl 			break;
5482170Sevanl 		case EBADF:	/* Invalid door */
5492170Sevanl 		case EINVAL:    /* Not a door, wrong target */
5500Sstevel@tonic-gate 			/*
5512170Sevanl 			 * A fatal door error, if our failing door
5522170Sevanl 			 * handle is the current door handle, clean
5532170Sevanl 			 * up our state.
5540Sstevel@tonic-gate 			 */
5552170Sevanl 			mutex_enter(&fngp->fng_autofs_daemon_lock);
5562170Sevanl 			if (dh == fngp->fng_autofs_daemon_dh) {
5572170Sevanl 				door_ki_rele(fngp->fng_autofs_daemon_dh);
5582170Sevanl 				fngp->fng_autofs_daemon_dh = NULL;
5590Sstevel@tonic-gate 			}
5602170Sevanl 			mutex_exit(&fngp->fng_autofs_daemon_lock);
5616046Srmesta 			AUTOFS_DPRINT((5, "auto_calldaemon error=%d\n", error));
5622170Sevanl 			if (hard) {
5632170Sevanl 				if (!fngp->fng_printed_not_running_msg) {
5646046Srmesta 					fngp->fng_printed_not_running_msg = 1;
5656046Srmesta 					zprintf(zoneid, "automountd not "
5666046Srmesta 					    "running, retrying\n");
5672170Sevanl 				}
5682170Sevanl 				delay(hz);
5692170Sevanl 				retry = 1;
5702170Sevanl 				break;
5712170Sevanl 			} else {
5722170Sevanl 				error = ECONNREFUSED;
5732170Sevanl 				kmem_free(xdr_argsp,
5746046Srmesta 				    xdr_len + sizeof (*xdr_argsp));
5756046Srmesta 				if (orp)
5766046Srmesta 					kmem_free(orp, orl);
5772170Sevanl 				return (error);
5782170Sevanl 			}
5792170Sevanl 		default:	/* Unknown must be fatal */
5800Sstevel@tonic-gate 			error = ENOENT;
5812170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
5826046Srmesta 			if (orp)
5836046Srmesta 				kmem_free(orp, orl);
5842170Sevanl 			return (error);
5850Sstevel@tonic-gate 		}
5862170Sevanl 	} while (retry);
5870Sstevel@tonic-gate 
5882170Sevanl 	if (fngp->fng_printed_not_running_msg == 1) {
5892170Sevanl 		fngp->fng_printed_not_running_msg = 0;
5902170Sevanl 		zprintf(zoneid, "automountd OK\n");
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 
5936046Srmesta 	if (orp && orl) {
5942170Sevanl 		autofs_door_res_t	*door_resp;
5956046Srmesta 		door_resp = (autofs_door_res_t *)door_args.rbuf;
5966046Srmesta 
5976046Srmesta 		if ((void *)door_args.rbuf != orp)
5986046Srmesta 			kmem_free(orp, orl);
5996046Srmesta 
6002170Sevanl 		xdrmem_create(&xdrres, (char *)&door_resp->xdr_res,
6016046Srmesta 		    door_resp->xdr_len, XDR_DECODE);
6026046Srmesta 
6032170Sevanl 		if (!((*xresp_func)(&xdrres, resp)))
6042170Sevanl 			error = EINVAL;
6052170Sevanl 		kmem_free(door_args.rbuf, door_args.rsize);
6062170Sevanl 	}
6072170Sevanl 	kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
6080Sstevel@tonic-gate 	return (error);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate static int
auto_null_request(zoneid_t zoneid,bool_t hard)61212184SJan.Kryl@Sun.COM auto_null_request(zoneid_t zoneid, bool_t hard)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	int error;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tauto_null_request\n"));
6170Sstevel@tonic-gate 
61812184SJan.Kryl@Sun.COM 	error = auto_calldaemon(zoneid, NULLPROC,
6196046Srmesta 	    xdr_void, NULL, xdr_void, NULL, 0, hard);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tauto_null_request: error=%d\n", error));
6220Sstevel@tonic-gate 	return (error);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate static int
auto_lookup_request(fninfo_t * fnip,char * key,struct linka * lnp,bool_t hard,bool_t * mountreq,cred_t * cred)6260Sstevel@tonic-gate auto_lookup_request(
6270Sstevel@tonic-gate 	fninfo_t *fnip,
6280Sstevel@tonic-gate 	char *key,
6290Sstevel@tonic-gate 	struct linka *lnp,
6300Sstevel@tonic-gate 	bool_t hard,
6313391Ssemery 	bool_t *mountreq,
6323391Ssemery 	cred_t *cred)
6330Sstevel@tonic-gate {
6342170Sevanl 	int 				error;
6352170Sevanl 	struct autofs_globals 		*fngp;
6366046Srmesta 	struct autofs_lookupargs	 reqst;
6372170Sevanl 	autofs_lookupres 		*resp;
6382170Sevanl 	struct linka 			*p;
6390Sstevel@tonic-gate 
6402170Sevanl 
6412170Sevanl 	AUTOFS_DPRINT((4, "auto_lookup_equest: path=%s name=%s\n",
6420Sstevel@tonic-gate 	    fnip->fi_path, key));
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
6452170Sevanl 
6462170Sevanl 	reqst.map = fnip->fi_map;
6472170Sevanl 	reqst.path = fnip->fi_path;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	if (fnip->fi_flags & MF_DIRECT)
6502170Sevanl 		reqst.name = fnip->fi_key;
6510Sstevel@tonic-gate 	else
6522170Sevanl 		reqst.name = key;
6532170Sevanl 	AUTOFS_DPRINT((4, "auto_lookup_request: using key=%s\n", reqst.name));
6540Sstevel@tonic-gate 
6552170Sevanl 	reqst.subdir = fnip->fi_subdir;
6562170Sevanl 	reqst.opts = fnip->fi_opts;
6572170Sevanl 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
6583391Ssemery 	reqst.uid = crgetuid(cred);
6592170Sevanl 
6602170Sevanl 	resp = kmem_zalloc(sizeof (*resp), KM_SLEEP);
6610Sstevel@tonic-gate 
6626046Srmesta 	error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_LOOKUP,
6636046Srmesta 	    xdr_autofs_lookupargs, &reqst, xdr_autofs_lookupres,
6646046Srmesta 	    (void *)resp, sizeof (autofs_lookupres), hard);
6652170Sevanl 
6662170Sevanl 	if (error) {
6672170Sevanl 		xdr_free(xdr_autofs_lookupres, (char *)resp);
6682170Sevanl 		kmem_free(resp, sizeof (*resp));
6692170Sevanl 		return (error);
6702170Sevanl 	}
6712170Sevanl 
6720Sstevel@tonic-gate 	if (!error) {
6732170Sevanl 		fngp->fng_verbose = resp->lu_verbose;
6742170Sevanl 		switch (resp->lu_res) {
6750Sstevel@tonic-gate 		case AUTOFS_OK:
6762170Sevanl 			switch (resp->lu_type.action) {
6770Sstevel@tonic-gate 			case AUTOFS_MOUNT_RQ:
6780Sstevel@tonic-gate 				lnp->link = NULL;
6790Sstevel@tonic-gate 				lnp->dir = NULL;
6800Sstevel@tonic-gate 				*mountreq = TRUE;
6810Sstevel@tonic-gate 				break;
6826046Srmesta 
6830Sstevel@tonic-gate 			case AUTOFS_LINK_RQ:
6846046Srmesta 			p = &resp->lu_type.lookup_result_type_u.lt_linka;
6850Sstevel@tonic-gate 				lnp->dir = kmem_alloc(strlen(p->dir) + 1,
6866046Srmesta 				    KM_SLEEP);
6870Sstevel@tonic-gate 				(void) strcpy(lnp->dir, p->dir);
6880Sstevel@tonic-gate 				lnp->link = kmem_alloc(strlen(p->link) + 1,
6896046Srmesta 				    KM_SLEEP);
6900Sstevel@tonic-gate 				(void) strcpy(lnp->link, p->link);
6910Sstevel@tonic-gate 				break;
6926046Srmesta 
6930Sstevel@tonic-gate 			case AUTOFS_NONE:
6940Sstevel@tonic-gate 				lnp->link = NULL;
6950Sstevel@tonic-gate 				lnp->dir = NULL;
6960Sstevel@tonic-gate 				break;
6976046Srmesta 
6980Sstevel@tonic-gate 			default:
6996046Srmesta 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
7006046Srmesta 				    CE_WARN, "auto_lookup_request: bad action "
7016046Srmesta 				    "type %d", resp->lu_res);
7020Sstevel@tonic-gate 				error = ENOENT;
7030Sstevel@tonic-gate 			}
7040Sstevel@tonic-gate 			break;
7056046Srmesta 
7060Sstevel@tonic-gate 		case AUTOFS_NOENT:
7070Sstevel@tonic-gate 			error = ENOENT;
7080Sstevel@tonic-gate 			break;
7096046Srmesta 
7100Sstevel@tonic-gate 		default:
7110Sstevel@tonic-gate 			error = ENOENT;
7122170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
7130Sstevel@tonic-gate 			    "auto_lookup_request: unknown result: %d",
7142170Sevanl 			    resp->lu_res);
7150Sstevel@tonic-gate 			break;
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate done:
7192170Sevanl 	xdr_free(xdr_autofs_lookupres, (char *)resp);
7202170Sevanl 	kmem_free(resp, sizeof (*resp));
7210Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_lookup_request: path=%s name=%s error=%d\n",
7220Sstevel@tonic-gate 	    fnip->fi_path, key, error));
7230Sstevel@tonic-gate 	return (error);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate static int
auto_mount_request(fninfo_t * fnip,char * key,action_list ** alpp,cred_t * cred,bool_t hard)7270Sstevel@tonic-gate auto_mount_request(
7280Sstevel@tonic-gate 	fninfo_t *fnip,
7290Sstevel@tonic-gate 	char *key,
7300Sstevel@tonic-gate 	action_list **alpp,
7313391Ssemery 	cred_t *cred,
7320Sstevel@tonic-gate 	bool_t hard)
7330Sstevel@tonic-gate {
7342170Sevanl 	int 			error;
7352170Sevanl 	struct autofs_globals 	*fngp;
7362170Sevanl 	autofs_lookupargs 	reqst;
7372170Sevanl 	autofs_mountres		*xdrres = NULL;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mount_request: path=%s name=%s\n",
7400Sstevel@tonic-gate 	    fnip->fi_path, key));
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
7432170Sevanl 	reqst.map = fnip->fi_map;
7442170Sevanl 	reqst.path = fnip->fi_path;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (fnip->fi_flags & MF_DIRECT)
7472170Sevanl 		reqst.name = fnip->fi_key;
7480Sstevel@tonic-gate 	else
7492170Sevanl 		reqst.name = key;
7502170Sevanl 
7512170Sevanl 	AUTOFS_DPRINT((4, "auto_mount_request: using key=%s\n", reqst.name));
7520Sstevel@tonic-gate 
7532170Sevanl 	reqst.subdir = fnip->fi_subdir;
7542170Sevanl 	reqst.opts = fnip->fi_opts;
7552170Sevanl 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
7563391Ssemery 	reqst.uid = crgetuid(cred);
7572170Sevanl 
7582170Sevanl 	xdrres = kmem_zalloc(sizeof (*xdrres), KM_SLEEP);
7590Sstevel@tonic-gate 
7606046Srmesta 	error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_MNTINFO,
7616046Srmesta 	    xdr_autofs_lookupargs, &reqst, xdr_autofs_mountres,
7626046Srmesta 	    (void *)xdrres, sizeof (autofs_mountres), hard);
7632170Sevanl 
7640Sstevel@tonic-gate 	if (!error) {
7652170Sevanl 		fngp->fng_verbose = xdrres->mr_verbose;
7662170Sevanl 		switch (xdrres->mr_type.status) {
7670Sstevel@tonic-gate 		case AUTOFS_ACTION:
7680Sstevel@tonic-gate 			error = 0;
7690Sstevel@tonic-gate 			/*
7700Sstevel@tonic-gate 			 * Save the action list since it is used by
7710Sstevel@tonic-gate 			 * the caller. We NULL the action list pointer
7720Sstevel@tonic-gate 			 * in 'result' so that xdr_free() will not free
7730Sstevel@tonic-gate 			 * the list.
7740Sstevel@tonic-gate 			 */
7752170Sevanl 			*alpp = xdrres->mr_type.mount_result_type_u.list;
7762170Sevanl 			xdrres->mr_type.mount_result_type_u.list = NULL;
7770Sstevel@tonic-gate 			break;
7780Sstevel@tonic-gate 		case AUTOFS_DONE:
7792170Sevanl 			error = xdrres->mr_type.mount_result_type_u.error;
7800Sstevel@tonic-gate 			break;
7810Sstevel@tonic-gate 		default:
7820Sstevel@tonic-gate 			error = ENOENT;
7832170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
7840Sstevel@tonic-gate 			    "auto_mount_request: unknown status %d",
7852170Sevanl 			    xdrres->mr_type.status);
7860Sstevel@tonic-gate 			break;
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7902170Sevanl 	xdr_free(xdr_autofs_mountres, (char *)xdrres);
7912170Sevanl 	kmem_free(xdrres, sizeof (*xdrres));
7922170Sevanl 
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount_request: path=%s name=%s error=%d\n",
7950Sstevel@tonic-gate 	    fnip->fi_path, key, error));
7960Sstevel@tonic-gate 	return (error);
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate static int
auto_send_unmount_request(fninfo_t * fnip,umntrequest * ul,bool_t hard)8010Sstevel@tonic-gate auto_send_unmount_request(
8020Sstevel@tonic-gate 	fninfo_t *fnip,
8030Sstevel@tonic-gate 	umntrequest *ul,
8040Sstevel@tonic-gate 	bool_t hard)
8050Sstevel@tonic-gate {
8062170Sevanl 	int 	error;
8072170Sevanl 	umntres	xdrres;
8082170Sevanl 
8092170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tauto_send_unmount_request: fstype=%s "
8126046Srmesta 	    " mntpnt=%s\n", ul->fstype, ul->mntpnt));
8130Sstevel@tonic-gate 
8142170Sevanl 	bzero(&xdrres, sizeof (umntres));
8156046Srmesta 	error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_UNMOUNT,
8166046Srmesta 	    xdr_umntrequest, (void *)ul, xdr_umntres, (void *)&xdrres,
8176046Srmesta 	    sizeof (umntres), hard);
8180Sstevel@tonic-gate 
8192395Sevanl 	if (!error)
8202395Sevanl 		error = xdrres.status;
8212395Sevanl 
8220Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tauto_send_unmount_request: error=%d\n", error));
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	return (error);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static int
auto_perform_link(fnnode_t * fnp,struct linka * linkp,cred_t * cred)8280Sstevel@tonic-gate auto_perform_link(fnnode_t *fnp, struct linka *linkp, cred_t *cred)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	vnode_t *vp;
8310Sstevel@tonic-gate 	size_t len;
8320Sstevel@tonic-gate 	char *tmp;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_perform_link: fnp=%p dir=%s link=%s\n",
8350Sstevel@tonic-gate 	    (void *)fnp, linkp->dir, linkp->link));
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	len = strlen(linkp->link) + 1;		/* include '\0' */
8380Sstevel@tonic-gate 	tmp = kmem_zalloc(len, KM_SLEEP);
8390Sstevel@tonic-gate 	(void) kcopy(linkp->link, tmp, len);
8400Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
8410Sstevel@tonic-gate 	fnp->fn_symlink = tmp;
8420Sstevel@tonic-gate 	fnp->fn_symlinklen = (uint_t)len;
8430Sstevel@tonic-gate 	fnp->fn_flags |= MF_THISUID_MATCH_RQD;
8440Sstevel@tonic-gate 	crhold(cred);
8450Sstevel@tonic-gate 	fnp->fn_cred = cred;
8460Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	vp = fntovn(fnp);
8490Sstevel@tonic-gate 	vp->v_type = VLNK;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	return (0);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8544068Sdm120769 static void
auto_free_autofs_args(struct mounta * m)8554068Sdm120769 auto_free_autofs_args(struct mounta *m)
8564068Sdm120769 {
8574068Sdm120769 	autofs_args	*aargs = (autofs_args *)m->dataptr;
8584068Sdm120769 
8594068Sdm120769 	if (aargs->addr.buf)
8604068Sdm120769 		kmem_free(aargs->addr.buf, aargs->addr.len);
8614068Sdm120769 	if (aargs->path)
8624068Sdm120769 		kmem_free(aargs->path, strlen(aargs->path) + 1);
8634068Sdm120769 	if (aargs->opts)
8644068Sdm120769 		kmem_free(aargs->opts, strlen(aargs->opts) + 1);
8654068Sdm120769 	if (aargs->map)
8664068Sdm120769 		kmem_free(aargs->map, strlen(aargs->map) + 1);
8674068Sdm120769 	if (aargs->subdir)
8684068Sdm120769 		kmem_free(aargs->subdir, strlen(aargs->subdir) + 1);
8694068Sdm120769 	if (aargs->key)
8704068Sdm120769 		kmem_free(aargs->key, strlen(aargs->key) + 1);
8714068Sdm120769 	kmem_free(aargs, sizeof (*aargs));
8724068Sdm120769 }
8734068Sdm120769 
8744068Sdm120769 static void
auto_free_action_list(action_list * alp)8754068Sdm120769 auto_free_action_list(action_list *alp)
8764068Sdm120769 {
8774068Sdm120769 	struct	mounta	*m;
8784068Sdm120769 	action_list	*lastalp;
8794068Sdm120769 	char		*fstype;
8804068Sdm120769 
8814068Sdm120769 	m = &alp->action.action_list_entry_u.mounta;
8824068Sdm120769 	while (alp != NULL) {
8834068Sdm120769 		fstype = alp->action.action_list_entry_u.mounta.fstype;
8844068Sdm120769 		m = &alp->action.action_list_entry_u.mounta;
8854068Sdm120769 		if (m->dataptr) {
8864068Sdm120769 			if (strcmp(fstype, "autofs") == 0) {
8874068Sdm120769 				auto_free_autofs_args(m);
8884068Sdm120769 			}
8894068Sdm120769 		}
8904068Sdm120769 		if (m->spec)
8914068Sdm120769 			kmem_free(m->spec, strlen(m->spec) + 1);
8924068Sdm120769 		if (m->dir)
8934068Sdm120769 			kmem_free(m->dir, strlen(m->dir) + 1);
8944068Sdm120769 		if (m->fstype)
8954068Sdm120769 			kmem_free(m->fstype, strlen(m->fstype) + 1);
8964068Sdm120769 		if (m->optptr)
8974068Sdm120769 			kmem_free(m->optptr, m->optlen);
8984068Sdm120769 		lastalp = alp;
8994068Sdm120769 		alp = alp->next;
9004068Sdm120769 		kmem_free(lastalp, sizeof (*lastalp));
9014068Sdm120769 	}
9024068Sdm120769 }
9034068Sdm120769 
9040Sstevel@tonic-gate static boolean_t
auto_invalid_autofs(fninfo_t * dfnip,fnnode_t * dfnp,action_list * p)9052170Sevanl auto_invalid_autofs(fninfo_t *dfnip, fnnode_t *dfnp, action_list *p)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	struct mounta *m;
9080Sstevel@tonic-gate 	struct autofs_args *argsp;
9090Sstevel@tonic-gate 	vnode_t *dvp;
9100Sstevel@tonic-gate 	char buff[AUTOFS_MAXPATHLEN];
9110Sstevel@tonic-gate 	size_t len;
9120Sstevel@tonic-gate 	struct autofs_globals *fngp;
9134068Sdm120769 
9140Sstevel@tonic-gate 	fngp = dfnp->fn_globals;
9150Sstevel@tonic-gate 	dvp = fntovn(dfnp);
9162170Sevanl 
9170Sstevel@tonic-gate 	m = &p->action.action_list_entry_u.mounta;
9180Sstevel@tonic-gate 	/*
9190Sstevel@tonic-gate 	 * Make sure we aren't geting passed NULL values or a "dir" that
9200Sstevel@tonic-gate 	 * isn't "." and doesn't begin with "./".
9210Sstevel@tonic-gate 	 *
9220Sstevel@tonic-gate 	 * We also only want to perform autofs mounts, so make sure
9230Sstevel@tonic-gate 	 * no-one is trying to trick us into doing anything else.
9240Sstevel@tonic-gate 	 */
9254068Sdm120769 	if (m->spec == NULL || m->dir == NULL || m->dir[0] != '.' ||
9260Sstevel@tonic-gate 	    (m->dir[1] != '/' && m->dir[1] != '\0') ||
9274068Sdm120769 	    m->fstype == NULL || strcmp(m->fstype, "autofs") != 0 ||
9284068Sdm120769 	    m->dataptr == NULL || m->datalen != sizeof (struct autofs_args) ||
9290Sstevel@tonic-gate 	    m->optptr == NULL)
9300Sstevel@tonic-gate 		return (B_TRUE);
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * We also don't like ".."s in the pathname.  Symlinks are
9330Sstevel@tonic-gate 	 * handled by the fact that we'll use NOFOLLOW when we do
9340Sstevel@tonic-gate 	 * lookup()s.
9350Sstevel@tonic-gate 	 */
9360Sstevel@tonic-gate 	if (strstr(m->dir, "/../") != NULL ||
9370Sstevel@tonic-gate 	    (len = strlen(m->dir)) > sizeof ("/..") - 1 &&
9380Sstevel@tonic-gate 	    m->dir[len] == '.' && m->dir[len - 1] == '.' &&
9390Sstevel@tonic-gate 	    m->dir[len - 2] == '/')
9400Sstevel@tonic-gate 		return (B_TRUE);
9410Sstevel@tonic-gate 	argsp = (struct autofs_args *)m->dataptr;
9420Sstevel@tonic-gate 	/*
9430Sstevel@tonic-gate 	 * We don't want NULL values here either.
9440Sstevel@tonic-gate 	 */
9450Sstevel@tonic-gate 	if (argsp->addr.buf == NULL || argsp->path == NULL ||
9460Sstevel@tonic-gate 	    argsp->opts == NULL || argsp->map == NULL || argsp->subdir == NULL)
9470Sstevel@tonic-gate 		return (B_TRUE);
9480Sstevel@tonic-gate 	/*
9490Sstevel@tonic-gate 	 * We know what the claimed pathname *should* look like:
9500Sstevel@tonic-gate 	 *
9510Sstevel@tonic-gate 	 * If the parent (dfnp) is a mount point (VROOT), then
9520Sstevel@tonic-gate 	 * the path should be (dfnip->fi_path + m->dir).
9530Sstevel@tonic-gate 	 *
9540Sstevel@tonic-gate 	 * Else, we know we're only two levels deep, so we use
9550Sstevel@tonic-gate 	 * (dfnip->fi_path + dfnp->fn_name + m->dir).
9560Sstevel@tonic-gate 	 *
9570Sstevel@tonic-gate 	 * Furthermore, "." only makes sense if dfnp is a
9580Sstevel@tonic-gate 	 * trigger node.
9590Sstevel@tonic-gate 	 *
9600Sstevel@tonic-gate 	 * At this point it seems like the passed-in path is
9610Sstevel@tonic-gate 	 * redundant.
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	if (dvp->v_flag & VROOT) {
9640Sstevel@tonic-gate 		if (m->dir[1] == '\0' && !(dfnp->fn_flags & MF_TRIGGER))
9650Sstevel@tonic-gate 			return (B_TRUE);
9660Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s%s",
9670Sstevel@tonic-gate 		    dfnip->fi_path, m->dir + 1);
9680Sstevel@tonic-gate 	} else {
9690Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s/%s%s",
9700Sstevel@tonic-gate 		    dfnip->fi_path, dfnp->fn_name, m->dir + 1);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 	if (strcmp(argsp->path, buff) != 0) {
9732170Sevanl 		auto_log(fngp->fng_verbose, fngp->fng_zoneid,
9742170Sevanl 		    CE_WARN, "autofs: expected path of '%s', "
9750Sstevel@tonic-gate 		    "got '%s' instead.", buff, argsp->path);
9760Sstevel@tonic-gate 		return (B_TRUE);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	return (B_FALSE); /* looks OK */
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9812170Sevanl /*
9822170Sevanl  * auto_invalid_action will validate the action_list received.  If all is good
9834068Sdm120769  * this function returns FALSE, if there is a problem it returns TRUE.
9842170Sevanl  */
9852170Sevanl static boolean_t
auto_invalid_action(fninfo_t * dfnip,fnnode_t * dfnp,action_list * alistpp)9862170Sevanl auto_invalid_action(fninfo_t *dfnip, fnnode_t *dfnp, action_list *alistpp)
9872170Sevanl {
9882170Sevanl 
9892170Sevanl 	/*
9902170Sevanl 	 * Before we go any further, this better be a mount request.
9912170Sevanl 	 */
9922170Sevanl 	if (alistpp->action.action != AUTOFS_MOUNT_RQ)
9932170Sevanl 		return (B_TRUE);
9944068Sdm120769 	return (auto_invalid_autofs(dfnip, dfnp, alistpp));
9952170Sevanl 
9962170Sevanl }
9972170Sevanl 
9980Sstevel@tonic-gate static int
auto_perform_actions(fninfo_t * dfnip,fnnode_t * dfnp,action_list * alp,cred_t * cred)9990Sstevel@tonic-gate auto_perform_actions(
10000Sstevel@tonic-gate 	fninfo_t *dfnip,
10010Sstevel@tonic-gate 	fnnode_t *dfnp,
10020Sstevel@tonic-gate 	action_list *alp,
10030Sstevel@tonic-gate 	cred_t *cred)	/* Credentials of the caller */
10040Sstevel@tonic-gate {
10052170Sevanl 
10060Sstevel@tonic-gate 	action_list *p;
10072170Sevanl 	struct mounta		*m, margs;
10082170Sevanl 	struct autofs_args 		*argsp;
10092170Sevanl 	int 			error, success = 0;
10102170Sevanl 	vnode_t 		*mvp, *dvp, *newvp;
10112170Sevanl 	fnnode_t 		*newfnp, *mfnp;
10122170Sevanl 	int 			auto_mount = 0;
10132170Sevanl 	int 			save_triggers = 0;
10142170Sevanl 	int 			update_times = 0;
10152170Sevanl 	char 			*mntpnt;
10162170Sevanl 	char 			buff[AUTOFS_MAXPATHLEN];
10172170Sevanl 	timestruc_t 		now;
10182170Sevanl 	struct autofs_globals 	*fngp;
10192170Sevanl 	cred_t 			*zcred;
10200Sstevel@tonic-gate 
10216046Srmesta 	AUTOFS_DPRINT((4, "auto_perform_actions: alp=%p\n", (void *)alp));
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	fngp = dfnp->fn_globals;
10240Sstevel@tonic-gate 	dvp = fntovn(dfnp);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * As automountd running in a zone may be compromised, and this may be
10280Sstevel@tonic-gate 	 * an attack, we can't trust everything passed in by automountd, and we
10290Sstevel@tonic-gate 	 * need to do argument verification.  We'll issue a warning and drop
10300Sstevel@tonic-gate 	 * the request if it doesn't seem right.
10310Sstevel@tonic-gate 	 */
10322170Sevanl 
10330Sstevel@tonic-gate 	for (p = alp; p != NULL; p = p->next) {
10340Sstevel@tonic-gate 		if (auto_invalid_action(dfnip, dfnp, p)) {
10350Sstevel@tonic-gate 			/*
10360Sstevel@tonic-gate 			 * This warning should be sent to the global zone,
10370Sstevel@tonic-gate 			 * since presumably the zone administrator is the same
10380Sstevel@tonic-gate 			 * as the attacker.
10390Sstevel@tonic-gate 			 */
10400Sstevel@tonic-gate 			cmn_err(CE_WARN, "autofs: invalid action list received "
10410Sstevel@tonic-gate 			    "by automountd in zone %s.",
10420Sstevel@tonic-gate 			    curproc->p_zone->zone_name);
10430Sstevel@tonic-gate 			/*
10440Sstevel@tonic-gate 			 * This conversation is over.
10450Sstevel@tonic-gate 			 */
10460Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
10470Sstevel@tonic-gate 			return (EINVAL);
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	zcred = zone_get_kcred(getzoneid());
10520Sstevel@tonic-gate 	ASSERT(zcred != NULL);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	if (vn_mountedvfs(dvp) != NULL) {
10550Sstevel@tonic-gate 		/*
10560Sstevel@tonic-gate 		 * The daemon successfully mounted a filesystem
10570Sstevel@tonic-gate 		 * on the AUTOFS root node.
10580Sstevel@tonic-gate 		 */
10590Sstevel@tonic-gate 		mutex_enter(&dfnp->fn_lock);
10600Sstevel@tonic-gate 		dfnp->fn_flags |= MF_MOUNTPOINT;
10610Sstevel@tonic-gate 		ASSERT(dfnp->fn_dirents == NULL);
10620Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
10630Sstevel@tonic-gate 		success++;
10640Sstevel@tonic-gate 	} else {
10650Sstevel@tonic-gate 		/*
10660Sstevel@tonic-gate 		 * Clear MF_MOUNTPOINT.
10670Sstevel@tonic-gate 		 */
10680Sstevel@tonic-gate 		mutex_enter(&dfnp->fn_lock);
10690Sstevel@tonic-gate 		if (dfnp->fn_flags & MF_MOUNTPOINT) {
10700Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "autofs: clearing mountpoint "
10716046Srmesta 			    "flag on %s.", dfnp->fn_name));
10720Sstevel@tonic-gate 			ASSERT(dfnp->fn_dirents == NULL);
10730Sstevel@tonic-gate 			ASSERT(dfnp->fn_trigger == NULL);
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 		dfnp->fn_flags &= ~MF_MOUNTPOINT;
10760Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	for (p = alp; p != NULL; p = p->next) {
10802170Sevanl 
10810Sstevel@tonic-gate 		vfs_t *vfsp;	/* dummy argument */
10820Sstevel@tonic-gate 		vfs_t *mvfsp;
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		auto_mount = 0;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		m = &p->action.action_list_entry_u.mounta;
10870Sstevel@tonic-gate 		argsp = (struct autofs_args *)m->dataptr;
10884068Sdm120769 		ASSERT(strcmp(m->fstype, "autofs") == 0);
10894068Sdm120769 		/*
10904068Sdm120769 		 * use the parent directory's timeout since it's the
10914068Sdm120769 		 * one specified/inherited by automount.
10924068Sdm120769 		 */
10934068Sdm120769 		argsp->mount_to = dfnip->fi_mount_to;
10944068Sdm120769 		/*
10954068Sdm120769 		 * The mountpoint is relative, and it is guaranteed to
10964068Sdm120769 		 * begin with "."
10974068Sdm120769 		 *
10984068Sdm120769 		 */
10994068Sdm120769 		ASSERT(m->dir[0] == '.');
11004068Sdm120769 		if (m->dir[0] == '.' && m->dir[1] == '\0') {
11010Sstevel@tonic-gate 			/*
11024068Sdm120769 			 * mounting on the trigger node
11030Sstevel@tonic-gate 			 */
11044068Sdm120769 			mvp = dvp;
11054068Sdm120769 			VN_HOLD(mvp);
11064068Sdm120769 			goto mount;
11074068Sdm120769 		}
11084068Sdm120769 		/*
11094068Sdm120769 		 * ignore "./" in front of mountpoint
11104068Sdm120769 		 */
11114068Sdm120769 		ASSERT(m->dir[1] == '/');
11124068Sdm120769 		mntpnt = m->dir + 2;
11130Sstevel@tonic-gate 
11144068Sdm120769 		AUTOFS_DPRINT((10, "\tdfnip->fi_path=%s\n", dfnip->fi_path));
11154068Sdm120769 		AUTOFS_DPRINT((10, "\tdfnip->fi_flags=%x\n", dfnip->fi_flags));
11164068Sdm120769 		AUTOFS_DPRINT((10, "\tmntpnt=%s\n", mntpnt));
11170Sstevel@tonic-gate 
11184068Sdm120769 		if (dfnip->fi_flags & MF_DIRECT) {
11194068Sdm120769 			AUTOFS_DPRINT((10, "\tDIRECT\n"));
11206046Srmesta 			(void) sprintf(buff, "%s/%s", dfnip->fi_path, mntpnt);
11214068Sdm120769 		} else {
11224068Sdm120769 			AUTOFS_DPRINT((10, "\tINDIRECT\n"));
11234068Sdm120769 			(void) sprintf(buff, "%s/%s/%s",
11246046Srmesta 			    dfnip->fi_path, dfnp->fn_name, mntpnt);
11254068Sdm120769 		}
11260Sstevel@tonic-gate 
11274068Sdm120769 		if (vn_mountedvfs(dvp) == NULL) {
11284068Sdm120769 			/*
11294068Sdm120769 			 * Daemon didn't mount anything on the root
11304068Sdm120769 			 * We have to create the mountpoint if it
11314068Sdm120769 			 * doesn't exist already
11324068Sdm120769 			 *
11334068Sdm120769 			 * We use the caller's credentials in case a
11344068Sdm120769 			 * UID-match is required
11354068Sdm120769 			 * (MF_THISUID_MATCH_RQD).
11364068Sdm120769 			 */
11374068Sdm120769 			rw_enter(&dfnp->fn_rwlock, RW_WRITER);
11384068Sdm120769 			error = auto_search(dfnp, mntpnt, &mfnp, cred);
11394068Sdm120769 			if (error == 0) {
11400Sstevel@tonic-gate 				/*
11414068Sdm120769 				 * AUTOFS mountpoint exists
11420Sstevel@tonic-gate 				 */
11434068Sdm120769 				if (vn_mountedvfs(fntovn(mfnp)) != NULL) {
11444068Sdm120769 					cmn_err(CE_PANIC,
11456046Srmesta 					    "auto_perform_actions:"
11466046Srmesta 					    " mfnp=%p covered", (void *)mfnp);
11470Sstevel@tonic-gate 				}
11480Sstevel@tonic-gate 			} else {
11490Sstevel@tonic-gate 				/*
11504068Sdm120769 				 * Create AUTOFS mountpoint
11510Sstevel@tonic-gate 				 */
11524068Sdm120769 				ASSERT((dfnp->fn_flags & MF_MOUNTPOINT) == 0);
11534068Sdm120769 				error = auto_enter(dfnp, mntpnt, &mfnp, cred);
11544068Sdm120769 				ASSERT(mfnp->fn_linkcnt == 1);
11554068Sdm120769 				mfnp->fn_linkcnt++;
11564068Sdm120769 			}
11574068Sdm120769 			if (!error)
11584068Sdm120769 				update_times = 1;
11594068Sdm120769 			rw_exit(&dfnp->fn_rwlock);
11604068Sdm120769 			ASSERT(error != EEXIST);
11614068Sdm120769 			if (!error) {
11624068Sdm120769 				/*
11634068Sdm120769 				 * mfnp is already held.
11644068Sdm120769 				 */
11654068Sdm120769 				mvp = fntovn(mfnp);
11664068Sdm120769 			} else {
11674068Sdm120769 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
11686046Srmesta 				    CE_WARN, "autofs: mount of %s "
11696046Srmesta 				    "failed - can't create"
11706046Srmesta 				    " mountpoint.", buff);
11716046Srmesta 				continue;
11720Sstevel@tonic-gate 			}
11734068Sdm120769 		} else {
11744068Sdm120769 			/*
11754068Sdm120769 			 * Find mountpoint in VFS mounted here. If not
11764068Sdm120769 			 * found, fail the submount, though the overall
11774068Sdm120769 			 * mount has succeeded since the root is
11784068Sdm120769 			 * mounted.
11794068Sdm120769 			 */
11806046Srmesta 			if (error = auto_getmntpnt(dvp, mntpnt, &mvp, kcred)) {
11816046Srmesta 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
11826046Srmesta 				    CE_WARN, "autofs: mount of %s "
11836046Srmesta 				    "failed - mountpoint doesn't"
11846046Srmesta 				    " exist.", buff);
11854068Sdm120769 				continue;
11864068Sdm120769 			}
11874068Sdm120769 			if (mvp->v_type == VLNK) {
11886046Srmesta 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
11896046Srmesta 				    CE_WARN, "autofs: %s symbolic "
11906046Srmesta 				    "link: not a valid mountpoint "
11916046Srmesta 				    "- mount failed", buff);
11924068Sdm120769 				VN_RELE(mvp);
11934068Sdm120769 				error = ENOENT;
11944068Sdm120769 				continue;
11954068Sdm120769 			}
11964068Sdm120769 		}
11970Sstevel@tonic-gate mount:
11984068Sdm120769 		m->flags |= MS_SYSSPACE | MS_OPTIONSTR;
11994068Sdm120769 
12004068Sdm120769 		/*
12014068Sdm120769 		 * Copy mounta struct here so we can substitute a
12024068Sdm120769 		 * buffer that is large enough to hold the returned
12034068Sdm120769 		 * option string, if that string is longer than the
12044068Sdm120769 		 * input option string.
12054068Sdm120769 		 * This can happen if there are default options enabled
12064068Sdm120769 		 * that were not in the input option string.
12074068Sdm120769 		 */
12084068Sdm120769 		bcopy(m, &margs, sizeof (*m));
12094068Sdm120769 		margs.optptr = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
12104068Sdm120769 		margs.optlen = MAX_MNTOPT_STR;
12114068Sdm120769 		(void) strcpy(margs.optptr, m->optptr);
12124068Sdm120769 		margs.dir = argsp->path;
12134068Sdm120769 
12144068Sdm120769 		/*
12154068Sdm120769 		 * We use the zone's kcred because we don't want the
12164068Sdm120769 		 * zone to be able to thus do something it wouldn't
12174068Sdm120769 		 * normally be able to.
12184068Sdm120769 		 */
12194068Sdm120769 		error = domount(NULL, &margs, mvp, zcred, &vfsp);
12204068Sdm120769 		kmem_free(margs.optptr, MAX_MNTOPT_STR);
12214068Sdm120769 		if (error != 0) {
12224068Sdm120769 			auto_log(fngp->fng_verbose, fngp->fng_zoneid,
12236046Srmesta 			    CE_WARN, "autofs: domount of %s failed "
12246046Srmesta 			    "error=%d", buff, error);
12254068Sdm120769 			VN_RELE(mvp);
12264068Sdm120769 			continue;
12274068Sdm120769 		}
12284068Sdm120769 		VFS_RELE(vfsp);
12292170Sevanl 
12304068Sdm120769 		/*
12314068Sdm120769 		 * If mountpoint is an AUTOFS node, then I'm going to
12324068Sdm120769 		 * flag it that the Filesystem mounted on top was
12334068Sdm120769 		 * mounted in the kernel so that the unmount can be
12344068Sdm120769 		 * done inside the kernel as well.
12354068Sdm120769 		 * I don't care to flag non-AUTOFS mountpoints when an
12364068Sdm120769 		 * AUTOFS in-kernel mount was done on top, because the
12374068Sdm120769 		 * unmount routine already knows that such case was
12384068Sdm120769 		 * done in the kernel.
12394068Sdm120769 		 */
12406046Srmesta 		if (vfs_matchops(dvp->v_vfsp, vfs_getops(mvp->v_vfsp))) {
12414068Sdm120769 			mfnp = vntofn(mvp);
12424068Sdm120769 			mutex_enter(&mfnp->fn_lock);
12434068Sdm120769 			mfnp->fn_flags |= MF_IK_MOUNT;
12444068Sdm120769 			mutex_exit(&mfnp->fn_lock);
12454068Sdm120769 		}
12460Sstevel@tonic-gate 
12474068Sdm120769 		(void) vn_vfswlock_wait(mvp);
12484068Sdm120769 		mvfsp = vn_mountedvfs(mvp);
12494068Sdm120769 		if (mvfsp != NULL) {
12504068Sdm120769 			vfs_lock_wait(mvfsp);
12514068Sdm120769 			vn_vfsunlock(mvp);
12524068Sdm120769 			error = VFS_ROOT(mvfsp, &newvp);
12534068Sdm120769 			vfs_unlock(mvfsp);
12544068Sdm120769 			if (error) {
12554068Sdm120769 				/*
12564068Sdm120769 				 * We've dropped the locks, so let's
12574068Sdm120769 				 * get the mounted vfs again in case
12584068Sdm120769 				 * it changed.
12594068Sdm120769 				 */
12604068Sdm120769 				(void) vn_vfswlock_wait(mvp);
12614068Sdm120769 				mvfsp = vn_mountedvfs(mvp);
12624068Sdm120769 				if (mvfsp != NULL) {
12634068Sdm120769 					error = dounmount(mvfsp, 0, CRED());
12644068Sdm120769 					if (error) {
12654068Sdm120769 						cmn_err(CE_WARN,
12666046Srmesta 						    "autofs: could not unmount"
12676046Srmesta 						    " vfs=%p", (void *)mvfsp);
12684068Sdm120769 					}
12694068Sdm120769 				} else
12704068Sdm120769 					vn_vfsunlock(mvp);
12710Sstevel@tonic-gate 				VN_RELE(mvp);
12720Sstevel@tonic-gate 				continue;
12730Sstevel@tonic-gate 			}
12744068Sdm120769 		} else {
12754068Sdm120769 			vn_vfsunlock(mvp);
12764068Sdm120769 			VN_RELE(mvp);
12774068Sdm120769 			continue;
12784068Sdm120769 		}
12793901Skr143551 
12804068Sdm120769 		auto_mount = vfs_matchops(dvp->v_vfsp,
12816046Srmesta 		    vfs_getops(newvp->v_vfsp));
12824068Sdm120769 		newfnp = vntofn(newvp);
12834068Sdm120769 		newfnp->fn_parent = dfnp;
12843901Skr143551 
12854068Sdm120769 		/*
12864068Sdm120769 		 * At this time we want to save the AUTOFS filesystem
12874068Sdm120769 		 * as a trigger node. (We only do this if the mount
12884068Sdm120769 		 * occurred on a node different from the root.
12894068Sdm120769 		 * We look at the trigger nodes during
12904068Sdm120769 		 * the automatic unmounting to make sure we remove them
12914068Sdm120769 		 * as a unit and remount them as a unit if the
12924068Sdm120769 		 * filesystem mounted at the root could not be
12934068Sdm120769 		 * unmounted.
12944068Sdm120769 		 */
12954068Sdm120769 		if (auto_mount && (error == 0) && (mvp != dvp)) {
12964068Sdm120769 			save_triggers++;
12970Sstevel@tonic-gate 			/*
12984068Sdm120769 			 * Add AUTOFS mount to hierarchy
12990Sstevel@tonic-gate 			 */
13004068Sdm120769 			newfnp->fn_flags |= MF_TRIGGER;
13014068Sdm120769 			rw_enter(&newfnp->fn_rwlock, RW_WRITER);
13024068Sdm120769 			newfnp->fn_next = dfnp->fn_trigger;
13034068Sdm120769 			rw_exit(&newfnp->fn_rwlock);
13044068Sdm120769 			rw_enter(&dfnp->fn_rwlock, RW_WRITER);
13054068Sdm120769 			dfnp->fn_trigger = newfnp;
13064068Sdm120769 			rw_exit(&dfnp->fn_rwlock);
13074068Sdm120769 			/*
13084068Sdm120769 			 * Don't VN_RELE(newvp) here since dfnp now
13094068Sdm120769 			 * holds reference to it as its trigger node.
13104068Sdm120769 			 */
13114068Sdm120769 			AUTOFS_DPRINT((10, "\tadding trigger %s to %s\n",
13126046Srmesta 			    newfnp->fn_name, dfnp->fn_name));
13134068Sdm120769 			AUTOFS_DPRINT((10, "\tfirst trigger is %s\n",
13146046Srmesta 			    dfnp->fn_trigger->fn_name));
13154068Sdm120769 			if (newfnp->fn_next != NULL)
13166046Srmesta 				AUTOFS_DPRINT((10, "\tnext trigger is %s\n",
13176046Srmesta 				    newfnp->fn_next->fn_name));
13184068Sdm120769 			else
13196046Srmesta 				AUTOFS_DPRINT((10, "\tno next trigger\n"));
13204068Sdm120769 		} else
13214068Sdm120769 			VN_RELE(newvp);
13220Sstevel@tonic-gate 
13234068Sdm120769 		if (!error)
13244068Sdm120769 			success++;
13250Sstevel@tonic-gate 
13264068Sdm120769 		if (update_times) {
13274068Sdm120769 			gethrestime(&now);
13284068Sdm120769 			dfnp->fn_atime = dfnp->fn_mtime = now;
13294068Sdm120769 		}
13303901Skr143551 
13314068Sdm120769 		VN_RELE(mvp);
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	if (save_triggers) {
13350Sstevel@tonic-gate 		/*
13360Sstevel@tonic-gate 		 * Make sure the parent can't be freed while it has triggers.
13370Sstevel@tonic-gate 		 */
13380Sstevel@tonic-gate 		VN_HOLD(dvp);
13390Sstevel@tonic-gate 	}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	crfree(zcred);
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate done:
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * Return failure if daemon didn't mount anything, and all
13460Sstevel@tonic-gate 	 * kernel mounts attempted failed.
13470Sstevel@tonic-gate 	 */
13480Sstevel@tonic-gate 	error = success ? 0 : ENOENT;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	if (alp != NULL) {
13510Sstevel@tonic-gate 		if ((error == 0) && save_triggers) {
13520Sstevel@tonic-gate 			/*
13530Sstevel@tonic-gate 			 * Save action_list information, so that we can use it
13540Sstevel@tonic-gate 			 * when it comes time to remount the trigger nodes
13550Sstevel@tonic-gate 			 * The action list is freed when the directory node
13560Sstevel@tonic-gate 			 * containing the reference to it is unmounted in
13570Sstevel@tonic-gate 			 * unmount_tree().
13580Sstevel@tonic-gate 			 */
13590Sstevel@tonic-gate 			mutex_enter(&dfnp->fn_lock);
13600Sstevel@tonic-gate 			ASSERT(dfnp->fn_alp == NULL);
13610Sstevel@tonic-gate 			dfnp->fn_alp = alp;
13620Sstevel@tonic-gate 			mutex_exit(&dfnp->fn_lock);
13630Sstevel@tonic-gate 		} else {
13640Sstevel@tonic-gate 			/*
13650Sstevel@tonic-gate 			 * free the action list now,
13660Sstevel@tonic-gate 			 */
13670Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 	}
13700Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_perform_actions: error=%d\n", error));
13710Sstevel@tonic-gate 	return (error);
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate fnnode_t *
auto_makefnnode(vtype_t type,vfs_t * vfsp,char * name,cred_t * cred,struct autofs_globals * fngp)13750Sstevel@tonic-gate auto_makefnnode(
13760Sstevel@tonic-gate 	vtype_t type,
13770Sstevel@tonic-gate 	vfs_t *vfsp,
13780Sstevel@tonic-gate 	char *name,
13790Sstevel@tonic-gate 	cred_t *cred,
13800Sstevel@tonic-gate 	struct autofs_globals *fngp)
13810Sstevel@tonic-gate {
13820Sstevel@tonic-gate 	fnnode_t *fnp;
13830Sstevel@tonic-gate 	vnode_t *vp;
13840Sstevel@tonic-gate 	char *tmpname;
13850Sstevel@tonic-gate 	timestruc_t now;
13860Sstevel@tonic-gate 	/*
13870Sstevel@tonic-gate 	 * autofs uses odd inode numbers
13880Sstevel@tonic-gate 	 * automountd uses even inode numbers
13890Sstevel@tonic-gate 	 *
13900Sstevel@tonic-gate 	 * To preserve the age-old semantics that inum+devid is unique across
13910Sstevel@tonic-gate 	 * the system, this variable must be global across zones.
13920Sstevel@tonic-gate 	 */
13930Sstevel@tonic-gate 	static ino_t nodeid = 3;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	fnp = kmem_zalloc(sizeof (*fnp), KM_SLEEP);
13960Sstevel@tonic-gate 	fnp->fn_vnode = vn_alloc(KM_SLEEP);
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	vp = fntovn(fnp);
13990Sstevel@tonic-gate 	tmpname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
14000Sstevel@tonic-gate 	(void) strcpy(tmpname, name);
14010Sstevel@tonic-gate 	fnp->fn_name = &tmpname[0];
14020Sstevel@tonic-gate 	fnp->fn_namelen = (int)strlen(tmpname) + 1;	/* include '\0' */
14030Sstevel@tonic-gate 	fnp->fn_uid = crgetuid(cred);
14040Sstevel@tonic-gate 	fnp->fn_gid = crgetgid(cred);
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * ".." is added in auto_enter and auto_mount.
14070Sstevel@tonic-gate 	 * "." is added in auto_mkdir and auto_mount.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	/*
14100Sstevel@tonic-gate 	 * Note that fn_size and fn_linkcnt are already 0 since
14110Sstevel@tonic-gate 	 * we used kmem_zalloc to allocated fnp
14120Sstevel@tonic-gate 	 */
14130Sstevel@tonic-gate 	fnp->fn_mode = AUTOFS_MODE;
14140Sstevel@tonic-gate 	gethrestime(&now);
14150Sstevel@tonic-gate 	fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now;
14160Sstevel@tonic-gate 	fnp->fn_ref_time = now.tv_sec;
14170Sstevel@tonic-gate 	mutex_enter(&autofs_nodeid_lock);
14180Sstevel@tonic-gate 	fnp->fn_nodeid = nodeid;
14190Sstevel@tonic-gate 	nodeid += 2;
14200Sstevel@tonic-gate 	fnp->fn_globals = fngp;
14210Sstevel@tonic-gate 	fngp->fng_fnnode_count++;
14220Sstevel@tonic-gate 	mutex_exit(&autofs_nodeid_lock);
14230Sstevel@tonic-gate 	vn_setops(vp, auto_vnodeops);
14240Sstevel@tonic-gate 	vp->v_type = type;
14250Sstevel@tonic-gate 	vp->v_data = (void *)fnp;
14260Sstevel@tonic-gate 	vp->v_vfsp = vfsp;
14270Sstevel@tonic-gate 	mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL);
14280Sstevel@tonic-gate 	rw_init(&fnp->fn_rwlock, NULL, RW_DEFAULT, NULL);
14290Sstevel@tonic-gate 	cv_init(&fnp->fn_cv_mount, NULL, CV_DEFAULT, NULL);
14300Sstevel@tonic-gate 	vn_exists(vp);
14310Sstevel@tonic-gate 	return (fnp);
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate void
auto_freefnnode(fnnode_t * fnp)14360Sstevel@tonic-gate auto_freefnnode(fnnode_t *fnp)
14370Sstevel@tonic-gate {
14380Sstevel@tonic-gate 	vnode_t *vp = fntovn(fnp);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_freefnnode: fnp=%p\n", (void *)fnp));
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	ASSERT(fnp->fn_linkcnt == 0);
14430Sstevel@tonic-gate 	ASSERT(vp->v_count == 0);
14440Sstevel@tonic-gate 	ASSERT(fnp->fn_dirents == NULL);
14450Sstevel@tonic-gate 	ASSERT(fnp->fn_parent == NULL);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	vn_invalid(vp);
14480Sstevel@tonic-gate 	kmem_free(fnp->fn_name, fnp->fn_namelen);
14490Sstevel@tonic-gate 	if (fnp->fn_symlink) {
14500Sstevel@tonic-gate 		ASSERT(fnp->fn_flags & MF_THISUID_MATCH_RQD);
14510Sstevel@tonic-gate 		kmem_free(fnp->fn_symlink, fnp->fn_symlinklen);
14520Sstevel@tonic-gate 	}
14530Sstevel@tonic-gate 	if (fnp->fn_cred)
14540Sstevel@tonic-gate 		crfree(fnp->fn_cred);
14550Sstevel@tonic-gate 	mutex_destroy(&fnp->fn_lock);
14560Sstevel@tonic-gate 	rw_destroy(&fnp->fn_rwlock);
14570Sstevel@tonic-gate 	cv_destroy(&fnp->fn_cv_mount);
14580Sstevel@tonic-gate 	vn_free(vp);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	mutex_enter(&autofs_nodeid_lock);
14610Sstevel@tonic-gate 	fnp->fn_globals->fng_fnnode_count--;
14620Sstevel@tonic-gate 	mutex_exit(&autofs_nodeid_lock);
14630Sstevel@tonic-gate 	kmem_free(fnp, sizeof (*fnp));
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate void
auto_disconnect(fnnode_t * dfnp,fnnode_t * fnp)14670Sstevel@tonic-gate auto_disconnect(
14680Sstevel@tonic-gate 	fnnode_t *dfnp,
14690Sstevel@tonic-gate 	fnnode_t *fnp)
14700Sstevel@tonic-gate {
14710Sstevel@tonic-gate 	fnnode_t *tmp, **fnpp;
14720Sstevel@tonic-gate 	vnode_t *vp = fntovn(fnp);
14730Sstevel@tonic-gate 	timestruc_t now;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	AUTOFS_DPRINT((4,
14760Sstevel@tonic-gate 	    "auto_disconnect: dfnp=%p fnp=%p linkcnt=%d\n v_count=%d",
14770Sstevel@tonic-gate 	    (void *)dfnp, (void *)fnp, fnp->fn_linkcnt, vp->v_count));
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
14800Sstevel@tonic-gate 	ASSERT(fnp->fn_linkcnt == 1);
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	if (vn_mountedvfs(vp) != NULL) {
14830Sstevel@tonic-gate 		cmn_err(CE_PANIC, "auto_disconnect: vp %p mounted on",
14840Sstevel@tonic-gate 		    (void *)vp);
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/*
14880Sstevel@tonic-gate 	 * Decrement by 1 because we're removing the entry in dfnp.
14890Sstevel@tonic-gate 	 */
14900Sstevel@tonic-gate 	fnp->fn_linkcnt--;
14910Sstevel@tonic-gate 	fnp->fn_size--;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	/*
14940Sstevel@tonic-gate 	 * only changed while holding parent's (dfnp) rw_lock
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	fnp->fn_parent = NULL;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	fnpp = &dfnp->fn_dirents;
14990Sstevel@tonic-gate 	for (;;) {
15000Sstevel@tonic-gate 		tmp = *fnpp;
15010Sstevel@tonic-gate 		if (tmp == NULL) {
15020Sstevel@tonic-gate 			cmn_err(CE_PANIC,
15030Sstevel@tonic-gate 			    "auto_disconnect: %p not in %p dirent list",
15040Sstevel@tonic-gate 			    (void *)fnp, (void *)dfnp);
15050Sstevel@tonic-gate 		}
15060Sstevel@tonic-gate 		if (tmp == fnp) {
15070Sstevel@tonic-gate 			*fnpp = tmp->fn_next; 	/* remove it from the list */
15080Sstevel@tonic-gate 			ASSERT(vp->v_count == 0);
15090Sstevel@tonic-gate 			/* child had a pointer to parent ".." */
15100Sstevel@tonic-gate 			dfnp->fn_linkcnt--;
15110Sstevel@tonic-gate 			dfnp->fn_size--;
15120Sstevel@tonic-gate 			break;
15130Sstevel@tonic-gate 		}
15140Sstevel@tonic-gate 		fnpp = &tmp->fn_next;
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
15180Sstevel@tonic-gate 	gethrestime(&now);
15190Sstevel@tonic-gate 	fnp->fn_atime = fnp->fn_mtime = now;
15200Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_disconnect: done\n"));
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate int
auto_enter(fnnode_t * dfnp,char * name,fnnode_t ** fnpp,cred_t * cred)15260Sstevel@tonic-gate auto_enter(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	struct fnnode *cfnp, **spp;
15290Sstevel@tonic-gate 	vnode_t *dvp = fntovn(dfnp);
15300Sstevel@tonic-gate 	ushort_t offset = 0;
15310Sstevel@tonic-gate 	ushort_t diff;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_enter: dfnp=%p, name=%s ", (void *)dfnp, name));
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	cfnp = dfnp->fn_dirents;
15380Sstevel@tonic-gate 	if (cfnp == NULL) {
15390Sstevel@tonic-gate 		/*
15400Sstevel@tonic-gate 		 * offset = 0 for '.' and offset = 1 for '..'
15410Sstevel@tonic-gate 		 */
15420Sstevel@tonic-gate 		spp = &dfnp->fn_dirents;
15430Sstevel@tonic-gate 		offset = 2;
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	for (; cfnp; cfnp = cfnp->fn_next) {
15470Sstevel@tonic-gate 		if (strcmp(cfnp->fn_name, name) == 0) {
15480Sstevel@tonic-gate 			mutex_enter(&cfnp->fn_lock);
15490Sstevel@tonic-gate 			if (cfnp->fn_flags & MF_THISUID_MATCH_RQD) {
15500Sstevel@tonic-gate 				/*
15510Sstevel@tonic-gate 				 * "thisuser" kind of node, need to
15520Sstevel@tonic-gate 				 * match CREDs as well
15530Sstevel@tonic-gate 				 */
15540Sstevel@tonic-gate 				mutex_exit(&cfnp->fn_lock);
15550Sstevel@tonic-gate 				if (crcmp(cfnp->fn_cred, cred) == 0)
15560Sstevel@tonic-gate 					return (EEXIST);
15570Sstevel@tonic-gate 			} else {
15580Sstevel@tonic-gate 				mutex_exit(&cfnp->fn_lock);
15590Sstevel@tonic-gate 				return (EEXIST);
15600Sstevel@tonic-gate 			}
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 		if (cfnp->fn_next != NULL) {
15640Sstevel@tonic-gate 			diff = (ushort_t)
15650Sstevel@tonic-gate 			    (cfnp->fn_next->fn_offset - cfnp->fn_offset);
15660Sstevel@tonic-gate 			ASSERT(diff != 0);
15670Sstevel@tonic-gate 			if (diff > 1 && offset == 0) {
15680Sstevel@tonic-gate 				offset = (ushort_t)cfnp->fn_offset + 1;
15690Sstevel@tonic-gate 				spp = &cfnp->fn_next;
15700Sstevel@tonic-gate 			}
15710Sstevel@tonic-gate 		} else if (offset == 0) {
15720Sstevel@tonic-gate 			offset = (ushort_t)cfnp->fn_offset + 1;
15730Sstevel@tonic-gate 			spp = &cfnp->fn_next;
15740Sstevel@tonic-gate 		}
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	*fnpp = auto_makefnnode(VDIR, dvp->v_vfsp, name, cred,
15780Sstevel@tonic-gate 	    dfnp->fn_globals);
15790Sstevel@tonic-gate 	if (*fnpp == NULL)
15800Sstevel@tonic-gate 		return (ENOMEM);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	/*
15830Sstevel@tonic-gate 	 * I don't hold the mutex on fnpp because I created it, and
15840Sstevel@tonic-gate 	 * I'm already holding the writers lock for it's parent
15850Sstevel@tonic-gate 	 * directory, therefore nobody can reference it without me first
15860Sstevel@tonic-gate 	 * releasing the writers lock.
15870Sstevel@tonic-gate 	 */
15880Sstevel@tonic-gate 	(*fnpp)->fn_offset = offset;
15890Sstevel@tonic-gate 	(*fnpp)->fn_next = *spp;
15900Sstevel@tonic-gate 	*spp = *fnpp;
15910Sstevel@tonic-gate 	(*fnpp)->fn_parent = dfnp;
15920Sstevel@tonic-gate 	(*fnpp)->fn_linkcnt++;	/* parent now holds reference to entry */
15930Sstevel@tonic-gate 	(*fnpp)->fn_size++;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	/*
15960Sstevel@tonic-gate 	 * dfnp->fn_linkcnt and dfnp->fn_size protected by dfnp->rw_lock
15970Sstevel@tonic-gate 	 */
15980Sstevel@tonic-gate 	dfnp->fn_linkcnt++;	/* child now holds reference to parent '..' */
15990Sstevel@tonic-gate 	dfnp->fn_size++;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	dfnp->fn_ref_time = gethrestime_sec();
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "*fnpp=%p\n", (void *)*fnpp));
16040Sstevel@tonic-gate 	return (0);
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate int
auto_search(fnnode_t * dfnp,char * name,fnnode_t ** fnpp,cred_t * cred)16080Sstevel@tonic-gate auto_search(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
16090Sstevel@tonic-gate {
16100Sstevel@tonic-gate 	vnode_t *dvp;
16110Sstevel@tonic-gate 	fnnode_t *p;
16120Sstevel@tonic-gate 	int error = ENOENT, match = 0;
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_search: dfnp=%p, name=%s...\n",
16150Sstevel@tonic-gate 	    (void *)dfnp, name));
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	dvp = fntovn(dfnp);
16180Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
16190Sstevel@tonic-gate 		cmn_err(CE_PANIC, "auto_search: dvp=%p not a directory",
16200Sstevel@tonic-gate 		    (void *)dvp);
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dfnp->fn_rwlock));
16240Sstevel@tonic-gate 	for (p = dfnp->fn_dirents; p != NULL; p = p->fn_next) {
16250Sstevel@tonic-gate 		if (strcmp(p->fn_name, name) == 0) {
16260Sstevel@tonic-gate 			mutex_enter(&p->fn_lock);
16270Sstevel@tonic-gate 			if (p->fn_flags & MF_THISUID_MATCH_RQD) {
16280Sstevel@tonic-gate 				/*
16290Sstevel@tonic-gate 				 * "thisuser" kind of node
16300Sstevel@tonic-gate 				 * Need to match CREDs as well
16310Sstevel@tonic-gate 				 */
16320Sstevel@tonic-gate 				mutex_exit(&p->fn_lock);
16330Sstevel@tonic-gate 				match = crcmp(p->fn_cred, cred) == 0;
16340Sstevel@tonic-gate 			} else {
16350Sstevel@tonic-gate 				/*
16360Sstevel@tonic-gate 				 * No need to check CRED
16370Sstevel@tonic-gate 				 */
16380Sstevel@tonic-gate 				mutex_exit(&p->fn_lock);
16390Sstevel@tonic-gate 				match = 1;
16400Sstevel@tonic-gate 			}
16410Sstevel@tonic-gate 		}
16420Sstevel@tonic-gate 		if (match) {
16430Sstevel@tonic-gate 			error = 0;
16440Sstevel@tonic-gate 			if (fnpp) {
16450Sstevel@tonic-gate 				*fnpp = p;
16460Sstevel@tonic-gate 				VN_HOLD(fntovn(*fnpp));
16470Sstevel@tonic-gate 			}
16480Sstevel@tonic-gate 			break;
16490Sstevel@tonic-gate 		}
16500Sstevel@tonic-gate 	}
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_search: error=%d\n", error));
16530Sstevel@tonic-gate 	return (error);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate /*
16570Sstevel@tonic-gate  * If dvp is mounted on, get path's vnode in the mounted on
16580Sstevel@tonic-gate  * filesystem.  Path is relative to dvp, ie "./path".
16590Sstevel@tonic-gate  * If successful, *mvp points to a the held mountpoint vnode.
16600Sstevel@tonic-gate  */
16610Sstevel@tonic-gate /* ARGSUSED */
16620Sstevel@tonic-gate static int
auto_getmntpnt(vnode_t * dvp,char * path,vnode_t ** mvpp,cred_t * cred)16630Sstevel@tonic-gate auto_getmntpnt(
16640Sstevel@tonic-gate 	vnode_t *dvp,
16650Sstevel@tonic-gate 	char *path,
16660Sstevel@tonic-gate 	vnode_t **mvpp,		/* vnode for mountpoint */
16670Sstevel@tonic-gate 	cred_t *cred)
16680Sstevel@tonic-gate {
16690Sstevel@tonic-gate 	int error = 0;
16700Sstevel@tonic-gate 	vnode_t *newvp;
16710Sstevel@tonic-gate 	char namebuf[TYPICALMAXPATHLEN];
16720Sstevel@tonic-gate 	struct pathname lookpn;
16730Sstevel@tonic-gate 	vfs_t *vfsp;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_getmntpnt: path=%s\n", path));
16760Sstevel@tonic-gate 
16771153Snr123932 	if (error = vn_vfsrlock_wait(dvp))
16780Sstevel@tonic-gate 		return (error);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	/*
16810Sstevel@tonic-gate 	 * Now that we have the vfswlock, check to see if dvp
16820Sstevel@tonic-gate 	 * is still mounted on.  If not, then just bail out as
16830Sstevel@tonic-gate 	 * there is no need to remount the triggers since the
16840Sstevel@tonic-gate 	 * higher level mount point has gotten unmounted.
16850Sstevel@tonic-gate 	 */
16860Sstevel@tonic-gate 	vfsp = vn_mountedvfs(dvp);
16870Sstevel@tonic-gate 	if (vfsp == NULL) {
16880Sstevel@tonic-gate 		vn_vfsunlock(dvp);
16890Sstevel@tonic-gate 		error = EBUSY;
16900Sstevel@tonic-gate 		goto done;
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 	/*
16930Sstevel@tonic-gate 	 * Since mounted on, lookup "path" in the new filesystem,
16940Sstevel@tonic-gate 	 * it is important that we do the filesystem jump here to
16950Sstevel@tonic-gate 	 * avoid lookuppn() calling auto_lookup on dvp and deadlock.
16960Sstevel@tonic-gate 	 */
16971153Snr123932 	error = VFS_ROOT(vfsp, &newvp);
16980Sstevel@tonic-gate 	vn_vfsunlock(dvp);
16990Sstevel@tonic-gate 	if (error)
17000Sstevel@tonic-gate 		goto done;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	/*
17030Sstevel@tonic-gate 	 * We do a VN_HOLD on newvp just in case the first call to
17040Sstevel@tonic-gate 	 * lookuppnvp() fails with ENAMETOOLONG.  We should still have a
17050Sstevel@tonic-gate 	 * reference to this vnode for the second call to lookuppnvp().
17060Sstevel@tonic-gate 	 */
17070Sstevel@tonic-gate 	VN_HOLD(newvp);
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	/*
17100Sstevel@tonic-gate 	 * Now create the pathname struct so we can make use of lookuppnvp,
17110Sstevel@tonic-gate 	 * and pn_getcomponent.
17120Sstevel@tonic-gate 	 * This code is similar to lookupname() in fs/lookup.c.
17130Sstevel@tonic-gate 	 */
17140Sstevel@tonic-gate 	error = pn_get_buf(path, UIO_SYSSPACE, &lookpn,
17156046Srmesta 	    namebuf, sizeof (namebuf));
17160Sstevel@tonic-gate 	if (error == 0) {
17170Sstevel@tonic-gate 		error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
17180Sstevel@tonic-gate 		    mvpp, rootdir, newvp, cred);
17190Sstevel@tonic-gate 	} else
17200Sstevel@tonic-gate 		VN_RELE(newvp);
17210Sstevel@tonic-gate 	if (error == ENAMETOOLONG) {
17220Sstevel@tonic-gate 		/*
17230Sstevel@tonic-gate 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
17240Sstevel@tonic-gate 		 * newvp is VN_RELE'd by this call to lookuppnvp.
17250Sstevel@tonic-gate 		 *
17260Sstevel@tonic-gate 		 * Using 'rootdir' in a zone's context is OK here: we already
17270Sstevel@tonic-gate 		 * ascertained that there are no '..'s in the path, and we're
17280Sstevel@tonic-gate 		 * not following symlinks.
17290Sstevel@tonic-gate 		 */
17300Sstevel@tonic-gate 		if ((error = pn_get(path, UIO_SYSSPACE, &lookpn)) == 0) {
17310Sstevel@tonic-gate 			error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
17320Sstevel@tonic-gate 			    mvpp, rootdir, newvp, cred);
17330Sstevel@tonic-gate 			pn_free(&lookpn);
17340Sstevel@tonic-gate 		} else
17350Sstevel@tonic-gate 			VN_RELE(newvp);
17360Sstevel@tonic-gate 	} else {
17370Sstevel@tonic-gate 		/*
17380Sstevel@tonic-gate 		 * Need to release newvp here since we held it.
17390Sstevel@tonic-gate 		 */
17400Sstevel@tonic-gate 		VN_RELE(newvp);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate done:
17440Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_getmntpnt: path=%s *mvpp=%p error=%d\n",
17450Sstevel@tonic-gate 	    path, (void *)*mvpp, error));
17460Sstevel@tonic-gate 	return (error);
17470Sstevel@tonic-gate }
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate #define	DEEPER(x) (((x)->fn_dirents != NULL) || \
17500Sstevel@tonic-gate 			(vn_mountedvfs(fntovn((x)))) != NULL)
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate  * The caller, should have already VN_RELE'd its reference to the
17540Sstevel@tonic-gate  * root vnode of this filesystem.
17550Sstevel@tonic-gate  */
17560Sstevel@tonic-gate static int
auto_inkernel_unmount(vfs_t * vfsp)17570Sstevel@tonic-gate auto_inkernel_unmount(vfs_t *vfsp)
17580Sstevel@tonic-gate {
17590Sstevel@tonic-gate 	vnode_t *cvp = vfsp->vfs_vnodecovered;
17600Sstevel@tonic-gate 	int error;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	AUTOFS_DPRINT((4,
17630Sstevel@tonic-gate 	    "auto_inkernel_unmount: devid=%lx mntpnt(%p) count %u\n",
17640Sstevel@tonic-gate 	    vfsp->vfs_dev, (void *)cvp, cvp->v_count));
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(cvp));
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	/*
17690Sstevel@tonic-gate 	 * Perform the unmount
17700Sstevel@tonic-gate 	 * The mountpoint has already been locked by the caller.
17710Sstevel@tonic-gate 	 */
17720Sstevel@tonic-gate 	error = dounmount(vfsp, 0, kcred);
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_inkernel_unmount: exit count %u\n",
17750Sstevel@tonic-gate 	    cvp->v_count));
17760Sstevel@tonic-gate 	return (error);
17770Sstevel@tonic-gate }
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate /*
17800Sstevel@tonic-gate  * unmounts trigger nodes in the kernel.
17810Sstevel@tonic-gate  */
17820Sstevel@tonic-gate static void
unmount_triggers(fnnode_t * fnp,action_list ** alp)17830Sstevel@tonic-gate unmount_triggers(fnnode_t *fnp, action_list **alp)
17840Sstevel@tonic-gate {
17850Sstevel@tonic-gate 	fnnode_t *tp, *next;
17860Sstevel@tonic-gate 	int error = 0;
17870Sstevel@tonic-gate 	vfs_t *vfsp;
17880Sstevel@tonic-gate 	vnode_t *tvp;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "unmount_triggers: fnp=%p\n", (void *)fnp));
17910Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	*alp = fnp->fn_alp;
17940Sstevel@tonic-gate 	next = fnp->fn_trigger;
17950Sstevel@tonic-gate 	while ((tp = next) != NULL) {
17960Sstevel@tonic-gate 		tvp = fntovn(tp);
17970Sstevel@tonic-gate 		ASSERT(tvp->v_count >= 2);
17980Sstevel@tonic-gate 		next = tp->fn_next;
17990Sstevel@tonic-gate 		/*
18000Sstevel@tonic-gate 		 * drop writer's lock since the unmount will end up
18010Sstevel@tonic-gate 		 * disconnecting this node from fnp and needs to acquire
18020Sstevel@tonic-gate 		 * the writer's lock again.
18030Sstevel@tonic-gate 		 * next has at least a reference count >= 2 since it's
18040Sstevel@tonic-gate 		 * a trigger node, therefore can not be accidentally freed
18050Sstevel@tonic-gate 		 * by a VN_RELE
18060Sstevel@tonic-gate 		 */
18070Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 		vfsp = tvp->v_vfsp;
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 		/*
18120Sstevel@tonic-gate 		 * Its parent was holding a reference to it, since this
18130Sstevel@tonic-gate 		 * is a trigger vnode.
18140Sstevel@tonic-gate 		 */
18150Sstevel@tonic-gate 		VN_RELE(tvp);
18160Sstevel@tonic-gate 		if (error = auto_inkernel_unmount(vfsp)) {
18170Sstevel@tonic-gate 			cmn_err(CE_PANIC, "unmount_triggers: "
18180Sstevel@tonic-gate 			    "unmount of vp=%p failed error=%d",
18190Sstevel@tonic-gate 			    (void *)tvp, error);
18200Sstevel@tonic-gate 		}
18210Sstevel@tonic-gate 		/*
18220Sstevel@tonic-gate 		 * reacquire writer's lock
18230Sstevel@tonic-gate 		 */
18240Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
18250Sstevel@tonic-gate 	}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	/*
18280Sstevel@tonic-gate 	 * We were holding a reference to our parent.  Drop that.
18290Sstevel@tonic-gate 	 */
18300Sstevel@tonic-gate 	VN_RELE(fntovn(fnp));
18310Sstevel@tonic-gate 	fnp->fn_trigger = NULL;
18320Sstevel@tonic-gate 	fnp->fn_alp = NULL;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "unmount_triggers: finished\n"));
18350Sstevel@tonic-gate }
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate /*
18380Sstevel@tonic-gate  * This routine locks the mountpoint of every trigger node if they're
183912184SJan.Kryl@Sun.COM  * not busy, or returns EBUSY if any node is busy.
18400Sstevel@tonic-gate  */
184112184SJan.Kryl@Sun.COM static boolean_t
triggers_busy(fnnode_t * fnp)184212184SJan.Kryl@Sun.COM triggers_busy(fnnode_t *fnp)
18430Sstevel@tonic-gate {
184412184SJan.Kryl@Sun.COM 	int done;
18450Sstevel@tonic-gate 	int lck_error = 0;
18460Sstevel@tonic-gate 	fnnode_t *tp, *t1p;
18470Sstevel@tonic-gate 	vfs_t *vfsp;
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
18520Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\ttrigger: %s\n", tp->fn_name));
185312184SJan.Kryl@Sun.COM 		/* MF_LOOKUP should never be set on trigger nodes */
185412184SJan.Kryl@Sun.COM 		ASSERT((tp->fn_flags & MF_LOOKUP) == 0);
18550Sstevel@tonic-gate 		vfsp = fntovn(tp)->v_vfsp;
185612184SJan.Kryl@Sun.COM 
18570Sstevel@tonic-gate 		/*
18580Sstevel@tonic-gate 		 * The vn_vfsunlock will be done in auto_inkernel_unmount.
18590Sstevel@tonic-gate 		 */
18600Sstevel@tonic-gate 		lck_error = vn_vfswlock(vfsp->vfs_vnodecovered);
186112184SJan.Kryl@Sun.COM 
186212184SJan.Kryl@Sun.COM 		if (lck_error != 0 || (tp->fn_flags & MF_INPROG) ||
186312184SJan.Kryl@Sun.COM 		    DEEPER(tp) || ((fntovn(tp))->v_count) > 2) {
18640Sstevel@tonic-gate 			/*
18650Sstevel@tonic-gate 			 * couldn't lock it because it's busy,
18660Sstevel@tonic-gate 			 * It is mounted on or has dirents?
18670Sstevel@tonic-gate 			 * If reference count is greater than two, then
18680Sstevel@tonic-gate 			 * somebody else is holding a reference to this vnode.
18690Sstevel@tonic-gate 			 * One reference is for the mountpoint, and the second
18700Sstevel@tonic-gate 			 * is for the trigger node.
18710Sstevel@tonic-gate 			 */
18720Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\ttrigger busy\n"));
187312184SJan.Kryl@Sun.COM 
18740Sstevel@tonic-gate 			/*
18750Sstevel@tonic-gate 			 * Unlock previously locked mountpoints
18760Sstevel@tonic-gate 			 */
18770Sstevel@tonic-gate 			for (done = 0, t1p = fnp->fn_trigger; !done;
18780Sstevel@tonic-gate 			    t1p = t1p->fn_next) {
18790Sstevel@tonic-gate 				/*
18800Sstevel@tonic-gate 				 * Unlock all nodes previously
18810Sstevel@tonic-gate 				 * locked. All nodes up to 'tp'
18820Sstevel@tonic-gate 				 * were successfully locked. If 'lck_err' is
18830Sstevel@tonic-gate 				 * set, then 'tp' was not locked, and thus
18840Sstevel@tonic-gate 				 * should not be unlocked. If
18850Sstevel@tonic-gate 				 * 'lck_err' is not set, then 'tp' was
18860Sstevel@tonic-gate 				 * successfully locked, and it should
18870Sstevel@tonic-gate 				 * be unlocked.
18880Sstevel@tonic-gate 				 */
18890Sstevel@tonic-gate 				if (t1p != tp || !lck_error) {
18900Sstevel@tonic-gate 					vfsp = fntovn(t1p)->v_vfsp;
18910Sstevel@tonic-gate 					vn_vfsunlock(vfsp->vfs_vnodecovered);
18920Sstevel@tonic-gate 				}
18930Sstevel@tonic-gate 				done = (t1p == tp);
18940Sstevel@tonic-gate 			}
189512184SJan.Kryl@Sun.COM 			return (B_TRUE);
18960Sstevel@tonic-gate 		}
18970Sstevel@tonic-gate 	}
18980Sstevel@tonic-gate 
189912184SJan.Kryl@Sun.COM 	return (B_FALSE);
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate /*
19030Sstevel@tonic-gate  * It is the caller's responsibility to grab the VVFSLOCK.
19040Sstevel@tonic-gate  * Releases the VVFSLOCK upon return.
19050Sstevel@tonic-gate  */
19060Sstevel@tonic-gate static int
unmount_node(vnode_t * cvp,int force)19070Sstevel@tonic-gate unmount_node(vnode_t *cvp, int force)
19080Sstevel@tonic-gate {
19090Sstevel@tonic-gate 	int error = 0;
19100Sstevel@tonic-gate 	fnnode_t *cfnp;
19110Sstevel@tonic-gate 	vfs_t *vfsp;
19120Sstevel@tonic-gate 	umntrequest ul;
19130Sstevel@tonic-gate 	fninfo_t *fnip;
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tunmount_node cvp=%p\n", (void *)cvp));
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(cvp));
19180Sstevel@tonic-gate 	cfnp = vntofn(cvp);
19190Sstevel@tonic-gate 	vfsp = vn_mountedvfs(cvp);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	if (force || cfnp->fn_flags & MF_IK_MOUNT) {
19220Sstevel@tonic-gate 		/*
19230Sstevel@tonic-gate 		 * Mount was performed in the kernel, so
19240Sstevel@tonic-gate 		 * do an in-kernel unmount. auto_inkernel_unmount()
19250Sstevel@tonic-gate 		 * will vn_vfsunlock(cvp).
19260Sstevel@tonic-gate 		 */
19270Sstevel@tonic-gate 		error = auto_inkernel_unmount(vfsp);
19280Sstevel@tonic-gate 	} else {
19290Sstevel@tonic-gate 		zone_t *zone = NULL;
19300Sstevel@tonic-gate 		refstr_t *mntpt, *resource;
19310Sstevel@tonic-gate 		size_t mntoptslen;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 		/*
19340Sstevel@tonic-gate 		 * Get the mnttab information of the node
19350Sstevel@tonic-gate 		 * and ask the daemon to unmount it.
19360Sstevel@tonic-gate 		 */
19370Sstevel@tonic-gate 		bzero(&ul, sizeof (ul));
19380Sstevel@tonic-gate 		mntfs_getmntopts(vfsp, &ul.mntopts, &mntoptslen);
19390Sstevel@tonic-gate 		if (ul.mntopts == NULL) {
19402170Sevanl 			auto_log(cfnp->fn_globals->fng_verbose,
19416046Srmesta 			    cfnp->fn_globals->fng_zoneid, CE_WARN,
19426046Srmesta 			    "unmount_node: no memory");
19430Sstevel@tonic-gate 			vn_vfsunlock(cvp);
19440Sstevel@tonic-gate 			error = ENOMEM;
19450Sstevel@tonic-gate 			goto done;
19460Sstevel@tonic-gate 		}
19470Sstevel@tonic-gate 		if (mntoptslen > AUTOFS_MAXOPTSLEN)
19480Sstevel@tonic-gate 			ul.mntopts[AUTOFS_MAXOPTSLEN - 1] = '\0';
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		mntpt = vfs_getmntpoint(vfsp);
19510Sstevel@tonic-gate 		ul.mntpnt = (char *)refstr_value(mntpt);
19520Sstevel@tonic-gate 		resource = vfs_getresource(vfsp);
19530Sstevel@tonic-gate 		ul.mntresource = (char *)refstr_value(resource);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		fnip = vfstofni(cvp->v_vfsp);
19560Sstevel@tonic-gate 		ul.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 		/*
19590Sstevel@tonic-gate 		 * Since a zone'd automountd's view of the autofs mount points
19600Sstevel@tonic-gate 		 * differs from those in the kernel, we need to make sure we
19610Sstevel@tonic-gate 		 * give it consistent mount points.
19620Sstevel@tonic-gate 		 */
19630Sstevel@tonic-gate 		ASSERT(fnip->fi_zoneid == getzoneid());
19640Sstevel@tonic-gate 		zone = curproc->p_zone;
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 		if (fnip->fi_zoneid != GLOBAL_ZONEID) {
19670Sstevel@tonic-gate 			if (ZONE_PATH_VISIBLE(ul.mntpnt, zone)) {
19680Sstevel@tonic-gate 				ul.mntpnt =
19690Sstevel@tonic-gate 				    ZONE_PATH_TRANSLATE(ul.mntpnt, zone);
19700Sstevel@tonic-gate 			}
19710Sstevel@tonic-gate 			if (ZONE_PATH_VISIBLE(ul.mntresource, zone)) {
19720Sstevel@tonic-gate 				ul.mntresource =
19730Sstevel@tonic-gate 				    ZONE_PATH_TRANSLATE(ul.mntresource, zone);
19740Sstevel@tonic-gate 			}
19750Sstevel@tonic-gate 		}
19762170Sevanl 
19770Sstevel@tonic-gate 		ul.fstype = vfssw[vfsp->vfs_fstype].vsw_name;
19780Sstevel@tonic-gate 		vn_vfsunlock(cvp);
19790Sstevel@tonic-gate 
19802170Sevanl 		error = auto_send_unmount_request(fnip, &ul, FALSE);
19810Sstevel@tonic-gate 		kmem_free(ul.mntopts, mntoptslen);
19820Sstevel@tonic-gate 		refstr_rele(mntpt);
19830Sstevel@tonic-gate 		refstr_rele(resource);
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate done:
19870Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tunmount_node cvp=%p error=%d\n", (void *)cvp,
19880Sstevel@tonic-gate 	    error));
19890Sstevel@tonic-gate 	return (error);
19900Sstevel@tonic-gate }
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate /*
19930Sstevel@tonic-gate  * return EBUSY if any thread is holding a reference to this vnode
199412184SJan.Kryl@Sun.COM  * other than us. Result of this function cannot be relied on, since
199512184SJan.Kryl@Sun.COM  * it doesn't follow proper locking rules (i.e. vp->v_vfsmountedhere
199612184SJan.Kryl@Sun.COM  * and fnp->fn_trigger can change throughout this function). However
199712184SJan.Kryl@Sun.COM  * it's good enough for rough estimation.
19980Sstevel@tonic-gate  */
19990Sstevel@tonic-gate static int
check_auto_node(vnode_t * vp)20000Sstevel@tonic-gate check_auto_node(vnode_t *vp)
20010Sstevel@tonic-gate {
20020Sstevel@tonic-gate 	fnnode_t *fnp;
20030Sstevel@tonic-gate 	int error = 0;
20040Sstevel@tonic-gate 	/*
20050Sstevel@tonic-gate 	 * number of references to expect for
20060Sstevel@tonic-gate 	 * a non-busy vnode.
20070Sstevel@tonic-gate 	 */
20080Sstevel@tonic-gate 	uint_t count;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tcheck_auto_node vp=%p ", (void *)vp));
20110Sstevel@tonic-gate 	fnp = vntofn(vp);
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	count = 1;		/* we are holding a reference to vp */
20140Sstevel@tonic-gate 	if (fnp->fn_flags & MF_TRIGGER) {
20150Sstevel@tonic-gate 		/*
20160Sstevel@tonic-gate 		 * parent holds a pointer to us (trigger)
20170Sstevel@tonic-gate 		 */
20180Sstevel@tonic-gate 		count++;
20190Sstevel@tonic-gate 	}
20200Sstevel@tonic-gate 	if (fnp->fn_trigger != NULL) {
20210Sstevel@tonic-gate 		/*
20220Sstevel@tonic-gate 		 * The trigger nodes have a hold on us.
20230Sstevel@tonic-gate 		 */
20240Sstevel@tonic-gate 		count++;
20250Sstevel@tonic-gate 	}
202612184SJan.Kryl@Sun.COM 	if (vn_ismntpt(vp)) {
202712184SJan.Kryl@Sun.COM 		/*
202812184SJan.Kryl@Sun.COM 		 * File system is mounted on us.
202912184SJan.Kryl@Sun.COM 		 */
203012184SJan.Kryl@Sun.COM 		count++;
203112184SJan.Kryl@Sun.COM 	}
20320Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
203312184SJan.Kryl@Sun.COM 	ASSERT(vp->v_count > 0);
20340Sstevel@tonic-gate 	if (vp->v_flag & VROOT)
20350Sstevel@tonic-gate 		count++;
20360Sstevel@tonic-gate 	AUTOFS_DPRINT((10, "\tcount=%u ", vp->v_count));
20370Sstevel@tonic-gate 	if (vp->v_count > count)
20380Sstevel@tonic-gate 		error = EBUSY;
20390Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tcheck_auto_node error=%d ", error));
20420Sstevel@tonic-gate 	return (error);
20430Sstevel@tonic-gate }
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate /*
20460Sstevel@tonic-gate  * rootvp is the root of the AUTOFS filesystem.
20470Sstevel@tonic-gate  * If rootvp is busy (v_count > 1) returns EBUSY.
20480Sstevel@tonic-gate  * else removes every vnode under this tree.
20490Sstevel@tonic-gate  * ASSUMPTION: Assumes that the only node which can be busy is
20500Sstevel@tonic-gate  * the root vnode. This filesystem better be two levels deep only,
20510Sstevel@tonic-gate  * the root and its immediate subdirs.
20520Sstevel@tonic-gate  * The daemon will "AUTOFS direct-mount" only one level below the root.
20530Sstevel@tonic-gate  */
205412184SJan.Kryl@Sun.COM static void
unmount_autofs(vnode_t * rootvp)20550Sstevel@tonic-gate unmount_autofs(vnode_t *rootvp)
20560Sstevel@tonic-gate {
20570Sstevel@tonic-gate 	fnnode_t *fnp, *rootfnp, *nfnp;
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tunmount_autofs rootvp=%p ", (void *)rootvp));
20600Sstevel@tonic-gate 
206112184SJan.Kryl@Sun.COM 	/*
206212184SJan.Kryl@Sun.COM 	 * Remove all its immediate subdirectories.
206312184SJan.Kryl@Sun.COM 	 */
206412184SJan.Kryl@Sun.COM 	rootfnp = vntofn(rootvp);
206512184SJan.Kryl@Sun.COM 	rw_enter(&rootfnp->fn_rwlock, RW_WRITER);
206612184SJan.Kryl@Sun.COM 	for (fnp = rootfnp->fn_dirents; fnp != NULL; fnp = nfnp) {
206712184SJan.Kryl@Sun.COM 		ASSERT(fntovn(fnp)->v_count == 0);
206812184SJan.Kryl@Sun.COM 		ASSERT(fnp->fn_dirents == NULL);
206912184SJan.Kryl@Sun.COM 		ASSERT(fnp->fn_linkcnt == 2);
207012184SJan.Kryl@Sun.COM 		fnp->fn_linkcnt--;
207112184SJan.Kryl@Sun.COM 		auto_disconnect(rootfnp, fnp);
207212184SJan.Kryl@Sun.COM 		nfnp = fnp->fn_next;
207312184SJan.Kryl@Sun.COM 		auto_freefnnode(fnp);
20740Sstevel@tonic-gate 	}
207512184SJan.Kryl@Sun.COM 	rw_exit(&rootfnp->fn_rwlock);
20760Sstevel@tonic-gate }
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate /*
207912184SJan.Kryl@Sun.COM  * If a node matches all unmount criteria, do:
208012184SJan.Kryl@Sun.COM  *     destroy subordinate trigger node(s) if there is any
208112184SJan.Kryl@Sun.COM  *     unmount filesystem mounted on top of the node if there is any
208212184SJan.Kryl@Sun.COM  *
208312184SJan.Kryl@Sun.COM  * Function should be called with locked fnp's mutex. The mutex is
208412184SJan.Kryl@Sun.COM  * unlocked before return from function.
20850Sstevel@tonic-gate  */
208612184SJan.Kryl@Sun.COM static int
try_unmount_node(fnnode_t * fnp,boolean_t force)208712184SJan.Kryl@Sun.COM try_unmount_node(fnnode_t *fnp, boolean_t force)
20880Sstevel@tonic-gate {
208912184SJan.Kryl@Sun.COM 	boolean_t	trigger_unmount = B_FALSE;
209012184SJan.Kryl@Sun.COM 	action_list	*alp = NULL;
209112184SJan.Kryl@Sun.COM 	vnode_t		*vp;
209212184SJan.Kryl@Sun.COM 	int		error = 0;
209312184SJan.Kryl@Sun.COM 	fninfo_t	*fnip;
209412184SJan.Kryl@Sun.COM 	vfs_t		*vfsp;
209512184SJan.Kryl@Sun.COM 	struct autofs_globals *fngp;
20960Sstevel@tonic-gate 
209712184SJan.Kryl@Sun.COM 	AUTOFS_DPRINT((10, "\ttry_unmount_node: processing node %p\n",
209812184SJan.Kryl@Sun.COM 	    (void *)fnp));
20990Sstevel@tonic-gate 
210012184SJan.Kryl@Sun.COM 	ASSERT(MUTEX_HELD(&fnp->fn_lock));
210112184SJan.Kryl@Sun.COM 
210212184SJan.Kryl@Sun.COM 	fngp = fnp->fn_globals;
21030Sstevel@tonic-gate 	vp = fntovn(fnp);
21040Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
210512184SJan.Kryl@Sun.COM 
21060Sstevel@tonic-gate 	/*
210712184SJan.Kryl@Sun.COM 	 * If either a mount, lookup or another unmount of this subtree is in
210812184SJan.Kryl@Sun.COM 	 * progress, don't attempt to unmount at this time.
21090Sstevel@tonic-gate 	 */
211012184SJan.Kryl@Sun.COM 	if (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
211112184SJan.Kryl@Sun.COM 		mutex_exit(&fnp->fn_lock);
211212184SJan.Kryl@Sun.COM 		return (EBUSY);
21130Sstevel@tonic-gate 	}
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	/*
211612184SJan.Kryl@Sun.COM 	 * Bail out if someone else is holding reference to this vnode.
211712184SJan.Kryl@Sun.COM 	 * This check isn't just an optimization (someone is probably
211812184SJan.Kryl@Sun.COM 	 * just about to trigger mount). It is necessary to prevent a deadlock
211912184SJan.Kryl@Sun.COM 	 * in domount() called from auto_perform_actions() if unmount of
212012184SJan.Kryl@Sun.COM 	 * trigger parent fails. domount() calls lookupname() to resolve
212112184SJan.Kryl@Sun.COM 	 * special in mount arguments. Special is set to a map name in case
212212184SJan.Kryl@Sun.COM 	 * of autofs triggers (i.e. auto_ws.sun.com). Thus if current
212312184SJan.Kryl@Sun.COM 	 * working directory is set to currently processed node, lookupname()
212412184SJan.Kryl@Sun.COM 	 * calls into autofs vnops in order to resolve special, which deadlocks
212512184SJan.Kryl@Sun.COM 	 * the process.
212612184SJan.Kryl@Sun.COM 	 *
212712184SJan.Kryl@Sun.COM 	 * Note: This should be fixed. Autofs shouldn't pass the map name
212812184SJan.Kryl@Sun.COM 	 * in special and avoid useless lookup with potentially disasterous
212912184SJan.Kryl@Sun.COM 	 * consequence.
21300Sstevel@tonic-gate 	 */
213112184SJan.Kryl@Sun.COM 	if (check_auto_node(vp) == EBUSY) {
21320Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
213312184SJan.Kryl@Sun.COM 		return (EBUSY);
21340Sstevel@tonic-gate 	}
21350Sstevel@tonic-gate 
213612184SJan.Kryl@Sun.COM 	/*
213712184SJan.Kryl@Sun.COM 	 * If not forced operation, back out if node has been referenced
213812184SJan.Kryl@Sun.COM 	 * recently.
213912184SJan.Kryl@Sun.COM 	 */
214012184SJan.Kryl@Sun.COM 	if (!force &&
214112184SJan.Kryl@Sun.COM 	    fnp->fn_ref_time + fnip->fi_mount_to > gethrestime_sec()) {
214212184SJan.Kryl@Sun.COM 		mutex_exit(&fnp->fn_lock);
214312184SJan.Kryl@Sun.COM 		return (EBUSY);
214412184SJan.Kryl@Sun.COM 	}
214512184SJan.Kryl@Sun.COM 
214612184SJan.Kryl@Sun.COM 	/* block mounts/unmounts on the node */
21470Sstevel@tonic-gate 	AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
21480Sstevel@tonic-gate 	fnp->fn_error = 0;
21490Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
21500Sstevel@tonic-gate 
215112184SJan.Kryl@Sun.COM 	/* unmount next level triggers if there are any */
21520Sstevel@tonic-gate 	rw_enter(&fnp->fn_rwlock, RW_WRITER);
21530Sstevel@tonic-gate 	if (fnp->fn_trigger != NULL) {
215412184SJan.Kryl@Sun.COM 		trigger_unmount = B_TRUE;
21550Sstevel@tonic-gate 
215612184SJan.Kryl@Sun.COM 		if (triggers_busy(fnp)) {
21570Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
21580Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
21590Sstevel@tonic-gate 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
21600Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
216112184SJan.Kryl@Sun.COM 			return (EBUSY);
21620Sstevel@tonic-gate 		}
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 		/*
21650Sstevel@tonic-gate 		 * At this point, we know all trigger nodes are locked,
21660Sstevel@tonic-gate 		 * and they're not busy or mounted on.
216712184SJan.Kryl@Sun.COM 		 *
216812184SJan.Kryl@Sun.COM 		 * Attempt to unmount all trigger nodes, save the
216912184SJan.Kryl@Sun.COM 		 * action_list in case we need to remount them later.
217012184SJan.Kryl@Sun.COM 		 * The action_list will be freed later if there was no
217112184SJan.Kryl@Sun.COM 		 * need to remount the trigger nodes.
21720Sstevel@tonic-gate 		 */
217312184SJan.Kryl@Sun.COM 		unmount_triggers(fnp, &alp);
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 	rw_exit(&fnp->fn_rwlock);
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	(void) vn_vfswlock_wait(vp);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
21800Sstevel@tonic-gate 	if (vfsp != NULL) {
218112184SJan.Kryl@Sun.COM 		/* vn_vfsunlock(vp) is done inside unmount_node() */
21820Sstevel@tonic-gate 		error = unmount_node(vp, force);
21830Sstevel@tonic-gate 		if (error == ECONNRESET) {
21840Sstevel@tonic-gate 			if (vn_mountedvfs(vp) == NULL) {
21850Sstevel@tonic-gate 				/*
21860Sstevel@tonic-gate 				 * The filesystem was unmounted before the
21870Sstevel@tonic-gate 				 * daemon died. Unfortunately we can not
21880Sstevel@tonic-gate 				 * determine whether all the cleanup work was
21890Sstevel@tonic-gate 				 * successfully finished (i.e. update mnttab,
21900Sstevel@tonic-gate 				 * or notify NFS server of the unmount).
21910Sstevel@tonic-gate 				 * We should not retry the operation since the
21920Sstevel@tonic-gate 				 * filesystem has already been unmounted, and
21930Sstevel@tonic-gate 				 * may have already been removed from mnttab,
21940Sstevel@tonic-gate 				 * in such case the devid/rdevid we send to
21950Sstevel@tonic-gate 				 * the daemon will not be matched. So we have
21962170Sevanl 				 * to be content with the partial unmount.
21970Sstevel@tonic-gate 				 * Since the mountpoint is no longer covered, we
21980Sstevel@tonic-gate 				 * clear the error condition.
21990Sstevel@tonic-gate 				 */
22000Sstevel@tonic-gate 				error = 0;
22012170Sevanl 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
220212184SJan.Kryl@Sun.COM 				    CE_WARN, "autofs: automountd "
220312184SJan.Kryl@Sun.COM 				    "connection dropped when unmounting %s/%s",
220412184SJan.Kryl@Sun.COM 				    fnip->fi_path, (fnip->fi_flags & MF_DIRECT)
220512184SJan.Kryl@Sun.COM 				    ? "" : fnp->fn_name);
22060Sstevel@tonic-gate 			}
22070Sstevel@tonic-gate 		}
22080Sstevel@tonic-gate 	} else {
22090Sstevel@tonic-gate 		vn_vfsunlock(vp);
221012184SJan.Kryl@Sun.COM 		/* Destroy all dirents of fnp if we unmounted its triggers */
221112184SJan.Kryl@Sun.COM 		if (trigger_unmount)
221212184SJan.Kryl@Sun.COM 			unmount_autofs(vp);
221312184SJan.Kryl@Sun.COM 	}
221412184SJan.Kryl@Sun.COM 
221512184SJan.Kryl@Sun.COM 	/* If unmount failed, we got to remount triggers */
221612184SJan.Kryl@Sun.COM 	if (error != 0) {
221712184SJan.Kryl@Sun.COM 		if (trigger_unmount) {
221812184SJan.Kryl@Sun.COM 			int	ret;
221912184SJan.Kryl@Sun.COM 
222012184SJan.Kryl@Sun.COM 			ASSERT((fnp->fn_flags & MF_THISUID_MATCH_RQD) == 0);
22210Sstevel@tonic-gate 
222212184SJan.Kryl@Sun.COM 			/*
222312184SJan.Kryl@Sun.COM 			 * The action list was free'd by auto_perform_actions
222412184SJan.Kryl@Sun.COM 			 */
222512184SJan.Kryl@Sun.COM 			ret = auto_perform_actions(fnip, fnp, alp, CRED());
222612184SJan.Kryl@Sun.COM 			if (ret != 0) {
222712184SJan.Kryl@Sun.COM 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
222812184SJan.Kryl@Sun.COM 				    CE_WARN, "autofs: can't remount triggers "
222912184SJan.Kryl@Sun.COM 				    "fnp=%p error=%d", (void *)fnp, ret);
22300Sstevel@tonic-gate 			}
22310Sstevel@tonic-gate 		}
223212184SJan.Kryl@Sun.COM 		mutex_enter(&fnp->fn_lock);
223312184SJan.Kryl@Sun.COM 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
223412184SJan.Kryl@Sun.COM 		mutex_exit(&fnp->fn_lock);
223512184SJan.Kryl@Sun.COM 	} else {
223612184SJan.Kryl@Sun.COM 		/* Free the action list here */
223712184SJan.Kryl@Sun.COM 		if (trigger_unmount)
223812184SJan.Kryl@Sun.COM 			xdr_free(xdr_action_list, (char *)alp);
223912184SJan.Kryl@Sun.COM 
224012184SJan.Kryl@Sun.COM 		/*
224112184SJan.Kryl@Sun.COM 		 * Other threads may be waiting for this unmount to
224212184SJan.Kryl@Sun.COM 		 * finish. We must let it know that in order to
224312184SJan.Kryl@Sun.COM 		 * proceed, it must trigger the mount itself.
224412184SJan.Kryl@Sun.COM 		 */
224512184SJan.Kryl@Sun.COM 		mutex_enter(&fnp->fn_lock);
224612184SJan.Kryl@Sun.COM 		fnp->fn_flags &= ~MF_IK_MOUNT;
224712184SJan.Kryl@Sun.COM 		if (fnp->fn_flags & MF_WAITING)
224812184SJan.Kryl@Sun.COM 			fnp->fn_error = EAGAIN;
224912184SJan.Kryl@Sun.COM 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
225012184SJan.Kryl@Sun.COM 		mutex_exit(&fnp->fn_lock);
22510Sstevel@tonic-gate 	}
22520Sstevel@tonic-gate 
225312184SJan.Kryl@Sun.COM 	return (error);
225412184SJan.Kryl@Sun.COM }
225512184SJan.Kryl@Sun.COM 
225612184SJan.Kryl@Sun.COM /*
225712184SJan.Kryl@Sun.COM  * This is an implementation of depth-first search in a tree rooted by
225812184SJan.Kryl@Sun.COM  * start_fnp and composed from fnnodes. Links between tree levels are
225912184SJan.Kryl@Sun.COM  * fn_dirents, fn_trigger in fnnode_t and v_mountedvfs in vnode_t (if
226012184SJan.Kryl@Sun.COM  * mounted vfs is autofs). The algorithm keeps track of visited nodes
226112184SJan.Kryl@Sun.COM  * by means of a timestamp (fn_unmount_ref_time).
226212184SJan.Kryl@Sun.COM  *
226312184SJan.Kryl@Sun.COM  * Upon top-down traversal of the tree we apply following locking scheme:
226412184SJan.Kryl@Sun.COM  *	lock fn_rwlock of current node
226512184SJan.Kryl@Sun.COM  *	grab reference to child's vnode (VN_HOLD)
226612184SJan.Kryl@Sun.COM  *	unlock fn_rwlock
226712184SJan.Kryl@Sun.COM  *	free reference to current vnode (VN_RELE)
226812184SJan.Kryl@Sun.COM  * Similar locking scheme is used for down-top and left-right traversal.
226912184SJan.Kryl@Sun.COM  *
227012184SJan.Kryl@Sun.COM  * Algorithm examines the most down-left node in tree, which hasn't been
227112184SJan.Kryl@Sun.COM  * visited yet. From this follows that nodes are processed in bottom-up
227212184SJan.Kryl@Sun.COM  * fashion.
227312184SJan.Kryl@Sun.COM  *
227412184SJan.Kryl@Sun.COM  * Function returns either zero if unmount of root node was successful
227512184SJan.Kryl@Sun.COM  * or error code (mostly EBUSY).
227612184SJan.Kryl@Sun.COM  */
227712184SJan.Kryl@Sun.COM int
unmount_subtree(fnnode_t * rootfnp,boolean_t force)227812184SJan.Kryl@Sun.COM unmount_subtree(fnnode_t *rootfnp, boolean_t force)
227912184SJan.Kryl@Sun.COM {
228012184SJan.Kryl@Sun.COM 	fnnode_t	*currfnp; /* currently examined node in the tree */
228112184SJan.Kryl@Sun.COM 	fnnode_t	*lastfnp; /* previously processed node */
228212184SJan.Kryl@Sun.COM 	fnnode_t	*nextfnp; /* next examined node in the tree */
228312184SJan.Kryl@Sun.COM 	vnode_t		*curvp;
228412184SJan.Kryl@Sun.COM 	vnode_t		*newvp;
228512184SJan.Kryl@Sun.COM 	vfs_t		*vfsp;
228612184SJan.Kryl@Sun.COM 	time_t		timestamp;
228712184SJan.Kryl@Sun.COM 
228812184SJan.Kryl@Sun.COM 	ASSERT(fntovn(rootfnp)->v_type != VLNK);
228912184SJan.Kryl@Sun.COM 	AUTOFS_DPRINT((10, "unmount_subtree: root=%p (%s)\n", (void *)rootfnp,
229012184SJan.Kryl@Sun.COM 	    rootfnp->fn_name));
229112184SJan.Kryl@Sun.COM 
229212184SJan.Kryl@Sun.COM 	/*
229312184SJan.Kryl@Sun.COM 	 * Timestamp, which visited nodes are marked with, to distinguish them
229412184SJan.Kryl@Sun.COM 	 * from unvisited nodes.
229512184SJan.Kryl@Sun.COM 	 */
229612184SJan.Kryl@Sun.COM 	timestamp = gethrestime_sec();
229712184SJan.Kryl@Sun.COM 	currfnp = lastfnp = rootfnp;
229812184SJan.Kryl@Sun.COM 
229912184SJan.Kryl@Sun.COM 	/* Loop until we examine all nodes in the tree */
230012184SJan.Kryl@Sun.COM 	mutex_enter(&currfnp->fn_lock);
230112184SJan.Kryl@Sun.COM 	while (currfnp != rootfnp || rootfnp->fn_unmount_ref_time < timestamp) {
230212184SJan.Kryl@Sun.COM 		curvp = fntovn(currfnp);
230312184SJan.Kryl@Sun.COM 		AUTOFS_DPRINT((10, "\tunmount_subtree: entering node %p (%s)\n",
230412184SJan.Kryl@Sun.COM 		    (void *)currfnp, currfnp->fn_name));
230512184SJan.Kryl@Sun.COM 
230612184SJan.Kryl@Sun.COM 		/*
230712184SJan.Kryl@Sun.COM 		 * New candidate for processing must have been already visited,
230812184SJan.Kryl@Sun.COM 		 * by us because we want to process tree nodes in bottom-up
230912184SJan.Kryl@Sun.COM 		 * order.
231012184SJan.Kryl@Sun.COM 		 */
231112184SJan.Kryl@Sun.COM 		if (currfnp->fn_unmount_ref_time == timestamp &&
231212184SJan.Kryl@Sun.COM 		    currfnp != lastfnp) {
231312184SJan.Kryl@Sun.COM 			(void) try_unmount_node(currfnp, force);
231412184SJan.Kryl@Sun.COM 			lastfnp = currfnp;
231512184SJan.Kryl@Sun.COM 			mutex_enter(&currfnp->fn_lock);
23160Sstevel@tonic-gate 			/*
231712184SJan.Kryl@Sun.COM 			 * Fall through to next if-branch to pick
231812184SJan.Kryl@Sun.COM 			 * sibling or parent of this node.
23190Sstevel@tonic-gate 			 */
23200Sstevel@tonic-gate 		}
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 		/*
232312184SJan.Kryl@Sun.COM 		 * If this node has been already visited, it means that it's
232412184SJan.Kryl@Sun.COM 		 * dead end and we need to pick sibling or parent as next node.
23250Sstevel@tonic-gate 		 */
232612184SJan.Kryl@Sun.COM 		if (currfnp->fn_unmount_ref_time >= timestamp ||
232712184SJan.Kryl@Sun.COM 		    curvp->v_type == VLNK) {
232812184SJan.Kryl@Sun.COM 			mutex_exit(&currfnp->fn_lock);
232912184SJan.Kryl@Sun.COM 			/*
233012184SJan.Kryl@Sun.COM 			 * Obtain parent's readers lock before grabbing
233112184SJan.Kryl@Sun.COM 			 * reference to sibling.
233212184SJan.Kryl@Sun.COM 			 */
233312184SJan.Kryl@Sun.COM 			rw_enter(&currfnp->fn_parent->fn_rwlock, RW_READER);
233412184SJan.Kryl@Sun.COM 			if ((nextfnp = currfnp->fn_next) != NULL) {
233512184SJan.Kryl@Sun.COM 				VN_HOLD(fntovn(nextfnp));
233612184SJan.Kryl@Sun.COM 				rw_exit(&currfnp->fn_parent->fn_rwlock);
233712184SJan.Kryl@Sun.COM 				VN_RELE(curvp);
233812184SJan.Kryl@Sun.COM 				currfnp = nextfnp;
233912184SJan.Kryl@Sun.COM 				mutex_enter(&currfnp->fn_lock);
234012184SJan.Kryl@Sun.COM 				continue;
234112184SJan.Kryl@Sun.COM 			}
234212184SJan.Kryl@Sun.COM 			rw_exit(&currfnp->fn_parent->fn_rwlock);
23430Sstevel@tonic-gate 
234412184SJan.Kryl@Sun.COM 			/*
234512184SJan.Kryl@Sun.COM 			 * All descendants and siblings were visited. Perform
234612184SJan.Kryl@Sun.COM 			 * bottom-up move.
234712184SJan.Kryl@Sun.COM 			 */
234812184SJan.Kryl@Sun.COM 			nextfnp = currfnp->fn_parent;
234912184SJan.Kryl@Sun.COM 			VN_HOLD(fntovn(nextfnp));
235012184SJan.Kryl@Sun.COM 			VN_RELE(curvp);
235112184SJan.Kryl@Sun.COM 			currfnp = nextfnp;
235212184SJan.Kryl@Sun.COM 			mutex_enter(&currfnp->fn_lock);
235312184SJan.Kryl@Sun.COM 			continue;
235412184SJan.Kryl@Sun.COM 		}
235512184SJan.Kryl@Sun.COM 
235612184SJan.Kryl@Sun.COM 		/*
235712184SJan.Kryl@Sun.COM 		 * Mark node as visited. Note that the timestamp could have
235812184SJan.Kryl@Sun.COM 		 * been updated by somebody else in the meantime.
235912184SJan.Kryl@Sun.COM 		 */
236012184SJan.Kryl@Sun.COM 		if (currfnp->fn_unmount_ref_time < timestamp)
236112184SJan.Kryl@Sun.COM 			currfnp->fn_unmount_ref_time = timestamp;
2362*12883SJan.Kryl@Sun.COM 
2363*12883SJan.Kryl@Sun.COM 		/*
2364*12883SJan.Kryl@Sun.COM 		 * Don't descent below nodes, which are being unmounted/mounted.
2365*12883SJan.Kryl@Sun.COM 		 *
2366*12883SJan.Kryl@Sun.COM 		 * We need to hold both locks at once: fn_lock because we need
2367*12883SJan.Kryl@Sun.COM 		 * to read MF_INPROG and fn_rwlock to prevent anybody from
2368*12883SJan.Kryl@Sun.COM 		 * modifying fn_trigger until its used to traverse triggers
2369*12883SJan.Kryl@Sun.COM 		 * below.
2370*12883SJan.Kryl@Sun.COM 		 *
2371*12883SJan.Kryl@Sun.COM 		 * Acquire fn_rwlock in non-blocking mode to avoid deadlock.
2372*12883SJan.Kryl@Sun.COM 		 * If it can't be acquired, then acquire locks in correct
2373*12883SJan.Kryl@Sun.COM 		 * order.
2374*12883SJan.Kryl@Sun.COM 		 */
2375*12883SJan.Kryl@Sun.COM 		if (!rw_tryenter(&currfnp->fn_rwlock, RW_READER)) {
2376*12883SJan.Kryl@Sun.COM 			mutex_exit(&currfnp->fn_lock);
2377*12883SJan.Kryl@Sun.COM 			rw_enter(&currfnp->fn_rwlock, RW_READER);
2378*12883SJan.Kryl@Sun.COM 			mutex_enter(&currfnp->fn_lock);
2379*12883SJan.Kryl@Sun.COM 		}
2380*12883SJan.Kryl@Sun.COM 		if (currfnp->fn_flags & MF_INPROG) {
2381*12883SJan.Kryl@Sun.COM 			rw_exit(&currfnp->fn_rwlock);
2382*12883SJan.Kryl@Sun.COM 			continue;
2383*12883SJan.Kryl@Sun.COM 		}
238412184SJan.Kryl@Sun.COM 		mutex_exit(&currfnp->fn_lock);
23850Sstevel@tonic-gate 
238612184SJan.Kryl@Sun.COM 		/*
238712184SJan.Kryl@Sun.COM 		 * Examine descendants in this order: triggers, dirents, autofs
238812184SJan.Kryl@Sun.COM 		 * mounts.
238912184SJan.Kryl@Sun.COM 		 */
239012184SJan.Kryl@Sun.COM 
239112184SJan.Kryl@Sun.COM 		if ((nextfnp = currfnp->fn_trigger) != NULL) {
239212184SJan.Kryl@Sun.COM 			VN_HOLD(fntovn(nextfnp));
239312184SJan.Kryl@Sun.COM 			rw_exit(&currfnp->fn_rwlock);
239412184SJan.Kryl@Sun.COM 			VN_RELE(curvp);
239512184SJan.Kryl@Sun.COM 			currfnp = nextfnp;
239612184SJan.Kryl@Sun.COM 			mutex_enter(&currfnp->fn_lock);
239712184SJan.Kryl@Sun.COM 			continue;
239812184SJan.Kryl@Sun.COM 		}
239912184SJan.Kryl@Sun.COM 
240012184SJan.Kryl@Sun.COM 		if ((nextfnp = currfnp->fn_dirents) != NULL) {
240112184SJan.Kryl@Sun.COM 			VN_HOLD(fntovn(nextfnp));
240212184SJan.Kryl@Sun.COM 			rw_exit(&currfnp->fn_rwlock);
240312184SJan.Kryl@Sun.COM 			VN_RELE(curvp);
240412184SJan.Kryl@Sun.COM 			currfnp = nextfnp;
240512184SJan.Kryl@Sun.COM 			mutex_enter(&currfnp->fn_lock);
240612184SJan.Kryl@Sun.COM 			continue;
24070Sstevel@tonic-gate 		}
240812184SJan.Kryl@Sun.COM 		rw_exit(&currfnp->fn_rwlock);
24090Sstevel@tonic-gate 
241012184SJan.Kryl@Sun.COM 		(void) vn_vfswlock_wait(curvp);
241112184SJan.Kryl@Sun.COM 		vfsp = vn_mountedvfs(curvp);
241212184SJan.Kryl@Sun.COM 		if (vfsp != NULL &&
241312184SJan.Kryl@Sun.COM 		    vfs_matchops(vfsp, vfs_getops(curvp->v_vfsp))) {
241412184SJan.Kryl@Sun.COM 			/*
241512184SJan.Kryl@Sun.COM 			 * Deal with /xfn/host/jurassic alikes here...
241612184SJan.Kryl@Sun.COM 			 *
241712184SJan.Kryl@Sun.COM 			 * We know this call to VFS_ROOT is safe to call while
241812184SJan.Kryl@Sun.COM 			 * holding VVFSLOCK, since it resolves to a call to
241912184SJan.Kryl@Sun.COM 			 * auto_root().
242012184SJan.Kryl@Sun.COM 			 */
242112184SJan.Kryl@Sun.COM 			if (VFS_ROOT(vfsp, &newvp)) {
242212184SJan.Kryl@Sun.COM 				cmn_err(CE_PANIC,
242312184SJan.Kryl@Sun.COM 				    "autofs: VFS_ROOT(vfs=%p) failed",
242412184SJan.Kryl@Sun.COM 				    (void *)vfsp);
242512184SJan.Kryl@Sun.COM 			}
242612184SJan.Kryl@Sun.COM 			vn_vfsunlock(curvp);
242712184SJan.Kryl@Sun.COM 			VN_RELE(curvp);
242812184SJan.Kryl@Sun.COM 			currfnp = vntofn(newvp);
242912184SJan.Kryl@Sun.COM 			mutex_enter(&currfnp->fn_lock);
243012184SJan.Kryl@Sun.COM 			continue;
243112184SJan.Kryl@Sun.COM 		}
243212184SJan.Kryl@Sun.COM 		vn_vfsunlock(curvp);
243312184SJan.Kryl@Sun.COM 		mutex_enter(&currfnp->fn_lock);
24340Sstevel@tonic-gate 	}
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	/*
243712184SJan.Kryl@Sun.COM 	 * Now we deal with the root node (currfnp's mutex is unlocked
243812184SJan.Kryl@Sun.COM 	 * in try_unmount_node()).
24390Sstevel@tonic-gate 	 */
244012184SJan.Kryl@Sun.COM 	return (try_unmount_node(currfnp, force));
244112184SJan.Kryl@Sun.COM }
244212184SJan.Kryl@Sun.COM 
244312184SJan.Kryl@Sun.COM /*
244412184SJan.Kryl@Sun.COM  * XXX unmount_tree() is not suspend-safe within the scope of
244512184SJan.Kryl@Sun.COM  * the present model defined for cpr to suspend the system. Calls made
244612184SJan.Kryl@Sun.COM  * by the unmount_tree() that have been identified to be unsafe are
244712184SJan.Kryl@Sun.COM  * (1) RPC client handle setup and client calls to automountd which can
244812184SJan.Kryl@Sun.COM  * block deep down in the RPC library, (2) kmem_alloc() calls with the
244912184SJan.Kryl@Sun.COM  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*() and
245012184SJan.Kryl@Sun.COM  * VOP_*() calls which can result in over the wire calls to servers.
245112184SJan.Kryl@Sun.COM  * The thread should be completely reevaluated to make it suspend-safe in
245212184SJan.Kryl@Sun.COM  * case of future updates to the cpr model.
245312184SJan.Kryl@Sun.COM  */
245412184SJan.Kryl@Sun.COM void
unmount_tree(struct autofs_globals * fngp,boolean_t force)245512184SJan.Kryl@Sun.COM unmount_tree(struct autofs_globals *fngp, boolean_t force)
245612184SJan.Kryl@Sun.COM {
245712184SJan.Kryl@Sun.COM 	callb_cpr_t	cprinfo;
245812184SJan.Kryl@Sun.COM 	kmutex_t	unmount_tree_cpr_lock;
245912184SJan.Kryl@Sun.COM 	fnnode_t	*root, *fnp, *next;
246012184SJan.Kryl@Sun.COM 
246112184SJan.Kryl@Sun.COM 	mutex_init(&unmount_tree_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
246212184SJan.Kryl@Sun.COM 	CALLB_CPR_INIT(&cprinfo, &unmount_tree_cpr_lock, callb_generic_cpr,
246312184SJan.Kryl@Sun.COM 	    "unmount_tree");
246412184SJan.Kryl@Sun.COM 
246512184SJan.Kryl@Sun.COM 	/*
246612184SJan.Kryl@Sun.COM 	 * autofssys() will be calling in from the global zone and doing
246712184SJan.Kryl@Sun.COM 	 * work on the behalf of the given zone, hence we can't always
246812184SJan.Kryl@Sun.COM 	 * assert that we have the right credentials, nor that the
246912184SJan.Kryl@Sun.COM 	 * caller is always in the correct zone.
247012184SJan.Kryl@Sun.COM 	 *
247112184SJan.Kryl@Sun.COM 	 * We do, however, know that if this is a "forced unmount"
247212184SJan.Kryl@Sun.COM 	 * operation (which autofssys() does), then we won't go down to
247312184SJan.Kryl@Sun.COM 	 * the krpc layers, so we don't need to fudge with the
247412184SJan.Kryl@Sun.COM 	 * credentials.
247512184SJan.Kryl@Sun.COM 	 */
247612184SJan.Kryl@Sun.COM 	ASSERT(force || fngp->fng_zoneid == getzoneid());
247712184SJan.Kryl@Sun.COM 
247812184SJan.Kryl@Sun.COM 	/*
247912184SJan.Kryl@Sun.COM 	 * If automountd is not running in this zone,
248012184SJan.Kryl@Sun.COM 	 * don't attempt unmounting this round.
248112184SJan.Kryl@Sun.COM 	 */
248212184SJan.Kryl@Sun.COM 	if (force || auto_null_request(fngp->fng_zoneid, FALSE) == 0) {
24830Sstevel@tonic-gate 		/*
248412184SJan.Kryl@Sun.COM 		 * Iterate over top level autofs filesystems and call
248512184SJan.Kryl@Sun.COM 		 * unmount_subtree() for each of them.
24860Sstevel@tonic-gate 		 */
248712184SJan.Kryl@Sun.COM 		root = fngp->fng_rootfnnodep;
248812184SJan.Kryl@Sun.COM 		rw_enter(&root->fn_rwlock, RW_READER);
248912184SJan.Kryl@Sun.COM 		for (fnp = root->fn_dirents; fnp != NULL; fnp = next) {
249012184SJan.Kryl@Sun.COM 			VN_HOLD(fntovn(fnp));
249112184SJan.Kryl@Sun.COM 			rw_exit(&root->fn_rwlock);
249212184SJan.Kryl@Sun.COM 			(void) unmount_subtree(fnp, force);
249312184SJan.Kryl@Sun.COM 			rw_enter(&root->fn_rwlock, RW_READER);
249412184SJan.Kryl@Sun.COM 			next = fnp->fn_next;
249512184SJan.Kryl@Sun.COM 			VN_RELE(fntovn(fnp));
249612184SJan.Kryl@Sun.COM 		}
249712184SJan.Kryl@Sun.COM 		rw_exit(&root->fn_rwlock);
24980Sstevel@tonic-gate 	}
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	mutex_enter(&unmount_tree_cpr_lock);
25010Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
25020Sstevel@tonic-gate 	mutex_destroy(&unmount_tree_cpr_lock);
25030Sstevel@tonic-gate }
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate static void
unmount_zone_tree(struct autofs_globals * fngp)25060Sstevel@tonic-gate unmount_zone_tree(struct autofs_globals *fngp)
25070Sstevel@tonic-gate {
250812184SJan.Kryl@Sun.COM 	AUTOFS_DPRINT((5, "unmount_zone_tree started. Thread created.\n"));
250912184SJan.Kryl@Sun.COM 
251012184SJan.Kryl@Sun.COM 	unmount_tree(fngp, B_FALSE);
25110Sstevel@tonic-gate 	mutex_enter(&fngp->fng_unmount_threads_lock);
25120Sstevel@tonic-gate 	fngp->fng_unmount_threads--;
25130Sstevel@tonic-gate 	mutex_exit(&fngp->fng_unmount_threads_lock);
25140Sstevel@tonic-gate 
251512184SJan.Kryl@Sun.COM 	AUTOFS_DPRINT((5, "unmount_zone_tree done. Thread exiting.\n"));
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	zthread_exit();
25180Sstevel@tonic-gate 	/* NOTREACHED */
25190Sstevel@tonic-gate }
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate void
auto_do_unmount(struct autofs_globals * fngp)25220Sstevel@tonic-gate auto_do_unmount(struct autofs_globals *fngp)
25230Sstevel@tonic-gate {
25240Sstevel@tonic-gate 	callb_cpr_t cprinfo;
25250Sstevel@tonic-gate 	clock_t timeleft;
25260Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &fngp->fng_unmount_threads_lock,
25296046Srmesta 	    callb_generic_cpr, "auto_do_unmount");
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	for (;;) {	/* forever */
25320Sstevel@tonic-gate 		mutex_enter(&fngp->fng_unmount_threads_lock);
25330Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
25340Sstevel@tonic-gate newthread:
25350Sstevel@tonic-gate 		mutex_exit(&fngp->fng_unmount_threads_lock);
253611066Srafael.vanoni@sun.com 		timeleft = zone_status_timedwait(zone, ddi_get_lbolt() +
25370Sstevel@tonic-gate 		    autofs_unmount_thread_timer * hz, ZONE_IS_SHUTTING_DOWN);
25380Sstevel@tonic-gate 		mutex_enter(&fngp->fng_unmount_threads_lock);
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 		if (timeleft != -1) {	/* didn't time out */
25410Sstevel@tonic-gate 			ASSERT(zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN);
25420Sstevel@tonic-gate 			/*
25430Sstevel@tonic-gate 			 * zone is exiting... don't create any new threads.
25440Sstevel@tonic-gate 			 * fng_unmount_threads_lock is released implicitly by
25450Sstevel@tonic-gate 			 * the below.
25460Sstevel@tonic-gate 			 */
25470Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo,
25486046Srmesta 			    &fngp->fng_unmount_threads_lock);
25490Sstevel@tonic-gate 			CALLB_CPR_EXIT(&cprinfo);
25500Sstevel@tonic-gate 			zthread_exit();
25510Sstevel@tonic-gate 			/* NOTREACHED */
25520Sstevel@tonic-gate 		}
25530Sstevel@tonic-gate 		if (fngp->fng_unmount_threads < autofs_unmount_threads) {
25540Sstevel@tonic-gate 			fngp->fng_unmount_threads++;
25550Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo,
25566046Srmesta 			    &fngp->fng_unmount_threads_lock);
25570Sstevel@tonic-gate 			mutex_exit(&fngp->fng_unmount_threads_lock);
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 			(void) zthread_create(NULL, 0, unmount_zone_tree, fngp,
25600Sstevel@tonic-gate 			    0, minclsyspri);
25610Sstevel@tonic-gate 		} else
25620Sstevel@tonic-gate 			goto newthread;
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 	/* NOTREACHED */
25650Sstevel@tonic-gate }
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate /*
25680Sstevel@tonic-gate  * Is nobrowse specified in option string?
25690Sstevel@tonic-gate  * opts should be a null ('\0') terminated string.
25700Sstevel@tonic-gate  * Returns non-zero if nobrowse has been specified.
25710Sstevel@tonic-gate  */
25720Sstevel@tonic-gate int
auto_nobrowse_option(char * opts)25730Sstevel@tonic-gate auto_nobrowse_option(char *opts)
25740Sstevel@tonic-gate {
25750Sstevel@tonic-gate 	char *buf;
25760Sstevel@tonic-gate 	char *p;
25770Sstevel@tonic-gate 	char *t;
25780Sstevel@tonic-gate 	int nobrowse = 0;
25790Sstevel@tonic-gate 	int last_opt = 0;
25800Sstevel@tonic-gate 	size_t len;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	len = strlen(opts) + 1;
25830Sstevel@tonic-gate 	p = buf = kmem_alloc(len, KM_SLEEP);
25840Sstevel@tonic-gate 	(void) strcpy(buf, opts);
25850Sstevel@tonic-gate 	do {
25860Sstevel@tonic-gate 		if (t = strchr(p, ','))
25870Sstevel@tonic-gate 			*t++ = '\0';
25880Sstevel@tonic-gate 		else
25890Sstevel@tonic-gate 			last_opt++;
25900Sstevel@tonic-gate 		if (strcmp(p, MNTOPT_NOBROWSE) == 0)
25910Sstevel@tonic-gate 			nobrowse = 1;
25920Sstevel@tonic-gate 		else if (strcmp(p, MNTOPT_BROWSE) == 0)
25930Sstevel@tonic-gate 			nobrowse = 0;
25940Sstevel@tonic-gate 		p = t;
25950Sstevel@tonic-gate 	} while (!last_opt);
25960Sstevel@tonic-gate 	kmem_free(buf, len);
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	return (nobrowse);
25990Sstevel@tonic-gate }
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate /*
26020Sstevel@tonic-gate  * used to log warnings only if automountd is running
26030Sstevel@tonic-gate  * with verbose mode set
26040Sstevel@tonic-gate  */
26052170Sevanl 
26060Sstevel@tonic-gate void
auto_log(int verbose,zoneid_t zoneid,int level,const char * fmt,...)26072170Sevanl auto_log(int verbose, zoneid_t zoneid, int level, const char *fmt, ...)
26080Sstevel@tonic-gate {
26092170Sevanl 	va_list	args;
26100Sstevel@tonic-gate 
26112170Sevanl 	if (verbose) {
26120Sstevel@tonic-gate 		va_start(args, fmt);
26132170Sevanl 		vzcmn_err(zoneid, level, fmt, args);
26140Sstevel@tonic-gate 		va_end(args);
26150Sstevel@tonic-gate 	}
26160Sstevel@tonic-gate }
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate #ifdef DEBUG
26190Sstevel@tonic-gate static int autofs_debug = 0;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate /*
26220Sstevel@tonic-gate  * Utilities used by both client and server
26230Sstevel@tonic-gate  * Standard levels:
26240Sstevel@tonic-gate  * 0) no debugging
26250Sstevel@tonic-gate  * 1) hard failures
26260Sstevel@tonic-gate  * 2) soft failures
26270Sstevel@tonic-gate  * 3) current test software
26280Sstevel@tonic-gate  * 4) main procedure entry points
26290Sstevel@tonic-gate  * 5) main procedure exit points
26300Sstevel@tonic-gate  * 6) utility procedure entry points
26310Sstevel@tonic-gate  * 7) utility procedure exit points
26320Sstevel@tonic-gate  * 8) obscure procedure entry points
26330Sstevel@tonic-gate  * 9) obscure procedure exit points
26340Sstevel@tonic-gate  * 10) random stuff
26350Sstevel@tonic-gate  * 11) all <= 1
26360Sstevel@tonic-gate  * 12) all <= 2
26370Sstevel@tonic-gate  * 13) all <= 3
26380Sstevel@tonic-gate  * ...
26390Sstevel@tonic-gate  */
26400Sstevel@tonic-gate /* PRINTFLIKE2 */
26410Sstevel@tonic-gate void
auto_dprint(int level,const char * fmt,...)26420Sstevel@tonic-gate auto_dprint(int level, const char *fmt, ...)
26430Sstevel@tonic-gate {
26440Sstevel@tonic-gate 	va_list args;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	if (autofs_debug == level ||
26470Sstevel@tonic-gate 	    (autofs_debug > 10 && (autofs_debug - 10) >= level)) {
26480Sstevel@tonic-gate 		va_start(args, fmt);
26490Sstevel@tonic-gate 		(void) vprintf(fmt, args);
26500Sstevel@tonic-gate 		va_end(args);
26510Sstevel@tonic-gate 	}
26520Sstevel@tonic-gate }
26530Sstevel@tonic-gate #endif /* DEBUG */
2654