xref: /onnv-gate/usr/src/uts/common/fs/autofs/auto_subr.c (revision 2170:eb691d2a219e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2170Sevanl  * Common Development and Distribution License (the "License").
6*2170Sevanl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2170Sevanl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/proc.h>
320Sstevel@tonic-gate #include <sys/disp.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
340Sstevel@tonic-gate #include <sys/vnode.h>
350Sstevel@tonic-gate #include <sys/pathname.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/mount.h>
380Sstevel@tonic-gate #include <sys/cmn_err.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/systm.h>
410Sstevel@tonic-gate #include <sys/dirent.h>
420Sstevel@tonic-gate #include <fs/fs_subr.h>
430Sstevel@tonic-gate #include <sys/fs/autofs.h>
440Sstevel@tonic-gate #include <sys/callb.h>
450Sstevel@tonic-gate #include <sys/sysmacros.h>
460Sstevel@tonic-gate #include <sys/zone.h>
47*2170Sevanl #include <sys/door.h>
480Sstevel@tonic-gate #include <sys/fs/mntdata.h>
49*2170Sevanl #include <nfs/mount.h>
50*2170Sevanl #include <rpc/clnt.h>
51*2170Sevanl #include <rpcsvc/autofs_prot.h>
52*2170Sevanl #include <nfs/rnode.h>
53*2170Sevanl #include <sys/utsname.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Autofs and Zones:
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * Zones are delegated the responsibility of managing their own autofs mounts
590Sstevel@tonic-gate  * and maps.  Each zone runs its own copy of automountd, with its own timeouts,
600Sstevel@tonic-gate  * and other logically "global" parameters.  kRPC and virtualization in the
610Sstevel@tonic-gate  * loopback transport (tl) will prevent a zone from communicating with another
620Sstevel@tonic-gate  * zone's automountd.
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * Each zone has its own "rootfnnode" and associated tree of auto nodes.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * Each zone also has its own set of "unmounter" kernel threads; these are
670Sstevel@tonic-gate  * created and run within the zone's context (ie, they are created via
680Sstevel@tonic-gate  * zthread_create()).
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  * Cross-zone mount triggers are disallowed.  There is a check in
710Sstevel@tonic-gate  * auto_trigger_mount() to this effect; EPERM is returned to indicate that the
720Sstevel@tonic-gate  * mount is not owned by the caller.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * autofssys() enables a caller in the global zone to clean up in-kernel (as
750Sstevel@tonic-gate  * well as regular) autofs mounts via the unmount_tree() mechanism.  This is
760Sstevel@tonic-gate  * routinely done when all mounts are removed as part of zone shutdown.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate #define	TYPICALMAXPATHLEN	64
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static kmutex_t autofs_nodeid_lock;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int auto_perform_link(fnnode_t *, struct linka *, cred_t *);
830Sstevel@tonic-gate static int auto_perform_actions(fninfo_t *, fnnode_t *,
840Sstevel@tonic-gate     action_list *, cred_t *);
850Sstevel@tonic-gate static int auto_getmntpnt(vnode_t *, char *, vnode_t **, cred_t *);
860Sstevel@tonic-gate static int auto_lookup_request(fninfo_t *, char *, struct linka *,
87*2170Sevanl     bool_t, bool_t *);
880Sstevel@tonic-gate static int auto_mount_request(fninfo_t *, char *, action_list **,
89*2170Sevanl     bool_t);
90*2170Sevanl 
91*2170Sevanl extern struct autofs_globals *autofs_zone_init(void);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Clears the MF_INPROG flag, and wakes up those threads sleeping on
950Sstevel@tonic-gate  * fn_cv_mount if MF_WAITING is set.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate void
980Sstevel@tonic-gate auto_unblock_others(
990Sstevel@tonic-gate 	fnnode_t *fnp,
1000Sstevel@tonic-gate 	uint_t operation)		/* either MF_INPROG or MF_LOOKUP */
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	ASSERT(operation & (MF_INPROG | MF_LOOKUP));
1030Sstevel@tonic-gate 	fnp->fn_flags &= ~operation;
1040Sstevel@tonic-gate 	if (fnp->fn_flags & MF_WAITING) {
1050Sstevel@tonic-gate 		fnp->fn_flags &= ~MF_WAITING;
1060Sstevel@tonic-gate 		cv_broadcast(&fnp->fn_cv_mount);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate int
1110Sstevel@tonic-gate auto_wait4mount(fnnode_t *fnp)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	int error;
1140Sstevel@tonic-gate 	k_sigset_t smask;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_wait4mount: fnp=%p\n", (void *)fnp));
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
1190Sstevel@tonic-gate 	while (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
1200Sstevel@tonic-gate 		/*
1210Sstevel@tonic-gate 		 * There is a mount or a lookup in progress.
1220Sstevel@tonic-gate 		 */
1230Sstevel@tonic-gate 		fnp->fn_flags |= MF_WAITING;
1240Sstevel@tonic-gate 		sigintr(&smask, 1);
1250Sstevel@tonic-gate 		if (!cv_wait_sig(&fnp->fn_cv_mount, &fnp->fn_lock)) {
1260Sstevel@tonic-gate 			/*
1270Sstevel@tonic-gate 			 * Decided not to wait for operation to
1280Sstevel@tonic-gate 			 * finish after all.
1290Sstevel@tonic-gate 			 */
1300Sstevel@tonic-gate 			sigunintr(&smask);
1310Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1320Sstevel@tonic-gate 			return (EINTR);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 		sigunintr(&smask);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 	error = fnp->fn_error;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (error == EINTR) {
1390Sstevel@tonic-gate 		/*
1400Sstevel@tonic-gate 		 * The thread doing the mount got interrupted, we need to
1410Sstevel@tonic-gate 		 * try again, by returning EAGAIN.
1420Sstevel@tonic-gate 		 */
1430Sstevel@tonic-gate 		error = EAGAIN;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_wait4mount: fnp=%p error=%d\n", (void *)fnp,
1480Sstevel@tonic-gate 	    error));
1490Sstevel@tonic-gate 	return (error);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate int
1530Sstevel@tonic-gate auto_lookup_aux(fnnode_t *fnp, char *name, cred_t *cred)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	struct fninfo *fnip;
1560Sstevel@tonic-gate 	struct linka link;
1570Sstevel@tonic-gate 	bool_t mountreq = FALSE;
1580Sstevel@tonic-gate 	int error = 0;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	fnip = vfstofni(fntovn(fnp)->v_vfsp);
1610Sstevel@tonic-gate 	bzero(&link, sizeof (link));
162*2170Sevanl 	error = auto_lookup_request(fnip, name, &link, TRUE, &mountreq);
1630Sstevel@tonic-gate 	if (!error) {
164*2170Sevanl 		if (link.link != NULL || link.link != '\0') {
1650Sstevel@tonic-gate 			/*
1660Sstevel@tonic-gate 			 * This node should be a symlink
1670Sstevel@tonic-gate 			 */
1680Sstevel@tonic-gate 			error = auto_perform_link(fnp, &link, cred);
1690Sstevel@tonic-gate 		} else if (mountreq) {
1700Sstevel@tonic-gate 			/*
1710Sstevel@tonic-gate 			 * The automount daemon is requesting a mount,
1720Sstevel@tonic-gate 			 * implying this entry must be a wildcard match and
1730Sstevel@tonic-gate 			 * therefore in need of verification that the entry
1740Sstevel@tonic-gate 			 * exists on the server.
1750Sstevel@tonic-gate 			 */
1760Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
1770Sstevel@tonic-gate 			AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
1780Sstevel@tonic-gate 			fnp->fn_error = 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			/*
1810Sstevel@tonic-gate 			 * Unblock other lookup requests on this node,
1820Sstevel@tonic-gate 			 * this is needed to let the lookup generated by
1830Sstevel@tonic-gate 			 * the mount call to complete. The caveat is
1840Sstevel@tonic-gate 			 * other lookups on this node can also get by,
1850Sstevel@tonic-gate 			 * i.e., another lookup on this node that occurs
1860Sstevel@tonic-gate 			 * while this lookup is attempting the mount
1870Sstevel@tonic-gate 			 * would return a positive result no matter what.
1880Sstevel@tonic-gate 			 * Therefore two lookups on the this node could
1890Sstevel@tonic-gate 			 * potentially get disparate results.
1900Sstevel@tonic-gate 			 */
1910Sstevel@tonic-gate 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
1920Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
1930Sstevel@tonic-gate 			/*
1940Sstevel@tonic-gate 			 * auto_new_mount_thread fires up a new thread which
1950Sstevel@tonic-gate 			 * calls automountd finishing up the work
1960Sstevel@tonic-gate 			 */
1970Sstevel@tonic-gate 			auto_new_mount_thread(fnp, name, cred);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 			/*
2000Sstevel@tonic-gate 			 * At this point, we are simply another thread
2010Sstevel@tonic-gate 			 * waiting for the mount to complete
2020Sstevel@tonic-gate 			 */
2030Sstevel@tonic-gate 			error = auto_wait4mount(fnp);
2040Sstevel@tonic-gate 			if (error == AUTOFS_SHUTDOWN)
2050Sstevel@tonic-gate 				error = ENOENT;
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
209*2170Sevanl 	if (link.link)
210*2170Sevanl 		kmem_free(link.link, strlen(link.link) + 1);
211*2170Sevanl 	if (link.dir)
212*2170Sevanl 		kmem_free(link.dir, strlen(link.dir) + 1);
2130Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
2140Sstevel@tonic-gate 	fnp->fn_error = error;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * Notify threads waiting for lookup/mount that
2180Sstevel@tonic-gate 	 * it's done.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (mountreq) {
2210Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2220Sstevel@tonic-gate 	} else {
2230Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
2260Sstevel@tonic-gate 	return (error);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * Starting point for thread to handle mount requests with automountd.
2310Sstevel@tonic-gate  * XXX auto_mount_thread() is not suspend-safe within the scope of
2320Sstevel@tonic-gate  * the present model defined for cpr to suspend the system. Calls
2330Sstevel@tonic-gate  * made by the auto_mount_thread() that have been identified to be unsafe
2340Sstevel@tonic-gate  * are (1) RPC client handle setup and client calls to automountd which
2350Sstevel@tonic-gate  * can block deep down in the RPC library, (2) kmem_alloc() calls with the
2360Sstevel@tonic-gate  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*(), and
2370Sstevel@tonic-gate  * lookuppnvp() calls which can result in over the wire calls to servers.
2380Sstevel@tonic-gate  * The thread should be completely reevaluated to make it suspend-safe in
2390Sstevel@tonic-gate  * case of future updates to the cpr model.
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate static void
2420Sstevel@tonic-gate auto_mount_thread(struct autofs_callargs *argsp)
2430Sstevel@tonic-gate {
244*2170Sevanl 	struct fninfo 		*fnip;
245*2170Sevanl 	fnnode_t 		*fnp;
246*2170Sevanl 	vnode_t 		*vp;
247*2170Sevanl 	char 			*name;
248*2170Sevanl 	size_t 			namelen;
249*2170Sevanl 	cred_t 			*cred;
250*2170Sevanl 	action_list		*alp = NULL;
251*2170Sevanl 	int 			error;
252*2170Sevanl 	callb_cpr_t 		cprinfo;
253*2170Sevanl 	kmutex_t 		auto_mount_thread_cpr_lock;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	mutex_init(&auto_mount_thread_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
256*2170Sevanl 	CALLB_CPR_INIT(&cprinfo, &auto_mount_thread_cpr_lock,
257*2170Sevanl 		callb_generic_cpr, "auto_mount_thread");
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	fnp = argsp->fnc_fnp;
2600Sstevel@tonic-gate 	vp = fntovn(fnp);
2610Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
2620Sstevel@tonic-gate 	name = argsp->fnc_name;
2630Sstevel@tonic-gate 	cred = argsp->fnc_cred;
2640Sstevel@tonic-gate 	ASSERT(crgetzoneid(argsp->fnc_cred) == fnip->fi_zoneid);
2650Sstevel@tonic-gate 
266*2170Sevanl 	error = auto_mount_request(fnip, name, &alp, TRUE);
2670Sstevel@tonic-gate 	if (!error)
2680Sstevel@tonic-gate 		error = auto_perform_actions(fnip, fnp, alp, cred);
2690Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
2700Sstevel@tonic-gate 	fnp->fn_error = error;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Notify threads waiting for mount that
2740Sstevel@tonic-gate 	 * it's done.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2770Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	VN_RELE(vp);
2800Sstevel@tonic-gate 	crfree(argsp->fnc_cred);
2810Sstevel@tonic-gate 	namelen = strlen(argsp->fnc_name) + 1;
2820Sstevel@tonic-gate 	kmem_free(argsp->fnc_name, namelen);
2830Sstevel@tonic-gate 	kmem_free(argsp, sizeof (*argsp));
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	mutex_enter(&auto_mount_thread_cpr_lock);
2860Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
2870Sstevel@tonic-gate 	mutex_destroy(&auto_mount_thread_cpr_lock);
2880Sstevel@tonic-gate 	zthread_exit();
2890Sstevel@tonic-gate 	/* NOTREACHED */
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static int autofs_thr_success = 0;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Creates new thread which calls auto_mount_thread which does
2960Sstevel@tonic-gate  * the bulk of the work calling automountd, via 'auto_perform_actions'.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate void
2990Sstevel@tonic-gate auto_new_mount_thread(fnnode_t *fnp, char *name, cred_t *cred)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct autofs_callargs *argsp;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	argsp = kmem_alloc(sizeof (*argsp), KM_SLEEP);
3040Sstevel@tonic-gate 	VN_HOLD(fntovn(fnp));
3050Sstevel@tonic-gate 	argsp->fnc_fnp = fnp;
3060Sstevel@tonic-gate 	argsp->fnc_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
3070Sstevel@tonic-gate 	(void) strcpy(argsp->fnc_name, name);
3080Sstevel@tonic-gate 	argsp->fnc_origin = curthread;
3090Sstevel@tonic-gate 	crhold(cred);
3100Sstevel@tonic-gate 	argsp->fnc_cred = cred;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	(void) zthread_create(NULL, 0, auto_mount_thread, argsp, 0,
3130Sstevel@tonic-gate 	    minclsyspri);
3140Sstevel@tonic-gate 	autofs_thr_success++;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
317*2170Sevanl 
3180Sstevel@tonic-gate int
3190Sstevel@tonic-gate auto_calldaemon(
320*2170Sevanl 	zoneid_t 		zoneid,
321*2170Sevanl 	int			which,
322*2170Sevanl 	xdrproc_t		xarg_func,
323*2170Sevanl 	void 			*argsp,
324*2170Sevanl 	xdrproc_t		xresp_func,
325*2170Sevanl 	void 			*resp,
326*2170Sevanl 	int			reslen,
327*2170Sevanl 	bool_t 			hard)	/* retry forever? */
3280Sstevel@tonic-gate {
329*2170Sevanl 
330*2170Sevanl 	int 			retry, error = 0;
331*2170Sevanl 	k_sigset_t 		smask;
332*2170Sevanl 	door_arg_t		door_args;
333*2170Sevanl 	door_handle_t		dh;
334*2170Sevanl 	XDR			xdrarg, xdrres;
335*2170Sevanl 	struct autofs_globals 	*fngp = NULL;
336*2170Sevanl 	zone_t 			*autofs_zone;
337*2170Sevanl 	void			*orig_resp = NULL;
338*2170Sevanl 	int			orig_reslen = reslen;
339*2170Sevanl 	autofs_door_args_t	*xdr_argsp;
340*2170Sevanl 	int			xdr_len = 0;
341*2170Sevanl 
342*2170Sevanl 	autofs_zone = zone_find_by_id(zoneid);
343*2170Sevanl 
3440Sstevel@tonic-gate 
345*2170Sevanl 	/*
346*2170Sevanl 	 * We know that the current thread is doing work on
347*2170Sevanl 	 * behalf of its own zone, so it's ok to use
348*2170Sevanl 	 * curproc->p_zone.
349*2170Sevanl 	 */
350*2170Sevanl 	ASSERT(zoneid == getzoneid());
351*2170Sevanl 	if (zone_status_get(curproc->p_zone) >=
352*2170Sevanl 			ZONE_IS_SHUTTING_DOWN) {
353*2170Sevanl 		/*
354*2170Sevanl 		 * There's no point in trying to talk to
355*2170Sevanl 		 * automountd.  Plus, zone_shutdown() is
356*2170Sevanl 		 * waiting for us.
357*2170Sevanl 		 */
358*2170Sevanl 		return (ECONNREFUSED);
359*2170Sevanl 	}
3600Sstevel@tonic-gate 
361*2170Sevanl 	if ((fngp = zone_getspecific(autofs_key, autofs_zone)) ==
362*2170Sevanl 	    NULL) {
363*2170Sevanl 		fngp = autofs_zone_init();
364*2170Sevanl 		(void) zone_setspecific(autofs_key, autofs_zone, fngp);
365*2170Sevanl 	}
366*2170Sevanl 
367*2170Sevanl 	ASSERT(fngp != NULL);
368*2170Sevanl 
369*2170Sevanl 	if (argsp != NULL && (xdr_len = xdr_sizeof(xarg_func, argsp)) == 0)
370*2170Sevanl 		return (EINVAL);
371*2170Sevanl 	xdr_argsp = kmem_zalloc(xdr_len + sizeof (*xdr_argsp), KM_SLEEP);
372*2170Sevanl 	xdr_argsp->xdr_len = xdr_len;
373*2170Sevanl 	xdr_argsp->cmd = which;
374*2170Sevanl 
375*2170Sevanl 	if (argsp) {
376*2170Sevanl 		xdrmem_create(&xdrarg, (char *)&xdr_argsp->xdr_arg,
377*2170Sevanl 			xdr_argsp->xdr_len, XDR_ENCODE);
378*2170Sevanl 
379*2170Sevanl 		if (!(*xarg_func)(&xdrarg, argsp)) {
380*2170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
381*2170Sevanl 			return (EINVAL);
382*2170Sevanl 		}
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
386*2170Sevanl 	 * We're saving off the original pointer and length due to the
387*2170Sevanl 	 * possibility that the results buffer returned by the door
388*2170Sevanl 	 * upcall can be different then what we passed in. This is because
389*2170Sevanl 	 * the door will allocate new memory if the results buffer passed
390*2170Sevanl 	 * in isn't large enough to hold what we need to send back.
391*2170Sevanl 	 * In this case we need to free the memory originally allocated
392*2170Sevanl 	 * for that buffer.
3930Sstevel@tonic-gate 	 */
394*2170Sevanl 	if (orig_reslen)
395*2170Sevanl 		orig_resp = kmem_zalloc(orig_reslen, KM_SLEEP);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	do {
398*2170Sevanl 		retry = 0;
399*2170Sevanl 		mutex_enter(&fngp->fng_autofs_daemon_lock);
400*2170Sevanl 		dh = fngp->fng_autofs_daemon_dh;
401*2170Sevanl 		if (dh)
402*2170Sevanl 			door_ki_hold(dh);
403*2170Sevanl 		mutex_exit(&fngp->fng_autofs_daemon_lock);
4040Sstevel@tonic-gate 
405*2170Sevanl 		if (dh == NULL) {
406*2170Sevanl 			if (orig_resp)
407*2170Sevanl 				kmem_free(orig_resp, orig_reslen);
408*2170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
409*2170Sevanl 			return (ENOENT);
410*2170Sevanl 		}
411*2170Sevanl 		door_args.data_ptr = (char *)xdr_argsp;
412*2170Sevanl 		door_args.data_size = sizeof (*xdr_argsp) + xdr_argsp->xdr_len;
413*2170Sevanl 		door_args.desc_ptr = NULL;
414*2170Sevanl 		door_args.desc_num = 0;
415*2170Sevanl 		door_args.rbuf = orig_resp ? (char *)orig_resp : NULL;
416*2170Sevanl 		door_args.rsize = reslen;
4170Sstevel@tonic-gate 
418*2170Sevanl 		sigintr(&smask, 1);
419*2170Sevanl 		error = door_ki_upcall(dh, &door_args);
4200Sstevel@tonic-gate 		sigunintr(&smask);
4210Sstevel@tonic-gate 
422*2170Sevanl 		door_ki_rele(dh);
4230Sstevel@tonic-gate 
424*2170Sevanl 		if (!error) {
425*2170Sevanl 			autofs_door_res_t *adr =
426*2170Sevanl 				(autofs_door_res_t *)door_args.rbuf;
427*2170Sevanl 			if (door_args.rbuf != NULL &&
428*2170Sevanl 				(error = adr->res_status)) {
429*2170Sevanl 				kmem_free(xdr_argsp,
430*2170Sevanl 					xdr_len + sizeof (*xdr_argsp));
431*2170Sevanl 				if (orig_resp)
432*2170Sevanl 					kmem_free(orig_resp, orig_reslen);
433*2170Sevanl 				return (error);
434*2170Sevanl 			}
435*2170Sevanl 			continue;
436*2170Sevanl 		}
437*2170Sevanl 		switch (error) {
438*2170Sevanl 		case EINTR:
4390Sstevel@tonic-gate 			/*
440*2170Sevanl 			 * interrupts should be handled properly by the
441*2170Sevanl 			 * door upcall.
442*2170Sevanl 			 *
443*2170Sevanl 			 * We may have gotten EINTR for other reasons
444*2170Sevanl 			 * like the door being revoked on us. Instead
445*2170Sevanl 			 * of trying to extract this out of the door
446*2170Sevanl 			 * handle, sleep and try again, if still
447*2170Sevanl 			 * revoked we will get EBADF next time
448*2170Sevanl 			 * through.
4490Sstevel@tonic-gate 			 */
450*2170Sevanl 		case EAGAIN:    /* process may be forking */
451*2170Sevanl 			/*
452*2170Sevanl 			 * Back off for a bit
453*2170Sevanl 			 */
454*2170Sevanl 			delay(hz);
455*2170Sevanl 			retry = 1;
456*2170Sevanl 			break;
457*2170Sevanl 		case EBADF:	/* Invalid door */
458*2170Sevanl 		case EINVAL:    /* Not a door, wrong target */
4590Sstevel@tonic-gate 			/*
460*2170Sevanl 			 * A fatal door error, if our failing door
461*2170Sevanl 			 * handle is the current door handle, clean
462*2170Sevanl 			 * up our state.
4630Sstevel@tonic-gate 			 */
464*2170Sevanl 			mutex_enter(&fngp->fng_autofs_daemon_lock);
465*2170Sevanl 			if (dh == fngp->fng_autofs_daemon_dh) {
466*2170Sevanl 				door_ki_rele(fngp->fng_autofs_daemon_dh);
467*2170Sevanl 				fngp->fng_autofs_daemon_dh = NULL;
4680Sstevel@tonic-gate 			}
469*2170Sevanl 			mutex_exit(&fngp->fng_autofs_daemon_lock);
470*2170Sevanl 			AUTOFS_DPRINT((5,
471*2170Sevanl 				"auto_calldaemon error=%d\n", error));
472*2170Sevanl 			if (hard) {
473*2170Sevanl 				if (!fngp->fng_printed_not_running_msg) {
474*2170Sevanl 				    fngp->fng_printed_not_running_msg = 1;
475*2170Sevanl 				    zprintf(zoneid, "automountd not "\
476*2170Sevanl 					"running, retrying\n");
477*2170Sevanl 				}
478*2170Sevanl 				delay(hz);
479*2170Sevanl 				retry = 1;
480*2170Sevanl 				break;
481*2170Sevanl 			} else {
482*2170Sevanl 				error = ECONNREFUSED;
483*2170Sevanl 				kmem_free(xdr_argsp,
484*2170Sevanl 					xdr_len + sizeof (*xdr_argsp));
485*2170Sevanl 				if (orig_resp)
486*2170Sevanl 					kmem_free(orig_resp, orig_reslen);
487*2170Sevanl 				return (error);
488*2170Sevanl 			}
489*2170Sevanl 		default:	/* Unknown must be fatal */
4900Sstevel@tonic-gate 			error = ENOENT;
491*2170Sevanl 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
492*2170Sevanl 			if (orig_resp)
493*2170Sevanl 				kmem_free(orig_resp, orig_reslen);
494*2170Sevanl 			return (error);
4950Sstevel@tonic-gate 		}
496*2170Sevanl 	} while (retry);
4970Sstevel@tonic-gate 
498*2170Sevanl 	if (fngp->fng_printed_not_running_msg == 1) {
499*2170Sevanl 		fngp->fng_printed_not_running_msg = 0;
500*2170Sevanl 		zprintf(zoneid, "automountd OK\n");
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
503*2170Sevanl 	if (orig_resp && orig_reslen) {
504*2170Sevanl 		autofs_door_res_t	*door_resp;
505*2170Sevanl 		door_resp =
506*2170Sevanl 			(autofs_door_res_t *)door_args.rbuf;
507*2170Sevanl 		if ((void *)door_args.rbuf != orig_resp)
508*2170Sevanl 			kmem_free(orig_resp, orig_reslen);
509*2170Sevanl 		xdrmem_create(&xdrres, (char *)&door_resp->xdr_res,
510*2170Sevanl 			door_resp->xdr_len, XDR_DECODE);
511*2170Sevanl 		if (!((*xresp_func)(&xdrres, resp)))
512*2170Sevanl 			error = EINVAL;
513*2170Sevanl 		kmem_free(door_args.rbuf, door_args.rsize);
514*2170Sevanl 	}
515*2170Sevanl 	kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
5160Sstevel@tonic-gate 	return (error);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate static int
520*2170Sevanl auto_null_request(fninfo_t *fnip, bool_t hard)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	int error;
523*2170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tauto_null_request\n"));
5260Sstevel@tonic-gate 
527*2170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
528*2170Sevanl 		NULLPROC,
529*2170Sevanl 		xdr_void,
530*2170Sevanl 		NULL,
531*2170Sevanl 		xdr_void,
532*2170Sevanl 		NULL,
533*2170Sevanl 		0,
534*2170Sevanl 		hard);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tauto_null_request: error=%d\n", error));
5370Sstevel@tonic-gate 	return (error);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate static int
5410Sstevel@tonic-gate auto_lookup_request(
5420Sstevel@tonic-gate 	fninfo_t *fnip,
5430Sstevel@tonic-gate 	char *key,
5440Sstevel@tonic-gate 	struct linka *lnp,
5450Sstevel@tonic-gate 	bool_t hard,
5460Sstevel@tonic-gate 	bool_t *mountreq)
5470Sstevel@tonic-gate {
548*2170Sevanl 	int 				error;
549*2170Sevanl 	struct autofs_globals 		*fngp;
550*2170Sevanl 	struct autofs_lookupargs	reqst;
551*2170Sevanl 	autofs_lookupres 		*resp;
552*2170Sevanl 	struct linka 			*p;
5530Sstevel@tonic-gate 
554*2170Sevanl 
555*2170Sevanl 	AUTOFS_DPRINT((4, "auto_lookup_equest: path=%s name=%s\n",
5560Sstevel@tonic-gate 	    fnip->fi_path, key));
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
559*2170Sevanl 
560*2170Sevanl 	reqst.map = fnip->fi_map;
561*2170Sevanl 	reqst.path = fnip->fi_path;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (fnip->fi_flags & MF_DIRECT)
564*2170Sevanl 		reqst.name = fnip->fi_key;
5650Sstevel@tonic-gate 	else
566*2170Sevanl 		reqst.name = key;
567*2170Sevanl 	AUTOFS_DPRINT((4, "auto_lookup_request: using key=%s\n", reqst.name));
5680Sstevel@tonic-gate 
569*2170Sevanl 	reqst.subdir = fnip->fi_subdir;
570*2170Sevanl 	reqst.opts = fnip->fi_opts;
571*2170Sevanl 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
572*2170Sevanl 
573*2170Sevanl 	resp = kmem_zalloc(sizeof (*resp), KM_SLEEP);
5740Sstevel@tonic-gate 
575*2170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
576*2170Sevanl 		AUTOFS_LOOKUP,
577*2170Sevanl 		xdr_autofs_lookupargs,
578*2170Sevanl 		&reqst,
579*2170Sevanl 		xdr_autofs_lookupres,
580*2170Sevanl 		(void *)resp,
581*2170Sevanl 		sizeof (autofs_lookupres),
582*2170Sevanl 		hard);
583*2170Sevanl 
584*2170Sevanl 
585*2170Sevanl 	if (error) {
586*2170Sevanl 		xdr_free(xdr_autofs_lookupres, (char *)resp);
587*2170Sevanl 		kmem_free(resp, sizeof (*resp));
588*2170Sevanl 		return (error);
589*2170Sevanl 	}
590*2170Sevanl 
5910Sstevel@tonic-gate 	if (!error) {
592*2170Sevanl 		fngp->fng_verbose = resp->lu_verbose;
593*2170Sevanl 		switch (resp->lu_res) {
5940Sstevel@tonic-gate 		case AUTOFS_OK:
595*2170Sevanl 			switch (resp->lu_type.action) {
5960Sstevel@tonic-gate 			case AUTOFS_MOUNT_RQ:
5970Sstevel@tonic-gate 				lnp->link = NULL;
5980Sstevel@tonic-gate 				lnp->dir = NULL;
5990Sstevel@tonic-gate 				*mountreq = TRUE;
6000Sstevel@tonic-gate 				break;
6010Sstevel@tonic-gate 			case AUTOFS_LINK_RQ:
6020Sstevel@tonic-gate 				p =
603*2170Sevanl 				&resp->lu_type.lookup_result_type_u.lt_linka;
6040Sstevel@tonic-gate 				lnp->dir = kmem_alloc(strlen(p->dir) + 1,
605*2170Sevanl 					KM_SLEEP);
6060Sstevel@tonic-gate 				(void) strcpy(lnp->dir, p->dir);
6070Sstevel@tonic-gate 				lnp->link = kmem_alloc(strlen(p->link) + 1,
608*2170Sevanl 					KM_SLEEP);
6090Sstevel@tonic-gate 				(void) strcpy(lnp->link, p->link);
6100Sstevel@tonic-gate 				break;
6110Sstevel@tonic-gate 			case AUTOFS_NONE:
6120Sstevel@tonic-gate 				lnp->link = NULL;
6130Sstevel@tonic-gate 				lnp->dir = NULL;
6140Sstevel@tonic-gate 				break;
6150Sstevel@tonic-gate 			default:
616*2170Sevanl 				auto_log(fngp->fng_verbose,
617*2170Sevanl 					fngp->fng_zoneid, CE_WARN,
6180Sstevel@tonic-gate 				    "auto_lookup_request: bad action type %d",
619*2170Sevanl 				    resp->lu_res);
6200Sstevel@tonic-gate 				error = ENOENT;
6210Sstevel@tonic-gate 			}
6220Sstevel@tonic-gate 			break;
6230Sstevel@tonic-gate 		case AUTOFS_NOENT:
6240Sstevel@tonic-gate 			error = ENOENT;
6250Sstevel@tonic-gate 			break;
6260Sstevel@tonic-gate 		default:
6270Sstevel@tonic-gate 			error = ENOENT;
628*2170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
6290Sstevel@tonic-gate 			    "auto_lookup_request: unknown result: %d",
630*2170Sevanl 			    resp->lu_res);
6310Sstevel@tonic-gate 			break;
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate done:
635*2170Sevanl 	xdr_free(xdr_autofs_lookupres, (char *)resp);
636*2170Sevanl 	kmem_free(resp, sizeof (*resp));
6370Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_lookup_request: path=%s name=%s error=%d\n",
6380Sstevel@tonic-gate 	    fnip->fi_path, key, error));
6390Sstevel@tonic-gate 	return (error);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate static int
6430Sstevel@tonic-gate auto_mount_request(
6440Sstevel@tonic-gate 	fninfo_t *fnip,
6450Sstevel@tonic-gate 	char *key,
6460Sstevel@tonic-gate 	action_list **alpp,
6470Sstevel@tonic-gate 	bool_t hard)
6480Sstevel@tonic-gate {
649*2170Sevanl 	int 			error;
650*2170Sevanl 	struct autofs_globals 	*fngp;
651*2170Sevanl 	autofs_lookupargs 	reqst;
652*2170Sevanl 	autofs_mountres		*xdrres = NULL;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_mount_request: path=%s name=%s\n",
6550Sstevel@tonic-gate 	    fnip->fi_path, key));
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
658*2170Sevanl 	reqst.map = fnip->fi_map;
659*2170Sevanl 	reqst.path = fnip->fi_path;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (fnip->fi_flags & MF_DIRECT)
662*2170Sevanl 		reqst.name = fnip->fi_key;
6630Sstevel@tonic-gate 	else
664*2170Sevanl 		reqst.name = key;
665*2170Sevanl 
666*2170Sevanl 	AUTOFS_DPRINT((4, "auto_mount_request: using key=%s\n", reqst.name));
6670Sstevel@tonic-gate 
668*2170Sevanl 	reqst.subdir = fnip->fi_subdir;
669*2170Sevanl 	reqst.opts = fnip->fi_opts;
670*2170Sevanl 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
671*2170Sevanl 
672*2170Sevanl 	xdrres = kmem_zalloc(sizeof (*xdrres), KM_SLEEP);
6730Sstevel@tonic-gate 
674*2170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
675*2170Sevanl 		AUTOFS_MNTINFO,
676*2170Sevanl 		xdr_autofs_lookupargs,
677*2170Sevanl 		&reqst,
678*2170Sevanl 		xdr_autofs_mountres,
679*2170Sevanl 		(void *)xdrres,
680*2170Sevanl 		sizeof (autofs_mountres),
681*2170Sevanl 		hard);
682*2170Sevanl 
683*2170Sevanl 
6840Sstevel@tonic-gate 	if (!error) {
685*2170Sevanl 		fngp->fng_verbose = xdrres->mr_verbose;
686*2170Sevanl 		switch (xdrres->mr_type.status) {
6870Sstevel@tonic-gate 		case AUTOFS_ACTION:
6880Sstevel@tonic-gate 			error = 0;
6890Sstevel@tonic-gate 			/*
6900Sstevel@tonic-gate 			 * Save the action list since it is used by
6910Sstevel@tonic-gate 			 * the caller. We NULL the action list pointer
6920Sstevel@tonic-gate 			 * in 'result' so that xdr_free() will not free
6930Sstevel@tonic-gate 			 * the list.
6940Sstevel@tonic-gate 			 */
695*2170Sevanl 			*alpp = xdrres->mr_type.mount_result_type_u.list;
696*2170Sevanl 			xdrres->mr_type.mount_result_type_u.list = NULL;
6970Sstevel@tonic-gate 			break;
6980Sstevel@tonic-gate 		case AUTOFS_DONE:
699*2170Sevanl 			error = xdrres->mr_type.mount_result_type_u.error;
7000Sstevel@tonic-gate 			break;
7010Sstevel@tonic-gate 		default:
7020Sstevel@tonic-gate 			error = ENOENT;
703*2170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
7040Sstevel@tonic-gate 			    "auto_mount_request: unknown status %d",
705*2170Sevanl 			    xdrres->mr_type.status);
7060Sstevel@tonic-gate 			break;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 
710*2170Sevanl 	xdr_free(xdr_autofs_mountres, (char *)xdrres);
711*2170Sevanl 	kmem_free(xdrres, sizeof (*xdrres));
712*2170Sevanl 
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_mount_request: path=%s name=%s error=%d\n",
7150Sstevel@tonic-gate 	    fnip->fi_path, key, error));
7160Sstevel@tonic-gate 	return (error);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate static int
7210Sstevel@tonic-gate auto_send_unmount_request(
7220Sstevel@tonic-gate 	fninfo_t *fnip,
7230Sstevel@tonic-gate 	umntrequest *ul,
7240Sstevel@tonic-gate 	bool_t hard)
7250Sstevel@tonic-gate {
726*2170Sevanl 	int 	error;
727*2170Sevanl 	umntres	xdrres;
728*2170Sevanl 
729*2170Sevanl 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tauto_send_unmount_request: fstype=%s "
7320Sstevel@tonic-gate 			" mntpnt=%s\n", ul->fstype, ul->mntpnt));
7330Sstevel@tonic-gate 
734*2170Sevanl 	bzero(&xdrres, sizeof (umntres));
735*2170Sevanl 	error = auto_calldaemon(fngp->fng_zoneid,
736*2170Sevanl 		AUTOFS_UNMOUNT,
737*2170Sevanl 		xdr_umntrequest,
738*2170Sevanl 		(void *)ul,
739*2170Sevanl 		xdr_umntres,
740*2170Sevanl 		(void *)&xdrres,
741*2170Sevanl 		sizeof (umntres),
742*2170Sevanl 		hard);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tauto_send_unmount_request: error=%d\n", error));
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	return (error);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate static int
7500Sstevel@tonic-gate auto_perform_link(fnnode_t *fnp, struct linka *linkp, cred_t *cred)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	vnode_t *vp;
7530Sstevel@tonic-gate 	size_t len;
7540Sstevel@tonic-gate 	char *tmp;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	AUTOFS_DPRINT((3, "auto_perform_link: fnp=%p dir=%s link=%s\n",
7570Sstevel@tonic-gate 	    (void *)fnp, linkp->dir, linkp->link));
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	len = strlen(linkp->link) + 1;		/* include '\0' */
7600Sstevel@tonic-gate 	tmp = kmem_zalloc(len, KM_SLEEP);
7610Sstevel@tonic-gate 	(void) kcopy(linkp->link, tmp, len);
7620Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
7630Sstevel@tonic-gate 	fnp->fn_symlink = tmp;
7640Sstevel@tonic-gate 	fnp->fn_symlinklen = (uint_t)len;
7650Sstevel@tonic-gate 	fnp->fn_flags |= MF_THISUID_MATCH_RQD;
7660Sstevel@tonic-gate 	crhold(cred);
7670Sstevel@tonic-gate 	fnp->fn_cred = cred;
7680Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	vp = fntovn(fnp);
7710Sstevel@tonic-gate 	vp->v_type = VLNK;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	return (0);
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
776*2170Sevanl static void
777*2170Sevanl auto_free_autofs_args(struct mounta *m)
778*2170Sevanl {
779*2170Sevanl 	autofs_args	*aargs = (autofs_args *)m->dataptr;
780*2170Sevanl 
781*2170Sevanl 	if (aargs->addr.buf)
782*2170Sevanl 		kmem_free(aargs->addr.buf, aargs->addr.len);
783*2170Sevanl 	if (aargs->path)
784*2170Sevanl 		kmem_free(aargs->path, strlen(aargs->path) + 1);
785*2170Sevanl 	if (aargs->opts)
786*2170Sevanl 		kmem_free(aargs->opts, strlen(aargs->opts) + 1);
787*2170Sevanl 	if (aargs->map)
788*2170Sevanl 		kmem_free(aargs->map, strlen(aargs->map) + 1);
789*2170Sevanl 	if (aargs->subdir)
790*2170Sevanl 		kmem_free(aargs->subdir, strlen(aargs->subdir) + 1);
791*2170Sevanl 	if (aargs->key)
792*2170Sevanl 		kmem_free(aargs->key, strlen(aargs->key) + 1);
793*2170Sevanl 	kmem_free(aargs, sizeof (*aargs));
794*2170Sevanl }
795*2170Sevanl 
796*2170Sevanl static void
797*2170Sevanl auto_free_action_list(action_list *alp)
798*2170Sevanl {
799*2170Sevanl 	struct	mounta	*m;
800*2170Sevanl 	action_list	*lastalp;
801*2170Sevanl 	char		*fstype;
802*2170Sevanl 
803*2170Sevanl 	m = &alp->action.action_list_entry_u.mounta;
804*2170Sevanl 	while (alp != NULL) {
805*2170Sevanl 		fstype = alp->action.action_list_entry_u.mounta.fstype;
806*2170Sevanl 		m = &alp->action.action_list_entry_u.mounta;
807*2170Sevanl 		if (m->dataptr) {
808*2170Sevanl 			if (strcmp(fstype, "autofs") == 0) {
809*2170Sevanl 				auto_free_autofs_args(m);
810*2170Sevanl 			}
811*2170Sevanl 		}
812*2170Sevanl 		if (m->spec)
813*2170Sevanl 			kmem_free(m->spec, strlen(m->spec) + 1);
814*2170Sevanl 		if (m->dir)
815*2170Sevanl 			kmem_free(m->dir, strlen(m->dir) + 1);
816*2170Sevanl 		if (m->fstype)
817*2170Sevanl 			kmem_free(m->fstype, strlen(m->fstype) + 1);
818*2170Sevanl 		if (m->optptr)
819*2170Sevanl 			kmem_free(m->optptr, m->optlen);
820*2170Sevanl 		lastalp = alp;
821*2170Sevanl 		alp = alp->next;
822*2170Sevanl 		kmem_free(lastalp, sizeof (*lastalp));
823*2170Sevanl 	}
824*2170Sevanl }
825*2170Sevanl 
8260Sstevel@tonic-gate static boolean_t
827*2170Sevanl auto_invalid_autofs(fninfo_t *dfnip, fnnode_t *dfnp, action_list *p)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate 	struct mounta *m;
8300Sstevel@tonic-gate 	struct autofs_args *argsp;
8310Sstevel@tonic-gate 	vnode_t *dvp;
8320Sstevel@tonic-gate 	char buff[AUTOFS_MAXPATHLEN];
8330Sstevel@tonic-gate 	size_t len;
8340Sstevel@tonic-gate 	struct autofs_globals *fngp;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	fngp = dfnp->fn_globals;
8370Sstevel@tonic-gate 	dvp = fntovn(dfnp);
838*2170Sevanl 
8390Sstevel@tonic-gate 	m = &p->action.action_list_entry_u.mounta;
8400Sstevel@tonic-gate 	/*
8410Sstevel@tonic-gate 	 * Make sure we aren't geting passed NULL values or a "dir" that
8420Sstevel@tonic-gate 	 * isn't "." and doesn't begin with "./".
8430Sstevel@tonic-gate 	 *
8440Sstevel@tonic-gate 	 * We also only want to perform autofs mounts, so make sure
8450Sstevel@tonic-gate 	 * no-one is trying to trick us into doing anything else.
8460Sstevel@tonic-gate 	 */
8470Sstevel@tonic-gate 	if (m->spec == NULL || m->dir == NULL || m->dir[0] != '.' ||
8480Sstevel@tonic-gate 	    (m->dir[1] != '/' && m->dir[1] != '\0') ||
8490Sstevel@tonic-gate 	    m->fstype == NULL || strcmp(m->fstype, "autofs") != 0 ||
8500Sstevel@tonic-gate 	    m->dataptr == NULL || m->datalen != sizeof (struct autofs_args) ||
8510Sstevel@tonic-gate 	    m->optptr == NULL)
8520Sstevel@tonic-gate 		return (B_TRUE);
8530Sstevel@tonic-gate 	/*
8540Sstevel@tonic-gate 	 * We also don't like ".."s in the pathname.  Symlinks are
8550Sstevel@tonic-gate 	 * handled by the fact that we'll use NOFOLLOW when we do
8560Sstevel@tonic-gate 	 * lookup()s.
8570Sstevel@tonic-gate 	 */
8580Sstevel@tonic-gate 	if (strstr(m->dir, "/../") != NULL ||
8590Sstevel@tonic-gate 	    (len = strlen(m->dir)) > sizeof ("/..") - 1 &&
8600Sstevel@tonic-gate 	    m->dir[len] == '.' && m->dir[len - 1] == '.' &&
8610Sstevel@tonic-gate 	    m->dir[len - 2] == '/')
8620Sstevel@tonic-gate 		return (B_TRUE);
8630Sstevel@tonic-gate 	argsp = (struct autofs_args *)m->dataptr;
8640Sstevel@tonic-gate 	/*
8650Sstevel@tonic-gate 	 * We don't want NULL values here either.
8660Sstevel@tonic-gate 	 */
8670Sstevel@tonic-gate 	if (argsp->addr.buf == NULL || argsp->path == NULL ||
8680Sstevel@tonic-gate 	    argsp->opts == NULL || argsp->map == NULL || argsp->subdir == NULL)
8690Sstevel@tonic-gate 		return (B_TRUE);
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * We know what the claimed pathname *should* look like:
8720Sstevel@tonic-gate 	 *
8730Sstevel@tonic-gate 	 * If the parent (dfnp) is a mount point (VROOT), then
8740Sstevel@tonic-gate 	 * the path should be (dfnip->fi_path + m->dir).
8750Sstevel@tonic-gate 	 *
8760Sstevel@tonic-gate 	 * Else, we know we're only two levels deep, so we use
8770Sstevel@tonic-gate 	 * (dfnip->fi_path + dfnp->fn_name + m->dir).
8780Sstevel@tonic-gate 	 *
8790Sstevel@tonic-gate 	 * Furthermore, "." only makes sense if dfnp is a
8800Sstevel@tonic-gate 	 * trigger node.
8810Sstevel@tonic-gate 	 *
8820Sstevel@tonic-gate 	 * At this point it seems like the passed-in path is
8830Sstevel@tonic-gate 	 * redundant.
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 	if (dvp->v_flag & VROOT) {
8860Sstevel@tonic-gate 		if (m->dir[1] == '\0' && !(dfnp->fn_flags & MF_TRIGGER))
8870Sstevel@tonic-gate 			return (B_TRUE);
8880Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s%s",
8890Sstevel@tonic-gate 		    dfnip->fi_path, m->dir + 1);
8900Sstevel@tonic-gate 	} else {
8910Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s/%s%s",
8920Sstevel@tonic-gate 		    dfnip->fi_path, dfnp->fn_name, m->dir + 1);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 	if (strcmp(argsp->path, buff) != 0) {
895*2170Sevanl 		auto_log(fngp->fng_verbose, fngp->fng_zoneid,
896*2170Sevanl 		    CE_WARN, "autofs: expected path of '%s', "
8970Sstevel@tonic-gate 		    "got '%s' instead.", buff, argsp->path);
8980Sstevel@tonic-gate 		return (B_TRUE);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 	return (B_FALSE); /* looks OK */
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate 
903*2170Sevanl /*
904*2170Sevanl  * auto_invalid_action will validate the action_list received.  If all is good
905*2170Sevanl  * this function returns FALSE, if there is a problem it returns TRUE.
906*2170Sevanl  */
907*2170Sevanl static boolean_t
908*2170Sevanl auto_invalid_action(fninfo_t *dfnip, fnnode_t *dfnp, action_list *alistpp)
909*2170Sevanl {
910*2170Sevanl 
911*2170Sevanl 	/*
912*2170Sevanl 	 * Before we go any further, this better be a mount request.
913*2170Sevanl 	 */
914*2170Sevanl 	if (alistpp->action.action != AUTOFS_MOUNT_RQ)
915*2170Sevanl 		return (B_TRUE);
916*2170Sevanl 	return (auto_invalid_autofs(dfnip, dfnp, alistpp));
917*2170Sevanl 
918*2170Sevanl }
919*2170Sevanl 
9200Sstevel@tonic-gate static int
9210Sstevel@tonic-gate auto_perform_actions(
9220Sstevel@tonic-gate 	fninfo_t *dfnip,
9230Sstevel@tonic-gate 	fnnode_t *dfnp,
9240Sstevel@tonic-gate 	action_list *alp,
9250Sstevel@tonic-gate 	cred_t *cred)	/* Credentials of the caller */
9260Sstevel@tonic-gate {
927*2170Sevanl 
9280Sstevel@tonic-gate 	action_list *p;
929*2170Sevanl 	struct mounta		*m, margs;
930*2170Sevanl 	struct autofs_args 		*argsp;
931*2170Sevanl 	int 			error, success = 0;
932*2170Sevanl 	vnode_t 		*mvp, *dvp, *newvp;
933*2170Sevanl 	fnnode_t 		*newfnp, *mfnp;
934*2170Sevanl 	int 			auto_mount = 0;
935*2170Sevanl 	int 			save_triggers = 0;
936*2170Sevanl 	int 			update_times = 0;
937*2170Sevanl 	char 			*mntpnt;
938*2170Sevanl 	char 			buff[AUTOFS_MAXPATHLEN];
939*2170Sevanl 	timestruc_t 		now;
940*2170Sevanl 	struct autofs_globals 	*fngp;
941*2170Sevanl 	cred_t 			*zcred;
9420Sstevel@tonic-gate 
943*2170Sevanl 	AUTOFS_DPRINT((4, "auto_perform_actions: alp=%p\n",
944*2170Sevanl 		(void *)alp));
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	fngp = dfnp->fn_globals;
9470Sstevel@tonic-gate 	dvp = fntovn(dfnp);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	/*
9500Sstevel@tonic-gate 	 * As automountd running in a zone may be compromised, and this may be
9510Sstevel@tonic-gate 	 * an attack, we can't trust everything passed in by automountd, and we
9520Sstevel@tonic-gate 	 * need to do argument verification.  We'll issue a warning and drop
9530Sstevel@tonic-gate 	 * the request if it doesn't seem right.
9540Sstevel@tonic-gate 	 */
955*2170Sevanl 
9560Sstevel@tonic-gate 	for (p = alp; p != NULL; p = p->next) {
9570Sstevel@tonic-gate 		if (auto_invalid_action(dfnip, dfnp, p)) {
9580Sstevel@tonic-gate 			/*
9590Sstevel@tonic-gate 			 * This warning should be sent to the global zone,
9600Sstevel@tonic-gate 			 * since presumably the zone administrator is the same
9610Sstevel@tonic-gate 			 * as the attacker.
9620Sstevel@tonic-gate 			 */
9630Sstevel@tonic-gate 			cmn_err(CE_WARN, "autofs: invalid action list received "
9640Sstevel@tonic-gate 			    "by automountd in zone %s.",
9650Sstevel@tonic-gate 			    curproc->p_zone->zone_name);
9660Sstevel@tonic-gate 			/*
9670Sstevel@tonic-gate 			 * This conversation is over.
9680Sstevel@tonic-gate 			 */
9690Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
9700Sstevel@tonic-gate 			return (EINVAL);
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	zcred = zone_get_kcred(getzoneid());
9750Sstevel@tonic-gate 	ASSERT(zcred != NULL);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	if (vn_mountedvfs(dvp) != NULL) {
9780Sstevel@tonic-gate 		/*
9790Sstevel@tonic-gate 		 * The daemon successfully mounted a filesystem
9800Sstevel@tonic-gate 		 * on the AUTOFS root node.
9810Sstevel@tonic-gate 		 */
9820Sstevel@tonic-gate 		mutex_enter(&dfnp->fn_lock);
9830Sstevel@tonic-gate 		dfnp->fn_flags |= MF_MOUNTPOINT;
9840Sstevel@tonic-gate 		ASSERT(dfnp->fn_dirents == NULL);
9850Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
9860Sstevel@tonic-gate 		success++;
9870Sstevel@tonic-gate 	} else {
9880Sstevel@tonic-gate 		/*
9890Sstevel@tonic-gate 		 * Clear MF_MOUNTPOINT.
9900Sstevel@tonic-gate 		 */
9910Sstevel@tonic-gate 		mutex_enter(&dfnp->fn_lock);
9920Sstevel@tonic-gate 		if (dfnp->fn_flags & MF_MOUNTPOINT) {
9930Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "autofs: clearing mountpoint "
994*2170Sevanl 				"flag on %s.", dfnp->fn_name));
9950Sstevel@tonic-gate 			ASSERT(dfnp->fn_dirents == NULL);
9960Sstevel@tonic-gate 			ASSERT(dfnp->fn_trigger == NULL);
9970Sstevel@tonic-gate 		}
9980Sstevel@tonic-gate 		dfnp->fn_flags &= ~MF_MOUNTPOINT;
9990Sstevel@tonic-gate 		mutex_exit(&dfnp->fn_lock);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	for (p = alp; p != NULL; p = p->next) {
1003*2170Sevanl 
10040Sstevel@tonic-gate 		vfs_t *vfsp;	/* dummy argument */
10050Sstevel@tonic-gate 		vfs_t *mvfsp;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		auto_mount = 0;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 		m = &p->action.action_list_entry_u.mounta;
10100Sstevel@tonic-gate 		argsp = (struct autofs_args *)m->dataptr;
1011*2170Sevanl 		ASSERT(strcmp(m->fstype, "autofs") == 0);
10120Sstevel@tonic-gate 		/*
10130Sstevel@tonic-gate 		 * use the parent directory's timeout since it's the
10140Sstevel@tonic-gate 		 * one specified/inherited by automount.
10150Sstevel@tonic-gate 		 */
10160Sstevel@tonic-gate 		argsp->mount_to = dfnip->fi_mount_to;
10170Sstevel@tonic-gate 		/*
10180Sstevel@tonic-gate 		 * The mountpoint is relative, and it is guaranteed to
10190Sstevel@tonic-gate 		 * begin with "."
10200Sstevel@tonic-gate 		 *
10210Sstevel@tonic-gate 		 */
10220Sstevel@tonic-gate 		ASSERT(m->dir[0] == '.');
10230Sstevel@tonic-gate 		if (m->dir[0] == '.' && m->dir[1] == '\0') {
10240Sstevel@tonic-gate 			/*
10250Sstevel@tonic-gate 			 * mounting on the trigger node
10260Sstevel@tonic-gate 			 */
10270Sstevel@tonic-gate 			mvp = dvp;
10280Sstevel@tonic-gate 			VN_HOLD(mvp);
10290Sstevel@tonic-gate 			goto mount;
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 		/*
10320Sstevel@tonic-gate 		 * ignore "./" in front of mountpoint
10330Sstevel@tonic-gate 		 */
10340Sstevel@tonic-gate 		ASSERT(m->dir[1] == '/');
10350Sstevel@tonic-gate 		mntpnt = m->dir + 2;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tdfnip->fi_path=%s\n", dfnip->fi_path));
10380Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tdfnip->fi_flags=%x\n", dfnip->fi_flags));
10390Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tmntpnt=%s\n", mntpnt));
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 		if (dfnip->fi_flags & MF_DIRECT) {
10420Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tDIRECT\n"));
1043*2170Sevanl 			(void) sprintf(buff, "%s/%s", dfnip->fi_path,
1044*2170Sevanl 				mntpnt);
10450Sstevel@tonic-gate 		} else {
10460Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tINDIRECT\n"));
1047*2170Sevanl 			(void) sprintf(buff, "%s/%s/%s",
1048*2170Sevanl 				dfnip->fi_path,
1049*2170Sevanl 				dfnp->fn_name, mntpnt);
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		if (vn_mountedvfs(dvp) == NULL) {
10530Sstevel@tonic-gate 			/*
10540Sstevel@tonic-gate 			 * Daemon didn't mount anything on the root
1055*2170Sevanl 			 * We have to create the mountpoint if it
1056*2170Sevanl 			 * doesn't exist already
10570Sstevel@tonic-gate 			 *
1058*2170Sevanl 			 * We use the caller's credentials in case a
1059*2170Sevanl 			 * UID-match is required
1060*2170Sevanl 			 * (MF_THISUID_MATCH_RQD).
10610Sstevel@tonic-gate 			 */
10620Sstevel@tonic-gate 			rw_enter(&dfnp->fn_rwlock, RW_WRITER);
10630Sstevel@tonic-gate 			error = auto_search(dfnp, mntpnt, &mfnp, cred);
10640Sstevel@tonic-gate 			if (error == 0) {
10650Sstevel@tonic-gate 				/*
10660Sstevel@tonic-gate 				 * AUTOFS mountpoint exists
10670Sstevel@tonic-gate 				 */
10680Sstevel@tonic-gate 				if (vn_mountedvfs(fntovn(mfnp)) != NULL) {
10690Sstevel@tonic-gate 					cmn_err(CE_PANIC,
1070*2170Sevanl 						"auto_perform_actions:"
1071*2170Sevanl 						" mfnp=%p covered",
1072*2170Sevanl 						(void *)mfnp);
10730Sstevel@tonic-gate 				}
10740Sstevel@tonic-gate 			} else {
10750Sstevel@tonic-gate 				/*
10760Sstevel@tonic-gate 				 * Create AUTOFS mountpoint
10770Sstevel@tonic-gate 				 */
10780Sstevel@tonic-gate 				ASSERT((dfnp->fn_flags & MF_MOUNTPOINT) == 0);
10790Sstevel@tonic-gate 				error = auto_enter(dfnp, mntpnt, &mfnp, cred);
10800Sstevel@tonic-gate 				ASSERT(mfnp->fn_linkcnt == 1);
10810Sstevel@tonic-gate 				mfnp->fn_linkcnt++;
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			if (!error)
10840Sstevel@tonic-gate 				update_times = 1;
10850Sstevel@tonic-gate 			rw_exit(&dfnp->fn_rwlock);
10860Sstevel@tonic-gate 			ASSERT(error != EEXIST);
10870Sstevel@tonic-gate 			if (!error) {
10880Sstevel@tonic-gate 				/*
10890Sstevel@tonic-gate 				 * mfnp is already held.
10900Sstevel@tonic-gate 				 */
10910Sstevel@tonic-gate 				mvp = fntovn(mfnp);
10920Sstevel@tonic-gate 			} else {
1093*2170Sevanl 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1094*2170Sevanl 					CE_WARN, "autofs: mount of %s "
1095*2170Sevanl 					"failed - can't create"
1096*2170Sevanl 					" mountpoint.", buff);
1097*2170Sevanl 					continue;
10980Sstevel@tonic-gate 			}
10990Sstevel@tonic-gate 		} else {
11000Sstevel@tonic-gate 			/*
1101*2170Sevanl 			 * Find mountpoint in VFS mounted here. If not
1102*2170Sevanl 			 * found, fail the submount, though the overall
1103*2170Sevanl 			 * mount has succeeded since the root is
1104*2170Sevanl 			 * mounted.
11050Sstevel@tonic-gate 			 */
1106*2170Sevanl 			if (error = auto_getmntpnt(dvp, mntpnt, &mvp,
1107*2170Sevanl 				kcred)) {
1108*2170Sevanl 				auto_log(fngp->fng_verbose,
1109*2170Sevanl 					fngp->fng_zoneid,
1110*2170Sevanl 					CE_WARN, "autofs: mount of %s "
1111*2170Sevanl 					"failed - mountpoint doesn't"
1112*2170Sevanl 					" exist.", buff);
11130Sstevel@tonic-gate 				continue;
11140Sstevel@tonic-gate 			}
11150Sstevel@tonic-gate 			if (mvp->v_type == VLNK) {
1116*2170Sevanl 				auto_log(fngp->fng_verbose,
1117*2170Sevanl 					fngp->fng_zoneid,
1118*2170Sevanl 					CE_WARN, "autofs: %s symbolic "
1119*2170Sevanl 					"link: not a valid mountpoint "
1120*2170Sevanl 					"- mount failed", buff);
11210Sstevel@tonic-gate 				VN_RELE(mvp);
11220Sstevel@tonic-gate 				error = ENOENT;
11230Sstevel@tonic-gate 				continue;
11240Sstevel@tonic-gate 			}
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate mount:
11270Sstevel@tonic-gate 		m->flags |= MS_SYSSPACE | MS_OPTIONSTR;
1128*2170Sevanl 
11290Sstevel@tonic-gate 		/*
1130*2170Sevanl 		 * Copy mounta struct here so we can substitute a
1131*2170Sevanl 		 * buffer that is large enough to hold the returned
1132*2170Sevanl 		 * option string, if that string is longer than the
1133*2170Sevanl 		 * input option string.
11340Sstevel@tonic-gate 		 * This can happen if there are default options enabled
11350Sstevel@tonic-gate 		 * that were not in the input option string.
11360Sstevel@tonic-gate 		 */
11370Sstevel@tonic-gate 		bcopy(m, &margs, sizeof (*m));
11380Sstevel@tonic-gate 		margs.optptr = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
11390Sstevel@tonic-gate 		margs.optlen = MAX_MNTOPT_STR;
11400Sstevel@tonic-gate 		(void) strcpy(margs.optptr, m->optptr);
11410Sstevel@tonic-gate 		margs.dir = argsp->path;
1142*2170Sevanl 
11430Sstevel@tonic-gate 		/*
1144*2170Sevanl 		 * We use the zone's kcred because we don't want the
1145*2170Sevanl 		 * zone to be able to thus do something it wouldn't
1146*2170Sevanl 		 * normally be able to.
11470Sstevel@tonic-gate 		 */
11480Sstevel@tonic-gate 		error = domount(NULL, &margs, mvp, zcred, &vfsp);
11490Sstevel@tonic-gate 		kmem_free(margs.optptr, MAX_MNTOPT_STR);
11500Sstevel@tonic-gate 		if (error != 0) {
1151*2170Sevanl 			auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1152*2170Sevanl 				CE_WARN, "autofs: domount of %s failed "
1153*2170Sevanl 				"error=%d", buff, error);
11540Sstevel@tonic-gate 			VN_RELE(mvp);
11550Sstevel@tonic-gate 			continue;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		VFS_RELE(vfsp);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		/*
11600Sstevel@tonic-gate 		 * If mountpoint is an AUTOFS node, then I'm going to
1161*2170Sevanl 		 * flag it that the Filesystem mounted on top was
1162*2170Sevanl 		 * mounted in the kernel so that the unmount can be
1163*2170Sevanl 		 * done inside the kernel as well.
1164*2170Sevanl 		 * I don't care to flag non-AUTOFS mountpoints when an
1165*2170Sevanl 		 * AUTOFS in-kernel mount was done on top, because the
1166*2170Sevanl 		 * unmount routine already knows that such case was
1167*2170Sevanl 		 * done in the kernel.
11680Sstevel@tonic-gate 		 */
1169*2170Sevanl 		if (vfs_matchops(dvp->v_vfsp,
1170*2170Sevanl 			vfs_getops(mvp->v_vfsp))) {
11710Sstevel@tonic-gate 			mfnp = vntofn(mvp);
11720Sstevel@tonic-gate 			mutex_enter(&mfnp->fn_lock);
11730Sstevel@tonic-gate 			mfnp->fn_flags |= MF_IK_MOUNT;
11740Sstevel@tonic-gate 			mutex_exit(&mfnp->fn_lock);
11750Sstevel@tonic-gate 		}
11760Sstevel@tonic-gate 
1177*2170Sevanl 		(void) vn_vfswlock_wait(mvp);
11780Sstevel@tonic-gate 		mvfsp = vn_mountedvfs(mvp);
11790Sstevel@tonic-gate 		if (mvfsp != NULL) {
1180*2170Sevanl 			vfs_lock_wait(mvfsp);
1181*2170Sevanl 			vn_vfsunlock(mvp);
11821153Snr123932 			error = VFS_ROOT(mvfsp, &newvp);
1183*2170Sevanl 			vfs_unlock(mvfsp);
11840Sstevel@tonic-gate 			if (error) {
11850Sstevel@tonic-gate 				/*
1186*2170Sevanl 				 * We've dropped the locks, so let's
1187*2170Sevanl 				 * get the mounted vfs again in case
1188*2170Sevanl 				 * it changed.
11890Sstevel@tonic-gate 				 */
11900Sstevel@tonic-gate 				(void) vn_vfswlock_wait(mvp);
11910Sstevel@tonic-gate 				mvfsp = vn_mountedvfs(mvp);
11920Sstevel@tonic-gate 				if (mvfsp != NULL) {
11930Sstevel@tonic-gate 					error = dounmount(mvfsp, 0, CRED());
11940Sstevel@tonic-gate 					if (error) {
11950Sstevel@tonic-gate 						cmn_err(CE_WARN,
1196*2170Sevanl 							"autofs: could"
1197*2170Sevanl 							" not unmount"
1198*2170Sevanl 							" vfs=%p",
1199*2170Sevanl 							(void *)mvfsp);
12000Sstevel@tonic-gate 					}
12010Sstevel@tonic-gate 				} else
12020Sstevel@tonic-gate 					vn_vfsunlock(mvp);
12030Sstevel@tonic-gate 				VN_RELE(mvp);
12040Sstevel@tonic-gate 				continue;
12050Sstevel@tonic-gate 			}
12060Sstevel@tonic-gate 		} else {
12070Sstevel@tonic-gate 			vn_vfsunlock(mvp);
12080Sstevel@tonic-gate 			VN_RELE(mvp);
12090Sstevel@tonic-gate 			continue;
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 		auto_mount = vfs_matchops(dvp->v_vfsp,
1213*2170Sevanl 			vfs_getops(newvp->v_vfsp));
12140Sstevel@tonic-gate 		newfnp = vntofn(newvp);
12150Sstevel@tonic-gate 		newfnp->fn_parent = dfnp;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		/*
1218*2170Sevanl 		 * At this time we want to save the AUTOFS filesystem
1219*2170Sevanl 		 * as a trigger node. (We only do this if the mount
1220*2170Sevanl 		 * occurred on a node different from the root.
12210Sstevel@tonic-gate 		 * We look at the trigger nodes during
12220Sstevel@tonic-gate 		 * the automatic unmounting to make sure we remove them
1223*2170Sevanl 		 * as a unit and remount them as a unit if the
1224*2170Sevanl 		 * filesystem mounted at the root could not be
1225*2170Sevanl 		 * unmounted.
12260Sstevel@tonic-gate 		 */
12270Sstevel@tonic-gate 		if (auto_mount && (error == 0) && (mvp != dvp)) {
12280Sstevel@tonic-gate 			save_triggers++;
12290Sstevel@tonic-gate 			/*
12300Sstevel@tonic-gate 			 * Add AUTOFS mount to hierarchy
12310Sstevel@tonic-gate 			 */
12320Sstevel@tonic-gate 			newfnp->fn_flags |= MF_TRIGGER;
12330Sstevel@tonic-gate 			rw_enter(&newfnp->fn_rwlock, RW_WRITER);
12340Sstevel@tonic-gate 			newfnp->fn_next = dfnp->fn_trigger;
12350Sstevel@tonic-gate 			rw_exit(&newfnp->fn_rwlock);
12360Sstevel@tonic-gate 			rw_enter(&dfnp->fn_rwlock, RW_WRITER);
12370Sstevel@tonic-gate 			dfnp->fn_trigger = newfnp;
12380Sstevel@tonic-gate 			rw_exit(&dfnp->fn_rwlock);
12390Sstevel@tonic-gate 			/*
1240*2170Sevanl 			 * Don't VN_RELE(newvp) here since dfnp now
1241*2170Sevanl 			 * holds reference to it as its trigger node.
12420Sstevel@tonic-gate 			 */
12430Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tadding trigger %s to %s\n",
1244*2170Sevanl 				newfnp->fn_name, dfnp->fn_name));
12450Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tfirst trigger is %s\n",
1246*2170Sevanl 				dfnp->fn_trigger->fn_name));
12470Sstevel@tonic-gate 			if (newfnp->fn_next != NULL)
1248*2170Sevanl 				AUTOFS_DPRINT((10,
1249*2170Sevanl 					"\tnext trigger is %s\n",
1250*2170Sevanl 					newfnp->fn_next->fn_name));
12510Sstevel@tonic-gate 			else
1252*2170Sevanl 				AUTOFS_DPRINT((10,
1253*2170Sevanl 					"\tno next trigger\n"));
12540Sstevel@tonic-gate 		} else
12550Sstevel@tonic-gate 			VN_RELE(newvp);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 		if (!error)
12580Sstevel@tonic-gate 			success++;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		if (update_times) {
12610Sstevel@tonic-gate 			gethrestime(&now);
12620Sstevel@tonic-gate 			dfnp->fn_atime = dfnp->fn_mtime = now;
12630Sstevel@tonic-gate 		}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 		VN_RELE(mvp);
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	if (save_triggers) {
12690Sstevel@tonic-gate 		/*
12700Sstevel@tonic-gate 		 * Make sure the parent can't be freed while it has triggers.
12710Sstevel@tonic-gate 		 */
12720Sstevel@tonic-gate 		VN_HOLD(dvp);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	crfree(zcred);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate done:
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * Return failure if daemon didn't mount anything, and all
12800Sstevel@tonic-gate 	 * kernel mounts attempted failed.
12810Sstevel@tonic-gate 	 */
12820Sstevel@tonic-gate 	error = success ? 0 : ENOENT;
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (alp != NULL) {
12850Sstevel@tonic-gate 		if ((error == 0) && save_triggers) {
12860Sstevel@tonic-gate 			/*
12870Sstevel@tonic-gate 			 * Save action_list information, so that we can use it
12880Sstevel@tonic-gate 			 * when it comes time to remount the trigger nodes
12890Sstevel@tonic-gate 			 * The action list is freed when the directory node
12900Sstevel@tonic-gate 			 * containing the reference to it is unmounted in
12910Sstevel@tonic-gate 			 * unmount_tree().
12920Sstevel@tonic-gate 			 */
12930Sstevel@tonic-gate 			mutex_enter(&dfnp->fn_lock);
12940Sstevel@tonic-gate 			ASSERT(dfnp->fn_alp == NULL);
12950Sstevel@tonic-gate 			dfnp->fn_alp = alp;
12960Sstevel@tonic-gate 			mutex_exit(&dfnp->fn_lock);
12970Sstevel@tonic-gate 		} else {
12980Sstevel@tonic-gate 			/*
12990Sstevel@tonic-gate 			 * free the action list now,
13000Sstevel@tonic-gate 			 */
13010Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_perform_actions: error=%d\n", error));
13050Sstevel@tonic-gate 	return (error);
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate fnnode_t *
13090Sstevel@tonic-gate auto_makefnnode(
13100Sstevel@tonic-gate 	vtype_t type,
13110Sstevel@tonic-gate 	vfs_t *vfsp,
13120Sstevel@tonic-gate 	char *name,
13130Sstevel@tonic-gate 	cred_t *cred,
13140Sstevel@tonic-gate 	struct autofs_globals *fngp)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate 	fnnode_t *fnp;
13170Sstevel@tonic-gate 	vnode_t *vp;
13180Sstevel@tonic-gate 	char *tmpname;
13190Sstevel@tonic-gate 	timestruc_t now;
13200Sstevel@tonic-gate 	/*
13210Sstevel@tonic-gate 	 * autofs uses odd inode numbers
13220Sstevel@tonic-gate 	 * automountd uses even inode numbers
13230Sstevel@tonic-gate 	 *
13240Sstevel@tonic-gate 	 * To preserve the age-old semantics that inum+devid is unique across
13250Sstevel@tonic-gate 	 * the system, this variable must be global across zones.
13260Sstevel@tonic-gate 	 */
13270Sstevel@tonic-gate 	static ino_t nodeid = 3;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	fnp = kmem_zalloc(sizeof (*fnp), KM_SLEEP);
13300Sstevel@tonic-gate 	fnp->fn_vnode = vn_alloc(KM_SLEEP);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	vp = fntovn(fnp);
13330Sstevel@tonic-gate 	tmpname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
13340Sstevel@tonic-gate 	(void) strcpy(tmpname, name);
13350Sstevel@tonic-gate 	fnp->fn_name = &tmpname[0];
13360Sstevel@tonic-gate 	fnp->fn_namelen = (int)strlen(tmpname) + 1;	/* include '\0' */
13370Sstevel@tonic-gate 	fnp->fn_uid = crgetuid(cred);
13380Sstevel@tonic-gate 	fnp->fn_gid = crgetgid(cred);
13390Sstevel@tonic-gate 	/*
13400Sstevel@tonic-gate 	 * ".." is added in auto_enter and auto_mount.
13410Sstevel@tonic-gate 	 * "." is added in auto_mkdir and auto_mount.
13420Sstevel@tonic-gate 	 */
13430Sstevel@tonic-gate 	/*
13440Sstevel@tonic-gate 	 * Note that fn_size and fn_linkcnt are already 0 since
13450Sstevel@tonic-gate 	 * we used kmem_zalloc to allocated fnp
13460Sstevel@tonic-gate 	 */
13470Sstevel@tonic-gate 	fnp->fn_mode = AUTOFS_MODE;
13480Sstevel@tonic-gate 	gethrestime(&now);
13490Sstevel@tonic-gate 	fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now;
13500Sstevel@tonic-gate 	fnp->fn_ref_time = now.tv_sec;
13510Sstevel@tonic-gate 	mutex_enter(&autofs_nodeid_lock);
13520Sstevel@tonic-gate 	fnp->fn_nodeid = nodeid;
13530Sstevel@tonic-gate 	nodeid += 2;
13540Sstevel@tonic-gate 	fnp->fn_globals = fngp;
13550Sstevel@tonic-gate 	fngp->fng_fnnode_count++;
13560Sstevel@tonic-gate 	mutex_exit(&autofs_nodeid_lock);
13570Sstevel@tonic-gate 	vn_setops(vp, auto_vnodeops);
13580Sstevel@tonic-gate 	vp->v_type = type;
13590Sstevel@tonic-gate 	vp->v_data = (void *)fnp;
13600Sstevel@tonic-gate 	vp->v_vfsp = vfsp;
13610Sstevel@tonic-gate 	mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL);
13620Sstevel@tonic-gate 	rw_init(&fnp->fn_rwlock, NULL, RW_DEFAULT, NULL);
13630Sstevel@tonic-gate 	cv_init(&fnp->fn_cv_mount, NULL, CV_DEFAULT, NULL);
13640Sstevel@tonic-gate 	vn_exists(vp);
13650Sstevel@tonic-gate 	return (fnp);
13660Sstevel@tonic-gate }
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate void
13700Sstevel@tonic-gate auto_freefnnode(fnnode_t *fnp)
13710Sstevel@tonic-gate {
13720Sstevel@tonic-gate 	vnode_t *vp = fntovn(fnp);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_freefnnode: fnp=%p\n", (void *)fnp));
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	ASSERT(fnp->fn_linkcnt == 0);
13770Sstevel@tonic-gate 	ASSERT(vp->v_count == 0);
13780Sstevel@tonic-gate 	ASSERT(fnp->fn_dirents == NULL);
13790Sstevel@tonic-gate 	ASSERT(fnp->fn_parent == NULL);
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 	vn_invalid(vp);
13820Sstevel@tonic-gate 	kmem_free(fnp->fn_name, fnp->fn_namelen);
13830Sstevel@tonic-gate 	if (fnp->fn_symlink) {
13840Sstevel@tonic-gate 		ASSERT(fnp->fn_flags & MF_THISUID_MATCH_RQD);
13850Sstevel@tonic-gate 		kmem_free(fnp->fn_symlink, fnp->fn_symlinklen);
13860Sstevel@tonic-gate 	}
13870Sstevel@tonic-gate 	if (fnp->fn_cred)
13880Sstevel@tonic-gate 		crfree(fnp->fn_cred);
13890Sstevel@tonic-gate 	mutex_destroy(&fnp->fn_lock);
13900Sstevel@tonic-gate 	rw_destroy(&fnp->fn_rwlock);
13910Sstevel@tonic-gate 	cv_destroy(&fnp->fn_cv_mount);
13920Sstevel@tonic-gate 	vn_free(vp);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	mutex_enter(&autofs_nodeid_lock);
13950Sstevel@tonic-gate 	fnp->fn_globals->fng_fnnode_count--;
13960Sstevel@tonic-gate 	mutex_exit(&autofs_nodeid_lock);
13970Sstevel@tonic-gate 	kmem_free(fnp, sizeof (*fnp));
13980Sstevel@tonic-gate }
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate void
14010Sstevel@tonic-gate auto_disconnect(
14020Sstevel@tonic-gate 	fnnode_t *dfnp,
14030Sstevel@tonic-gate 	fnnode_t *fnp)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate 	fnnode_t *tmp, **fnpp;
14060Sstevel@tonic-gate 	vnode_t *vp = fntovn(fnp);
14070Sstevel@tonic-gate 	timestruc_t now;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	AUTOFS_DPRINT((4,
14100Sstevel@tonic-gate 	    "auto_disconnect: dfnp=%p fnp=%p linkcnt=%d\n v_count=%d",
14110Sstevel@tonic-gate 	    (void *)dfnp, (void *)fnp, fnp->fn_linkcnt, vp->v_count));
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
14140Sstevel@tonic-gate 	ASSERT(fnp->fn_linkcnt == 1);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if (vn_mountedvfs(vp) != NULL) {
14170Sstevel@tonic-gate 		cmn_err(CE_PANIC, "auto_disconnect: vp %p mounted on",
14180Sstevel@tonic-gate 		    (void *)vp);
14190Sstevel@tonic-gate 	}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	/*
14220Sstevel@tonic-gate 	 * Decrement by 1 because we're removing the entry in dfnp.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	fnp->fn_linkcnt--;
14250Sstevel@tonic-gate 	fnp->fn_size--;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	/*
14280Sstevel@tonic-gate 	 * only changed while holding parent's (dfnp) rw_lock
14290Sstevel@tonic-gate 	 */
14300Sstevel@tonic-gate 	fnp->fn_parent = NULL;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	fnpp = &dfnp->fn_dirents;
14330Sstevel@tonic-gate 	for (;;) {
14340Sstevel@tonic-gate 		tmp = *fnpp;
14350Sstevel@tonic-gate 		if (tmp == NULL) {
14360Sstevel@tonic-gate 			cmn_err(CE_PANIC,
14370Sstevel@tonic-gate 			    "auto_disconnect: %p not in %p dirent list",
14380Sstevel@tonic-gate 			    (void *)fnp, (void *)dfnp);
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 		if (tmp == fnp) {
14410Sstevel@tonic-gate 			*fnpp = tmp->fn_next; 	/* remove it from the list */
14420Sstevel@tonic-gate 			ASSERT(vp->v_count == 0);
14430Sstevel@tonic-gate 			/* child had a pointer to parent ".." */
14440Sstevel@tonic-gate 			dfnp->fn_linkcnt--;
14450Sstevel@tonic-gate 			dfnp->fn_size--;
14460Sstevel@tonic-gate 			break;
14470Sstevel@tonic-gate 		}
14480Sstevel@tonic-gate 		fnpp = &tmp->fn_next;
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
14520Sstevel@tonic-gate 	gethrestime(&now);
14530Sstevel@tonic-gate 	fnp->fn_atime = fnp->fn_mtime = now;
14540Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_disconnect: done\n"));
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate int
14600Sstevel@tonic-gate auto_enter(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
14610Sstevel@tonic-gate {
14620Sstevel@tonic-gate 	struct fnnode *cfnp, **spp;
14630Sstevel@tonic-gate 	vnode_t *dvp = fntovn(dfnp);
14640Sstevel@tonic-gate 	ushort_t offset = 0;
14650Sstevel@tonic-gate 	ushort_t diff;
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_enter: dfnp=%p, name=%s ", (void *)dfnp, name));
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	cfnp = dfnp->fn_dirents;
14720Sstevel@tonic-gate 	if (cfnp == NULL) {
14730Sstevel@tonic-gate 		/*
14740Sstevel@tonic-gate 		 * offset = 0 for '.' and offset = 1 for '..'
14750Sstevel@tonic-gate 		 */
14760Sstevel@tonic-gate 		spp = &dfnp->fn_dirents;
14770Sstevel@tonic-gate 		offset = 2;
14780Sstevel@tonic-gate 	}
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	for (; cfnp; cfnp = cfnp->fn_next) {
14810Sstevel@tonic-gate 		if (strcmp(cfnp->fn_name, name) == 0) {
14820Sstevel@tonic-gate 			mutex_enter(&cfnp->fn_lock);
14830Sstevel@tonic-gate 			if (cfnp->fn_flags & MF_THISUID_MATCH_RQD) {
14840Sstevel@tonic-gate 				/*
14850Sstevel@tonic-gate 				 * "thisuser" kind of node, need to
14860Sstevel@tonic-gate 				 * match CREDs as well
14870Sstevel@tonic-gate 				 */
14880Sstevel@tonic-gate 				mutex_exit(&cfnp->fn_lock);
14890Sstevel@tonic-gate 				if (crcmp(cfnp->fn_cred, cred) == 0)
14900Sstevel@tonic-gate 					return (EEXIST);
14910Sstevel@tonic-gate 			} else {
14920Sstevel@tonic-gate 				mutex_exit(&cfnp->fn_lock);
14930Sstevel@tonic-gate 				return (EEXIST);
14940Sstevel@tonic-gate 			}
14950Sstevel@tonic-gate 		}
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 		if (cfnp->fn_next != NULL) {
14980Sstevel@tonic-gate 			diff = (ushort_t)
14990Sstevel@tonic-gate 			    (cfnp->fn_next->fn_offset - cfnp->fn_offset);
15000Sstevel@tonic-gate 			ASSERT(diff != 0);
15010Sstevel@tonic-gate 			if (diff > 1 && offset == 0) {
15020Sstevel@tonic-gate 				offset = (ushort_t)cfnp->fn_offset + 1;
15030Sstevel@tonic-gate 				spp = &cfnp->fn_next;
15040Sstevel@tonic-gate 			}
15050Sstevel@tonic-gate 		} else if (offset == 0) {
15060Sstevel@tonic-gate 			offset = (ushort_t)cfnp->fn_offset + 1;
15070Sstevel@tonic-gate 			spp = &cfnp->fn_next;
15080Sstevel@tonic-gate 		}
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	*fnpp = auto_makefnnode(VDIR, dvp->v_vfsp, name, cred,
15120Sstevel@tonic-gate 	    dfnp->fn_globals);
15130Sstevel@tonic-gate 	if (*fnpp == NULL)
15140Sstevel@tonic-gate 		return (ENOMEM);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/*
15170Sstevel@tonic-gate 	 * I don't hold the mutex on fnpp because I created it, and
15180Sstevel@tonic-gate 	 * I'm already holding the writers lock for it's parent
15190Sstevel@tonic-gate 	 * directory, therefore nobody can reference it without me first
15200Sstevel@tonic-gate 	 * releasing the writers lock.
15210Sstevel@tonic-gate 	 */
15220Sstevel@tonic-gate 	(*fnpp)->fn_offset = offset;
15230Sstevel@tonic-gate 	(*fnpp)->fn_next = *spp;
15240Sstevel@tonic-gate 	*spp = *fnpp;
15250Sstevel@tonic-gate 	(*fnpp)->fn_parent = dfnp;
15260Sstevel@tonic-gate 	(*fnpp)->fn_linkcnt++;	/* parent now holds reference to entry */
15270Sstevel@tonic-gate 	(*fnpp)->fn_size++;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/*
15300Sstevel@tonic-gate 	 * dfnp->fn_linkcnt and dfnp->fn_size protected by dfnp->rw_lock
15310Sstevel@tonic-gate 	 */
15320Sstevel@tonic-gate 	dfnp->fn_linkcnt++;	/* child now holds reference to parent '..' */
15330Sstevel@tonic-gate 	dfnp->fn_size++;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	dfnp->fn_ref_time = gethrestime_sec();
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "*fnpp=%p\n", (void *)*fnpp));
15380Sstevel@tonic-gate 	return (0);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate int
15420Sstevel@tonic-gate auto_search(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
15430Sstevel@tonic-gate {
15440Sstevel@tonic-gate 	vnode_t *dvp;
15450Sstevel@tonic-gate 	fnnode_t *p;
15460Sstevel@tonic-gate 	int error = ENOENT, match = 0;
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_search: dfnp=%p, name=%s...\n",
15490Sstevel@tonic-gate 	    (void *)dfnp, name));
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	dvp = fntovn(dfnp);
15520Sstevel@tonic-gate 	if (dvp->v_type != VDIR) {
15530Sstevel@tonic-gate 		cmn_err(CE_PANIC, "auto_search: dvp=%p not a directory",
15540Sstevel@tonic-gate 		    (void *)dvp);
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dfnp->fn_rwlock));
15580Sstevel@tonic-gate 	for (p = dfnp->fn_dirents; p != NULL; p = p->fn_next) {
15590Sstevel@tonic-gate 		if (strcmp(p->fn_name, name) == 0) {
15600Sstevel@tonic-gate 			mutex_enter(&p->fn_lock);
15610Sstevel@tonic-gate 			if (p->fn_flags & MF_THISUID_MATCH_RQD) {
15620Sstevel@tonic-gate 				/*
15630Sstevel@tonic-gate 				 * "thisuser" kind of node
15640Sstevel@tonic-gate 				 * Need to match CREDs as well
15650Sstevel@tonic-gate 				 */
15660Sstevel@tonic-gate 				mutex_exit(&p->fn_lock);
15670Sstevel@tonic-gate 				match = crcmp(p->fn_cred, cred) == 0;
15680Sstevel@tonic-gate 			} else {
15690Sstevel@tonic-gate 				/*
15700Sstevel@tonic-gate 				 * No need to check CRED
15710Sstevel@tonic-gate 				 */
15720Sstevel@tonic-gate 				mutex_exit(&p->fn_lock);
15730Sstevel@tonic-gate 				match = 1;
15740Sstevel@tonic-gate 			}
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 		if (match) {
15770Sstevel@tonic-gate 			error = 0;
15780Sstevel@tonic-gate 			if (fnpp) {
15790Sstevel@tonic-gate 				*fnpp = p;
15800Sstevel@tonic-gate 				VN_HOLD(fntovn(*fnpp));
15810Sstevel@tonic-gate 			}
15820Sstevel@tonic-gate 			break;
15830Sstevel@tonic-gate 		}
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_search: error=%d\n", error));
15870Sstevel@tonic-gate 	return (error);
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate /*
15910Sstevel@tonic-gate  * If dvp is mounted on, get path's vnode in the mounted on
15920Sstevel@tonic-gate  * filesystem.  Path is relative to dvp, ie "./path".
15930Sstevel@tonic-gate  * If successful, *mvp points to a the held mountpoint vnode.
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate /* ARGSUSED */
15960Sstevel@tonic-gate static int
15970Sstevel@tonic-gate auto_getmntpnt(
15980Sstevel@tonic-gate 	vnode_t *dvp,
15990Sstevel@tonic-gate 	char *path,
16000Sstevel@tonic-gate 	vnode_t **mvpp,		/* vnode for mountpoint */
16010Sstevel@tonic-gate 	cred_t *cred)
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate 	int error = 0;
16040Sstevel@tonic-gate 	vnode_t *newvp;
16050Sstevel@tonic-gate 	char namebuf[TYPICALMAXPATHLEN];
16060Sstevel@tonic-gate 	struct pathname lookpn;
16070Sstevel@tonic-gate 	vfs_t *vfsp;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "auto_getmntpnt: path=%s\n", path));
16100Sstevel@tonic-gate 
16111153Snr123932 	if (error = vn_vfsrlock_wait(dvp))
16120Sstevel@tonic-gate 		return (error);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	/*
16150Sstevel@tonic-gate 	 * Now that we have the vfswlock, check to see if dvp
16160Sstevel@tonic-gate 	 * is still mounted on.  If not, then just bail out as
16170Sstevel@tonic-gate 	 * there is no need to remount the triggers since the
16180Sstevel@tonic-gate 	 * higher level mount point has gotten unmounted.
16190Sstevel@tonic-gate 	 */
16200Sstevel@tonic-gate 	vfsp = vn_mountedvfs(dvp);
16210Sstevel@tonic-gate 	if (vfsp == NULL) {
16220Sstevel@tonic-gate 		vn_vfsunlock(dvp);
16230Sstevel@tonic-gate 		error = EBUSY;
16240Sstevel@tonic-gate 		goto done;
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 	/*
16270Sstevel@tonic-gate 	 * Since mounted on, lookup "path" in the new filesystem,
16280Sstevel@tonic-gate 	 * it is important that we do the filesystem jump here to
16290Sstevel@tonic-gate 	 * avoid lookuppn() calling auto_lookup on dvp and deadlock.
16300Sstevel@tonic-gate 	 */
16311153Snr123932 	error = VFS_ROOT(vfsp, &newvp);
16320Sstevel@tonic-gate 	vn_vfsunlock(dvp);
16330Sstevel@tonic-gate 	if (error)
16340Sstevel@tonic-gate 		goto done;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	/*
16370Sstevel@tonic-gate 	 * We do a VN_HOLD on newvp just in case the first call to
16380Sstevel@tonic-gate 	 * lookuppnvp() fails with ENAMETOOLONG.  We should still have a
16390Sstevel@tonic-gate 	 * reference to this vnode for the second call to lookuppnvp().
16400Sstevel@tonic-gate 	 */
16410Sstevel@tonic-gate 	VN_HOLD(newvp);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	/*
16440Sstevel@tonic-gate 	 * Now create the pathname struct so we can make use of lookuppnvp,
16450Sstevel@tonic-gate 	 * and pn_getcomponent.
16460Sstevel@tonic-gate 	 * This code is similar to lookupname() in fs/lookup.c.
16470Sstevel@tonic-gate 	 */
16480Sstevel@tonic-gate 	error = pn_get_buf(path, UIO_SYSSPACE, &lookpn,
16490Sstevel@tonic-gate 		namebuf, sizeof (namebuf));
16500Sstevel@tonic-gate 	if (error == 0) {
16510Sstevel@tonic-gate 		error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
16520Sstevel@tonic-gate 		    mvpp, rootdir, newvp, cred);
16530Sstevel@tonic-gate 	} else
16540Sstevel@tonic-gate 		VN_RELE(newvp);
16550Sstevel@tonic-gate 	if (error == ENAMETOOLONG) {
16560Sstevel@tonic-gate 		/*
16570Sstevel@tonic-gate 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
16580Sstevel@tonic-gate 		 * newvp is VN_RELE'd by this call to lookuppnvp.
16590Sstevel@tonic-gate 		 *
16600Sstevel@tonic-gate 		 * Using 'rootdir' in a zone's context is OK here: we already
16610Sstevel@tonic-gate 		 * ascertained that there are no '..'s in the path, and we're
16620Sstevel@tonic-gate 		 * not following symlinks.
16630Sstevel@tonic-gate 		 */
16640Sstevel@tonic-gate 		if ((error = pn_get(path, UIO_SYSSPACE, &lookpn)) == 0) {
16650Sstevel@tonic-gate 			error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
16660Sstevel@tonic-gate 			    mvpp, rootdir, newvp, cred);
16670Sstevel@tonic-gate 			pn_free(&lookpn);
16680Sstevel@tonic-gate 		} else
16690Sstevel@tonic-gate 			VN_RELE(newvp);
16700Sstevel@tonic-gate 	} else {
16710Sstevel@tonic-gate 		/*
16720Sstevel@tonic-gate 		 * Need to release newvp here since we held it.
16730Sstevel@tonic-gate 		 */
16740Sstevel@tonic-gate 		VN_RELE(newvp);
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate done:
16780Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_getmntpnt: path=%s *mvpp=%p error=%d\n",
16790Sstevel@tonic-gate 	    path, (void *)*mvpp, error));
16800Sstevel@tonic-gate 	return (error);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate #define	DEEPER(x) (((x)->fn_dirents != NULL) || \
16840Sstevel@tonic-gate 			(vn_mountedvfs(fntovn((x)))) != NULL)
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate /*
16870Sstevel@tonic-gate  * The caller, should have already VN_RELE'd its reference to the
16880Sstevel@tonic-gate  * root vnode of this filesystem.
16890Sstevel@tonic-gate  */
16900Sstevel@tonic-gate static int
16910Sstevel@tonic-gate auto_inkernel_unmount(vfs_t *vfsp)
16920Sstevel@tonic-gate {
16930Sstevel@tonic-gate 	vnode_t *cvp = vfsp->vfs_vnodecovered;
16940Sstevel@tonic-gate 	int error;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	AUTOFS_DPRINT((4,
16970Sstevel@tonic-gate 	    "auto_inkernel_unmount: devid=%lx mntpnt(%p) count %u\n",
16980Sstevel@tonic-gate 	    vfsp->vfs_dev, (void *)cvp, cvp->v_count));
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(cvp));
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	/*
17030Sstevel@tonic-gate 	 * Perform the unmount
17040Sstevel@tonic-gate 	 * The mountpoint has already been locked by the caller.
17050Sstevel@tonic-gate 	 */
17060Sstevel@tonic-gate 	error = dounmount(vfsp, 0, kcred);
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "auto_inkernel_unmount: exit count %u\n",
17090Sstevel@tonic-gate 	    cvp->v_count));
17100Sstevel@tonic-gate 	return (error);
17110Sstevel@tonic-gate }
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate /*
17140Sstevel@tonic-gate  * unmounts trigger nodes in the kernel.
17150Sstevel@tonic-gate  */
17160Sstevel@tonic-gate static void
17170Sstevel@tonic-gate unmount_triggers(fnnode_t *fnp, action_list **alp)
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate 	fnnode_t *tp, *next;
17200Sstevel@tonic-gate 	int error = 0;
17210Sstevel@tonic-gate 	vfs_t *vfsp;
17220Sstevel@tonic-gate 	vnode_t *tvp;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "unmount_triggers: fnp=%p\n", (void *)fnp));
17250Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	*alp = fnp->fn_alp;
17280Sstevel@tonic-gate 	next = fnp->fn_trigger;
17290Sstevel@tonic-gate 	while ((tp = next) != NULL) {
17300Sstevel@tonic-gate 		tvp = fntovn(tp);
17310Sstevel@tonic-gate 		ASSERT(tvp->v_count >= 2);
17320Sstevel@tonic-gate 		next = tp->fn_next;
17330Sstevel@tonic-gate 		/*
17340Sstevel@tonic-gate 		 * drop writer's lock since the unmount will end up
17350Sstevel@tonic-gate 		 * disconnecting this node from fnp and needs to acquire
17360Sstevel@tonic-gate 		 * the writer's lock again.
17370Sstevel@tonic-gate 		 * next has at least a reference count >= 2 since it's
17380Sstevel@tonic-gate 		 * a trigger node, therefore can not be accidentally freed
17390Sstevel@tonic-gate 		 * by a VN_RELE
17400Sstevel@tonic-gate 		 */
17410Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 		vfsp = tvp->v_vfsp;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 		/*
17460Sstevel@tonic-gate 		 * Its parent was holding a reference to it, since this
17470Sstevel@tonic-gate 		 * is a trigger vnode.
17480Sstevel@tonic-gate 		 */
17490Sstevel@tonic-gate 		VN_RELE(tvp);
17500Sstevel@tonic-gate 		if (error = auto_inkernel_unmount(vfsp)) {
17510Sstevel@tonic-gate 			cmn_err(CE_PANIC, "unmount_triggers: "
17520Sstevel@tonic-gate 			    "unmount of vp=%p failed error=%d",
17530Sstevel@tonic-gate 			    (void *)tvp, error);
17540Sstevel@tonic-gate 		}
17550Sstevel@tonic-gate 		/*
17560Sstevel@tonic-gate 		 * reacquire writer's lock
17570Sstevel@tonic-gate 		 */
17580Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
17590Sstevel@tonic-gate 	}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	/*
17620Sstevel@tonic-gate 	 * We were holding a reference to our parent.  Drop that.
17630Sstevel@tonic-gate 	 */
17640Sstevel@tonic-gate 	VN_RELE(fntovn(fnp));
17650Sstevel@tonic-gate 	fnp->fn_trigger = NULL;
17660Sstevel@tonic-gate 	fnp->fn_alp = NULL;
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "unmount_triggers: finished\n"));
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate /*
17720Sstevel@tonic-gate  * This routine locks the mountpoint of every trigger node if they're
17730Sstevel@tonic-gate  * not busy, or returns EBUSY if any node is busy. If a trigger node should
17740Sstevel@tonic-gate  * be unmounted first, then it sets nfnp to point to it, otherwise nfnp
17750Sstevel@tonic-gate  * points to NULL.
17760Sstevel@tonic-gate  */
17770Sstevel@tonic-gate static int
17780Sstevel@tonic-gate triggers_busy(fnnode_t *fnp, fnnode_t **nfnp)
17790Sstevel@tonic-gate {
17800Sstevel@tonic-gate 	int error = 0, done;
17810Sstevel@tonic-gate 	int lck_error = 0;
17820Sstevel@tonic-gate 	fnnode_t *tp, *t1p;
17830Sstevel@tonic-gate 	vfs_t *vfsp;
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	*nfnp = NULL;
17880Sstevel@tonic-gate 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
17890Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\ttrigger: %s\n", tp->fn_name));
17900Sstevel@tonic-gate 		vfsp = fntovn(tp)->v_vfsp;
17910Sstevel@tonic-gate 		error = 0;
17920Sstevel@tonic-gate 		/*
17930Sstevel@tonic-gate 		 * The vn_vfsunlock will be done in auto_inkernel_unmount.
17940Sstevel@tonic-gate 		 */
17950Sstevel@tonic-gate 		lck_error = vn_vfswlock(vfsp->vfs_vnodecovered);
17960Sstevel@tonic-gate 		if (lck_error == 0) {
17970Sstevel@tonic-gate 			mutex_enter(&tp->fn_lock);
17980Sstevel@tonic-gate 			ASSERT((tp->fn_flags & MF_LOOKUP) == 0);
17990Sstevel@tonic-gate 			if (tp->fn_flags & MF_INPROG) {
18000Sstevel@tonic-gate 				/*
18010Sstevel@tonic-gate 				 * a mount is in progress
18020Sstevel@tonic-gate 				 */
18030Sstevel@tonic-gate 				error = EBUSY;
18040Sstevel@tonic-gate 			}
18050Sstevel@tonic-gate 			mutex_exit(&tp->fn_lock);
18060Sstevel@tonic-gate 		}
18070Sstevel@tonic-gate 		if (lck_error || error || DEEPER(tp) ||
18080Sstevel@tonic-gate 		    ((fntovn(tp))->v_count) > 2) {
18090Sstevel@tonic-gate 			/*
18100Sstevel@tonic-gate 			 * couldn't lock it because it's busy,
18110Sstevel@tonic-gate 			 * It is mounted on or has dirents?
18120Sstevel@tonic-gate 			 * If reference count is greater than two, then
18130Sstevel@tonic-gate 			 * somebody else is holding a reference to this vnode.
18140Sstevel@tonic-gate 			 * One reference is for the mountpoint, and the second
18150Sstevel@tonic-gate 			 * is for the trigger node.
18160Sstevel@tonic-gate 			 */
18170Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\ttrigger busy\n"));
18180Sstevel@tonic-gate 			if ((lck_error == 0) && (error == 0)) {
18190Sstevel@tonic-gate 				*nfnp = tp;
18200Sstevel@tonic-gate 				/*
18210Sstevel@tonic-gate 				 * The matching VN_RELE is done in
18220Sstevel@tonic-gate 				 * unmount_tree().
18230Sstevel@tonic-gate 				 */
18240Sstevel@tonic-gate 				VN_HOLD(fntovn(*nfnp));
18250Sstevel@tonic-gate 			}
18260Sstevel@tonic-gate 			/*
18270Sstevel@tonic-gate 			 * Unlock previously locked mountpoints
18280Sstevel@tonic-gate 			 */
18290Sstevel@tonic-gate 			for (done = 0, t1p = fnp->fn_trigger; !done;
18300Sstevel@tonic-gate 			    t1p = t1p->fn_next) {
18310Sstevel@tonic-gate 				/*
18320Sstevel@tonic-gate 				 * Unlock all nodes previously
18330Sstevel@tonic-gate 				 * locked. All nodes up to 'tp'
18340Sstevel@tonic-gate 				 * were successfully locked. If 'lck_err' is
18350Sstevel@tonic-gate 				 * set, then 'tp' was not locked, and thus
18360Sstevel@tonic-gate 				 * should not be unlocked. If
18370Sstevel@tonic-gate 				 * 'lck_err' is not set, then 'tp' was
18380Sstevel@tonic-gate 				 * successfully locked, and it should
18390Sstevel@tonic-gate 				 * be unlocked.
18400Sstevel@tonic-gate 				 */
18410Sstevel@tonic-gate 				if (t1p != tp || !lck_error) {
18420Sstevel@tonic-gate 					vfsp = fntovn(t1p)->v_vfsp;
18430Sstevel@tonic-gate 					vn_vfsunlock(vfsp->vfs_vnodecovered);
18440Sstevel@tonic-gate 				}
18450Sstevel@tonic-gate 				done = (t1p == tp);
18460Sstevel@tonic-gate 			}
18470Sstevel@tonic-gate 			error = EBUSY;
18480Sstevel@tonic-gate 			break;
18490Sstevel@tonic-gate 		}
18500Sstevel@tonic-gate 	}
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "triggers_busy: error=%d\n", error));
18530Sstevel@tonic-gate 	return (error);
18540Sstevel@tonic-gate }
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate /*
18570Sstevel@tonic-gate  * Unlock previously locked trigger nodes.
18580Sstevel@tonic-gate  */
18590Sstevel@tonic-gate static int
18600Sstevel@tonic-gate triggers_unlock(fnnode_t *fnp)
18610Sstevel@tonic-gate {
18620Sstevel@tonic-gate 	fnnode_t *tp;
18630Sstevel@tonic-gate 	vfs_t *vfsp;
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
18680Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tunlock trigger: %s\n", tp->fn_name));
18690Sstevel@tonic-gate 		vfsp = fntovn(tp)->v_vfsp;
18700Sstevel@tonic-gate 		vn_vfsunlock(vfsp->vfs_vnodecovered);
18710Sstevel@tonic-gate 	}
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	return (0);
18740Sstevel@tonic-gate }
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate /*
18770Sstevel@tonic-gate  * It is the caller's responsibility to grab the VVFSLOCK.
18780Sstevel@tonic-gate  * Releases the VVFSLOCK upon return.
18790Sstevel@tonic-gate  */
18800Sstevel@tonic-gate static int
18810Sstevel@tonic-gate unmount_node(vnode_t *cvp, int force)
18820Sstevel@tonic-gate {
18830Sstevel@tonic-gate 	int error = 0;
18840Sstevel@tonic-gate 	fnnode_t *cfnp;
18850Sstevel@tonic-gate 	vfs_t *vfsp;
18860Sstevel@tonic-gate 	umntrequest ul;
18870Sstevel@tonic-gate 	fninfo_t *fnip;
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tunmount_node cvp=%p\n", (void *)cvp));
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	ASSERT(vn_vfswlock_held(cvp));
18920Sstevel@tonic-gate 	cfnp = vntofn(cvp);
18930Sstevel@tonic-gate 	vfsp = vn_mountedvfs(cvp);
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	if (force || cfnp->fn_flags & MF_IK_MOUNT) {
18960Sstevel@tonic-gate 		/*
18970Sstevel@tonic-gate 		 * Mount was performed in the kernel, so
18980Sstevel@tonic-gate 		 * do an in-kernel unmount. auto_inkernel_unmount()
18990Sstevel@tonic-gate 		 * will vn_vfsunlock(cvp).
19000Sstevel@tonic-gate 		 */
19010Sstevel@tonic-gate 		error = auto_inkernel_unmount(vfsp);
19020Sstevel@tonic-gate 	} else {
19030Sstevel@tonic-gate 		zone_t *zone = NULL;
19040Sstevel@tonic-gate 		refstr_t *mntpt, *resource;
19050Sstevel@tonic-gate 		size_t mntoptslen;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 		/*
19080Sstevel@tonic-gate 		 * Get the mnttab information of the node
19090Sstevel@tonic-gate 		 * and ask the daemon to unmount it.
19100Sstevel@tonic-gate 		 */
19110Sstevel@tonic-gate 		bzero(&ul, sizeof (ul));
19120Sstevel@tonic-gate 		mntfs_getmntopts(vfsp, &ul.mntopts, &mntoptslen);
19130Sstevel@tonic-gate 		if (ul.mntopts == NULL) {
1914*2170Sevanl 			auto_log(cfnp->fn_globals->fng_verbose,
1915*2170Sevanl 				cfnp->fn_globals->fng_zoneid,
1916*2170Sevanl 				CE_WARN, "unmount_node: "
19170Sstevel@tonic-gate 			    "no memory");
19180Sstevel@tonic-gate 			vn_vfsunlock(cvp);
19190Sstevel@tonic-gate 			error = ENOMEM;
19200Sstevel@tonic-gate 			goto done;
19210Sstevel@tonic-gate 		}
19220Sstevel@tonic-gate 		if (mntoptslen > AUTOFS_MAXOPTSLEN)
19230Sstevel@tonic-gate 			ul.mntopts[AUTOFS_MAXOPTSLEN - 1] = '\0';
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 		mntpt = vfs_getmntpoint(vfsp);
19260Sstevel@tonic-gate 		ul.mntpnt = (char *)refstr_value(mntpt);
19270Sstevel@tonic-gate 		resource = vfs_getresource(vfsp);
19280Sstevel@tonic-gate 		ul.mntresource = (char *)refstr_value(resource);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 		fnip = vfstofni(cvp->v_vfsp);
19310Sstevel@tonic-gate 		ul.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 		/*
19340Sstevel@tonic-gate 		 * Since a zone'd automountd's view of the autofs mount points
19350Sstevel@tonic-gate 		 * differs from those in the kernel, we need to make sure we
19360Sstevel@tonic-gate 		 * give it consistent mount points.
19370Sstevel@tonic-gate 		 */
19380Sstevel@tonic-gate 		ASSERT(fnip->fi_zoneid == getzoneid());
19390Sstevel@tonic-gate 		zone = curproc->p_zone;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 		if (fnip->fi_zoneid != GLOBAL_ZONEID) {
19420Sstevel@tonic-gate 			if (ZONE_PATH_VISIBLE(ul.mntpnt, zone)) {
19430Sstevel@tonic-gate 				ul.mntpnt =
19440Sstevel@tonic-gate 				    ZONE_PATH_TRANSLATE(ul.mntpnt, zone);
19450Sstevel@tonic-gate 			}
19460Sstevel@tonic-gate 			if (ZONE_PATH_VISIBLE(ul.mntresource, zone)) {
19470Sstevel@tonic-gate 				ul.mntresource =
19480Sstevel@tonic-gate 				    ZONE_PATH_TRANSLATE(ul.mntresource, zone);
19490Sstevel@tonic-gate 			}
19500Sstevel@tonic-gate 		}
1951*2170Sevanl 
19520Sstevel@tonic-gate 		ul.fstype = vfssw[vfsp->vfs_fstype].vsw_name;
19530Sstevel@tonic-gate 		vn_vfsunlock(cvp);
19540Sstevel@tonic-gate 
1955*2170Sevanl 		error = auto_send_unmount_request(fnip, &ul, FALSE);
19560Sstevel@tonic-gate 		kmem_free(ul.mntopts, mntoptslen);
19570Sstevel@tonic-gate 		refstr_rele(mntpt);
19580Sstevel@tonic-gate 		refstr_rele(resource);
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate done:
19620Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tunmount_node cvp=%p error=%d\n", (void *)cvp,
19630Sstevel@tonic-gate 	    error));
19640Sstevel@tonic-gate 	return (error);
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate /*
19680Sstevel@tonic-gate  * vp is the "root" of the AUTOFS filesystem.
19690Sstevel@tonic-gate  * return EBUSY if any thread is holding a reference to this vnode
19700Sstevel@tonic-gate  * other than us.
19710Sstevel@tonic-gate  */
19720Sstevel@tonic-gate static int
19730Sstevel@tonic-gate check_auto_node(vnode_t *vp)
19740Sstevel@tonic-gate {
19750Sstevel@tonic-gate 	fnnode_t *fnp;
19760Sstevel@tonic-gate 	int error = 0;
19770Sstevel@tonic-gate 	/*
19780Sstevel@tonic-gate 	 * number of references to expect for
19790Sstevel@tonic-gate 	 * a non-busy vnode.
19800Sstevel@tonic-gate 	 */
19810Sstevel@tonic-gate 	uint_t count;
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tcheck_auto_node vp=%p ", (void *)vp));
19840Sstevel@tonic-gate 	fnp = vntofn(vp);
19850Sstevel@tonic-gate 	ASSERT(fnp->fn_flags & MF_INPROG);
19860Sstevel@tonic-gate 	ASSERT((fnp->fn_flags & MF_LOOKUP) == 0);
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	count = 1;		/* we are holding a reference to vp */
19890Sstevel@tonic-gate 	if (fnp->fn_flags & MF_TRIGGER) {
19900Sstevel@tonic-gate 		/*
19910Sstevel@tonic-gate 		 * parent holds a pointer to us (trigger)
19920Sstevel@tonic-gate 		 */
19930Sstevel@tonic-gate 		count++;
19940Sstevel@tonic-gate 	}
19950Sstevel@tonic-gate 	if (fnp->fn_trigger != NULL) {
19960Sstevel@tonic-gate 		/*
19970Sstevel@tonic-gate 		 * The trigger nodes have a hold on us.
19980Sstevel@tonic-gate 		 */
19990Sstevel@tonic-gate 		count++;
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
20020Sstevel@tonic-gate 	if (vp->v_flag & VROOT)
20030Sstevel@tonic-gate 		count++;
20040Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
20050Sstevel@tonic-gate 	AUTOFS_DPRINT((10, "\tcount=%u ", vp->v_count));
20060Sstevel@tonic-gate 	if (vp->v_count > count)
20070Sstevel@tonic-gate 		error = EBUSY;
20080Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tcheck_auto_node error=%d ", error));
20110Sstevel@tonic-gate 	return (error);
20120Sstevel@tonic-gate }
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate /*
20150Sstevel@tonic-gate  * rootvp is the root of the AUTOFS filesystem.
20160Sstevel@tonic-gate  * If rootvp is busy (v_count > 1) returns EBUSY.
20170Sstevel@tonic-gate  * else removes every vnode under this tree.
20180Sstevel@tonic-gate  * ASSUMPTION: Assumes that the only node which can be busy is
20190Sstevel@tonic-gate  * the root vnode. This filesystem better be two levels deep only,
20200Sstevel@tonic-gate  * the root and its immediate subdirs.
20210Sstevel@tonic-gate  * The daemon will "AUTOFS direct-mount" only one level below the root.
20220Sstevel@tonic-gate  */
20230Sstevel@tonic-gate static int
20240Sstevel@tonic-gate unmount_autofs(vnode_t *rootvp)
20250Sstevel@tonic-gate {
20260Sstevel@tonic-gate 	fnnode_t *fnp, *rootfnp, *nfnp;
20270Sstevel@tonic-gate 	int error;
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "\tunmount_autofs rootvp=%p ", (void *)rootvp));
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	error = check_auto_node(rootvp);
20320Sstevel@tonic-gate 	if (error == 0) {
20330Sstevel@tonic-gate 		/*
20340Sstevel@tonic-gate 		 * Remove all its immediate subdirectories.
20350Sstevel@tonic-gate 		 */
20360Sstevel@tonic-gate 		rootfnp = vntofn(rootvp);
20370Sstevel@tonic-gate 		rw_enter(&rootfnp->fn_rwlock, RW_WRITER);
20380Sstevel@tonic-gate 		nfnp = NULL;	/* lint clean */
20390Sstevel@tonic-gate 		for (fnp = rootfnp->fn_dirents; fnp != NULL; fnp = nfnp) {
20400Sstevel@tonic-gate 			ASSERT(fntovn(fnp)->v_count == 0);
20410Sstevel@tonic-gate 			ASSERT(fnp->fn_dirents == NULL);
20420Sstevel@tonic-gate 			ASSERT(fnp->fn_linkcnt == 2);
20430Sstevel@tonic-gate 			fnp->fn_linkcnt--;
20440Sstevel@tonic-gate 			auto_disconnect(rootfnp, fnp);
20450Sstevel@tonic-gate 			nfnp = fnp->fn_next;
20460Sstevel@tonic-gate 			auto_freefnnode(fnp);
20470Sstevel@tonic-gate 		}
20480Sstevel@tonic-gate 		rw_exit(&rootfnp->fn_rwlock);
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "\tunmount_autofs error=%d ", error));
20510Sstevel@tonic-gate 	return (error);
20520Sstevel@tonic-gate }
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate /*
20550Sstevel@tonic-gate  * max number of unmount threads running
20560Sstevel@tonic-gate  */
20570Sstevel@tonic-gate static int autofs_unmount_threads = 5;
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate /*
20600Sstevel@tonic-gate  * XXX unmount_tree() is not suspend-safe within the scope of
20610Sstevel@tonic-gate  * the present model defined for cpr to suspend the system. Calls made
20620Sstevel@tonic-gate  * by the unmount_tree() that have been identified to be unsafe are
20630Sstevel@tonic-gate  * (1) RPC client handle setup and client calls to automountd which can
20640Sstevel@tonic-gate  * block deep down in the RPC library, (2) kmem_alloc() calls with the
20650Sstevel@tonic-gate  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*() and
20660Sstevel@tonic-gate  * VOP_*() calls which can result in over the wire calls to servers.
20670Sstevel@tonic-gate  * The thread should be completely reevaluated to make it suspend-safe in
20680Sstevel@tonic-gate  * case of future updates to the cpr model.
20690Sstevel@tonic-gate  */
20700Sstevel@tonic-gate void
20710Sstevel@tonic-gate unmount_tree(struct autofs_globals *fngp, int force)
20720Sstevel@tonic-gate {
20730Sstevel@tonic-gate 	vnode_t *vp, *newvp;
20740Sstevel@tonic-gate 	vfs_t *vfsp;
20750Sstevel@tonic-gate 	fnnode_t *fnp, *nfnp, *pfnp;
20760Sstevel@tonic-gate 	action_list *alp;
20770Sstevel@tonic-gate 	int error, ilocked_it = 0;
20780Sstevel@tonic-gate 	fninfo_t *fnip;
20790Sstevel@tonic-gate 	time_t ref_time;
20800Sstevel@tonic-gate 	int autofs_busy_root, unmount_as_unit, unmount_done = 0;
20810Sstevel@tonic-gate 	timestruc_t now;
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	callb_cpr_t cprinfo;
20840Sstevel@tonic-gate 	kmutex_t unmount_tree_cpr_lock;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	mutex_init(&unmount_tree_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
20870Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &unmount_tree_cpr_lock, callb_generic_cpr,
20880Sstevel@tonic-gate 		"unmount_tree");
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	/*
20910Sstevel@tonic-gate 	 * Got to release lock before attempting unmount in case
20920Sstevel@tonic-gate 	 * it hangs.
20930Sstevel@tonic-gate 	 */
20940Sstevel@tonic-gate 	rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_READER);
20950Sstevel@tonic-gate 	if ((fnp = fngp->fng_rootfnnodep->fn_dirents) == NULL) {
20960Sstevel@tonic-gate 		ASSERT(fngp->fng_fnnode_count == 1);
20970Sstevel@tonic-gate 		/*
20980Sstevel@tonic-gate 		 * no autofs mounted, done.
20990Sstevel@tonic-gate 		 */
21000Sstevel@tonic-gate 		rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
21010Sstevel@tonic-gate 		goto done;
21020Sstevel@tonic-gate 	}
21030Sstevel@tonic-gate 	VN_HOLD(fntovn(fnp));
21040Sstevel@tonic-gate 	rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	vp = fntovn(fnp);
21070Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * autofssys() will be calling in from the global zone and doing
21100Sstevel@tonic-gate 	 * work on the behalf of the given zone, hence we can't always assert
21110Sstevel@tonic-gate 	 * that we have the right credentials, nor that the caller is always in
21120Sstevel@tonic-gate 	 * the correct zone.
21130Sstevel@tonic-gate 	 *
21140Sstevel@tonic-gate 	 * We do, however, know that if this is a "forced unmount" operation
21150Sstevel@tonic-gate 	 * (which autofssys() does), then we won't go down to the krpc layers,
21160Sstevel@tonic-gate 	 * so we don't need to fudge with the credentials.
21170Sstevel@tonic-gate 	 */
21180Sstevel@tonic-gate 	ASSERT(force || fnip->fi_zoneid == getzoneid());
2119*2170Sevanl 	if (!force && auto_null_request(fnip, FALSE) != 0) {
21200Sstevel@tonic-gate 		/*
21210Sstevel@tonic-gate 		 * automountd not running in this zone,
21220Sstevel@tonic-gate 		 * don't attempt unmounting this round.
21230Sstevel@tonic-gate 		 */
21240Sstevel@tonic-gate 		VN_RELE(vp);
21250Sstevel@tonic-gate 		goto done;
21260Sstevel@tonic-gate 	}
21270Sstevel@tonic-gate 	/* reference time for this unmount round */
21280Sstevel@tonic-gate 	ref_time = gethrestime_sec();
21290Sstevel@tonic-gate 	/*
21300Sstevel@tonic-gate 	 * If this an autofssys() call, we need to make sure we don't skip
21310Sstevel@tonic-gate 	 * nodes because we think we saw them recently.
21320Sstevel@tonic-gate 	 */
21330Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
21340Sstevel@tonic-gate 	if (force && fnp->fn_unmount_ref_time >= ref_time)
21350Sstevel@tonic-gate 		ref_time = fnp->fn_unmount_ref_time + 1;
21360Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	AUTOFS_DPRINT((4, "unmount_tree (ID=%ld)\n", ref_time));
21390Sstevel@tonic-gate top:
21400Sstevel@tonic-gate 	AUTOFS_DPRINT((10, "unmount_tree: %s\n", fnp->fn_name));
21410Sstevel@tonic-gate 	ASSERT(fnp);
21420Sstevel@tonic-gate 	vp = fntovn(fnp);
21430Sstevel@tonic-gate 	if (vp->v_type == VLNK) {
21440Sstevel@tonic-gate 		/*
21450Sstevel@tonic-gate 		 * can't unmount symbolic links
21460Sstevel@tonic-gate 		 */
21470Sstevel@tonic-gate 		goto next;
21480Sstevel@tonic-gate 	}
21490Sstevel@tonic-gate 	fnip = vfstofni(vp->v_vfsp);
21500Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
21510Sstevel@tonic-gate 	error = 0;
21520Sstevel@tonic-gate 	autofs_busy_root = unmount_as_unit = 0;
21530Sstevel@tonic-gate 	alp = NULL;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	ilocked_it = 0;
21560Sstevel@tonic-gate 	mutex_enter(&fnp->fn_lock);
21570Sstevel@tonic-gate 	if (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
21580Sstevel@tonic-gate 		/*
21590Sstevel@tonic-gate 		 * Either a mount, lookup or another unmount of this
21600Sstevel@tonic-gate 		 * subtree is in progress, don't attempt to unmount at
21610Sstevel@tonic-gate 		 * this time.
21620Sstevel@tonic-gate 		 */
21630Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
21640Sstevel@tonic-gate 		error = EBUSY;
21650Sstevel@tonic-gate 		goto next;
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 	if (fnp->fn_unmount_ref_time >= ref_time) {
21680Sstevel@tonic-gate 		/*
21690Sstevel@tonic-gate 		 * Already been here, try next node.
21700Sstevel@tonic-gate 		 */
21710Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
21720Sstevel@tonic-gate 		error = EBUSY;
21730Sstevel@tonic-gate 		goto next;
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 	fnp->fn_unmount_ref_time = ref_time;
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	/*
21780Sstevel@tonic-gate 	 * If forced operation ignore timeout values
21790Sstevel@tonic-gate 	 */
21800Sstevel@tonic-gate 	if (!force && fnp->fn_ref_time + fnip->fi_mount_to >
21810Sstevel@tonic-gate 	    gethrestime_sec()) {
21820Sstevel@tonic-gate 		/*
21830Sstevel@tonic-gate 		 * Node has been referenced recently, try the
21840Sstevel@tonic-gate 		 * unmount of its children if any.
21850Sstevel@tonic-gate 		 */
21860Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
21870Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "fn_ref_time within range\n"));
21880Sstevel@tonic-gate 		rw_enter(&fnp->fn_rwlock, RW_READER);
21890Sstevel@tonic-gate 		if (fnp->fn_dirents) {
21900Sstevel@tonic-gate 			/*
21910Sstevel@tonic-gate 			 * Has subdirectory, attempt their
21920Sstevel@tonic-gate 			 * unmount first
21930Sstevel@tonic-gate 			 */
21940Sstevel@tonic-gate 			nfnp = fnp->fn_dirents;
21950Sstevel@tonic-gate 			VN_HOLD(fntovn(nfnp));
21960Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 			VN_RELE(vp);
21990Sstevel@tonic-gate 			fnp = nfnp;
22000Sstevel@tonic-gate 			goto top;
22010Sstevel@tonic-gate 		}
22020Sstevel@tonic-gate 		rw_exit(&fnp->fn_rwlock);
22030Sstevel@tonic-gate 		/*
22040Sstevel@tonic-gate 		 * No children, try next node.
22050Sstevel@tonic-gate 		 */
22060Sstevel@tonic-gate 		error = EBUSY;
22070Sstevel@tonic-gate 		goto next;
22080Sstevel@tonic-gate 	}
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
22110Sstevel@tonic-gate 	fnp->fn_error = 0;
22120Sstevel@tonic-gate 	mutex_exit(&fnp->fn_lock);
22130Sstevel@tonic-gate 	ilocked_it = 1;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	rw_enter(&fnp->fn_rwlock, RW_WRITER);
22160Sstevel@tonic-gate 	if (fnp->fn_trigger != NULL) {
22170Sstevel@tonic-gate 		unmount_as_unit = 1;
22180Sstevel@tonic-gate 		if ((vn_mountedvfs(vp) == NULL) && (check_auto_node(vp))) {
22190Sstevel@tonic-gate 			/*
22200Sstevel@tonic-gate 			 * AUTOFS mountpoint is busy, there's
22210Sstevel@tonic-gate 			 * no point trying to unmount. Fall through
22220Sstevel@tonic-gate 			 * to attempt to unmount subtrees rooted
22230Sstevel@tonic-gate 			 * at a possible trigger node, but remember
22240Sstevel@tonic-gate 			 * not to unmount this tree.
22250Sstevel@tonic-gate 			 */
22260Sstevel@tonic-gate 			autofs_busy_root = 1;
22270Sstevel@tonic-gate 		}
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 		if (triggers_busy(fnp, &nfnp)) {
22300Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
22310Sstevel@tonic-gate 			if (nfnp == NULL) {
22320Sstevel@tonic-gate 				error = EBUSY;
22330Sstevel@tonic-gate 				goto next;
22340Sstevel@tonic-gate 			}
22350Sstevel@tonic-gate 			/*
22360Sstevel@tonic-gate 			 * nfnp is busy, try to unmount it first
22370Sstevel@tonic-gate 			 */
22380Sstevel@tonic-gate 			mutex_enter(&fnp->fn_lock);
22390Sstevel@tonic-gate 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
22400Sstevel@tonic-gate 			mutex_exit(&fnp->fn_lock);
22410Sstevel@tonic-gate 			VN_RELE(vp);
22420Sstevel@tonic-gate 			ASSERT(fntovn(nfnp)->v_count > 1);
22430Sstevel@tonic-gate 			fnp = nfnp;
22440Sstevel@tonic-gate 			goto top;
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 		/*
22480Sstevel@tonic-gate 		 * At this point, we know all trigger nodes are locked,
22490Sstevel@tonic-gate 		 * and they're not busy or mounted on.
22500Sstevel@tonic-gate 		 */
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 		if (autofs_busy_root) {
22530Sstevel@tonic-gate 			/*
22540Sstevel@tonic-gate 			 * Got to unlock the the trigger nodes since
22550Sstevel@tonic-gate 			 * I'm not really going to unmount the filesystem.
22560Sstevel@tonic-gate 			 */
22570Sstevel@tonic-gate 			(void) triggers_unlock(fnp);
22580Sstevel@tonic-gate 		} else {
22590Sstevel@tonic-gate 			/*
22600Sstevel@tonic-gate 			 * Attempt to unmount all the trigger nodes,
22610Sstevel@tonic-gate 			 * save the action_list in case we need to
2262*2170Sevanl 			 * remount them later. The action_list will be
22630Sstevel@tonic-gate 			 * freed later if there was no need to remount the
22640Sstevel@tonic-gate 			 * trigger nodes.
22650Sstevel@tonic-gate 			 */
22660Sstevel@tonic-gate 			unmount_triggers(fnp, &alp);
22670Sstevel@tonic-gate 		}
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 	rw_exit(&fnp->fn_rwlock);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	if (autofs_busy_root)
22720Sstevel@tonic-gate 		goto next;
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	(void) vn_vfswlock_wait(vp);
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	vfsp = vn_mountedvfs(vp);
22770Sstevel@tonic-gate 	if (vfsp != NULL) {
22780Sstevel@tonic-gate 		/*
22790Sstevel@tonic-gate 		 * Node is mounted on.
22800Sstevel@tonic-gate 		 */
22810Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tNode is mounted on\n"));
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 		/*
22840Sstevel@tonic-gate 		 * Deal with /xfn/host/jurassic alikes here...
22850Sstevel@tonic-gate 		 */
22860Sstevel@tonic-gate 		if (vfs_matchops(vfsp, vfs_getops(vp->v_vfsp))) {
22870Sstevel@tonic-gate 			/*
22880Sstevel@tonic-gate 			 * If the filesystem mounted here is AUTOFS, and it
22890Sstevel@tonic-gate 			 * is busy, try to unmount the tree rooted on it
22900Sstevel@tonic-gate 			 * first. We know this call to VFS_ROOT is safe to
22910Sstevel@tonic-gate 			 * call while holding VVFSLOCK, since it resolves
22920Sstevel@tonic-gate 			 * to a call to auto_root().
22930Sstevel@tonic-gate 			 */
22940Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\t\tAUTOFS mounted here\n"));
22950Sstevel@tonic-gate 			if (VFS_ROOT(vfsp, &newvp)) {
22960Sstevel@tonic-gate 				cmn_err(CE_PANIC,
22970Sstevel@tonic-gate 				    "unmount_tree: VFS_ROOT(vfs=%p) failed",
22980Sstevel@tonic-gate 				    (void *)vfsp);
22990Sstevel@tonic-gate 			}
23000Sstevel@tonic-gate 			nfnp = vntofn(newvp);
23010Sstevel@tonic-gate 			if (DEEPER(nfnp)) {
23020Sstevel@tonic-gate 				vn_vfsunlock(vp);
23030Sstevel@tonic-gate 				mutex_enter(&fnp->fn_lock);
23040Sstevel@tonic-gate 				AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
23050Sstevel@tonic-gate 				mutex_exit(&fnp->fn_lock);
23060Sstevel@tonic-gate 				VN_RELE(vp);
23070Sstevel@tonic-gate 				fnp = nfnp;
23080Sstevel@tonic-gate 				goto top;
23090Sstevel@tonic-gate 			}
23100Sstevel@tonic-gate 			/*
23110Sstevel@tonic-gate 			 * Fall through to unmount this filesystem
23120Sstevel@tonic-gate 			 */
23130Sstevel@tonic-gate 			VN_RELE(newvp);
23140Sstevel@tonic-gate 		}
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 		/*
23170Sstevel@tonic-gate 		 * vn_vfsunlock(vp) is done inside unmount_node()
23180Sstevel@tonic-gate 		 */
23190Sstevel@tonic-gate 		error = unmount_node(vp, force);
23200Sstevel@tonic-gate 		if (error == ECONNRESET) {
23210Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tConnection dropped\n"));
23220Sstevel@tonic-gate 			if (vn_mountedvfs(vp) == NULL) {
23230Sstevel@tonic-gate 				/*
23240Sstevel@tonic-gate 				 * The filesystem was unmounted before the
23250Sstevel@tonic-gate 				 * daemon died. Unfortunately we can not
23260Sstevel@tonic-gate 				 * determine whether all the cleanup work was
23270Sstevel@tonic-gate 				 * successfully finished (i.e. update mnttab,
23280Sstevel@tonic-gate 				 * or notify NFS server of the unmount).
23290Sstevel@tonic-gate 				 * We should not retry the operation since the
23300Sstevel@tonic-gate 				 * filesystem has already been unmounted, and
23310Sstevel@tonic-gate 				 * may have already been removed from mnttab,
23320Sstevel@tonic-gate 				 * in such case the devid/rdevid we send to
23330Sstevel@tonic-gate 				 * the daemon will not be matched. So we have
2334*2170Sevanl 				 * to be content with the partial unmount.
23350Sstevel@tonic-gate 				 * Since the mountpoint is no longer covered, we
23360Sstevel@tonic-gate 				 * clear the error condition.
23370Sstevel@tonic-gate 				 */
23380Sstevel@tonic-gate 				error = 0;
2339*2170Sevanl 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
2340*2170Sevanl 					CE_WARN,
23410Sstevel@tonic-gate 				    "unmount_tree: automountd connection "
23420Sstevel@tonic-gate 				    "dropped");
23430Sstevel@tonic-gate 				if (fnip->fi_flags & MF_DIRECT) {
2344*2170Sevanl 					auto_log(fngp->fng_verbose,
2345*2170Sevanl 						fngp->fng_zoneid, CE_WARN,
2346*2170Sevanl 						"unmount_tree: "
23470Sstevel@tonic-gate 					    "%s successfully unmounted - "
23480Sstevel@tonic-gate 					    "do not remount triggers",
23490Sstevel@tonic-gate 					    fnip->fi_path);
23500Sstevel@tonic-gate 				} else {
2351*2170Sevanl 					auto_log(fngp->fng_verbose,
2352*2170Sevanl 						fngp->fng_zoneid, CE_WARN,
2353*2170Sevanl 						"unmount_tree: "
23540Sstevel@tonic-gate 					    "%s/%s successfully unmounted - "
23550Sstevel@tonic-gate 					    "do not remount triggers",
23560Sstevel@tonic-gate 					    fnip->fi_path, fnp->fn_name);
23570Sstevel@tonic-gate 				}
23580Sstevel@tonic-gate 			}
23590Sstevel@tonic-gate 		}
23600Sstevel@tonic-gate 	} else {
23610Sstevel@tonic-gate 		vn_vfsunlock(vp);
23620Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tNode is AUTOFS\n"));
23630Sstevel@tonic-gate 		if (unmount_as_unit) {
23640Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tunmount as unit\n"));
23650Sstevel@tonic-gate 			error = unmount_autofs(vp);
23660Sstevel@tonic-gate 		} else {
23670Sstevel@tonic-gate 			AUTOFS_DPRINT((10, "\tunmount one at a time\n"));
23680Sstevel@tonic-gate 			rw_enter(&fnp->fn_rwlock, RW_READER);
23690Sstevel@tonic-gate 			if (fnp->fn_dirents != NULL) {
23700Sstevel@tonic-gate 				/*
23710Sstevel@tonic-gate 				 * Has subdirectory, attempt their
23720Sstevel@tonic-gate 				 * unmount first
23730Sstevel@tonic-gate 				 */
23740Sstevel@tonic-gate 				nfnp = fnp->fn_dirents;
23750Sstevel@tonic-gate 				VN_HOLD(fntovn(nfnp));
23760Sstevel@tonic-gate 				rw_exit(&fnp->fn_rwlock);
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 				mutex_enter(&fnp->fn_lock);
23790Sstevel@tonic-gate 				AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
23800Sstevel@tonic-gate 				mutex_exit(&fnp->fn_lock);
23810Sstevel@tonic-gate 				VN_RELE(vp);
23820Sstevel@tonic-gate 				fnp = nfnp;
23830Sstevel@tonic-gate 				goto top;
23840Sstevel@tonic-gate 			}
23850Sstevel@tonic-gate 			rw_exit(&fnp->fn_rwlock);
23860Sstevel@tonic-gate 			goto next;
23870Sstevel@tonic-gate 		}
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	if (error) {
23910Sstevel@tonic-gate 		AUTOFS_DPRINT((10, "\tUnmount failed\n"));
23920Sstevel@tonic-gate 		if (alp != NULL) {
23930Sstevel@tonic-gate 			/*
23940Sstevel@tonic-gate 			 * Unmount failed, got to remount triggers.
23950Sstevel@tonic-gate 			 */
23960Sstevel@tonic-gate 			ASSERT((fnp->fn_flags & MF_THISUID_MATCH_RQD) == 0);
2397*2170Sevanl 			error = auto_perform_actions(fnip, fnp, alp,
2398*2170Sevanl 				CRED());
23990Sstevel@tonic-gate 			if (error) {
2400*2170Sevanl 				auto_log(fngp->fng_verbose,
2401*2170Sevanl 					fngp->fng_zoneid, CE_WARN,
2402*2170Sevanl 					"autofs: can't remount "
24030Sstevel@tonic-gate 				    "triggers fnp=%p error=%d", (void *)fnp,
24040Sstevel@tonic-gate 				    error);
24050Sstevel@tonic-gate 				error = 0;
24060Sstevel@tonic-gate 				/*
24070Sstevel@tonic-gate 				 * The action list should have been
2408*2170Sevanl 				 * free'd by auto_perform_actions
24090Sstevel@tonic-gate 				 * since an error occured
24100Sstevel@tonic-gate 				 */
24110Sstevel@tonic-gate 				alp = NULL;
2412*2170Sevanl 
24130Sstevel@tonic-gate 			}
24140Sstevel@tonic-gate 		}
24150Sstevel@tonic-gate 	} else {
24160Sstevel@tonic-gate 		/*
24170Sstevel@tonic-gate 		 * The unmount succeeded, which will cause this node to
24180Sstevel@tonic-gate 		 * be removed from its parent if its an indirect mount,
24190Sstevel@tonic-gate 		 * therefore update the parent's atime and mtime now.
24200Sstevel@tonic-gate 		 * I don't update them in auto_disconnect() because I
24210Sstevel@tonic-gate 		 * don't want atime and mtime changing every time a
24220Sstevel@tonic-gate 		 * lookup goes to the daemon and creates a new node.
24230Sstevel@tonic-gate 		 */
24240Sstevel@tonic-gate 		unmount_done = 1;
24250Sstevel@tonic-gate 		if ((fnip->fi_flags & MF_DIRECT) == 0) {
24260Sstevel@tonic-gate 			gethrestime(&now);
24270Sstevel@tonic-gate 			if (fnp->fn_parent == fngp->fng_rootfnnodep)
24280Sstevel@tonic-gate 				fnp->fn_atime = fnp->fn_mtime = now;
24290Sstevel@tonic-gate 			else
24300Sstevel@tonic-gate 				fnp->fn_parent->fn_atime =
24310Sstevel@tonic-gate 					fnp->fn_parent->fn_mtime = now;
24320Sstevel@tonic-gate 		}
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 		/*
24350Sstevel@tonic-gate 		 * Free the action list here
24360Sstevel@tonic-gate 		 */
24370Sstevel@tonic-gate 		if (alp != NULL) {
24380Sstevel@tonic-gate 			xdr_free(xdr_action_list, (char *)alp);
24390Sstevel@tonic-gate 			alp = NULL;
24400Sstevel@tonic-gate 		}
24410Sstevel@tonic-gate 	}
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	fnp->fn_ref_time = gethrestime_sec();
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate next:
24460Sstevel@tonic-gate 	/*
24470Sstevel@tonic-gate 	 * Obtain parent's readers lock before grabbing
24480Sstevel@tonic-gate 	 * reference to next sibling.
24490Sstevel@tonic-gate 	 * XXX Note that nodes in the top level list (mounted
24500Sstevel@tonic-gate 	 * in user space not by the daemon in the kernel) parent is itself,
24510Sstevel@tonic-gate 	 * therefore grabbing the lock makes no sense, but doesn't
24520Sstevel@tonic-gate 	 * hurt either.
24530Sstevel@tonic-gate 	 */
24540Sstevel@tonic-gate 	pfnp = fnp->fn_parent;
24550Sstevel@tonic-gate 	ASSERT(pfnp != NULL);
24560Sstevel@tonic-gate 	rw_enter(&pfnp->fn_rwlock, RW_READER);
24570Sstevel@tonic-gate 	if ((nfnp = fnp->fn_next) != NULL)
24580Sstevel@tonic-gate 		VN_HOLD(fntovn(nfnp));
24590Sstevel@tonic-gate 	rw_exit(&pfnp->fn_rwlock);
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 	if (ilocked_it) {
24620Sstevel@tonic-gate 		mutex_enter(&fnp->fn_lock);
24630Sstevel@tonic-gate 		if (unmount_done) {
24640Sstevel@tonic-gate 			/*
24650Sstevel@tonic-gate 			 * Other threads may be waiting for this unmount to
24660Sstevel@tonic-gate 			 * finish. We must let it know that in order to
24670Sstevel@tonic-gate 			 * proceed, it must trigger the mount itself.
24680Sstevel@tonic-gate 			 */
24690Sstevel@tonic-gate 			fnp->fn_flags &= ~MF_IK_MOUNT;
24700Sstevel@tonic-gate 			if (fnp->fn_flags & MF_WAITING)
24710Sstevel@tonic-gate 				fnp->fn_error = EAGAIN;
24720Sstevel@tonic-gate 			unmount_done = 0;
24730Sstevel@tonic-gate 		}
24740Sstevel@tonic-gate 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
24750Sstevel@tonic-gate 		mutex_exit(&fnp->fn_lock);
24760Sstevel@tonic-gate 		ilocked_it = 0;
24770Sstevel@tonic-gate 	}
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	if (nfnp != NULL) {
24800Sstevel@tonic-gate 		VN_RELE(vp);
24810Sstevel@tonic-gate 		fnp = nfnp;
24820Sstevel@tonic-gate 		/*
24830Sstevel@tonic-gate 		 * Unmount next element
24840Sstevel@tonic-gate 		 */
24850Sstevel@tonic-gate 		goto top;
24860Sstevel@tonic-gate 	}
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	/*
24890Sstevel@tonic-gate 	 * We don't want to unmount rootfnnodep, so the check is made here
24900Sstevel@tonic-gate 	 */
24910Sstevel@tonic-gate 	ASSERT(pfnp != fnp);
24920Sstevel@tonic-gate 	if (pfnp != fngp->fng_rootfnnodep) {
24930Sstevel@tonic-gate 		/*
24940Sstevel@tonic-gate 		 * Now attempt to unmount my parent
24950Sstevel@tonic-gate 		 */
24960Sstevel@tonic-gate 		VN_HOLD(fntovn(pfnp));
24970Sstevel@tonic-gate 		VN_RELE(vp);
24980Sstevel@tonic-gate 		fnp = pfnp;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		goto top;
25010Sstevel@tonic-gate 	}
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	VN_RELE(vp);
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	/*
25060Sstevel@tonic-gate 	 * At this point we've walked the entire tree and attempted to unmount
25070Sstevel@tonic-gate 	 * as much as we can one level at a time.
25080Sstevel@tonic-gate 	 */
25090Sstevel@tonic-gate done:
25100Sstevel@tonic-gate 	mutex_enter(&unmount_tree_cpr_lock);
25110Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
25120Sstevel@tonic-gate 	mutex_destroy(&unmount_tree_cpr_lock);
25130Sstevel@tonic-gate }
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate static void
25160Sstevel@tonic-gate unmount_zone_tree(struct autofs_globals *fngp)
25170Sstevel@tonic-gate {
25180Sstevel@tonic-gate 	unmount_tree(fngp, 0);
25190Sstevel@tonic-gate 	mutex_enter(&fngp->fng_unmount_threads_lock);
25200Sstevel@tonic-gate 	fngp->fng_unmount_threads--;
25210Sstevel@tonic-gate 	mutex_exit(&fngp->fng_unmount_threads_lock);
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	AUTOFS_DPRINT((5, "unmount_tree done. Thread exiting.\n"));
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	zthread_exit();
25260Sstevel@tonic-gate 	/* NOTREACHED */
25270Sstevel@tonic-gate }
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate static int autofs_unmount_thread_timer = 120;	/* in seconds */
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate void
25320Sstevel@tonic-gate auto_do_unmount(struct autofs_globals *fngp)
25330Sstevel@tonic-gate {
25340Sstevel@tonic-gate 	callb_cpr_t cprinfo;
25350Sstevel@tonic-gate 	clock_t timeleft;
25360Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &fngp->fng_unmount_threads_lock,
25390Sstevel@tonic-gate 		callb_generic_cpr, "auto_do_unmount");
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	for (;;) {	/* forever */
25420Sstevel@tonic-gate 		mutex_enter(&fngp->fng_unmount_threads_lock);
25430Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
25440Sstevel@tonic-gate newthread:
25450Sstevel@tonic-gate 		mutex_exit(&fngp->fng_unmount_threads_lock);
25460Sstevel@tonic-gate 		timeleft = zone_status_timedwait(zone, lbolt +
25470Sstevel@tonic-gate 		    autofs_unmount_thread_timer * hz, ZONE_IS_SHUTTING_DOWN);
25480Sstevel@tonic-gate 		mutex_enter(&fngp->fng_unmount_threads_lock);
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 		if (timeleft != -1) {	/* didn't time out */
25510Sstevel@tonic-gate 			ASSERT(zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN);
25520Sstevel@tonic-gate 			/*
25530Sstevel@tonic-gate 			 * zone is exiting... don't create any new threads.
25540Sstevel@tonic-gate 			 * fng_unmount_threads_lock is released implicitly by
25550Sstevel@tonic-gate 			 * the below.
25560Sstevel@tonic-gate 			 */
25570Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo,
25580Sstevel@tonic-gate 				&fngp->fng_unmount_threads_lock);
25590Sstevel@tonic-gate 			CALLB_CPR_EXIT(&cprinfo);
25600Sstevel@tonic-gate 			zthread_exit();
25610Sstevel@tonic-gate 			/* NOTREACHED */
25620Sstevel@tonic-gate 		}
25630Sstevel@tonic-gate 		if (fngp->fng_unmount_threads < autofs_unmount_threads) {
25640Sstevel@tonic-gate 			fngp->fng_unmount_threads++;
25650Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo,
25660Sstevel@tonic-gate 				&fngp->fng_unmount_threads_lock);
25670Sstevel@tonic-gate 			mutex_exit(&fngp->fng_unmount_threads_lock);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 			(void) zthread_create(NULL, 0, unmount_zone_tree, fngp,
25700Sstevel@tonic-gate 			    0, minclsyspri);
25710Sstevel@tonic-gate 		} else
25720Sstevel@tonic-gate 			goto newthread;
25730Sstevel@tonic-gate 	}
25740Sstevel@tonic-gate 	/* NOTREACHED */
25750Sstevel@tonic-gate }
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate /*
25780Sstevel@tonic-gate  * Is nobrowse specified in option string?
25790Sstevel@tonic-gate  * opts should be a null ('\0') terminated string.
25800Sstevel@tonic-gate  * Returns non-zero if nobrowse has been specified.
25810Sstevel@tonic-gate  */
25820Sstevel@tonic-gate int
25830Sstevel@tonic-gate auto_nobrowse_option(char *opts)
25840Sstevel@tonic-gate {
25850Sstevel@tonic-gate 	char *buf;
25860Sstevel@tonic-gate 	char *p;
25870Sstevel@tonic-gate 	char *t;
25880Sstevel@tonic-gate 	int nobrowse = 0;
25890Sstevel@tonic-gate 	int last_opt = 0;
25900Sstevel@tonic-gate 	size_t len;
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	len = strlen(opts) + 1;
25930Sstevel@tonic-gate 	p = buf = kmem_alloc(len, KM_SLEEP);
25940Sstevel@tonic-gate 	(void) strcpy(buf, opts);
25950Sstevel@tonic-gate 	do {
25960Sstevel@tonic-gate 		if (t = strchr(p, ','))
25970Sstevel@tonic-gate 			*t++ = '\0';
25980Sstevel@tonic-gate 		else
25990Sstevel@tonic-gate 			last_opt++;
26000Sstevel@tonic-gate 		if (strcmp(p, MNTOPT_NOBROWSE) == 0)
26010Sstevel@tonic-gate 			nobrowse = 1;
26020Sstevel@tonic-gate 		else if (strcmp(p, MNTOPT_BROWSE) == 0)
26030Sstevel@tonic-gate 			nobrowse = 0;
26040Sstevel@tonic-gate 		p = t;
26050Sstevel@tonic-gate 	} while (!last_opt);
26060Sstevel@tonic-gate 	kmem_free(buf, len);
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	return (nobrowse);
26090Sstevel@tonic-gate }
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate /*
26120Sstevel@tonic-gate  * used to log warnings only if automountd is running
26130Sstevel@tonic-gate  * with verbose mode set
26140Sstevel@tonic-gate  */
2615*2170Sevanl 
26160Sstevel@tonic-gate void
2617*2170Sevanl auto_log(int verbose, zoneid_t zoneid, int level, const char *fmt, ...)
26180Sstevel@tonic-gate {
2619*2170Sevanl 	va_list	args;
26200Sstevel@tonic-gate 
2621*2170Sevanl 	if (verbose) {
26220Sstevel@tonic-gate 		va_start(args, fmt);
2623*2170Sevanl 		vzcmn_err(zoneid, level, fmt, args);
26240Sstevel@tonic-gate 		va_end(args);
26250Sstevel@tonic-gate 	}
26260Sstevel@tonic-gate }
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate #ifdef DEBUG
26290Sstevel@tonic-gate static int autofs_debug = 0;
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate /*
26320Sstevel@tonic-gate  * Utilities used by both client and server
26330Sstevel@tonic-gate  * Standard levels:
26340Sstevel@tonic-gate  * 0) no debugging
26350Sstevel@tonic-gate  * 1) hard failures
26360Sstevel@tonic-gate  * 2) soft failures
26370Sstevel@tonic-gate  * 3) current test software
26380Sstevel@tonic-gate  * 4) main procedure entry points
26390Sstevel@tonic-gate  * 5) main procedure exit points
26400Sstevel@tonic-gate  * 6) utility procedure entry points
26410Sstevel@tonic-gate  * 7) utility procedure exit points
26420Sstevel@tonic-gate  * 8) obscure procedure entry points
26430Sstevel@tonic-gate  * 9) obscure procedure exit points
26440Sstevel@tonic-gate  * 10) random stuff
26450Sstevel@tonic-gate  * 11) all <= 1
26460Sstevel@tonic-gate  * 12) all <= 2
26470Sstevel@tonic-gate  * 13) all <= 3
26480Sstevel@tonic-gate  * ...
26490Sstevel@tonic-gate  */
26500Sstevel@tonic-gate /* PRINTFLIKE2 */
26510Sstevel@tonic-gate void
26520Sstevel@tonic-gate auto_dprint(int level, const char *fmt, ...)
26530Sstevel@tonic-gate {
26540Sstevel@tonic-gate 	va_list args;
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	if (autofs_debug == level ||
26570Sstevel@tonic-gate 	    (autofs_debug > 10 && (autofs_debug - 10) >= level)) {
26580Sstevel@tonic-gate 		va_start(args, fmt);
26590Sstevel@tonic-gate 		(void) vprintf(fmt, args);
26600Sstevel@tonic-gate 		va_end(args);
26610Sstevel@tonic-gate 	}
26620Sstevel@tonic-gate }
26630Sstevel@tonic-gate #endif /* DEBUG */
2664