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