xref: /titanic_52/usr/src/uts/common/fs/autofs/auto_subr.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/param.h>
30 #include <sys/kmem.h>
31 #include <sys/errno.h>
32 #include <sys/proc.h>
33 #include <sys/disp.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/pathname.h>
37 #include <sys/cred.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include <sys/debug.h>
41 #include <sys/systm.h>
42 #include <sys/dirent.h>
43 #include <fs/fs_subr.h>
44 #include <sys/fs/autofs.h>
45 #include <sys/callb.h>
46 #include <sys/sysmacros.h>
47 #include <sys/zone.h>
48 #include <sys/door.h>
49 #include <sys/fs/mntdata.h>
50 #include <nfs/mount.h>
51 #include <rpc/clnt.h>
52 #include <rpcsvc/autofs_prot.h>
53 #include <nfs/rnode.h>
54 #include <sys/utsname.h>
55 #include <sys/schedctl.h>
56 
57 /*
58  * Autofs and Zones:
59  *
60  * Zones are delegated the responsibility of managing their own autofs mounts
61  * and maps.  Each zone runs its own copy of automountd, with its own timeouts,
62  * and other logically "global" parameters.  kRPC and virtualization in the
63  * loopback transport (tl) will prevent a zone from communicating with another
64  * zone's automountd.
65  *
66  * Each zone has its own "rootfnnode" and associated tree of auto nodes.
67  *
68  * Each zone also has its own set of "unmounter" kernel threads; these are
69  * created and run within the zone's context (ie, they are created via
70  * zthread_create()).
71  *
72  * Cross-zone mount triggers are disallowed.  There is a check in
73  * auto_trigger_mount() to this effect; EPERM is returned to indicate that the
74  * mount is not owned by the caller.
75  *
76  * autofssys() enables a caller in the global zone to clean up in-kernel (as
77  * well as regular) autofs mounts via the unmount_tree() mechanism.  This is
78  * routinely done when all mounts are removed as part of zone shutdown.
79  */
80 #define	TYPICALMAXPATHLEN	64
81 
82 static kmutex_t autofs_nodeid_lock;
83 
84 static int auto_perform_link(fnnode_t *, struct linka *, cred_t *);
85 static int auto_perform_actions(fninfo_t *, fnnode_t *,
86     action_list *, cred_t *);
87 static int auto_getmntpnt(vnode_t *, char *, vnode_t **, cred_t *);
88 static int auto_lookup_request(fninfo_t *, char *, struct linka *,
89     bool_t, bool_t *, cred_t *);
90 static int auto_mount_request(fninfo_t *, char *, action_list **, cred_t *,
91     bool_t);
92 
93 /*
94  * Clears the MF_INPROG flag, and wakes up those threads sleeping on
95  * fn_cv_mount if MF_WAITING is set.
96  */
97 void
98 auto_unblock_others(
99 	fnnode_t *fnp,
100 	uint_t operation)		/* either MF_INPROG or MF_LOOKUP */
101 {
102 	ASSERT(operation & (MF_INPROG | MF_LOOKUP));
103 	fnp->fn_flags &= ~operation;
104 	if (fnp->fn_flags & MF_WAITING) {
105 		fnp->fn_flags &= ~MF_WAITING;
106 		cv_broadcast(&fnp->fn_cv_mount);
107 	}
108 }
109 
110 int
111 auto_wait4mount(fnnode_t *fnp)
112 {
113 	int error;
114 	k_sigset_t smask;
115 
116 	AUTOFS_DPRINT((4, "auto_wait4mount: fnp=%p\n", (void *)fnp));
117 
118 	mutex_enter(&fnp->fn_lock);
119 	while (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
120 		/*
121 		 * There is a mount or a lookup in progress.
122 		 */
123 		fnp->fn_flags |= MF_WAITING;
124 		sigintr(&smask, 1);
125 		if (!cv_wait_sig(&fnp->fn_cv_mount, &fnp->fn_lock)) {
126 			/*
127 			 * Decided not to wait for operation to
128 			 * finish after all.
129 			 */
130 			sigunintr(&smask);
131 			mutex_exit(&fnp->fn_lock);
132 			return (EINTR);
133 		}
134 		sigunintr(&smask);
135 	}
136 	error = fnp->fn_error;
137 
138 	if (error == EINTR) {
139 		/*
140 		 * The thread doing the mount got interrupted, we need to
141 		 * try again, by returning EAGAIN.
142 		 */
143 		error = EAGAIN;
144 	}
145 	mutex_exit(&fnp->fn_lock);
146 
147 	AUTOFS_DPRINT((5, "auto_wait4mount: fnp=%p error=%d\n", (void *)fnp,
148 	    error));
149 	return (error);
150 }
151 
152 int
153 auto_lookup_aux(fnnode_t *fnp, char *name, cred_t *cred)
154 {
155 	struct fninfo *fnip;
156 	struct linka link;
157 	bool_t mountreq = FALSE;
158 	int error = 0;
159 
160 	fnip = vfstofni(fntovn(fnp)->v_vfsp);
161 	bzero(&link, sizeof (link));
162 	error = auto_lookup_request(fnip, name, &link, TRUE, &mountreq, cred);
163 	if (!error) {
164 		if (link.link != NULL || link.link != '\0') {
165 			/*
166 			 * This node should be a symlink
167 			 */
168 			error = auto_perform_link(fnp, &link, cred);
169 		} else if (mountreq) {
170 			/*
171 			 * The automount daemon is requesting a mount,
172 			 * implying this entry must be a wildcard match and
173 			 * therefore in need of verification that the entry
174 			 * exists on the server.
175 			 */
176 			mutex_enter(&fnp->fn_lock);
177 			AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
178 			fnp->fn_error = 0;
179 
180 			/*
181 			 * Unblock other lookup requests on this node,
182 			 * this is needed to let the lookup generated by
183 			 * the mount call to complete. The caveat is
184 			 * other lookups on this node can also get by,
185 			 * i.e., another lookup on this node that occurs
186 			 * while this lookup is attempting the mount
187 			 * would return a positive result no matter what.
188 			 * Therefore two lookups on the this node could
189 			 * potentially get disparate results.
190 			 */
191 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
192 			mutex_exit(&fnp->fn_lock);
193 			/*
194 			 * auto_new_mount_thread fires up a new thread which
195 			 * calls automountd finishing up the work
196 			 */
197 			auto_new_mount_thread(fnp, name, cred);
198 
199 			/*
200 			 * At this point, we are simply another thread
201 			 * waiting for the mount to complete
202 			 */
203 			error = auto_wait4mount(fnp);
204 			if (error == AUTOFS_SHUTDOWN)
205 				error = ENOENT;
206 		}
207 	}
208 
209 	if (link.link)
210 		kmem_free(link.link, strlen(link.link) + 1);
211 	if (link.dir)
212 		kmem_free(link.dir, strlen(link.dir) + 1);
213 	mutex_enter(&fnp->fn_lock);
214 	fnp->fn_error = error;
215 
216 	/*
217 	 * Notify threads waiting for lookup/mount that
218 	 * it's done.
219 	 */
220 	if (mountreq) {
221 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
222 	} else {
223 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP);
224 	}
225 	mutex_exit(&fnp->fn_lock);
226 	return (error);
227 }
228 
229 /*
230  * Starting point for thread to handle mount requests with automountd.
231  * XXX auto_mount_thread() is not suspend-safe within the scope of
232  * the present model defined for cpr to suspend the system. Calls
233  * made by the auto_mount_thread() that have been identified to be unsafe
234  * are (1) RPC client handle setup and client calls to automountd which
235  * can block deep down in the RPC library, (2) kmem_alloc() calls with the
236  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*(), and
237  * lookuppnvp() calls which can result in over the wire calls to servers.
238  * The thread should be completely reevaluated to make it suspend-safe in
239  * case of future updates to the cpr model.
240  */
241 static void
242 auto_mount_thread(struct autofs_callargs *argsp)
243 {
244 	struct fninfo 		*fnip;
245 	fnnode_t 		*fnp;
246 	vnode_t 		*vp;
247 	char 			*name;
248 	size_t 			namelen;
249 	cred_t 			*cred;
250 	action_list		*alp = NULL;
251 	int 			error;
252 	callb_cpr_t 		cprinfo;
253 	kmutex_t 		auto_mount_thread_cpr_lock;
254 
255 	mutex_init(&auto_mount_thread_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
256 	CALLB_CPR_INIT(&cprinfo, &auto_mount_thread_cpr_lock,
257 	    callb_generic_cpr, "auto_mount_thread");
258 
259 	fnp = argsp->fnc_fnp;
260 	vp = fntovn(fnp);
261 	fnip = vfstofni(vp->v_vfsp);
262 	name = argsp->fnc_name;
263 	cred = argsp->fnc_cred;
264 	ASSERT(crgetzoneid(argsp->fnc_cred) == fnip->fi_zoneid);
265 
266 	error = auto_mount_request(fnip, name, &alp, cred, TRUE);
267 	if (!error)
268 		error = auto_perform_actions(fnip, fnp, alp, cred);
269 	mutex_enter(&fnp->fn_lock);
270 	fnp->fn_error = error;
271 
272 	/*
273 	 * Notify threads waiting for mount that
274 	 * it's done.
275 	 */
276 	AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
277 	mutex_exit(&fnp->fn_lock);
278 
279 	VN_RELE(vp);
280 	crfree(argsp->fnc_cred);
281 	namelen = strlen(argsp->fnc_name) + 1;
282 	kmem_free(argsp->fnc_name, namelen);
283 	kmem_free(argsp, sizeof (*argsp));
284 
285 	mutex_enter(&auto_mount_thread_cpr_lock);
286 	CALLB_CPR_EXIT(&cprinfo);
287 	mutex_destroy(&auto_mount_thread_cpr_lock);
288 	zthread_exit();
289 	/* NOTREACHED */
290 }
291 
292 static int autofs_thr_success = 0;
293 
294 /*
295  * Creates new thread which calls auto_mount_thread which does
296  * the bulk of the work calling automountd, via 'auto_perform_actions'.
297  */
298 void
299 auto_new_mount_thread(fnnode_t *fnp, char *name, cred_t *cred)
300 {
301 	struct autofs_callargs *argsp;
302 
303 	argsp = kmem_alloc(sizeof (*argsp), KM_SLEEP);
304 	VN_HOLD(fntovn(fnp));
305 	argsp->fnc_fnp = fnp;
306 	argsp->fnc_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
307 	(void) strcpy(argsp->fnc_name, name);
308 	argsp->fnc_origin = curthread;
309 	crhold(cred);
310 	argsp->fnc_cred = cred;
311 
312 	(void) zthread_create(NULL, 0, auto_mount_thread, argsp, 0,
313 	    minclsyspri);
314 	autofs_thr_success++;
315 }
316 
317 #define	DOOR_BUF_ALIGN		(1024*1024)
318 #define	DOOR_BUF_MULTIPLIER	3
319 #define	DOOR_BUF_DEFAULT_SZ	(DOOR_BUF_MULTIPLIER * DOOR_BUF_ALIGN)
320 int	doorbuf_defsz = DOOR_BUF_DEFAULT_SZ;
321 
322 /*ARGSUSED*/
323 int
324 auto_calldaemon(
325 	zoneid_t 		zoneid,
326 	int			which,
327 	xdrproc_t		xarg_func,
328 	void 			*argsp,
329 	xdrproc_t		xresp_func,
330 	void 			*resp,
331 	int			reslen,
332 	bool_t 			hard)	/* retry forever? */
333 {
334 	int			 retry;
335 	int			 error = 0;
336 	k_sigset_t		 smask;
337 	door_arg_t		 door_args;
338 	door_handle_t		 dh;
339 	XDR			 xdrarg;
340 	XDR			 xdrres;
341 	struct autofs_globals 	*fngp = NULL;
342 	void			*orp = NULL;
343 	int			 orl;
344 	int			 rlen = 0;	/* MUST be initialized */
345 	autofs_door_args_t	*xdr_argsp;
346 	int			 xdr_len = 0;
347 	int			 printed_not_running_msg = 0;
348 	klwp_t			*lwp = ttolwp(curthread);
349 
350 	/*
351 	 * We know that the current thread is doing work on
352 	 * behalf of its own zone, so it's ok to use
353 	 * curproc->p_zone.
354 	 */
355 	ASSERT(zoneid == getzoneid());
356 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) {
357 		/*
358 		 * There's no point in trying to talk to
359 		 * automountd.  Plus, zone_shutdown() is
360 		 * waiting for us.
361 		 */
362 		return (ECONNREFUSED);
363 	}
364 
365 	do {
366 		retry = 0;
367 		mutex_enter(&autofs_minor_lock);
368 		fngp = zone_getspecific(autofs_key, curproc->p_zone);
369 		mutex_exit(&autofs_minor_lock);
370 		if (fngp == NULL) {
371 			if (hard) {
372 				AUTOFS_DPRINT((5,
373 				    "auto_calldaemon: "\
374 				    "failed to get door handle\n"));
375 				if (!printed_not_running_msg) {
376 					printed_not_running_msg = 1;
377 					zprintf(zoneid, "automountd not "\
378 					    "running, retrying\n");
379 				}
380 				delay(hz);
381 				retry = 1;
382 			} else {
383 				/*
384 				 * There is no global data so no door.
385 				 * There's no point in attempting to talk
386 				 * to automountd if we can't get the door
387 				 * handle.
388 				 */
389 				return (ECONNREFUSED);
390 			}
391 		}
392 	} while (retry);
393 
394 	if (printed_not_running_msg) {
395 		fngp->fng_printed_not_running_msg = printed_not_running_msg;
396 	}
397 
398 	ASSERT(fngp != NULL);
399 
400 	if (argsp != NULL && (xdr_len = xdr_sizeof(xarg_func, argsp)) == 0)
401 		return (EINVAL);
402 	xdr_argsp = kmem_zalloc(xdr_len + sizeof (*xdr_argsp), KM_SLEEP);
403 	xdr_argsp->xdr_len = xdr_len;
404 	xdr_argsp->cmd = which;
405 
406 	if (argsp) {
407 		xdrmem_create(&xdrarg, (char *)&xdr_argsp->xdr_arg,
408 		    xdr_argsp->xdr_len, XDR_ENCODE);
409 
410 		if (!(*xarg_func)(&xdrarg, argsp)) {
411 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
412 			return (EINVAL);
413 		}
414 	}
415 
416 	/*
417 	 * We're saving off the original pointer and length due to the
418 	 * possibility that the results buffer returned by the door
419 	 * upcall can be different then what we passed in. This is because
420 	 * the door will allocate new memory if the results buffer passed
421 	 * in isn't large enough to hold what we need to send back.
422 	 * In this case we need to free the memory originally allocated
423 	 * for that buffer.
424 	 */
425 	if (resp)
426 		rlen = xdr_sizeof(xresp_func, resp);
427 	orl = (rlen == 0) ? doorbuf_defsz : MAX(rlen, doorbuf_defsz);
428 	orp = kmem_zalloc(orl, KM_SLEEP);
429 
430 	do {
431 		retry = 0;
432 		mutex_enter(&fngp->fng_autofs_daemon_lock);
433 		dh = fngp->fng_autofs_daemon_dh;
434 		if (dh)
435 			door_ki_hold(dh);
436 		mutex_exit(&fngp->fng_autofs_daemon_lock);
437 
438 		if (dh == NULL) {
439 			if (orp)
440 				kmem_free(orp, orl);
441 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
442 			return (ENOENT);
443 		}
444 		door_args.data_ptr = (char *)xdr_argsp;
445 		door_args.data_size = sizeof (*xdr_argsp) + xdr_argsp->xdr_len;
446 		door_args.desc_ptr = NULL;
447 		door_args.desc_num = 0;
448 		door_args.rbuf = orp ? (char *)orp : NULL;
449 		door_args.rsize = orl;
450 
451 		sigintr(&smask, 1);
452 		error = door_ki_upcall(dh, &door_args);
453 		sigunintr(&smask);
454 
455 		door_ki_rele(dh);
456 
457 		/*
458 		 * Handle daemon errors
459 		 */
460 		if (!error) {
461 			/*
462 			 * Upcall successful. Let's check for soft errors
463 			 * from the daemon. We only recover from overflow
464 			 * type scenarios. Any other errors, we return to
465 			 * the caller.
466 			 */
467 			autofs_door_res_t *adr =
468 			    (autofs_door_res_t *)door_args.rbuf;
469 
470 			if (door_args.rbuf != NULL) {
471 				int	 nl;
472 
473 				switch (error = adr->res_status) {
474 				case 0:	/* no error; continue */
475 					break;
476 
477 				case EOVERFLOW:
478 					/*
479 					 * orig landing buf not big enough.
480 					 * xdr_len in XDR_BYTES_PER_UNIT
481 					 */
482 					if ((nl = adr->xdr_len) > 0 &&
483 					    (btopr(nl) < freemem/64)) {
484 						if (orp)
485 							kmem_free(orp, orl);
486 						orp = kmem_zalloc(nl, KM_SLEEP);
487 						orl = nl;
488 						retry = 1;
489 						break;
490 					}
491 					/*FALLTHROUGH*/
492 
493 				default:
494 					kmem_free(xdr_argsp,
495 					    xdr_len + sizeof (*xdr_argsp));
496 					if (orp)
497 						kmem_free(orp, orl);
498 					return (error);
499 				}
500 			}
501 			continue;
502 		}
503 
504 		/*
505 		 * no daemon errors; now process door/comm errors (if any)
506 		 */
507 		switch (error) {
508 		case EINTR:
509 			/*
510 			 * interrupts should be handled properly by the
511 			 * door upcall. If the door doesn't handle the
512 			 * interupt completely then we need to bail out.
513 			 */
514 			if (lwp && (ISSIG(curthread,
515 			    JUSTLOOKING) || MUSTRETURN(curproc, curthread))) {
516 				if (ISSIG(curthread, FORREAL) ||
517 				    lwp->lwp_sysabort ||
518 				    MUSTRETURN(curproc, curthread)) {
519 					lwp->lwp_sysabort = 0;
520 					return (EINTR);
521 				}
522 			}
523 			/*
524 			 * We may have gotten EINTR for other reasons
525 			 * like the door being revoked on us. Instead
526 			 * of trying to extract this out of the door
527 			 * handle, sleep and try again, if still
528 			 * revoked we will get EBADF next time
529 			 * through.
530 			 *
531 			 * If we have a pending cancellation and we don't
532 			 * have cancellation disabled, we will get EINTR
533 			 * forever, no matter how many times we retry,
534 			 * so just get out now if this is the case.
535 			 */
536 			if (schedctl_cancel_pending())
537 				break;
538 			/* FALLTHROUGH */
539 		case EAGAIN:    /* process may be forking */
540 			/*
541 			 * Back off for a bit
542 			 */
543 			delay(hz);
544 			retry = 1;
545 			break;
546 		case EBADF:	/* Invalid door */
547 		case EINVAL:    /* Not a door, wrong target */
548 			/*
549 			 * A fatal door error, if our failing door
550 			 * handle is the current door handle, clean
551 			 * up our state.
552 			 */
553 			mutex_enter(&fngp->fng_autofs_daemon_lock);
554 			if (dh == fngp->fng_autofs_daemon_dh) {
555 				door_ki_rele(fngp->fng_autofs_daemon_dh);
556 				fngp->fng_autofs_daemon_dh = NULL;
557 			}
558 			mutex_exit(&fngp->fng_autofs_daemon_lock);
559 			AUTOFS_DPRINT((5, "auto_calldaemon error=%d\n", error));
560 			if (hard) {
561 				if (!fngp->fng_printed_not_running_msg) {
562 					fngp->fng_printed_not_running_msg = 1;
563 					zprintf(zoneid, "automountd not "
564 					    "running, retrying\n");
565 				}
566 				delay(hz);
567 				retry = 1;
568 				break;
569 			} else {
570 				error = ECONNREFUSED;
571 				kmem_free(xdr_argsp,
572 				    xdr_len + sizeof (*xdr_argsp));
573 				if (orp)
574 					kmem_free(orp, orl);
575 				return (error);
576 			}
577 		default:	/* Unknown must be fatal */
578 			error = ENOENT;
579 			kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
580 			if (orp)
581 				kmem_free(orp, orl);
582 			return (error);
583 		}
584 	} while (retry);
585 
586 	if (fngp->fng_printed_not_running_msg == 1) {
587 		fngp->fng_printed_not_running_msg = 0;
588 		zprintf(zoneid, "automountd OK\n");
589 	}
590 
591 	if (orp && orl) {
592 		autofs_door_res_t	*door_resp;
593 		door_resp = (autofs_door_res_t *)door_args.rbuf;
594 
595 		if ((void *)door_args.rbuf != orp)
596 			kmem_free(orp, orl);
597 
598 		xdrmem_create(&xdrres, (char *)&door_resp->xdr_res,
599 		    door_resp->xdr_len, XDR_DECODE);
600 
601 		if (!((*xresp_func)(&xdrres, resp)))
602 			error = EINVAL;
603 		kmem_free(door_args.rbuf, door_args.rsize);
604 	}
605 	kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp));
606 	return (error);
607 }
608 
609 static int
610 auto_null_request(fninfo_t *fnip, bool_t hard)
611 {
612 	int error;
613 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
614 
615 	AUTOFS_DPRINT((4, "\tauto_null_request\n"));
616 
617 	error = auto_calldaemon(fngp->fng_zoneid, NULLPROC,
618 	    xdr_void, NULL, xdr_void, NULL, 0, hard);
619 
620 	AUTOFS_DPRINT((5, "\tauto_null_request: error=%d\n", error));
621 	return (error);
622 }
623 
624 static int
625 auto_lookup_request(
626 	fninfo_t *fnip,
627 	char *key,
628 	struct linka *lnp,
629 	bool_t hard,
630 	bool_t *mountreq,
631 	cred_t *cred)
632 {
633 	int 				error;
634 	struct autofs_globals 		*fngp;
635 	struct autofs_lookupargs	 reqst;
636 	autofs_lookupres 		*resp;
637 	struct linka 			*p;
638 
639 
640 	AUTOFS_DPRINT((4, "auto_lookup_equest: path=%s name=%s\n",
641 	    fnip->fi_path, key));
642 
643 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
644 
645 	reqst.map = fnip->fi_map;
646 	reqst.path = fnip->fi_path;
647 
648 	if (fnip->fi_flags & MF_DIRECT)
649 		reqst.name = fnip->fi_key;
650 	else
651 		reqst.name = key;
652 	AUTOFS_DPRINT((4, "auto_lookup_request: using key=%s\n", reqst.name));
653 
654 	reqst.subdir = fnip->fi_subdir;
655 	reqst.opts = fnip->fi_opts;
656 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
657 	reqst.uid = crgetuid(cred);
658 
659 	resp = kmem_zalloc(sizeof (*resp), KM_SLEEP);
660 
661 	error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_LOOKUP,
662 	    xdr_autofs_lookupargs, &reqst, xdr_autofs_lookupres,
663 	    (void *)resp, sizeof (autofs_lookupres), hard);
664 
665 	if (error) {
666 		xdr_free(xdr_autofs_lookupres, (char *)resp);
667 		kmem_free(resp, sizeof (*resp));
668 		return (error);
669 	}
670 
671 	if (!error) {
672 		fngp->fng_verbose = resp->lu_verbose;
673 		switch (resp->lu_res) {
674 		case AUTOFS_OK:
675 			switch (resp->lu_type.action) {
676 			case AUTOFS_MOUNT_RQ:
677 				lnp->link = NULL;
678 				lnp->dir = NULL;
679 				*mountreq = TRUE;
680 				break;
681 
682 			case AUTOFS_LINK_RQ:
683 			p = &resp->lu_type.lookup_result_type_u.lt_linka;
684 				lnp->dir = kmem_alloc(strlen(p->dir) + 1,
685 				    KM_SLEEP);
686 				(void) strcpy(lnp->dir, p->dir);
687 				lnp->link = kmem_alloc(strlen(p->link) + 1,
688 				    KM_SLEEP);
689 				(void) strcpy(lnp->link, p->link);
690 				break;
691 
692 			case AUTOFS_NONE:
693 				lnp->link = NULL;
694 				lnp->dir = NULL;
695 				break;
696 
697 			default:
698 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
699 				    CE_WARN, "auto_lookup_request: bad action "
700 				    "type %d", resp->lu_res);
701 				error = ENOENT;
702 			}
703 			break;
704 
705 		case AUTOFS_NOENT:
706 			error = ENOENT;
707 			break;
708 
709 		default:
710 			error = ENOENT;
711 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
712 			    "auto_lookup_request: unknown result: %d",
713 			    resp->lu_res);
714 			break;
715 		}
716 	}
717 done:
718 	xdr_free(xdr_autofs_lookupres, (char *)resp);
719 	kmem_free(resp, sizeof (*resp));
720 	AUTOFS_DPRINT((5, "auto_lookup_request: path=%s name=%s error=%d\n",
721 	    fnip->fi_path, key, error));
722 	return (error);
723 }
724 
725 static int
726 auto_mount_request(
727 	fninfo_t *fnip,
728 	char *key,
729 	action_list **alpp,
730 	cred_t *cred,
731 	bool_t hard)
732 {
733 	int 			error;
734 	struct autofs_globals 	*fngp;
735 	autofs_lookupargs 	reqst;
736 	autofs_mountres		*xdrres = NULL;
737 
738 	AUTOFS_DPRINT((4, "auto_mount_request: path=%s name=%s\n",
739 	    fnip->fi_path, key));
740 
741 	fngp = vntofn(fnip->fi_rootvp)->fn_globals;
742 	reqst.map = fnip->fi_map;
743 	reqst.path = fnip->fi_path;
744 
745 	if (fnip->fi_flags & MF_DIRECT)
746 		reqst.name = fnip->fi_key;
747 	else
748 		reqst.name = key;
749 
750 	AUTOFS_DPRINT((4, "auto_mount_request: using key=%s\n", reqst.name));
751 
752 	reqst.subdir = fnip->fi_subdir;
753 	reqst.opts = fnip->fi_opts;
754 	reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
755 	reqst.uid = crgetuid(cred);
756 
757 	xdrres = kmem_zalloc(sizeof (*xdrres), KM_SLEEP);
758 
759 	error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_MNTINFO,
760 	    xdr_autofs_lookupargs, &reqst, xdr_autofs_mountres,
761 	    (void *)xdrres, sizeof (autofs_mountres), hard);
762 
763 	if (!error) {
764 		fngp->fng_verbose = xdrres->mr_verbose;
765 		switch (xdrres->mr_type.status) {
766 		case AUTOFS_ACTION:
767 			error = 0;
768 			/*
769 			 * Save the action list since it is used by
770 			 * the caller. We NULL the action list pointer
771 			 * in 'result' so that xdr_free() will not free
772 			 * the list.
773 			 */
774 			*alpp = xdrres->mr_type.mount_result_type_u.list;
775 			xdrres->mr_type.mount_result_type_u.list = NULL;
776 			break;
777 		case AUTOFS_DONE:
778 			error = xdrres->mr_type.mount_result_type_u.error;
779 			break;
780 		default:
781 			error = ENOENT;
782 			auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN,
783 			    "auto_mount_request: unknown status %d",
784 			    xdrres->mr_type.status);
785 			break;
786 		}
787 	}
788 
789 	xdr_free(xdr_autofs_mountres, (char *)xdrres);
790 	kmem_free(xdrres, sizeof (*xdrres));
791 
792 
793 	AUTOFS_DPRINT((5, "auto_mount_request: path=%s name=%s error=%d\n",
794 	    fnip->fi_path, key, error));
795 	return (error);
796 }
797 
798 
799 static int
800 auto_send_unmount_request(
801 	fninfo_t *fnip,
802 	umntrequest *ul,
803 	bool_t hard)
804 {
805 	int 	error;
806 	umntres	xdrres;
807 
808 	struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals;
809 
810 	AUTOFS_DPRINT((4, "\tauto_send_unmount_request: fstype=%s "
811 	    " mntpnt=%s\n", ul->fstype, ul->mntpnt));
812 
813 	bzero(&xdrres, sizeof (umntres));
814 	error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_UNMOUNT,
815 	    xdr_umntrequest, (void *)ul, xdr_umntres, (void *)&xdrres,
816 	    sizeof (umntres), hard);
817 
818 	if (!error)
819 		error = xdrres.status;
820 
821 	AUTOFS_DPRINT((5, "\tauto_send_unmount_request: error=%d\n", error));
822 
823 	return (error);
824 }
825 
826 static int
827 auto_perform_link(fnnode_t *fnp, struct linka *linkp, cred_t *cred)
828 {
829 	vnode_t *vp;
830 	size_t len;
831 	char *tmp;
832 
833 	AUTOFS_DPRINT((3, "auto_perform_link: fnp=%p dir=%s link=%s\n",
834 	    (void *)fnp, linkp->dir, linkp->link));
835 
836 	len = strlen(linkp->link) + 1;		/* include '\0' */
837 	tmp = kmem_zalloc(len, KM_SLEEP);
838 	(void) kcopy(linkp->link, tmp, len);
839 	mutex_enter(&fnp->fn_lock);
840 	fnp->fn_symlink = tmp;
841 	fnp->fn_symlinklen = (uint_t)len;
842 	fnp->fn_flags |= MF_THISUID_MATCH_RQD;
843 	crhold(cred);
844 	fnp->fn_cred = cred;
845 	mutex_exit(&fnp->fn_lock);
846 
847 	vp = fntovn(fnp);
848 	vp->v_type = VLNK;
849 
850 	return (0);
851 }
852 
853 static void
854 auto_free_autofs_args(struct mounta *m)
855 {
856 	autofs_args	*aargs = (autofs_args *)m->dataptr;
857 
858 	if (aargs->addr.buf)
859 		kmem_free(aargs->addr.buf, aargs->addr.len);
860 	if (aargs->path)
861 		kmem_free(aargs->path, strlen(aargs->path) + 1);
862 	if (aargs->opts)
863 		kmem_free(aargs->opts, strlen(aargs->opts) + 1);
864 	if (aargs->map)
865 		kmem_free(aargs->map, strlen(aargs->map) + 1);
866 	if (aargs->subdir)
867 		kmem_free(aargs->subdir, strlen(aargs->subdir) + 1);
868 	if (aargs->key)
869 		kmem_free(aargs->key, strlen(aargs->key) + 1);
870 	kmem_free(aargs, sizeof (*aargs));
871 }
872 
873 static void
874 auto_free_action_list(action_list *alp)
875 {
876 	struct	mounta	*m;
877 	action_list	*lastalp;
878 	char		*fstype;
879 
880 	m = &alp->action.action_list_entry_u.mounta;
881 	while (alp != NULL) {
882 		fstype = alp->action.action_list_entry_u.mounta.fstype;
883 		m = &alp->action.action_list_entry_u.mounta;
884 		if (m->dataptr) {
885 			if (strcmp(fstype, "autofs") == 0) {
886 				auto_free_autofs_args(m);
887 			}
888 		}
889 		if (m->spec)
890 			kmem_free(m->spec, strlen(m->spec) + 1);
891 		if (m->dir)
892 			kmem_free(m->dir, strlen(m->dir) + 1);
893 		if (m->fstype)
894 			kmem_free(m->fstype, strlen(m->fstype) + 1);
895 		if (m->optptr)
896 			kmem_free(m->optptr, m->optlen);
897 		lastalp = alp;
898 		alp = alp->next;
899 		kmem_free(lastalp, sizeof (*lastalp));
900 	}
901 }
902 
903 static boolean_t
904 auto_invalid_autofs(fninfo_t *dfnip, fnnode_t *dfnp, action_list *p)
905 {
906 	struct mounta *m;
907 	struct autofs_args *argsp;
908 	vnode_t *dvp;
909 	char buff[AUTOFS_MAXPATHLEN];
910 	size_t len;
911 	struct autofs_globals *fngp;
912 
913 	fngp = dfnp->fn_globals;
914 	dvp = fntovn(dfnp);
915 
916 	m = &p->action.action_list_entry_u.mounta;
917 	/*
918 	 * Make sure we aren't geting passed NULL values or a "dir" that
919 	 * isn't "." and doesn't begin with "./".
920 	 *
921 	 * We also only want to perform autofs mounts, so make sure
922 	 * no-one is trying to trick us into doing anything else.
923 	 */
924 	if (m->spec == NULL || m->dir == NULL || m->dir[0] != '.' ||
925 	    (m->dir[1] != '/' && m->dir[1] != '\0') ||
926 	    m->fstype == NULL || strcmp(m->fstype, "autofs") != 0 ||
927 	    m->dataptr == NULL || m->datalen != sizeof (struct autofs_args) ||
928 	    m->optptr == NULL)
929 		return (B_TRUE);
930 	/*
931 	 * We also don't like ".."s in the pathname.  Symlinks are
932 	 * handled by the fact that we'll use NOFOLLOW when we do
933 	 * lookup()s.
934 	 */
935 	if (strstr(m->dir, "/../") != NULL ||
936 	    (len = strlen(m->dir)) > sizeof ("/..") - 1 &&
937 	    m->dir[len] == '.' && m->dir[len - 1] == '.' &&
938 	    m->dir[len - 2] == '/')
939 		return (B_TRUE);
940 	argsp = (struct autofs_args *)m->dataptr;
941 	/*
942 	 * We don't want NULL values here either.
943 	 */
944 	if (argsp->addr.buf == NULL || argsp->path == NULL ||
945 	    argsp->opts == NULL || argsp->map == NULL || argsp->subdir == NULL)
946 		return (B_TRUE);
947 	/*
948 	 * We know what the claimed pathname *should* look like:
949 	 *
950 	 * If the parent (dfnp) is a mount point (VROOT), then
951 	 * the path should be (dfnip->fi_path + m->dir).
952 	 *
953 	 * Else, we know we're only two levels deep, so we use
954 	 * (dfnip->fi_path + dfnp->fn_name + m->dir).
955 	 *
956 	 * Furthermore, "." only makes sense if dfnp is a
957 	 * trigger node.
958 	 *
959 	 * At this point it seems like the passed-in path is
960 	 * redundant.
961 	 */
962 	if (dvp->v_flag & VROOT) {
963 		if (m->dir[1] == '\0' && !(dfnp->fn_flags & MF_TRIGGER))
964 			return (B_TRUE);
965 		(void) snprintf(buff, sizeof (buff), "%s%s",
966 		    dfnip->fi_path, m->dir + 1);
967 	} else {
968 		(void) snprintf(buff, sizeof (buff), "%s/%s%s",
969 		    dfnip->fi_path, dfnp->fn_name, m->dir + 1);
970 	}
971 	if (strcmp(argsp->path, buff) != 0) {
972 		auto_log(fngp->fng_verbose, fngp->fng_zoneid,
973 		    CE_WARN, "autofs: expected path of '%s', "
974 		    "got '%s' instead.", buff, argsp->path);
975 		return (B_TRUE);
976 	}
977 	return (B_FALSE); /* looks OK */
978 }
979 
980 /*
981  * auto_invalid_action will validate the action_list received.  If all is good
982  * this function returns FALSE, if there is a problem it returns TRUE.
983  */
984 static boolean_t
985 auto_invalid_action(fninfo_t *dfnip, fnnode_t *dfnp, action_list *alistpp)
986 {
987 
988 	/*
989 	 * Before we go any further, this better be a mount request.
990 	 */
991 	if (alistpp->action.action != AUTOFS_MOUNT_RQ)
992 		return (B_TRUE);
993 	return (auto_invalid_autofs(dfnip, dfnp, alistpp));
994 
995 }
996 
997 static int
998 auto_perform_actions(
999 	fninfo_t *dfnip,
1000 	fnnode_t *dfnp,
1001 	action_list *alp,
1002 	cred_t *cred)	/* Credentials of the caller */
1003 {
1004 
1005 	action_list *p;
1006 	struct mounta		*m, margs;
1007 	struct autofs_args 		*argsp;
1008 	int 			error, success = 0;
1009 	vnode_t 		*mvp, *dvp, *newvp;
1010 	fnnode_t 		*newfnp, *mfnp;
1011 	int 			auto_mount = 0;
1012 	int 			save_triggers = 0;
1013 	int 			update_times = 0;
1014 	char 			*mntpnt;
1015 	char 			buff[AUTOFS_MAXPATHLEN];
1016 	timestruc_t 		now;
1017 	struct autofs_globals 	*fngp;
1018 	cred_t 			*zcred;
1019 
1020 	AUTOFS_DPRINT((4, "auto_perform_actions: alp=%p\n", (void *)alp));
1021 
1022 	fngp = dfnp->fn_globals;
1023 	dvp = fntovn(dfnp);
1024 
1025 	/*
1026 	 * As automountd running in a zone may be compromised, and this may be
1027 	 * an attack, we can't trust everything passed in by automountd, and we
1028 	 * need to do argument verification.  We'll issue a warning and drop
1029 	 * the request if it doesn't seem right.
1030 	 */
1031 
1032 	for (p = alp; p != NULL; p = p->next) {
1033 		if (auto_invalid_action(dfnip, dfnp, p)) {
1034 			/*
1035 			 * This warning should be sent to the global zone,
1036 			 * since presumably the zone administrator is the same
1037 			 * as the attacker.
1038 			 */
1039 			cmn_err(CE_WARN, "autofs: invalid action list received "
1040 			    "by automountd in zone %s.",
1041 			    curproc->p_zone->zone_name);
1042 			/*
1043 			 * This conversation is over.
1044 			 */
1045 			xdr_free(xdr_action_list, (char *)alp);
1046 			return (EINVAL);
1047 		}
1048 	}
1049 
1050 	zcred = zone_get_kcred(getzoneid());
1051 	ASSERT(zcred != NULL);
1052 
1053 	if (vn_mountedvfs(dvp) != NULL) {
1054 		/*
1055 		 * The daemon successfully mounted a filesystem
1056 		 * on the AUTOFS root node.
1057 		 */
1058 		mutex_enter(&dfnp->fn_lock);
1059 		dfnp->fn_flags |= MF_MOUNTPOINT;
1060 		ASSERT(dfnp->fn_dirents == NULL);
1061 		mutex_exit(&dfnp->fn_lock);
1062 		success++;
1063 	} else {
1064 		/*
1065 		 * Clear MF_MOUNTPOINT.
1066 		 */
1067 		mutex_enter(&dfnp->fn_lock);
1068 		if (dfnp->fn_flags & MF_MOUNTPOINT) {
1069 			AUTOFS_DPRINT((10, "autofs: clearing mountpoint "
1070 			    "flag on %s.", dfnp->fn_name));
1071 			ASSERT(dfnp->fn_dirents == NULL);
1072 			ASSERT(dfnp->fn_trigger == NULL);
1073 		}
1074 		dfnp->fn_flags &= ~MF_MOUNTPOINT;
1075 		mutex_exit(&dfnp->fn_lock);
1076 	}
1077 
1078 	for (p = alp; p != NULL; p = p->next) {
1079 
1080 		vfs_t *vfsp;	/* dummy argument */
1081 		vfs_t *mvfsp;
1082 
1083 		auto_mount = 0;
1084 
1085 		m = &p->action.action_list_entry_u.mounta;
1086 		argsp = (struct autofs_args *)m->dataptr;
1087 		ASSERT(strcmp(m->fstype, "autofs") == 0);
1088 		/*
1089 		 * use the parent directory's timeout since it's the
1090 		 * one specified/inherited by automount.
1091 		 */
1092 		argsp->mount_to = dfnip->fi_mount_to;
1093 		/*
1094 		 * The mountpoint is relative, and it is guaranteed to
1095 		 * begin with "."
1096 		 *
1097 		 */
1098 		ASSERT(m->dir[0] == '.');
1099 		if (m->dir[0] == '.' && m->dir[1] == '\0') {
1100 			/*
1101 			 * mounting on the trigger node
1102 			 */
1103 			mvp = dvp;
1104 			VN_HOLD(mvp);
1105 			goto mount;
1106 		}
1107 		/*
1108 		 * ignore "./" in front of mountpoint
1109 		 */
1110 		ASSERT(m->dir[1] == '/');
1111 		mntpnt = m->dir + 2;
1112 
1113 		AUTOFS_DPRINT((10, "\tdfnip->fi_path=%s\n", dfnip->fi_path));
1114 		AUTOFS_DPRINT((10, "\tdfnip->fi_flags=%x\n", dfnip->fi_flags));
1115 		AUTOFS_DPRINT((10, "\tmntpnt=%s\n", mntpnt));
1116 
1117 		if (dfnip->fi_flags & MF_DIRECT) {
1118 			AUTOFS_DPRINT((10, "\tDIRECT\n"));
1119 			(void) sprintf(buff, "%s/%s", dfnip->fi_path, mntpnt);
1120 		} else {
1121 			AUTOFS_DPRINT((10, "\tINDIRECT\n"));
1122 			(void) sprintf(buff, "%s/%s/%s",
1123 			    dfnip->fi_path, dfnp->fn_name, mntpnt);
1124 		}
1125 
1126 		if (vn_mountedvfs(dvp) == NULL) {
1127 			/*
1128 			 * Daemon didn't mount anything on the root
1129 			 * We have to create the mountpoint if it
1130 			 * doesn't exist already
1131 			 *
1132 			 * We use the caller's credentials in case a
1133 			 * UID-match is required
1134 			 * (MF_THISUID_MATCH_RQD).
1135 			 */
1136 			rw_enter(&dfnp->fn_rwlock, RW_WRITER);
1137 			error = auto_search(dfnp, mntpnt, &mfnp, cred);
1138 			if (error == 0) {
1139 				/*
1140 				 * AUTOFS mountpoint exists
1141 				 */
1142 				if (vn_mountedvfs(fntovn(mfnp)) != NULL) {
1143 					cmn_err(CE_PANIC,
1144 					    "auto_perform_actions:"
1145 					    " mfnp=%p covered", (void *)mfnp);
1146 				}
1147 			} else {
1148 				/*
1149 				 * Create AUTOFS mountpoint
1150 				 */
1151 				ASSERT((dfnp->fn_flags & MF_MOUNTPOINT) == 0);
1152 				error = auto_enter(dfnp, mntpnt, &mfnp, cred);
1153 				ASSERT(mfnp->fn_linkcnt == 1);
1154 				mfnp->fn_linkcnt++;
1155 			}
1156 			if (!error)
1157 				update_times = 1;
1158 			rw_exit(&dfnp->fn_rwlock);
1159 			ASSERT(error != EEXIST);
1160 			if (!error) {
1161 				/*
1162 				 * mfnp is already held.
1163 				 */
1164 				mvp = fntovn(mfnp);
1165 			} else {
1166 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1167 				    CE_WARN, "autofs: mount of %s "
1168 				    "failed - can't create"
1169 				    " mountpoint.", buff);
1170 				continue;
1171 			}
1172 		} else {
1173 			/*
1174 			 * Find mountpoint in VFS mounted here. If not
1175 			 * found, fail the submount, though the overall
1176 			 * mount has succeeded since the root is
1177 			 * mounted.
1178 			 */
1179 			if (error = auto_getmntpnt(dvp, mntpnt, &mvp, kcred)) {
1180 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1181 				    CE_WARN, "autofs: mount of %s "
1182 				    "failed - mountpoint doesn't"
1183 				    " exist.", buff);
1184 				continue;
1185 			}
1186 			if (mvp->v_type == VLNK) {
1187 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1188 				    CE_WARN, "autofs: %s symbolic "
1189 				    "link: not a valid mountpoint "
1190 				    "- mount failed", buff);
1191 				VN_RELE(mvp);
1192 				error = ENOENT;
1193 				continue;
1194 			}
1195 		}
1196 mount:
1197 		m->flags |= MS_SYSSPACE | MS_OPTIONSTR;
1198 
1199 		/*
1200 		 * Copy mounta struct here so we can substitute a
1201 		 * buffer that is large enough to hold the returned
1202 		 * option string, if that string is longer than the
1203 		 * input option string.
1204 		 * This can happen if there are default options enabled
1205 		 * that were not in the input option string.
1206 		 */
1207 		bcopy(m, &margs, sizeof (*m));
1208 		margs.optptr = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP);
1209 		margs.optlen = MAX_MNTOPT_STR;
1210 		(void) strcpy(margs.optptr, m->optptr);
1211 		margs.dir = argsp->path;
1212 
1213 		/*
1214 		 * We use the zone's kcred because we don't want the
1215 		 * zone to be able to thus do something it wouldn't
1216 		 * normally be able to.
1217 		 */
1218 		error = domount(NULL, &margs, mvp, zcred, &vfsp);
1219 		kmem_free(margs.optptr, MAX_MNTOPT_STR);
1220 		if (error != 0) {
1221 			auto_log(fngp->fng_verbose, fngp->fng_zoneid,
1222 			    CE_WARN, "autofs: domount of %s failed "
1223 			    "error=%d", buff, error);
1224 			VN_RELE(mvp);
1225 			continue;
1226 		}
1227 		VFS_RELE(vfsp);
1228 
1229 		/*
1230 		 * If mountpoint is an AUTOFS node, then I'm going to
1231 		 * flag it that the Filesystem mounted on top was
1232 		 * mounted in the kernel so that the unmount can be
1233 		 * done inside the kernel as well.
1234 		 * I don't care to flag non-AUTOFS mountpoints when an
1235 		 * AUTOFS in-kernel mount was done on top, because the
1236 		 * unmount routine already knows that such case was
1237 		 * done in the kernel.
1238 		 */
1239 		if (vfs_matchops(dvp->v_vfsp, vfs_getops(mvp->v_vfsp))) {
1240 			mfnp = vntofn(mvp);
1241 			mutex_enter(&mfnp->fn_lock);
1242 			mfnp->fn_flags |= MF_IK_MOUNT;
1243 			mutex_exit(&mfnp->fn_lock);
1244 		}
1245 
1246 		(void) vn_vfswlock_wait(mvp);
1247 		mvfsp = vn_mountedvfs(mvp);
1248 		if (mvfsp != NULL) {
1249 			vfs_lock_wait(mvfsp);
1250 			vn_vfsunlock(mvp);
1251 			error = VFS_ROOT(mvfsp, &newvp);
1252 			vfs_unlock(mvfsp);
1253 			if (error) {
1254 				/*
1255 				 * We've dropped the locks, so let's
1256 				 * get the mounted vfs again in case
1257 				 * it changed.
1258 				 */
1259 				(void) vn_vfswlock_wait(mvp);
1260 				mvfsp = vn_mountedvfs(mvp);
1261 				if (mvfsp != NULL) {
1262 					error = dounmount(mvfsp, 0, CRED());
1263 					if (error) {
1264 						cmn_err(CE_WARN,
1265 						    "autofs: could not unmount"
1266 						    " vfs=%p", (void *)mvfsp);
1267 					}
1268 				} else
1269 					vn_vfsunlock(mvp);
1270 				VN_RELE(mvp);
1271 				continue;
1272 			}
1273 		} else {
1274 			vn_vfsunlock(mvp);
1275 			VN_RELE(mvp);
1276 			continue;
1277 		}
1278 
1279 		auto_mount = vfs_matchops(dvp->v_vfsp,
1280 		    vfs_getops(newvp->v_vfsp));
1281 		newfnp = vntofn(newvp);
1282 		newfnp->fn_parent = dfnp;
1283 
1284 		/*
1285 		 * At this time we want to save the AUTOFS filesystem
1286 		 * as a trigger node. (We only do this if the mount
1287 		 * occurred on a node different from the root.
1288 		 * We look at the trigger nodes during
1289 		 * the automatic unmounting to make sure we remove them
1290 		 * as a unit and remount them as a unit if the
1291 		 * filesystem mounted at the root could not be
1292 		 * unmounted.
1293 		 */
1294 		if (auto_mount && (error == 0) && (mvp != dvp)) {
1295 			save_triggers++;
1296 			/*
1297 			 * Add AUTOFS mount to hierarchy
1298 			 */
1299 			newfnp->fn_flags |= MF_TRIGGER;
1300 			rw_enter(&newfnp->fn_rwlock, RW_WRITER);
1301 			newfnp->fn_next = dfnp->fn_trigger;
1302 			rw_exit(&newfnp->fn_rwlock);
1303 			rw_enter(&dfnp->fn_rwlock, RW_WRITER);
1304 			dfnp->fn_trigger = newfnp;
1305 			rw_exit(&dfnp->fn_rwlock);
1306 			/*
1307 			 * Don't VN_RELE(newvp) here since dfnp now
1308 			 * holds reference to it as its trigger node.
1309 			 */
1310 			AUTOFS_DPRINT((10, "\tadding trigger %s to %s\n",
1311 			    newfnp->fn_name, dfnp->fn_name));
1312 			AUTOFS_DPRINT((10, "\tfirst trigger is %s\n",
1313 			    dfnp->fn_trigger->fn_name));
1314 			if (newfnp->fn_next != NULL)
1315 				AUTOFS_DPRINT((10, "\tnext trigger is %s\n",
1316 				    newfnp->fn_next->fn_name));
1317 			else
1318 				AUTOFS_DPRINT((10, "\tno next trigger\n"));
1319 		} else
1320 			VN_RELE(newvp);
1321 
1322 		if (!error)
1323 			success++;
1324 
1325 		if (update_times) {
1326 			gethrestime(&now);
1327 			dfnp->fn_atime = dfnp->fn_mtime = now;
1328 		}
1329 
1330 		VN_RELE(mvp);
1331 	}
1332 
1333 	if (save_triggers) {
1334 		/*
1335 		 * Make sure the parent can't be freed while it has triggers.
1336 		 */
1337 		VN_HOLD(dvp);
1338 	}
1339 
1340 	crfree(zcred);
1341 
1342 done:
1343 	/*
1344 	 * Return failure if daemon didn't mount anything, and all
1345 	 * kernel mounts attempted failed.
1346 	 */
1347 	error = success ? 0 : ENOENT;
1348 
1349 	if (alp != NULL) {
1350 		if ((error == 0) && save_triggers) {
1351 			/*
1352 			 * Save action_list information, so that we can use it
1353 			 * when it comes time to remount the trigger nodes
1354 			 * The action list is freed when the directory node
1355 			 * containing the reference to it is unmounted in
1356 			 * unmount_tree().
1357 			 */
1358 			mutex_enter(&dfnp->fn_lock);
1359 			ASSERT(dfnp->fn_alp == NULL);
1360 			dfnp->fn_alp = alp;
1361 			mutex_exit(&dfnp->fn_lock);
1362 		} else {
1363 			/*
1364 			 * free the action list now,
1365 			 */
1366 			xdr_free(xdr_action_list, (char *)alp);
1367 		}
1368 	}
1369 	AUTOFS_DPRINT((5, "auto_perform_actions: error=%d\n", error));
1370 	return (error);
1371 }
1372 
1373 fnnode_t *
1374 auto_makefnnode(
1375 	vtype_t type,
1376 	vfs_t *vfsp,
1377 	char *name,
1378 	cred_t *cred,
1379 	struct autofs_globals *fngp)
1380 {
1381 	fnnode_t *fnp;
1382 	vnode_t *vp;
1383 	char *tmpname;
1384 	timestruc_t now;
1385 	/*
1386 	 * autofs uses odd inode numbers
1387 	 * automountd uses even inode numbers
1388 	 *
1389 	 * To preserve the age-old semantics that inum+devid is unique across
1390 	 * the system, this variable must be global across zones.
1391 	 */
1392 	static ino_t nodeid = 3;
1393 
1394 	fnp = kmem_zalloc(sizeof (*fnp), KM_SLEEP);
1395 	fnp->fn_vnode = vn_alloc(KM_SLEEP);
1396 
1397 	vp = fntovn(fnp);
1398 	tmpname = kmem_alloc(strlen(name) + 1, KM_SLEEP);
1399 	(void) strcpy(tmpname, name);
1400 	fnp->fn_name = &tmpname[0];
1401 	fnp->fn_namelen = (int)strlen(tmpname) + 1;	/* include '\0' */
1402 	fnp->fn_uid = crgetuid(cred);
1403 	fnp->fn_gid = crgetgid(cred);
1404 	/*
1405 	 * ".." is added in auto_enter and auto_mount.
1406 	 * "." is added in auto_mkdir and auto_mount.
1407 	 */
1408 	/*
1409 	 * Note that fn_size and fn_linkcnt are already 0 since
1410 	 * we used kmem_zalloc to allocated fnp
1411 	 */
1412 	fnp->fn_mode = AUTOFS_MODE;
1413 	gethrestime(&now);
1414 	fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now;
1415 	fnp->fn_ref_time = now.tv_sec;
1416 	mutex_enter(&autofs_nodeid_lock);
1417 	fnp->fn_nodeid = nodeid;
1418 	nodeid += 2;
1419 	fnp->fn_globals = fngp;
1420 	fngp->fng_fnnode_count++;
1421 	mutex_exit(&autofs_nodeid_lock);
1422 	vn_setops(vp, auto_vnodeops);
1423 	vp->v_type = type;
1424 	vp->v_data = (void *)fnp;
1425 	vp->v_vfsp = vfsp;
1426 	mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL);
1427 	rw_init(&fnp->fn_rwlock, NULL, RW_DEFAULT, NULL);
1428 	cv_init(&fnp->fn_cv_mount, NULL, CV_DEFAULT, NULL);
1429 	vn_exists(vp);
1430 	return (fnp);
1431 }
1432 
1433 
1434 void
1435 auto_freefnnode(fnnode_t *fnp)
1436 {
1437 	vnode_t *vp = fntovn(fnp);
1438 
1439 	AUTOFS_DPRINT((4, "auto_freefnnode: fnp=%p\n", (void *)fnp));
1440 
1441 	ASSERT(fnp->fn_linkcnt == 0);
1442 	ASSERT(vp->v_count == 0);
1443 	ASSERT(fnp->fn_dirents == NULL);
1444 	ASSERT(fnp->fn_parent == NULL);
1445 
1446 	vn_invalid(vp);
1447 	kmem_free(fnp->fn_name, fnp->fn_namelen);
1448 	if (fnp->fn_symlink) {
1449 		ASSERT(fnp->fn_flags & MF_THISUID_MATCH_RQD);
1450 		kmem_free(fnp->fn_symlink, fnp->fn_symlinklen);
1451 	}
1452 	if (fnp->fn_cred)
1453 		crfree(fnp->fn_cred);
1454 	mutex_destroy(&fnp->fn_lock);
1455 	rw_destroy(&fnp->fn_rwlock);
1456 	cv_destroy(&fnp->fn_cv_mount);
1457 	vn_free(vp);
1458 
1459 	mutex_enter(&autofs_nodeid_lock);
1460 	fnp->fn_globals->fng_fnnode_count--;
1461 	mutex_exit(&autofs_nodeid_lock);
1462 	kmem_free(fnp, sizeof (*fnp));
1463 }
1464 
1465 void
1466 auto_disconnect(
1467 	fnnode_t *dfnp,
1468 	fnnode_t *fnp)
1469 {
1470 	fnnode_t *tmp, **fnpp;
1471 	vnode_t *vp = fntovn(fnp);
1472 	timestruc_t now;
1473 
1474 	AUTOFS_DPRINT((4,
1475 	    "auto_disconnect: dfnp=%p fnp=%p linkcnt=%d\n v_count=%d",
1476 	    (void *)dfnp, (void *)fnp, fnp->fn_linkcnt, vp->v_count));
1477 
1478 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
1479 	ASSERT(fnp->fn_linkcnt == 1);
1480 
1481 	if (vn_mountedvfs(vp) != NULL) {
1482 		cmn_err(CE_PANIC, "auto_disconnect: vp %p mounted on",
1483 		    (void *)vp);
1484 	}
1485 
1486 	/*
1487 	 * Decrement by 1 because we're removing the entry in dfnp.
1488 	 */
1489 	fnp->fn_linkcnt--;
1490 	fnp->fn_size--;
1491 
1492 	/*
1493 	 * only changed while holding parent's (dfnp) rw_lock
1494 	 */
1495 	fnp->fn_parent = NULL;
1496 
1497 	fnpp = &dfnp->fn_dirents;
1498 	for (;;) {
1499 		tmp = *fnpp;
1500 		if (tmp == NULL) {
1501 			cmn_err(CE_PANIC,
1502 			    "auto_disconnect: %p not in %p dirent list",
1503 			    (void *)fnp, (void *)dfnp);
1504 		}
1505 		if (tmp == fnp) {
1506 			*fnpp = tmp->fn_next; 	/* remove it from the list */
1507 			ASSERT(vp->v_count == 0);
1508 			/* child had a pointer to parent ".." */
1509 			dfnp->fn_linkcnt--;
1510 			dfnp->fn_size--;
1511 			break;
1512 		}
1513 		fnpp = &tmp->fn_next;
1514 	}
1515 
1516 	mutex_enter(&fnp->fn_lock);
1517 	gethrestime(&now);
1518 	fnp->fn_atime = fnp->fn_mtime = now;
1519 	mutex_exit(&fnp->fn_lock);
1520 
1521 	AUTOFS_DPRINT((5, "auto_disconnect: done\n"));
1522 }
1523 
1524 int
1525 auto_enter(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
1526 {
1527 	struct fnnode *cfnp, **spp;
1528 	vnode_t *dvp = fntovn(dfnp);
1529 	ushort_t offset = 0;
1530 	ushort_t diff;
1531 
1532 	AUTOFS_DPRINT((4, "auto_enter: dfnp=%p, name=%s ", (void *)dfnp, name));
1533 
1534 	ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock));
1535 
1536 	cfnp = dfnp->fn_dirents;
1537 	if (cfnp == NULL) {
1538 		/*
1539 		 * offset = 0 for '.' and offset = 1 for '..'
1540 		 */
1541 		spp = &dfnp->fn_dirents;
1542 		offset = 2;
1543 	}
1544 
1545 	for (; cfnp; cfnp = cfnp->fn_next) {
1546 		if (strcmp(cfnp->fn_name, name) == 0) {
1547 			mutex_enter(&cfnp->fn_lock);
1548 			if (cfnp->fn_flags & MF_THISUID_MATCH_RQD) {
1549 				/*
1550 				 * "thisuser" kind of node, need to
1551 				 * match CREDs as well
1552 				 */
1553 				mutex_exit(&cfnp->fn_lock);
1554 				if (crcmp(cfnp->fn_cred, cred) == 0)
1555 					return (EEXIST);
1556 			} else {
1557 				mutex_exit(&cfnp->fn_lock);
1558 				return (EEXIST);
1559 			}
1560 		}
1561 
1562 		if (cfnp->fn_next != NULL) {
1563 			diff = (ushort_t)
1564 			    (cfnp->fn_next->fn_offset - cfnp->fn_offset);
1565 			ASSERT(diff != 0);
1566 			if (diff > 1 && offset == 0) {
1567 				offset = (ushort_t)cfnp->fn_offset + 1;
1568 				spp = &cfnp->fn_next;
1569 			}
1570 		} else if (offset == 0) {
1571 			offset = (ushort_t)cfnp->fn_offset + 1;
1572 			spp = &cfnp->fn_next;
1573 		}
1574 	}
1575 
1576 	*fnpp = auto_makefnnode(VDIR, dvp->v_vfsp, name, cred,
1577 	    dfnp->fn_globals);
1578 	if (*fnpp == NULL)
1579 		return (ENOMEM);
1580 
1581 	/*
1582 	 * I don't hold the mutex on fnpp because I created it, and
1583 	 * I'm already holding the writers lock for it's parent
1584 	 * directory, therefore nobody can reference it without me first
1585 	 * releasing the writers lock.
1586 	 */
1587 	(*fnpp)->fn_offset = offset;
1588 	(*fnpp)->fn_next = *spp;
1589 	*spp = *fnpp;
1590 	(*fnpp)->fn_parent = dfnp;
1591 	(*fnpp)->fn_linkcnt++;	/* parent now holds reference to entry */
1592 	(*fnpp)->fn_size++;
1593 
1594 	/*
1595 	 * dfnp->fn_linkcnt and dfnp->fn_size protected by dfnp->rw_lock
1596 	 */
1597 	dfnp->fn_linkcnt++;	/* child now holds reference to parent '..' */
1598 	dfnp->fn_size++;
1599 
1600 	dfnp->fn_ref_time = gethrestime_sec();
1601 
1602 	AUTOFS_DPRINT((5, "*fnpp=%p\n", (void *)*fnpp));
1603 	return (0);
1604 }
1605 
1606 int
1607 auto_search(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred)
1608 {
1609 	vnode_t *dvp;
1610 	fnnode_t *p;
1611 	int error = ENOENT, match = 0;
1612 
1613 	AUTOFS_DPRINT((4, "auto_search: dfnp=%p, name=%s...\n",
1614 	    (void *)dfnp, name));
1615 
1616 	dvp = fntovn(dfnp);
1617 	if (dvp->v_type != VDIR) {
1618 		cmn_err(CE_PANIC, "auto_search: dvp=%p not a directory",
1619 		    (void *)dvp);
1620 	}
1621 
1622 	ASSERT(RW_LOCK_HELD(&dfnp->fn_rwlock));
1623 	for (p = dfnp->fn_dirents; p != NULL; p = p->fn_next) {
1624 		if (strcmp(p->fn_name, name) == 0) {
1625 			mutex_enter(&p->fn_lock);
1626 			if (p->fn_flags & MF_THISUID_MATCH_RQD) {
1627 				/*
1628 				 * "thisuser" kind of node
1629 				 * Need to match CREDs as well
1630 				 */
1631 				mutex_exit(&p->fn_lock);
1632 				match = crcmp(p->fn_cred, cred) == 0;
1633 			} else {
1634 				/*
1635 				 * No need to check CRED
1636 				 */
1637 				mutex_exit(&p->fn_lock);
1638 				match = 1;
1639 			}
1640 		}
1641 		if (match) {
1642 			error = 0;
1643 			if (fnpp) {
1644 				*fnpp = p;
1645 				VN_HOLD(fntovn(*fnpp));
1646 			}
1647 			break;
1648 		}
1649 	}
1650 
1651 	AUTOFS_DPRINT((5, "auto_search: error=%d\n", error));
1652 	return (error);
1653 }
1654 
1655 /*
1656  * If dvp is mounted on, get path's vnode in the mounted on
1657  * filesystem.  Path is relative to dvp, ie "./path".
1658  * If successful, *mvp points to a the held mountpoint vnode.
1659  */
1660 /* ARGSUSED */
1661 static int
1662 auto_getmntpnt(
1663 	vnode_t *dvp,
1664 	char *path,
1665 	vnode_t **mvpp,		/* vnode for mountpoint */
1666 	cred_t *cred)
1667 {
1668 	int error = 0;
1669 	vnode_t *newvp;
1670 	char namebuf[TYPICALMAXPATHLEN];
1671 	struct pathname lookpn;
1672 	vfs_t *vfsp;
1673 
1674 	AUTOFS_DPRINT((4, "auto_getmntpnt: path=%s\n", path));
1675 
1676 	if (error = vn_vfsrlock_wait(dvp))
1677 		return (error);
1678 
1679 	/*
1680 	 * Now that we have the vfswlock, check to see if dvp
1681 	 * is still mounted on.  If not, then just bail out as
1682 	 * there is no need to remount the triggers since the
1683 	 * higher level mount point has gotten unmounted.
1684 	 */
1685 	vfsp = vn_mountedvfs(dvp);
1686 	if (vfsp == NULL) {
1687 		vn_vfsunlock(dvp);
1688 		error = EBUSY;
1689 		goto done;
1690 	}
1691 	/*
1692 	 * Since mounted on, lookup "path" in the new filesystem,
1693 	 * it is important that we do the filesystem jump here to
1694 	 * avoid lookuppn() calling auto_lookup on dvp and deadlock.
1695 	 */
1696 	error = VFS_ROOT(vfsp, &newvp);
1697 	vn_vfsunlock(dvp);
1698 	if (error)
1699 		goto done;
1700 
1701 	/*
1702 	 * We do a VN_HOLD on newvp just in case the first call to
1703 	 * lookuppnvp() fails with ENAMETOOLONG.  We should still have a
1704 	 * reference to this vnode for the second call to lookuppnvp().
1705 	 */
1706 	VN_HOLD(newvp);
1707 
1708 	/*
1709 	 * Now create the pathname struct so we can make use of lookuppnvp,
1710 	 * and pn_getcomponent.
1711 	 * This code is similar to lookupname() in fs/lookup.c.
1712 	 */
1713 	error = pn_get_buf(path, UIO_SYSSPACE, &lookpn,
1714 	    namebuf, sizeof (namebuf));
1715 	if (error == 0) {
1716 		error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
1717 		    mvpp, rootdir, newvp, cred);
1718 	} else
1719 		VN_RELE(newvp);
1720 	if (error == ENAMETOOLONG) {
1721 		/*
1722 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
1723 		 * newvp is VN_RELE'd by this call to lookuppnvp.
1724 		 *
1725 		 * Using 'rootdir' in a zone's context is OK here: we already
1726 		 * ascertained that there are no '..'s in the path, and we're
1727 		 * not following symlinks.
1728 		 */
1729 		if ((error = pn_get(path, UIO_SYSSPACE, &lookpn)) == 0) {
1730 			error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP,
1731 			    mvpp, rootdir, newvp, cred);
1732 			pn_free(&lookpn);
1733 		} else
1734 			VN_RELE(newvp);
1735 	} else {
1736 		/*
1737 		 * Need to release newvp here since we held it.
1738 		 */
1739 		VN_RELE(newvp);
1740 	}
1741 
1742 done:
1743 	AUTOFS_DPRINT((5, "auto_getmntpnt: path=%s *mvpp=%p error=%d\n",
1744 	    path, (void *)*mvpp, error));
1745 	return (error);
1746 }
1747 
1748 #define	DEEPER(x) (((x)->fn_dirents != NULL) || \
1749 			(vn_mountedvfs(fntovn((x)))) != NULL)
1750 
1751 /*
1752  * The caller, should have already VN_RELE'd its reference to the
1753  * root vnode of this filesystem.
1754  */
1755 static int
1756 auto_inkernel_unmount(vfs_t *vfsp)
1757 {
1758 	vnode_t *cvp = vfsp->vfs_vnodecovered;
1759 	int error;
1760 
1761 	AUTOFS_DPRINT((4,
1762 	    "auto_inkernel_unmount: devid=%lx mntpnt(%p) count %u\n",
1763 	    vfsp->vfs_dev, (void *)cvp, cvp->v_count));
1764 
1765 	ASSERT(vn_vfswlock_held(cvp));
1766 
1767 	/*
1768 	 * Perform the unmount
1769 	 * The mountpoint has already been locked by the caller.
1770 	 */
1771 	error = dounmount(vfsp, 0, kcred);
1772 
1773 	AUTOFS_DPRINT((5, "auto_inkernel_unmount: exit count %u\n",
1774 	    cvp->v_count));
1775 	return (error);
1776 }
1777 
1778 /*
1779  * unmounts trigger nodes in the kernel.
1780  */
1781 static void
1782 unmount_triggers(fnnode_t *fnp, action_list **alp)
1783 {
1784 	fnnode_t *tp, *next;
1785 	int error = 0;
1786 	vfs_t *vfsp;
1787 	vnode_t *tvp;
1788 
1789 	AUTOFS_DPRINT((4, "unmount_triggers: fnp=%p\n", (void *)fnp));
1790 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
1791 
1792 	*alp = fnp->fn_alp;
1793 	next = fnp->fn_trigger;
1794 	while ((tp = next) != NULL) {
1795 		tvp = fntovn(tp);
1796 		ASSERT(tvp->v_count >= 2);
1797 		next = tp->fn_next;
1798 		/*
1799 		 * drop writer's lock since the unmount will end up
1800 		 * disconnecting this node from fnp and needs to acquire
1801 		 * the writer's lock again.
1802 		 * next has at least a reference count >= 2 since it's
1803 		 * a trigger node, therefore can not be accidentally freed
1804 		 * by a VN_RELE
1805 		 */
1806 		rw_exit(&fnp->fn_rwlock);
1807 
1808 		vfsp = tvp->v_vfsp;
1809 
1810 		/*
1811 		 * Its parent was holding a reference to it, since this
1812 		 * is a trigger vnode.
1813 		 */
1814 		VN_RELE(tvp);
1815 		if (error = auto_inkernel_unmount(vfsp)) {
1816 			cmn_err(CE_PANIC, "unmount_triggers: "
1817 			    "unmount of vp=%p failed error=%d",
1818 			    (void *)tvp, error);
1819 		}
1820 		/*
1821 		 * reacquire writer's lock
1822 		 */
1823 		rw_enter(&fnp->fn_rwlock, RW_WRITER);
1824 	}
1825 
1826 	/*
1827 	 * We were holding a reference to our parent.  Drop that.
1828 	 */
1829 	VN_RELE(fntovn(fnp));
1830 	fnp->fn_trigger = NULL;
1831 	fnp->fn_alp = NULL;
1832 
1833 	AUTOFS_DPRINT((5, "unmount_triggers: finished\n"));
1834 }
1835 
1836 /*
1837  * This routine locks the mountpoint of every trigger node if they're
1838  * not busy, or returns EBUSY if any node is busy. If a trigger node should
1839  * be unmounted first, then it sets nfnp to point to it, otherwise nfnp
1840  * points to NULL.
1841  */
1842 static int
1843 triggers_busy(fnnode_t *fnp, fnnode_t **nfnp)
1844 {
1845 	int error = 0, done;
1846 	int lck_error = 0;
1847 	fnnode_t *tp, *t1p;
1848 	vfs_t *vfsp;
1849 
1850 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
1851 
1852 	*nfnp = NULL;
1853 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
1854 		AUTOFS_DPRINT((10, "\ttrigger: %s\n", tp->fn_name));
1855 		vfsp = fntovn(tp)->v_vfsp;
1856 		error = 0;
1857 		/*
1858 		 * The vn_vfsunlock will be done in auto_inkernel_unmount.
1859 		 */
1860 		lck_error = vn_vfswlock(vfsp->vfs_vnodecovered);
1861 		if (lck_error == 0) {
1862 			mutex_enter(&tp->fn_lock);
1863 			ASSERT((tp->fn_flags & MF_LOOKUP) == 0);
1864 			if (tp->fn_flags & MF_INPROG) {
1865 				/*
1866 				 * a mount is in progress
1867 				 */
1868 				error = EBUSY;
1869 			}
1870 			mutex_exit(&tp->fn_lock);
1871 		}
1872 		if (lck_error || error || DEEPER(tp) ||
1873 		    ((fntovn(tp))->v_count) > 2) {
1874 			/*
1875 			 * couldn't lock it because it's busy,
1876 			 * It is mounted on or has dirents?
1877 			 * If reference count is greater than two, then
1878 			 * somebody else is holding a reference to this vnode.
1879 			 * One reference is for the mountpoint, and the second
1880 			 * is for the trigger node.
1881 			 */
1882 			AUTOFS_DPRINT((10, "\ttrigger busy\n"));
1883 			if ((lck_error == 0) && (error == 0)) {
1884 				*nfnp = tp;
1885 				/*
1886 				 * The matching VN_RELE is done in
1887 				 * unmount_tree().
1888 				 */
1889 				VN_HOLD(fntovn(*nfnp));
1890 			}
1891 			/*
1892 			 * Unlock previously locked mountpoints
1893 			 */
1894 			for (done = 0, t1p = fnp->fn_trigger; !done;
1895 			    t1p = t1p->fn_next) {
1896 				/*
1897 				 * Unlock all nodes previously
1898 				 * locked. All nodes up to 'tp'
1899 				 * were successfully locked. If 'lck_err' is
1900 				 * set, then 'tp' was not locked, and thus
1901 				 * should not be unlocked. If
1902 				 * 'lck_err' is not set, then 'tp' was
1903 				 * successfully locked, and it should
1904 				 * be unlocked.
1905 				 */
1906 				if (t1p != tp || !lck_error) {
1907 					vfsp = fntovn(t1p)->v_vfsp;
1908 					vn_vfsunlock(vfsp->vfs_vnodecovered);
1909 				}
1910 				done = (t1p == tp);
1911 			}
1912 			error = EBUSY;
1913 			break;
1914 		}
1915 	}
1916 
1917 	AUTOFS_DPRINT((4, "triggers_busy: error=%d\n", error));
1918 	return (error);
1919 }
1920 
1921 /*
1922  * Unlock previously locked trigger nodes.
1923  */
1924 static int
1925 triggers_unlock(fnnode_t *fnp)
1926 {
1927 	fnnode_t *tp;
1928 	vfs_t *vfsp;
1929 
1930 	ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock));
1931 
1932 	for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) {
1933 		AUTOFS_DPRINT((10, "\tunlock trigger: %s\n", tp->fn_name));
1934 		vfsp = fntovn(tp)->v_vfsp;
1935 		vn_vfsunlock(vfsp->vfs_vnodecovered);
1936 	}
1937 
1938 	return (0);
1939 }
1940 
1941 /*
1942  * It is the caller's responsibility to grab the VVFSLOCK.
1943  * Releases the VVFSLOCK upon return.
1944  */
1945 static int
1946 unmount_node(vnode_t *cvp, int force)
1947 {
1948 	int error = 0;
1949 	fnnode_t *cfnp;
1950 	vfs_t *vfsp;
1951 	umntrequest ul;
1952 	fninfo_t *fnip;
1953 
1954 	AUTOFS_DPRINT((4, "\tunmount_node cvp=%p\n", (void *)cvp));
1955 
1956 	ASSERT(vn_vfswlock_held(cvp));
1957 	cfnp = vntofn(cvp);
1958 	vfsp = vn_mountedvfs(cvp);
1959 
1960 	if (force || cfnp->fn_flags & MF_IK_MOUNT) {
1961 		/*
1962 		 * Mount was performed in the kernel, so
1963 		 * do an in-kernel unmount. auto_inkernel_unmount()
1964 		 * will vn_vfsunlock(cvp).
1965 		 */
1966 		error = auto_inkernel_unmount(vfsp);
1967 	} else {
1968 		zone_t *zone = NULL;
1969 		refstr_t *mntpt, *resource;
1970 		size_t mntoptslen;
1971 
1972 		/*
1973 		 * Get the mnttab information of the node
1974 		 * and ask the daemon to unmount it.
1975 		 */
1976 		bzero(&ul, sizeof (ul));
1977 		mntfs_getmntopts(vfsp, &ul.mntopts, &mntoptslen);
1978 		if (ul.mntopts == NULL) {
1979 			auto_log(cfnp->fn_globals->fng_verbose,
1980 			    cfnp->fn_globals->fng_zoneid, CE_WARN,
1981 			    "unmount_node: no memory");
1982 			vn_vfsunlock(cvp);
1983 			error = ENOMEM;
1984 			goto done;
1985 		}
1986 		if (mntoptslen > AUTOFS_MAXOPTSLEN)
1987 			ul.mntopts[AUTOFS_MAXOPTSLEN - 1] = '\0';
1988 
1989 		mntpt = vfs_getmntpoint(vfsp);
1990 		ul.mntpnt = (char *)refstr_value(mntpt);
1991 		resource = vfs_getresource(vfsp);
1992 		ul.mntresource = (char *)refstr_value(resource);
1993 
1994 		fnip = vfstofni(cvp->v_vfsp);
1995 		ul.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE;
1996 
1997 		/*
1998 		 * Since a zone'd automountd's view of the autofs mount points
1999 		 * differs from those in the kernel, we need to make sure we
2000 		 * give it consistent mount points.
2001 		 */
2002 		ASSERT(fnip->fi_zoneid == getzoneid());
2003 		zone = curproc->p_zone;
2004 
2005 		if (fnip->fi_zoneid != GLOBAL_ZONEID) {
2006 			if (ZONE_PATH_VISIBLE(ul.mntpnt, zone)) {
2007 				ul.mntpnt =
2008 				    ZONE_PATH_TRANSLATE(ul.mntpnt, zone);
2009 			}
2010 			if (ZONE_PATH_VISIBLE(ul.mntresource, zone)) {
2011 				ul.mntresource =
2012 				    ZONE_PATH_TRANSLATE(ul.mntresource, zone);
2013 			}
2014 		}
2015 
2016 		ul.fstype = vfssw[vfsp->vfs_fstype].vsw_name;
2017 		vn_vfsunlock(cvp);
2018 
2019 		error = auto_send_unmount_request(fnip, &ul, FALSE);
2020 		kmem_free(ul.mntopts, mntoptslen);
2021 		refstr_rele(mntpt);
2022 		refstr_rele(resource);
2023 	}
2024 
2025 done:
2026 	AUTOFS_DPRINT((5, "\tunmount_node cvp=%p error=%d\n", (void *)cvp,
2027 	    error));
2028 	return (error);
2029 }
2030 
2031 /*
2032  * vp is the "root" of the AUTOFS filesystem.
2033  * return EBUSY if any thread is holding a reference to this vnode
2034  * other than us.
2035  */
2036 static int
2037 check_auto_node(vnode_t *vp)
2038 {
2039 	fnnode_t *fnp;
2040 	int error = 0;
2041 	/*
2042 	 * number of references to expect for
2043 	 * a non-busy vnode.
2044 	 */
2045 	uint_t count;
2046 
2047 	AUTOFS_DPRINT((4, "\tcheck_auto_node vp=%p ", (void *)vp));
2048 	fnp = vntofn(vp);
2049 	ASSERT(fnp->fn_flags & MF_INPROG);
2050 	ASSERT((fnp->fn_flags & MF_LOOKUP) == 0);
2051 
2052 	count = 1;		/* we are holding a reference to vp */
2053 	if (fnp->fn_flags & MF_TRIGGER) {
2054 		/*
2055 		 * parent holds a pointer to us (trigger)
2056 		 */
2057 		count++;
2058 	}
2059 	if (fnp->fn_trigger != NULL) {
2060 		/*
2061 		 * The trigger nodes have a hold on us.
2062 		 */
2063 		count++;
2064 	}
2065 	mutex_enter(&vp->v_lock);
2066 	if (vp->v_flag & VROOT)
2067 		count++;
2068 	ASSERT(vp->v_count > 0);
2069 	AUTOFS_DPRINT((10, "\tcount=%u ", vp->v_count));
2070 	if (vp->v_count > count)
2071 		error = EBUSY;
2072 	mutex_exit(&vp->v_lock);
2073 
2074 	AUTOFS_DPRINT((5, "\tcheck_auto_node error=%d ", error));
2075 	return (error);
2076 }
2077 
2078 /*
2079  * rootvp is the root of the AUTOFS filesystem.
2080  * If rootvp is busy (v_count > 1) returns EBUSY.
2081  * else removes every vnode under this tree.
2082  * ASSUMPTION: Assumes that the only node which can be busy is
2083  * the root vnode. This filesystem better be two levels deep only,
2084  * the root and its immediate subdirs.
2085  * The daemon will "AUTOFS direct-mount" only one level below the root.
2086  */
2087 static int
2088 unmount_autofs(vnode_t *rootvp)
2089 {
2090 	fnnode_t *fnp, *rootfnp, *nfnp;
2091 	int error;
2092 
2093 	AUTOFS_DPRINT((4, "\tunmount_autofs rootvp=%p ", (void *)rootvp));
2094 
2095 	error = check_auto_node(rootvp);
2096 	if (error == 0) {
2097 		/*
2098 		 * Remove all its immediate subdirectories.
2099 		 */
2100 		rootfnp = vntofn(rootvp);
2101 		rw_enter(&rootfnp->fn_rwlock, RW_WRITER);
2102 		nfnp = NULL;	/* lint clean */
2103 		for (fnp = rootfnp->fn_dirents; fnp != NULL; fnp = nfnp) {
2104 			ASSERT(fntovn(fnp)->v_count == 0);
2105 			ASSERT(fnp->fn_dirents == NULL);
2106 			ASSERT(fnp->fn_linkcnt == 2);
2107 			fnp->fn_linkcnt--;
2108 			auto_disconnect(rootfnp, fnp);
2109 			nfnp = fnp->fn_next;
2110 			auto_freefnnode(fnp);
2111 		}
2112 		rw_exit(&rootfnp->fn_rwlock);
2113 	}
2114 	AUTOFS_DPRINT((5, "\tunmount_autofs error=%d ", error));
2115 	return (error);
2116 }
2117 
2118 /*
2119  * max number of unmount threads running
2120  */
2121 static int autofs_unmount_threads = 5;
2122 
2123 /*
2124  * XXX unmount_tree() is not suspend-safe within the scope of
2125  * the present model defined for cpr to suspend the system. Calls made
2126  * by the unmount_tree() that have been identified to be unsafe are
2127  * (1) RPC client handle setup and client calls to automountd which can
2128  * block deep down in the RPC library, (2) kmem_alloc() calls with the
2129  * KM_SLEEP flag which can block if memory is low, and (3) VFS_*() and
2130  * VOP_*() calls which can result in over the wire calls to servers.
2131  * The thread should be completely reevaluated to make it suspend-safe in
2132  * case of future updates to the cpr model.
2133  */
2134 void
2135 unmount_tree(struct autofs_globals *fngp, int force)
2136 {
2137 	vnode_t *vp, *newvp;
2138 	vfs_t *vfsp;
2139 	fnnode_t *fnp, *nfnp, *pfnp;
2140 	action_list *alp;
2141 	int error, ilocked_it = 0;
2142 	fninfo_t *fnip;
2143 	time_t ref_time;
2144 	int autofs_busy_root, unmount_as_unit, unmount_done = 0;
2145 	timestruc_t now;
2146 
2147 	callb_cpr_t cprinfo;
2148 	kmutex_t unmount_tree_cpr_lock;
2149 
2150 	mutex_init(&unmount_tree_cpr_lock, NULL, MUTEX_DEFAULT, NULL);
2151 	CALLB_CPR_INIT(&cprinfo, &unmount_tree_cpr_lock, callb_generic_cpr,
2152 	    "unmount_tree");
2153 
2154 	/*
2155 	 * Got to release lock before attempting unmount in case
2156 	 * it hangs.
2157 	 */
2158 	rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_READER);
2159 	if ((fnp = fngp->fng_rootfnnodep->fn_dirents) == NULL) {
2160 		ASSERT(fngp->fng_fnnode_count == 1);
2161 		/*
2162 		 * no autofs mounted, done.
2163 		 */
2164 		rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
2165 		goto done;
2166 	}
2167 	VN_HOLD(fntovn(fnp));
2168 	rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
2169 
2170 	vp = fntovn(fnp);
2171 	fnip = vfstofni(vp->v_vfsp);
2172 	/*
2173 	 * autofssys() will be calling in from the global zone and doing
2174 	 * work on the behalf of the given zone, hence we can't always assert
2175 	 * that we have the right credentials, nor that the caller is always in
2176 	 * the correct zone.
2177 	 *
2178 	 * We do, however, know that if this is a "forced unmount" operation
2179 	 * (which autofssys() does), then we won't go down to the krpc layers,
2180 	 * so we don't need to fudge with the credentials.
2181 	 */
2182 	ASSERT(force || fnip->fi_zoneid == getzoneid());
2183 	if (!force && auto_null_request(fnip, FALSE) != 0) {
2184 		/*
2185 		 * automountd not running in this zone,
2186 		 * don't attempt unmounting this round.
2187 		 */
2188 		VN_RELE(vp);
2189 		goto done;
2190 	}
2191 	/* reference time for this unmount round */
2192 	ref_time = gethrestime_sec();
2193 	/*
2194 	 * If this an autofssys() call, we need to make sure we don't skip
2195 	 * nodes because we think we saw them recently.
2196 	 */
2197 	mutex_enter(&fnp->fn_lock);
2198 	if (force && fnp->fn_unmount_ref_time >= ref_time)
2199 		ref_time = fnp->fn_unmount_ref_time + 1;
2200 	mutex_exit(&fnp->fn_lock);
2201 
2202 	AUTOFS_DPRINT((4, "unmount_tree (ID=%ld)\n", ref_time));
2203 top:
2204 	AUTOFS_DPRINT((10, "unmount_tree: %s\n", fnp->fn_name));
2205 	ASSERT(fnp);
2206 	vp = fntovn(fnp);
2207 	if (vp->v_type == VLNK) {
2208 		/*
2209 		 * can't unmount symbolic links
2210 		 */
2211 		goto next;
2212 	}
2213 	fnip = vfstofni(vp->v_vfsp);
2214 	ASSERT(vp->v_count > 0);
2215 	error = 0;
2216 	autofs_busy_root = unmount_as_unit = 0;
2217 	alp = NULL;
2218 
2219 	ilocked_it = 0;
2220 	mutex_enter(&fnp->fn_lock);
2221 	if (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) {
2222 		/*
2223 		 * Either a mount, lookup or another unmount of this
2224 		 * subtree is in progress, don't attempt to unmount at
2225 		 * this time.
2226 		 */
2227 		mutex_exit(&fnp->fn_lock);
2228 		error = EBUSY;
2229 		goto next;
2230 	}
2231 	if (fnp->fn_unmount_ref_time >= ref_time) {
2232 		/*
2233 		 * Already been here, try next node.
2234 		 */
2235 		mutex_exit(&fnp->fn_lock);
2236 		error = EBUSY;
2237 		goto next;
2238 	}
2239 	fnp->fn_unmount_ref_time = ref_time;
2240 
2241 	/*
2242 	 * If forced operation ignore timeout values
2243 	 */
2244 	if (!force && fnp->fn_ref_time + fnip->fi_mount_to >
2245 	    gethrestime_sec()) {
2246 		/*
2247 		 * Node has been referenced recently, try the
2248 		 * unmount of its children if any.
2249 		 */
2250 		mutex_exit(&fnp->fn_lock);
2251 		AUTOFS_DPRINT((10, "fn_ref_time within range\n"));
2252 		rw_enter(&fnp->fn_rwlock, RW_READER);
2253 		if (fnp->fn_dirents) {
2254 			/*
2255 			 * Has subdirectory, attempt their
2256 			 * unmount first
2257 			 */
2258 			nfnp = fnp->fn_dirents;
2259 			VN_HOLD(fntovn(nfnp));
2260 			rw_exit(&fnp->fn_rwlock);
2261 
2262 			VN_RELE(vp);
2263 			fnp = nfnp;
2264 			goto top;
2265 		}
2266 		rw_exit(&fnp->fn_rwlock);
2267 		/*
2268 		 * No children, try next node.
2269 		 */
2270 		error = EBUSY;
2271 		goto next;
2272 	}
2273 
2274 	AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG);
2275 	fnp->fn_error = 0;
2276 	mutex_exit(&fnp->fn_lock);
2277 	ilocked_it = 1;
2278 
2279 	rw_enter(&fnp->fn_rwlock, RW_WRITER);
2280 	if (fnp->fn_trigger != NULL) {
2281 		unmount_as_unit = 1;
2282 		if ((vn_mountedvfs(vp) == NULL) && (check_auto_node(vp))) {
2283 			/*
2284 			 * AUTOFS mountpoint is busy, there's
2285 			 * no point trying to unmount. Fall through
2286 			 * to attempt to unmount subtrees rooted
2287 			 * at a possible trigger node, but remember
2288 			 * not to unmount this tree.
2289 			 */
2290 			autofs_busy_root = 1;
2291 		}
2292 
2293 		if (triggers_busy(fnp, &nfnp)) {
2294 			rw_exit(&fnp->fn_rwlock);
2295 			if (nfnp == NULL) {
2296 				error = EBUSY;
2297 				goto next;
2298 			}
2299 			/*
2300 			 * nfnp is busy, try to unmount it first
2301 			 */
2302 			mutex_enter(&fnp->fn_lock);
2303 			AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2304 			mutex_exit(&fnp->fn_lock);
2305 			VN_RELE(vp);
2306 			ASSERT(fntovn(nfnp)->v_count > 1);
2307 			fnp = nfnp;
2308 			goto top;
2309 		}
2310 
2311 		/*
2312 		 * At this point, we know all trigger nodes are locked,
2313 		 * and they're not busy or mounted on.
2314 		 */
2315 
2316 		if (autofs_busy_root) {
2317 			/*
2318 			 * Got to unlock the the trigger nodes since
2319 			 * I'm not really going to unmount the filesystem.
2320 			 */
2321 			(void) triggers_unlock(fnp);
2322 		} else {
2323 			/*
2324 			 * Attempt to unmount all the trigger nodes,
2325 			 * save the action_list in case we need to
2326 			 * remount them later. The action_list will be
2327 			 * freed later if there was no need to remount the
2328 			 * trigger nodes.
2329 			 */
2330 			unmount_triggers(fnp, &alp);
2331 		}
2332 	}
2333 	rw_exit(&fnp->fn_rwlock);
2334 
2335 	if (autofs_busy_root)
2336 		goto next;
2337 
2338 	(void) vn_vfswlock_wait(vp);
2339 
2340 	vfsp = vn_mountedvfs(vp);
2341 	if (vfsp != NULL) {
2342 		/*
2343 		 * Node is mounted on.
2344 		 */
2345 		AUTOFS_DPRINT((10, "\tNode is mounted on\n"));
2346 
2347 		/*
2348 		 * Deal with /xfn/host/jurassic alikes here...
2349 		 */
2350 		if (vfs_matchops(vfsp, vfs_getops(vp->v_vfsp))) {
2351 			/*
2352 			 * If the filesystem mounted here is AUTOFS, and it
2353 			 * is busy, try to unmount the tree rooted on it
2354 			 * first. We know this call to VFS_ROOT is safe to
2355 			 * call while holding VVFSLOCK, since it resolves
2356 			 * to a call to auto_root().
2357 			 */
2358 			AUTOFS_DPRINT((10, "\t\tAUTOFS mounted here\n"));
2359 			if (VFS_ROOT(vfsp, &newvp)) {
2360 				cmn_err(CE_PANIC,
2361 				    "unmount_tree: VFS_ROOT(vfs=%p) failed",
2362 				    (void *)vfsp);
2363 			}
2364 			nfnp = vntofn(newvp);
2365 			if (DEEPER(nfnp)) {
2366 				vn_vfsunlock(vp);
2367 				mutex_enter(&fnp->fn_lock);
2368 				AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2369 				mutex_exit(&fnp->fn_lock);
2370 				VN_RELE(vp);
2371 				fnp = nfnp;
2372 				goto top;
2373 			}
2374 			/*
2375 			 * Fall through to unmount this filesystem
2376 			 */
2377 			VN_RELE(newvp);
2378 		}
2379 
2380 		/*
2381 		 * vn_vfsunlock(vp) is done inside unmount_node()
2382 		 */
2383 		error = unmount_node(vp, force);
2384 		if (error == ECONNRESET) {
2385 			AUTOFS_DPRINT((10, "\tConnection dropped\n"));
2386 			if (vn_mountedvfs(vp) == NULL) {
2387 				/*
2388 				 * The filesystem was unmounted before the
2389 				 * daemon died. Unfortunately we can not
2390 				 * determine whether all the cleanup work was
2391 				 * successfully finished (i.e. update mnttab,
2392 				 * or notify NFS server of the unmount).
2393 				 * We should not retry the operation since the
2394 				 * filesystem has already been unmounted, and
2395 				 * may have already been removed from mnttab,
2396 				 * in such case the devid/rdevid we send to
2397 				 * the daemon will not be matched. So we have
2398 				 * to be content with the partial unmount.
2399 				 * Since the mountpoint is no longer covered, we
2400 				 * clear the error condition.
2401 				 */
2402 				error = 0;
2403 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
2404 				    CE_WARN, "unmount_tree: automountd "
2405 				    "connection dropped");
2406 				if (fnip->fi_flags & MF_DIRECT) {
2407 					auto_log(fngp->fng_verbose,
2408 					    fngp->fng_zoneid, CE_WARN,
2409 					    "unmount_tree: "
2410 					    "%s successfully unmounted - "
2411 					    "do not remount triggers",
2412 					    fnip->fi_path);
2413 				} else {
2414 					auto_log(fngp->fng_verbose,
2415 					    fngp->fng_zoneid, CE_WARN,
2416 					    "unmount_tree: "
2417 					    "%s/%s successfully unmounted - "
2418 					    "do not remount triggers",
2419 					    fnip->fi_path, fnp->fn_name);
2420 				}
2421 			}
2422 		}
2423 	} else {
2424 		vn_vfsunlock(vp);
2425 		AUTOFS_DPRINT((10, "\tNode is AUTOFS\n"));
2426 		if (unmount_as_unit) {
2427 			AUTOFS_DPRINT((10, "\tunmount as unit\n"));
2428 			error = unmount_autofs(vp);
2429 		} else {
2430 			AUTOFS_DPRINT((10, "\tunmount one at a time\n"));
2431 			rw_enter(&fnp->fn_rwlock, RW_READER);
2432 			if (fnp->fn_dirents != NULL) {
2433 				/*
2434 				 * Has subdirectory, attempt their
2435 				 * unmount first
2436 				 */
2437 				nfnp = fnp->fn_dirents;
2438 				VN_HOLD(fntovn(nfnp));
2439 				rw_exit(&fnp->fn_rwlock);
2440 
2441 				mutex_enter(&fnp->fn_lock);
2442 				AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2443 				mutex_exit(&fnp->fn_lock);
2444 				VN_RELE(vp);
2445 				fnp = nfnp;
2446 				goto top;
2447 			}
2448 			rw_exit(&fnp->fn_rwlock);
2449 			goto next;
2450 		}
2451 	}
2452 
2453 	if (error) {
2454 		AUTOFS_DPRINT((10, "\tUnmount failed\n"));
2455 		if (alp != NULL) {
2456 			/*
2457 			 * Unmount failed, got to remount triggers.
2458 			 */
2459 			ASSERT((fnp->fn_flags & MF_THISUID_MATCH_RQD) == 0);
2460 			error = auto_perform_actions(fnip, fnp, alp, CRED());
2461 			if (error) {
2462 				auto_log(fngp->fng_verbose, fngp->fng_zoneid,
2463 				    CE_WARN, "autofs: can't remount triggers "
2464 				    "fnp=%p error=%d", (void *)fnp, error);
2465 				error = 0;
2466 				/*
2467 				 * The action list should have been
2468 				 * free'd by auto_perform_actions
2469 				 * since an error occured
2470 				 */
2471 				alp = NULL;
2472 			}
2473 		}
2474 	} else {
2475 		/*
2476 		 * The unmount succeeded, which will cause this node to
2477 		 * be removed from its parent if its an indirect mount,
2478 		 * therefore update the parent's atime and mtime now.
2479 		 * I don't update them in auto_disconnect() because I
2480 		 * don't want atime and mtime changing every time a
2481 		 * lookup goes to the daemon and creates a new node.
2482 		 */
2483 		unmount_done = 1;
2484 		if ((fnip->fi_flags & MF_DIRECT) == 0) {
2485 			gethrestime(&now);
2486 			if (fnp->fn_parent == fngp->fng_rootfnnodep)
2487 				fnp->fn_atime = fnp->fn_mtime = now;
2488 			else {
2489 				fnp->fn_parent->fn_atime = now;
2490 				fnp->fn_parent->fn_mtime = now;
2491 			}
2492 		}
2493 
2494 		/*
2495 		 * Free the action list here
2496 		 */
2497 		if (alp != NULL) {
2498 			xdr_free(xdr_action_list, (char *)alp);
2499 			alp = NULL;
2500 		}
2501 	}
2502 
2503 	fnp->fn_ref_time = gethrestime_sec();
2504 
2505 next:
2506 	/*
2507 	 * Obtain parent's readers lock before grabbing
2508 	 * reference to next sibling.
2509 	 * XXX Note that nodes in the top level list (mounted
2510 	 * in user space not by the daemon in the kernel) parent is itself,
2511 	 * therefore grabbing the lock makes no sense, but doesn't
2512 	 * hurt either.
2513 	 */
2514 	pfnp = fnp->fn_parent;
2515 	ASSERT(pfnp != NULL);
2516 	rw_enter(&pfnp->fn_rwlock, RW_READER);
2517 	if ((nfnp = fnp->fn_next) != NULL)
2518 		VN_HOLD(fntovn(nfnp));
2519 	rw_exit(&pfnp->fn_rwlock);
2520 
2521 	if (ilocked_it) {
2522 		mutex_enter(&fnp->fn_lock);
2523 		if (unmount_done) {
2524 			/*
2525 			 * Other threads may be waiting for this unmount to
2526 			 * finish. We must let it know that in order to
2527 			 * proceed, it must trigger the mount itself.
2528 			 */
2529 			fnp->fn_flags &= ~MF_IK_MOUNT;
2530 			if (fnp->fn_flags & MF_WAITING)
2531 				fnp->fn_error = EAGAIN;
2532 			unmount_done = 0;
2533 		}
2534 		AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG);
2535 		mutex_exit(&fnp->fn_lock);
2536 		ilocked_it = 0;
2537 	}
2538 
2539 	if (nfnp != NULL) {
2540 		VN_RELE(vp);
2541 		fnp = nfnp;
2542 		/*
2543 		 * Unmount next element
2544 		 */
2545 		goto top;
2546 	}
2547 
2548 	/*
2549 	 * We don't want to unmount rootfnnodep, so the check is made here
2550 	 */
2551 	ASSERT(pfnp != fnp);
2552 	if (pfnp != fngp->fng_rootfnnodep) {
2553 		/*
2554 		 * Now attempt to unmount my parent
2555 		 */
2556 		VN_HOLD(fntovn(pfnp));
2557 		VN_RELE(vp);
2558 		fnp = pfnp;
2559 
2560 		goto top;
2561 	}
2562 
2563 	VN_RELE(vp);
2564 
2565 	/*
2566 	 * At this point we've walked the entire tree and attempted to unmount
2567 	 * as much as we can one level at a time.
2568 	 */
2569 done:
2570 	mutex_enter(&unmount_tree_cpr_lock);
2571 	CALLB_CPR_EXIT(&cprinfo);
2572 	mutex_destroy(&unmount_tree_cpr_lock);
2573 }
2574 
2575 static void
2576 unmount_zone_tree(struct autofs_globals *fngp)
2577 {
2578 	unmount_tree(fngp, 0);
2579 	mutex_enter(&fngp->fng_unmount_threads_lock);
2580 	fngp->fng_unmount_threads--;
2581 	mutex_exit(&fngp->fng_unmount_threads_lock);
2582 
2583 	AUTOFS_DPRINT((5, "unmount_tree done. Thread exiting.\n"));
2584 
2585 	zthread_exit();
2586 	/* NOTREACHED */
2587 }
2588 
2589 static int autofs_unmount_thread_timer = 120;	/* in seconds */
2590 
2591 void
2592 auto_do_unmount(struct autofs_globals *fngp)
2593 {
2594 	callb_cpr_t cprinfo;
2595 	clock_t timeleft;
2596 	zone_t *zone = curproc->p_zone;
2597 
2598 	CALLB_CPR_INIT(&cprinfo, &fngp->fng_unmount_threads_lock,
2599 	    callb_generic_cpr, "auto_do_unmount");
2600 
2601 	for (;;) {	/* forever */
2602 		mutex_enter(&fngp->fng_unmount_threads_lock);
2603 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
2604 newthread:
2605 		mutex_exit(&fngp->fng_unmount_threads_lock);
2606 		timeleft = zone_status_timedwait(zone, lbolt +
2607 		    autofs_unmount_thread_timer * hz, ZONE_IS_SHUTTING_DOWN);
2608 		mutex_enter(&fngp->fng_unmount_threads_lock);
2609 
2610 		if (timeleft != -1) {	/* didn't time out */
2611 			ASSERT(zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN);
2612 			/*
2613 			 * zone is exiting... don't create any new threads.
2614 			 * fng_unmount_threads_lock is released implicitly by
2615 			 * the below.
2616 			 */
2617 			CALLB_CPR_SAFE_END(&cprinfo,
2618 			    &fngp->fng_unmount_threads_lock);
2619 			CALLB_CPR_EXIT(&cprinfo);
2620 			zthread_exit();
2621 			/* NOTREACHED */
2622 		}
2623 		if (fngp->fng_unmount_threads < autofs_unmount_threads) {
2624 			fngp->fng_unmount_threads++;
2625 			CALLB_CPR_SAFE_END(&cprinfo,
2626 			    &fngp->fng_unmount_threads_lock);
2627 			mutex_exit(&fngp->fng_unmount_threads_lock);
2628 
2629 			(void) zthread_create(NULL, 0, unmount_zone_tree, fngp,
2630 			    0, minclsyspri);
2631 		} else
2632 			goto newthread;
2633 	}
2634 	/* NOTREACHED */
2635 }
2636 
2637 /*
2638  * Is nobrowse specified in option string?
2639  * opts should be a null ('\0') terminated string.
2640  * Returns non-zero if nobrowse has been specified.
2641  */
2642 int
2643 auto_nobrowse_option(char *opts)
2644 {
2645 	char *buf;
2646 	char *p;
2647 	char *t;
2648 	int nobrowse = 0;
2649 	int last_opt = 0;
2650 	size_t len;
2651 
2652 	len = strlen(opts) + 1;
2653 	p = buf = kmem_alloc(len, KM_SLEEP);
2654 	(void) strcpy(buf, opts);
2655 	do {
2656 		if (t = strchr(p, ','))
2657 			*t++ = '\0';
2658 		else
2659 			last_opt++;
2660 		if (strcmp(p, MNTOPT_NOBROWSE) == 0)
2661 			nobrowse = 1;
2662 		else if (strcmp(p, MNTOPT_BROWSE) == 0)
2663 			nobrowse = 0;
2664 		p = t;
2665 	} while (!last_opt);
2666 	kmem_free(buf, len);
2667 
2668 	return (nobrowse);
2669 }
2670 
2671 /*
2672  * used to log warnings only if automountd is running
2673  * with verbose mode set
2674  */
2675 
2676 void
2677 auto_log(int verbose, zoneid_t zoneid, int level, const char *fmt, ...)
2678 {
2679 	va_list	args;
2680 
2681 	if (verbose) {
2682 		va_start(args, fmt);
2683 		vzcmn_err(zoneid, level, fmt, args);
2684 		va_end(args);
2685 	}
2686 }
2687 
2688 #ifdef DEBUG
2689 static int autofs_debug = 0;
2690 
2691 /*
2692  * Utilities used by both client and server
2693  * Standard levels:
2694  * 0) no debugging
2695  * 1) hard failures
2696  * 2) soft failures
2697  * 3) current test software
2698  * 4) main procedure entry points
2699  * 5) main procedure exit points
2700  * 6) utility procedure entry points
2701  * 7) utility procedure exit points
2702  * 8) obscure procedure entry points
2703  * 9) obscure procedure exit points
2704  * 10) random stuff
2705  * 11) all <= 1
2706  * 12) all <= 2
2707  * 13) all <= 3
2708  * ...
2709  */
2710 /* PRINTFLIKE2 */
2711 void
2712 auto_dprint(int level, const char *fmt, ...)
2713 {
2714 	va_list args;
2715 
2716 	if (autofs_debug == level ||
2717 	    (autofs_debug > 10 && (autofs_debug - 10) >= level)) {
2718 		va_start(args, fmt);
2719 		(void) vprintf(fmt, args);
2720 		va_end(args);
2721 	}
2722 }
2723 #endif /* DEBUG */
2724