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 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/*
27 * Portions Copyright 2007-2013 Apple Inc.
28 */
29
30#pragma ident	"@(#)auto_vfsops.c	1.58	06/03/24 SMI"
31
32#include <sys/param.h>
33#include <sys/queue.h>
34#include <sys/systm.h>
35#include <sys/namei.h>
36#include <sys/proc.h>
37#include <sys/kernel.h>
38#include <mach/machine/vm_types.h>
39#include <sys/vnode.h>
40#include <sys/vfs_context.h>
41#include <sys/socket.h>
42#include <sys/mount.h>
43#include <sys/errno.h>
44#include <sys/malloc.h>
45#include <sys/attr.h>
46#include <sys/sysctl.h>
47#include <sys/conf.h>
48#include <miscfs/devfs/devfs.h>
49#include <kern/locks.h>
50#include <kern/assert.h>
51
52#include <IOKit/IOLib.h>
53
54#include "autofs.h"
55#include "triggers.h"
56#include "triggers_priv.h"
57#include "autofs_kern.h"
58
59lck_grp_t *autofs_lck_grp;
60static lck_mtx_t *autofs_global_lock;
61lck_mtx_t *autofs_nodeid_lock;
62
63static u_int autofs_mounts;
64
65__private_extern__ int auto_module_start(kmod_info_t *, void *);
66__private_extern__ int auto_module_stop(kmod_info_t *, void *);
67
68/*
69 * autofs VFS operations
70 */
71static int auto_mount(mount_t, vnode_t, user_addr_t, vfs_context_t);
72static int auto_start(mount_t, int, vfs_context_t);
73static int auto_unmount(mount_t, int, vfs_context_t);
74static int auto_vfs_getattr(mount_t, struct vfs_attr *, vfs_context_t);
75static int auto_sync(mount_t, int, vfs_context_t);
76static int auto_vget(mount_t, ino64_t, vnode_t *, vfs_context_t);
77static int autofs_sysctl(int *, u_int, user_addr_t, size_t *, user_addr_t, size_t, vfs_context_t);
78
79static struct vfsops autofs_vfsops = {
80	auto_mount,
81	auto_start,
82	auto_unmount,
83	auto_root,
84	NULL,			/* auto_quotactl */
85	auto_vfs_getattr, 	/* was auto_statfs */
86	auto_sync,
87	auto_vget,
88	NULL,			/* auto_fhtovp */
89	NULL,			/* auto_vptofh */
90	NULL,			/* auto_init */
91	autofs_sysctl,
92	NULL,			/* auto_vfs_setattr */
93	{ NULL, NULL, NULL, NULL, NULL, NULL, NULL }	/* reserved ops */
94};
95
96extern struct vnodeopv_desc autofsfs_vnodeop_opv_desc;
97struct vnodeopv_desc * autofs_vnodeop_opv_descs[] =
98	{&autofsfs_vnodeop_opv_desc, NULL};
99
100struct vfs_fsentry autofs_fsentry = {
101	&autofs_vfsops,			/* vfs operations */
102	1,				/* # of vnodeopv_desc being registered (reg, spec, fifo ...) */
103	autofs_vnodeop_opv_descs,	/* null terminated;  */
104	0,				/* historic filesystem type number [ unused w. VFS_TBLNOTYPENUM specified ] */
105	MNTTYPE_AUTOFS,			/* filesystem type name */
106	VFS_TBLTHREADSAFE | VFS_TBLNOTYPENUM | VFS_TBL64BITREADY | VFS_TBLNATIVEXATTR,
107					/* defines the FS capabilities */
108	{ NULL, NULL }			/* reserved for future use; set this to zero*/
109};
110
111static vfstable_t  auto_vfsconf;
112
113/*
114 * No zones in OS X, so only one autofs_globals structure.
115 */
116static struct autofs_globals *global_fngp;
117
118static struct autofs_globals *
119autofs_zone_get_globals(void)
120{
121	return (global_fngp);
122}
123
124static void
125autofs_zone_set_globals(struct autofs_globals *fngp)
126{
127	global_fngp = fngp;
128}
129
130/*
131 * rootfnnodep is allocated here.  Its sole purpose is to provide
132 * read/write locking for top level fnnodes.  This object is
133 * persistent and will not be deallocated until the module is unloaded.
134 * XXX - is there some compelling reason for it to exist?  Where is
135 * it used?
136 *
137 * There are no zones in OS X, so this is system-wide.
138 */
139static struct autofs_globals *
140autofs_zone_init(void)
141{
142	struct autofs_globals *fngp = NULL;
143	fnnode_t *fnp = NULL;
144	static const char fnnode_name[] = "root_fnnode";
145	struct timeval now;
146
147	MALLOC(fngp, struct autofs_globals *, sizeof (*fngp), M_AUTOFS,
148	    M_WAITOK);
149	if (fngp == NULL) {
150		IOLog("autofs_zone_init: Couldn't create autofs globals structure\n");
151		goto fail;
152	}
153	bzero(fngp, sizeof (*fngp));
154	/*
155	 * Allocate a "dummy" fnnode, not associated with any vnode.
156	 */
157	MALLOC(fnp, fnnode_t *, sizeof(fnnode_t), M_AUTOFS, M_WAITOK);
158	if (fnp == NULL) {
159		IOLog("autofs_zone_init: Couldn't create autofs global fnnode\n");
160		goto fail;
161	}
162	bzero(fnp, sizeof(*fnp));
163	fnp->fn_namelen = sizeof fnnode_name;
164	MALLOC(fnp->fn_name, char *, fnp->fn_namelen + 1, M_AUTOFS, M_WAITOK);
165	if (fnp->fn_name == NULL) {
166		IOLog("autofs_zone_init: Couldn't create autofs global fnnode name\n");
167		goto fail;
168	}
169	bcopy(fnnode_name, fnp->fn_name, sizeof fnnode_name);
170	fnp->fn_name[sizeof fnnode_name] = '\0';
171	fnp->fn_mode = AUTOFS_MODE;
172	microtime(&now);
173	fnp->fn_crtime = fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now;
174	fnp->fn_nodeid = 1;	/* XXX - could just be zero? */
175	fnp->fn_globals = fngp;
176	fnp->fn_lock = lck_mtx_alloc_init(autofs_lck_grp, NULL);
177	if (fnp->fn_lock == NULL) {
178		IOLog("autofs_zone_init: Couldn't create autofs global fnnode mutex\n");
179		goto fail;
180	}
181	fnp->fn_rwlock = lck_rw_alloc_init(autofs_lck_grp, NULL);
182	if (fnp->fn_rwlock == NULL) {
183		IOLog("autofs_zone_init: Couldn't create autofs global fnnode rwlock\n");
184		goto fail;
185	}
186
187	fnp->fn_mnt_lock = lck_mtx_alloc_init(autofs_lck_grp, NULL);
188	if (fnp->fn_mnt_lock == NULL) {
189		IOLog("autofs_zone_init: Couldn't create autofs global fnnode mnt mutex\n");
190		goto fail;
191	}
192
193	fngp->fng_rootfnnodep = fnp;
194	fngp->fng_fnnode_count = 1;
195	fngp->fng_printed_not_running_msg = 0;
196	fngp->fng_flush_notification_lock = lck_mtx_alloc_init(autofs_lck_grp,
197	    LCK_ATTR_NULL);
198	if (fngp->fng_flush_notification_lock == NULL) {
199		IOLog("autofs_zone_init: Couldn't create autofs global flush notification lock\n");
200		goto fail;
201	}
202	return (fngp);
203
204fail:
205	if (fnp != NULL) {
206		if (fnp->fn_mnt_lock != NULL)
207			lck_mtx_free(fnp->fn_mnt_lock, autofs_lck_grp);
208		if (fnp->fn_rwlock != NULL)
209			lck_rw_free(fnp->fn_rwlock, autofs_lck_grp);
210		if (fnp->fn_lock != NULL)
211			lck_mtx_free(fnp->fn_lock, autofs_lck_grp);
212		if (fnp->fn_name != NULL)
213			FREE(fnp->fn_name, M_AUTOFS);
214		FREE(fnp, M_AUTOFS);
215	}
216	if (fngp != NULL) {
217		if (fngp->fng_flush_notification_lock != NULL)
218			lck_mtx_free(fngp->fng_flush_notification_lock,
219			    autofs_lck_grp);
220		FREE(fngp, M_AUTOFS);
221	}
222	return (NULL);
223}
224
225/*
226 * Find the first occurrence of find in s.
227 */
228static char *
229strstr(const char *s, const char *find)
230{
231	char c, sc;
232	size_t len;
233
234	if ((c = *find++) != 0) {
235		len = strlen(find);
236		do {
237			do {
238				if ((sc = *s++) == 0)
239					return (NULL);
240			} while (sc != c);
241		} while (strncmp(s, find, len) != 0);
242		s--;
243	}
244	return ((char *)s);
245}
246
247/*
248 * A "struct mntopt" table is terminated with an entry with a null
249 * m_option pointer; therefore, the number of real entries in the
250 * table is one fewer than the total number of entries.
251 */
252static const struct mntopt restropts[] = {
253	RESTRICTED_MNTOPTS
254};
255#define	NROPTS	((sizeof (restropts)/sizeof (restropts[0])) - 1)
256
257/*
258 * Check whether the specified option is set in the specified file
259 * system.
260 */
261static uint64_t
262optionisset(mount_t mp, const struct mntopt *opt)
263{
264	uint64_t flags;
265
266	flags = vfs_flags(mp) & opt->m_flag;
267	if (opt->m_inverse)
268		return !flags;
269	else
270		return flags;
271}
272
273/*
274 * This routine adds those options to the option string `buf' which are
275 * forced by secpolicy_fs_mount.  If the automatic "security" options
276 * are set, the option string gets them added if they aren't already
277 * there.  We search the string with "strstr" and make sure that
278 * the string we find is bracketed with <start|",">MNTOPT<","|"\0">
279 *
280 * This is one half of the option inheritence algorithm which
281 * implements the "restrict" option.  The other half is implemented
282 * in automountd; it takes its cue from the options we add here.
283 */
284static int
285autofs_restrict_opts(mount_t mp, char *buf, size_t maxlen, size_t *curlen)
286{
287	fninfo_t *fnip = vfstofni(mp);
288	u_int i;
289	char *p;
290	size_t len = *curlen - 1;
291
292	/* Unrestricted */
293	if (!(fnip->fi_mntflags & AUTOFS_MNT_RESTRICT))
294		return (0);
295
296	for (i = 0; i < NROPTS; i++) {
297		size_t olen = strlen(restropts[i].m_option);
298
299		/* Add "restrict" always and the others insofar set */
300		if ((i == 0 || optionisset(mp, &restropts[i])) &&
301		    ((p = strstr(buf, restropts[i].m_option)) == NULL ||
302		    !((p == buf || p[-1] == ',') &&
303		    (p[olen] == '\0' || p[olen] == ',')))) {
304
305			if (len + olen + 1 > maxlen)
306				return (-1);
307
308			if (*buf != '\0')
309				buf[len++] = ',';
310			bcopy(restropts[i].m_option, &buf[len], olen);
311			len += olen;
312		}
313	}
314	if (len + 1 > maxlen)
315		return (-1);
316	buf[len++] = '\0';
317	*curlen = len;
318	return (0);
319}
320
321/* ARGSUSED */
322static int
323auto_mount(mount_t mp, __unused vnode_t devvp, user_addr_t data,
324    vfs_context_t context)
325{
326	int error;
327	size_t len;
328	uint32_t argsvers;
329	struct autofs_args_64 args;
330	fninfo_t *fnip = NULL;
331	vnode_t myrootvp = NULL;
332	fnnode_t *rootfnp = NULL;
333	char strbuff[PATH_MAX+1];
334	uint64_t flags;
335#ifdef DEBUG
336	lck_attr_t *lckattr;
337#endif
338	struct autofs_globals *fngp;
339	int node_type;
340	boolean_t lu_verbose;
341
342	AUTOFS_DPRINT((4, "auto_mount: mp %p devvp %p\n", mp, devvp));
343
344	lck_mtx_lock(autofs_global_lock);
345	if ((fngp = autofs_zone_get_globals()) == NULL) {
346		fngp = autofs_zone_init();
347		autofs_zone_set_globals(fngp);
348	}
349	lck_mtx_unlock(autofs_global_lock);
350	if (fngp == NULL)
351		return (ENOMEM);
352
353	/*
354	 * Get argument version
355	 */
356	error = copyin(data, (caddr_t)&argsvers, sizeof (argsvers));
357	if (error)
358		return (error);
359
360	/*
361	 * Get arguments
362	 */
363	switch (argsvers) {
364
365	case 2:
366		if (vfs_context_is64bit(context))
367			error = copyin(data, &args, sizeof (args));
368		else {
369			struct autofs_args_32 args32;
370
371			error = copyin(data, &args32, sizeof (args32));
372			if (error == 0) {
373				args.path = CAST_USER_ADDR_T(args32.path);
374				args.opts = CAST_USER_ADDR_T(args32.opts);
375				args.map = CAST_USER_ADDR_T(args32.map);
376				args.subdir = CAST_USER_ADDR_T(args32.subdir);
377				args.key = CAST_USER_ADDR_T(args32.key);
378				args.mntflags = args32.mntflags;
379				args.direct = args32.direct;
380				args.mount_type = args32.mount_type;
381				args.node_type = args32.node_type;
382			}
383		}
384		if (error)
385			return (error);
386		break;
387
388	default:
389		return (EINVAL);
390	}
391
392	/*
393	 * We don't support remounts; the AUTOFS_UPDATE_OPTIONS ioctl
394	 * should be used for that.
395	 */
396	flags = vfs_flags(mp) & MNT_CMDFLAGS;
397	if (flags & MNT_UPDATE)
398		return (EINVAL);
399
400	/*
401	 * Allocate fninfo struct and attach it to vfs
402	 */
403	MALLOC(fnip, fninfo_t *, sizeof(*fnip), M_AUTOFS, M_WAITOK);
404	if (fnip == NULL)
405		return (ENOMEM);
406	bzero(fnip, sizeof(*fnip));
407
408	/*
409	 * Assign a unique fsid to the mount
410	 */
411	vfs_getnewfsid(mp);
412	vfs_setfsprivate(mp, fnip);
413
414	/*
415	 * Get path for mountpoint
416	 */
417	error = copyinstr(args.path, strbuff, sizeof (strbuff), &len);
418	if (error) {
419		error = EFAULT;
420		goto errout;
421	}
422	MALLOC(fnip->fi_path, char *, len, M_AUTOFS, M_WAITOK);
423	if (fnip->fi_path == NULL) {
424		error = ENOMEM;
425		goto errout;
426	}
427	fnip->fi_pathlen = (int)len;
428	bcopy(strbuff, fnip->fi_path, len);
429
430	/*
431	 * Get default options
432	 */
433	error = copyinstr(args.opts, strbuff, sizeof (strbuff), &len);
434
435	if (error != 0)
436		goto errout;
437
438	if (autofs_restrict_opts(mp, strbuff, sizeof (strbuff), &len) != 0) {
439		error = EFAULT;
440		goto errout;
441	}
442	MALLOC(fnip->fi_opts, char *, len, M_AUTOFS, M_WAITOK);
443	if (fnip->fi_opts == NULL) {
444		error = ENOMEM;
445		goto errout;
446	}
447	fnip->fi_optslen = (int)len;
448	bcopy(strbuff, fnip->fi_opts, len);
449
450	/*
451	 * Get context/map name
452	 */
453	error = copyinstr(args.map, strbuff, sizeof (strbuff), &len);
454	if (error)
455		goto errout;
456	MALLOC(fnip->fi_map, char *, len, M_AUTOFS, M_WAITOK);
457	if (fnip->fi_map == NULL) {
458		error = ENOMEM;
459		goto errout;
460	}
461	fnip->fi_maplen = (int)len;
462	bcopy(strbuff, fnip->fi_map, len);
463
464	switch (args.mount_type) {
465
466	case MOUNT_TYPE_MAP:
467		/*
468		 * Top-level mount of a map, done by automount.
469		 * Show it as "map XXX".
470		 */
471		snprintf(vfs_statfs(mp)->f_mntfromname, sizeof(vfs_statfs(mp)->f_mntfromname),
472		    "map %.*s", fnip->fi_maplen, fnip->fi_map);
473		break;
474
475	case MOUNT_TYPE_TRIGGERED_MAP:
476		/*
477		 * Map mounted as a result of an automounter map
478		 * entry that said "do an autofs mount here".
479		 * Show it as "triggered map XXX", to let
480		 * the automount command know it's not a top-level
481		 * map and that it shouldn't unmount it if auto_master
482		 * doesn't mention it.
483		 */
484		snprintf(vfs_statfs(mp)->f_mntfromname, sizeof(vfs_statfs(mp)->f_mntfromname),
485		    "triggered map %.*s", fnip->fi_maplen, fnip->fi_map);
486		break;
487
488	case MOUNT_TYPE_SUBTRIGGER:
489		/*
490		 * Mark this in the from name as a subtrigger, both
491		 * to let users know that it's a subtrigger and to
492		 * let the automount command know that it's a subtrigger
493		 * and that it shouldn't unmount it if auto_master
494		 * doesn't mention it.
495		 */
496		strncpy(vfs_statfs(mp)->f_mntfromname, "subtrigger",
497		    sizeof(vfs_statfs(mp)->f_mntfromname)-1);
498		vfs_statfs(mp)->f_mntfromname[sizeof(vfs_statfs(mp)->f_mntfromname)-1] = (char)0;
499
500		/*
501		 * Flag it as a (sub)trigger for our purposes as well.
502		 */
503		fnip->fi_flags |= MF_SUBTRIGGER;
504		break;
505	}
506
507	/*
508	 * Get subdirectory within map
509	 */
510	error = copyinstr(args.subdir, strbuff, sizeof (strbuff), &len);
511	if (error)
512		goto errout;
513	MALLOC(fnip->fi_subdir, char *, len, M_AUTOFS, M_WAITOK);
514	if (fnip->fi_subdir == NULL) {
515		error = ENOMEM;
516		goto errout;
517	}
518	fnip->fi_subdirlen = (int)len;
519	bcopy(strbuff, fnip->fi_subdir, len);
520
521	/*
522	 * Get the key
523	 */
524	error = copyinstr(args.key, strbuff, sizeof (strbuff), &len);
525	if (error)
526		goto errout;
527	MALLOC(fnip->fi_key, char *, len, M_AUTOFS, M_WAITOK);
528	if (fnip->fi_key == NULL) {
529		error = ENOMEM;
530		goto errout;
531	}
532	fnip->fi_keylen = (int)len;
533	bcopy(strbuff, fnip->fi_key, len);
534
535	fnip->fi_mntflags = args.mntflags;
536
537	/*
538	 * Is this a direct mount?
539	 */
540	if (args.direct == 1)
541		fnip->fi_flags |= MF_DIRECT;
542
543	/*
544	 * Get an rwlock.
545	 */
546#ifdef DEBUG
547	/*
548	 * Enable debugging on the lock.
549	 */
550	lckattr = lck_attr_alloc_init();
551	lck_attr_setdebug(lckattr);
552	fnip->fi_rwlock = lck_rw_alloc_init(autofs_lck_grp, lckattr);
553	lck_attr_free(lckattr);
554#else
555	fnip->fi_rwlock = lck_rw_alloc_init(autofs_lck_grp, NULL);
556#endif
557
558	if (args.direct == 1) {
559		/*
560		 * For subtriggers, mark this not just as a trigger,
561		 * but as a "real" trigger, rather than a "just mounts
562		 * autofs" trigger, so that the higher-level frameworks
563		 * will see it as ultimately having a real file system
564		 * mounted on it, regardless of whether the autofs
565		 * trigger is mounted atop it.
566		 *
567		 * Otherwise, do a lookup on the root node, to determine
568		 * whether anything is supposed to be mounted on it or not,
569		 * i.e. whether it's a trigger.
570		 *
571		 * XXX - this means that, if the map entry changes,
572		 * the vnode type can't change from trigger to
573		 * non-trigger or vice versa.  It also means that
574		 * if a map changes from direct to indirect, or
575		 * vice versa, the vnode type can't change.
576		 */
577		if (args.mount_type == MOUNT_TYPE_SUBTRIGGER)
578			node_type = NT_TRIGGER;
579		else {
580			error = auto_lookup_request(fnip, fnip->fi_key,
581			    fnip->fi_keylen, fnip->fi_subdir,
582			    context, &node_type, &lu_verbose);
583			if (error) {
584				/*
585				 * Well, this will get an error when we
586				 * try to refer to it, so let's call it
587				 * a trigger, so the error will show up.
588				 */
589				node_type = NT_TRIGGER;
590			}
591		}
592	} else {
593		/*
594		 * The root directory of an indirect map is never a
595		 * trigger.
596		 */
597		node_type = 0;
598	}
599
600	/*
601	 * Make the root vnode.
602	 */
603	error = auto_makefnnode(&rootfnp, node_type, mp, NULL, "", NULL,
604	    1, fngp);
605	if (error)
606		goto errout;
607	myrootvp = fntovn(rootfnp);
608
609	rootfnp->fn_mode = AUTOFS_MODE;
610	rootfnp->fn_parent = rootfnp;
611	/* account for ".." entry (fn_parent) */
612	rootfnp->fn_linkcnt = 1;
613	error = vnode_ref(myrootvp);	/* released in auto_unmount */
614	if (error) {
615		vnode_recycle(myrootvp);
616		vnode_put(myrootvp);
617		goto errout;
618	}
619	rootfnp->fn_parentvp = myrootvp;
620	rootfnp->fn_parentvid = vnode_vid(myrootvp);
621	fnip->fi_rootvp = myrootvp;
622
623	/*
624	 * Add to list of top level AUTOFS's if this is a top-level
625	 * mount.
626	 */
627	if (args.mount_type == MOUNT_TYPE_MAP) {
628		lck_rw_lock_exclusive(fngp->fng_rootfnnodep->fn_rwlock);
629		rootfnp->fn_parent = fngp->fng_rootfnnodep;
630		/*fngp->fng_rootfnnodep does not have an associated vp */
631		rootfnp->fn_parentvp = NULL;
632		rootfnp->fn_next = fngp->fng_rootfnnodep->fn_dirents;
633		fngp->fng_rootfnnodep->fn_dirents = rootfnp;
634		lck_rw_unlock_exclusive(fngp->fng_rootfnnodep->fn_rwlock);
635	}
636
637	/*
638	 * One more mounted file system.
639	 */
640	lck_mtx_lock(autofs_global_lock);
641	autofs_mounts++;
642	lck_mtx_unlock(autofs_global_lock);
643
644	vnode_put(myrootvp);
645
646	AUTOFS_DPRINT((5, "auto_mount: mp %p root %p fnip %p return %d\n",
647	    mp, myrootvp, fnip, error));
648
649	return (0);
650
651errout:
652	assert(fnip != NULL);
653
654	if (fnip->fi_rwlock != NULL)
655		lck_rw_free(fnip->fi_rwlock, autofs_lck_grp);
656	if (fnip->fi_path != NULL)
657		FREE(fnip->fi_path, M_AUTOFS);
658	if (fnip->fi_opts != NULL)
659		FREE(fnip->fi_opts, M_AUTOFS);
660	if (fnip->fi_map != NULL)
661		FREE(fnip->fi_map, M_AUTOFS);
662	if (fnip->fi_subdir != NULL)
663		FREE(fnip->fi_subdir, M_AUTOFS);
664	if (fnip->fi_key != NULL)
665		FREE(fnip->fi_key, M_AUTOFS);
666	FREE(fnip, sizeof M_AUTOFS);
667
668	AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
669	    (void *)mp, (void *)myrootvp, (void *)fnip, error));
670
671	return (error);
672}
673
674static int
675auto_update_options(struct autofs_update_args_64 *update_argsp)
676{
677	mount_t mp;
678	fninfo_t *fnip;
679	fnnode_t *fnp;
680	char strbuff[PATH_MAX+1];
681	int error;
682	char *opts, *map;
683	size_t optslen, maplen;
684	int was_nobrowse;
685
686	/*
687	 * Update the mount options on this autofs node.
688	 * XXX - how do we make sure mp isn't freed out from under
689	 * us?
690	 */
691	mp = vfs_getvfs(&update_argsp->fsid);
692	if (mp == NULL)
693		return (ENOENT);	/* no such mount */
694
695	/*
696	 * Make sure this is an autofs mount.
697	 */
698	if (!auto_is_autofs(mp))
699		return (EINVAL);
700
701	fnip = vfstofni(mp);
702	if (fnip == NULL)
703		return (EINVAL);
704
705	/*
706	 * We can't change the map type if the top-level directory has
707	 * subdirectories (i.e., if stuff has happened under it).
708	 *
709	 * XXX - change whether it's a trigger or not?
710	 */
711	if ((update_argsp->direct == 1 && !(fnip->fi_flags & MF_DIRECT)) ||
712	    (update_argsp->direct != 1 && (fnip->fi_flags & MF_DIRECT))) {
713		fnp = vntofn(fnip->fi_rootvp);
714		if (fnp->fn_dirents != NULL)
715			return (EINVAL);
716	}
717
718	/*
719	 * Get default options
720	 */
721	error = copyinstr(update_argsp->opts, strbuff, sizeof (strbuff),
722	    &optslen);
723	if (error)
724		return (error);
725
726	if (autofs_restrict_opts(mp, strbuff, sizeof (strbuff), &optslen) != 0)
727		return (EFAULT);
728
729	MALLOC(opts, char *, optslen, M_AUTOFS, M_WAITOK);
730	bcopy(strbuff, opts, optslen);
731
732	/*
733	 * Get context/map name
734	 */
735	error = copyinstr(update_argsp->map, strbuff, sizeof (strbuff),
736	    &maplen);
737	if (error) {
738		FREE(opts, M_AUTOFS);
739		return (error);
740	}
741	MALLOC(map, char *, maplen, M_AUTOFS, M_WAITOK);
742	bcopy(strbuff, map, maplen);
743
744	/*
745	 * We've fetched all the strings; lock out any other references
746	 * to fnip, and update the mount information.
747	 */
748	lck_rw_lock_exclusive(fnip->fi_rwlock);
749	was_nobrowse = auto_nobrowse(fnip->fi_rootvp);
750	FREE(fnip->fi_opts, M_AUTOFS);
751	fnip->fi_opts = opts;
752	fnip->fi_optslen = (int)optslen;
753	FREE(fnip->fi_map, M_AUTOFS);
754	fnip->fi_map = map;
755	fnip->fi_maplen = (int)maplen;
756	if (update_argsp->direct == 1)
757		fnip->fi_flags |= MF_DIRECT;
758	else
759		fnip->fi_flags &= ~MF_DIRECT;
760	fnip->fi_flags &= ~MF_UNMOUNTING;
761	fnip->fi_mntflags = update_argsp->mntflags;
762
763	snprintf(vfs_statfs(mp)->f_mntfromname, sizeof(vfs_statfs(mp)->f_mntfromname),
764	    "map %.*s", fnip->fi_maplen, fnip->fi_map);
765	lck_rw_unlock_exclusive(fnip->fi_rwlock);
766
767	/*
768	 * Notify anybody looking at this mount's root directory
769	 * that it might have changed, unless it's a directory
770	 * whose contents reflect only the stuff for which we
771	 * already have vnodes and was that way before the change;
772	 * this remount might have been done as the result of a network
773	 * change, so the map backing it might have changed.  (That
774	 * matters only if we enumerate the map on a readdir, which
775	 * isn't the case for a directory of the sort described above,
776	 * although, if it wasn't a map of that sort before, but is
777	 * one now, that could also change what's displayed, so we
778	 * deliver a notification in that case.)
779	 */
780	if (vnode_ismonitored(fnip->fi_rootvp) &&
781	    (!was_nobrowse || !auto_nobrowse(fnip->fi_rootvp))) {
782		struct vnode_attr vattr;
783
784		vfs_get_notify_attributes(&vattr);
785		auto_get_attributes(fnip->fi_rootvp, &vattr);
786		vnode_notify(fnip->fi_rootvp, VNODE_EVENT_WRITE, &vattr);
787	}
788	return (0);
789}
790
791int
792auto_start(__unused mount_t mp, __unused int flags,
793    __unused vfs_context_t context)
794{
795	return (0);
796}
797
798/* ARGSUSED */
799static int
800auto_unmount(mount_t mp, int mntflags, __unused vfs_context_t context)
801{
802	fninfo_t *fnip;
803	vnode_t rvp;
804	fnnode_t *rfnp, *fnp, *pfnp;
805	fnnode_t *myrootfnnodep;
806	int error;
807
808	fnip = vfstofni(mp);
809	AUTOFS_DPRINT((4, "auto_unmount mp %p fnip %p\n", (void *)mp,
810			(void *)fnip));
811
812	/*
813	 * forced unmount is not supported by this file system
814	 * and thus, ENOTSUP, is being returned.
815	 *
816	 * XXX - forced unmounts are done at system shutdown time;
817	 * do we need to make them work?
818	 */
819	if (mntflags & MNT_FORCE)
820		return (ENOTSUP);
821
822	rvp = fnip->fi_rootvp;
823	rfnp = vntofn(rvp);
824
825	/*
826	 * At this point, the VFS layer is holding a write lock on
827	 * mp, preventing threads from crossing through that mount
828	 * point on a lookup; there might, however, be threads that
829	 * have already crossed through that mount point and are
830	 * in the process of a lookup in this file system.
831	 *
832	 * The VFS layer has already done a vflush() that checked
833	 * all non-root, non-swap, non-system vnodes and, if any
834	 * had a zero usecount or a usecount equal to the kusecount,
835	 * waited for the iocount to drop to 0 and then reclaimed
836	 * them (and, if it's a forced unmount, for vnodes with a
837	 * a non-zero usecount, waited for the iocount to drop to
838	 * 0 and then deadfs'ed them), so, except for the root
839	 * vnode, there should be no vnodes left.
840	 *
841	 * Unfortunately, if somebody's holding onto the root
842	 * vnode, e.g. in a lookup, they could end up getting
843	 * some vnode on this file system, whether it's the
844	 * root vnode or some other vnode, and possibly getting
845	 * a usecount on it, so the vflush() didn't give us a
846	 * particularly useful guarantee.
847	 *
848	 * Holding the write lock will block any mount atop
849	 * a vnode in this file system from crossing into
850	 * this file system on a lookup.  A triggered mount
851	 * will hold a usecount on the mount point, so, if
852	 * that vnode is *not* the root vnode, a vflush()
853	 * should fail - but, again, there's no guarantee
854	 * that the mount won't be triggered *after* the
855	 * vflush() is done.  Furthermore, if it *is* being
856	 * done on the root vnode, the vflush() won't help.
857	 *
858	 * So:
859	 *
860	 *	nobody should be crossing *into* this file
861	 *	system and triggering a mount, and, if they
862	 *	do, they'll block (modulo 3424361, alas, but
863	 *	that just means they'll fail);
864	 *
865	 *	there might, however, be lookups that have
866	 *	already crossed into this file system and
867	 *	that will trigger mounts;
868	 *
869	 *	the only way to wait for all of them to
870	 *	finish would be to release the root vnode
871	 *	and then do a vflush() waiting for everything
872	 *	to drain - but if that fails we'd have to
873	 *	recreate the root vnode, and we'd have to
874	 *	make sure that the temporary lack of a
875	 *	root vnode doesn't break anything;
876	 *
877	 *	if we don't wait for anll of them to finish,
878	 *	we run the risk of dismantling this mount
879	 *	while operations are in progress on it (see
880	 *	8960014);
881	 *
882	 * Lock the fninfo, so that no mounts get started until we
883	 * release it.
884	 */
885	lck_rw_lock_exclusive(fnip->fi_rwlock);
886
887	/*
888	 * Wait for in-progress operations to finish on vnodes other than
889	 * the root vnode, and fail if any usecounts are held on any of
890	 * those vnodes.
891	 */
892	error = vflush(mp, rvp, 0);
893	if (error) {
894		lck_rw_unlock_exclusive(fnip->fi_rwlock);
895		return (error);
896	}
897
898	/*
899	 * OK, do we have any usecounts on the root vnode, other than
900	 * our own, or does it have any live subdirectories?
901	 */
902	if (vnode_isinuse(rvp, 1) || rfnp->fn_dirents != NULL) {
903		/*
904		 * Yes - drop the lock and fail.
905		 */
906		lck_rw_unlock_exclusive(fnip->fi_rwlock);
907		return (EBUSY);
908	}
909
910	/*
911	 * The root vnode is on the linked list of root fnnodes only if
912	 * this was not a trigger node. Since we have no way of knowing,
913	 * if we don't find it, then we assume it was a trigger node.
914	 */
915	myrootfnnodep = rfnp->fn_globals->fng_rootfnnodep;
916	pfnp = NULL;
917	lck_rw_lock_exclusive(myrootfnnodep->fn_rwlock);
918	fnp = myrootfnnodep->fn_dirents;
919	while (fnp != NULL) {
920		if (fnp == rfnp) {
921			/*
922			 * A check here is made to see if rvp is busy.  If
923			 * so, return EBUSY.  Otherwise proceed with
924			 * disconnecting it from the list.
925			 */
926			if (vnode_isinuse(rvp, 1) || rfnp->fn_dirents != NULL) {
927				lck_rw_unlock_exclusive(myrootfnnodep->fn_rwlock);
928				lck_rw_unlock_exclusive(fnip->fi_rwlock);
929				return (EBUSY);
930			}
931			if (pfnp)
932				pfnp->fn_next = fnp->fn_next;
933			else
934				myrootfnnodep->fn_dirents = fnp->fn_next;
935			fnp->fn_next = NULL;
936			break;
937		}
938		pfnp = fnp;
939		fnp = fnp->fn_next;
940	}
941	lck_rw_unlock_exclusive(myrootfnnodep->fn_rwlock);
942
943	/*
944	 * OK, we're committed to the unmount.  Mark the file system
945	 * as being unmounted, so that any triggered mounts done on
946	 * it will fail rather than trying to refer to the fninfo_t
947	 * for it, as we're going to release that fninfo_t.  Then drop
948	 * the write lock on the fninfo_t, so that any triggered mounts
949	 * waiting for us to release the lock can proceed, see that
950	 * MF_UNMOUNTING is set, and fail.
951	 */
952	fnip->fi_flags |= MF_UNMOUNTING;
953	lck_rw_unlock_exclusive(fnip->fi_rwlock);
954
955	assert(vnode_isinuse(rvp, 0) && !vnode_isinuse(rvp, 1));
956	assert(rfnp->fn_direntcnt == 0);
957	assert(rfnp->fn_linkcnt == 1);
958	/*
959	 * The following drops linkcnt to 0, therefore the disconnect is
960	 * not attempted when auto_inactive() is called by
961	 * vn_rele(). This is necessary because we have nothing to get
962	 * disconnected from since we're the root of the filesystem. As a
963	 * side effect the node is not freed, therefore I should free the
964	 * node here.
965	 *
966	 * XXX - I really need to think of a better way of doing this.
967	 * XXX - this is not Solaris, so maybe there is a better way.
968	 */
969	rfnp->fn_linkcnt--;
970
971	/*
972	 * release last reference to the root vnode
973	 */
974	vnode_rele(rvp);	/* release reference from auto_mount() */
975
976	/*
977	 * Wait for in-flight operations to complete on any remaining vnodes
978	 * (such as the root vnode, which we just released).
979	 *
980	 * The root vnode will be recycled in vflush() when there are
981	 * no longer any in-flight operations.
982	 */
983	vflush(mp, NULLVP, 0);
984
985	lck_rw_free(fnip->fi_rwlock, autofs_lck_grp);
986	FREE(fnip->fi_path, M_AUTOFS);
987	FREE(fnip->fi_map, M_AUTOFS);
988	FREE(fnip->fi_subdir, M_AUTOFS);
989	FREE(fnip->fi_key, M_AUTOFS);
990	FREE(fnip->fi_opts, M_AUTOFS);
991	FREE(fnip, M_AUTOFS);
992
993	/*
994	 * One fewer mounted file system.
995	 */
996	lck_mtx_lock(autofs_global_lock);
997	autofs_mounts--;
998	lck_mtx_unlock(autofs_global_lock);
999
1000	AUTOFS_DPRINT((5, "auto_unmount: return=0\n"));
1001
1002	return (0);
1003}
1004
1005/*
1006 * find root of autofs
1007 */
1008int
1009auto_root(mount_t mp, vnode_t *vpp, __unused vfs_context_t context)
1010{
1011	int error;
1012	fninfo_t *fnip;
1013
1014	fnip = vfstofni(mp);
1015	*vpp = fnip->fi_rootvp;
1016	error = vnode_get(*vpp);
1017
1018	AUTOFS_DPRINT((5, "auto_root: mp %p, *vpp %p, error %d\n",
1019	    mp, *vpp, error));
1020	return (error);
1021}
1022
1023/*
1024 * Get file system information.
1025 */
1026static int
1027auto_vfs_getattr(__unused mount_t mp, struct vfs_attr *vfap,
1028    __unused vfs_context_t context)
1029{
1030	AUTOFS_DPRINT((4, "auto_vfs_getattr %p\n", (void *)mp));
1031
1032	VFSATTR_RETURN(vfap, f_bsize, AUTOFS_BLOCKSIZE);
1033	VFSATTR_RETURN(vfap, f_iosize, 512);
1034	VFSATTR_RETURN(vfap, f_blocks, 0);
1035	VFSATTR_RETURN(vfap, f_bfree, 0);
1036	VFSATTR_RETURN(vfap, f_bavail, 0);
1037	VFSATTR_RETURN(vfap, f_files, 0);
1038	VFSATTR_RETURN(vfap, f_ffree, 0);
1039
1040	if (VFSATTR_IS_ACTIVE(vfap, f_capabilities)) {
1041		/*
1042		 * We support symlinks (although you can't create them,
1043		 * or anything else), and hard links (to the extent
1044		 * that ".." looks like a hard link to the parent).
1045		 *
1046		 * We're case-sensitive and case-preserving, and
1047		 * statfs() doesn't involve any over-the-wire ops,
1048		 * so it's fast.
1049		 *
1050		 * We set the hidden bit on some directories.
1051		 */
1052		vfap->f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] =
1053			VOL_CAP_FMT_SYMBOLICLINKS |
1054			VOL_CAP_FMT_HARDLINKS |
1055			VOL_CAP_FMT_NO_ROOT_TIMES |
1056			VOL_CAP_FMT_CASE_SENSITIVE |
1057			VOL_CAP_FMT_CASE_PRESERVING |
1058			VOL_CAP_FMT_FAST_STATFS |
1059			VOL_CAP_FMT_2TB_FILESIZE |
1060			VOL_CAP_FMT_HIDDEN_FILES;
1061		vfap->f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] =
1062			VOL_CAP_INT_ATTRLIST;
1063		vfap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
1064		vfap->f_capabilities.capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
1065
1066		vfap->f_capabilities.valid[VOL_CAPABILITIES_FORMAT] =
1067			VOL_CAP_FMT_PERSISTENTOBJECTIDS |
1068			VOL_CAP_FMT_SYMBOLICLINKS |
1069			VOL_CAP_FMT_HARDLINKS |
1070			VOL_CAP_FMT_JOURNAL |
1071			VOL_CAP_FMT_JOURNAL_ACTIVE |
1072			VOL_CAP_FMT_NO_ROOT_TIMES |
1073			VOL_CAP_FMT_SPARSE_FILES |
1074			VOL_CAP_FMT_ZERO_RUNS |
1075			VOL_CAP_FMT_CASE_SENSITIVE |
1076			VOL_CAP_FMT_CASE_PRESERVING |
1077			VOL_CAP_FMT_FAST_STATFS |
1078			VOL_CAP_FMT_2TB_FILESIZE |
1079			VOL_CAP_FMT_OPENDENYMODES |
1080			VOL_CAP_FMT_HIDDEN_FILES |
1081			VOL_CAP_FMT_PATH_FROM_ID |
1082			VOL_CAP_FMT_NO_VOLUME_SIZES;
1083		vfap->f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] =
1084			VOL_CAP_INT_SEARCHFS |
1085			VOL_CAP_INT_ATTRLIST |
1086			VOL_CAP_INT_NFSEXPORT |
1087			VOL_CAP_INT_READDIRATTR |
1088			VOL_CAP_INT_EXCHANGEDATA |
1089			VOL_CAP_INT_COPYFILE |
1090			VOL_CAP_INT_ALLOCATE |
1091			VOL_CAP_INT_VOL_RENAME |
1092			VOL_CAP_INT_ADVLOCK |
1093			VOL_CAP_INT_FLOCK |
1094			VOL_CAP_INT_EXTENDED_SECURITY |
1095			VOL_CAP_INT_USERACCESS |
1096			VOL_CAP_INT_MANLOCK |
1097			VOL_CAP_INT_EXTENDED_ATTR |
1098			VOL_CAP_INT_NAMEDSTREAMS;
1099		vfap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED1] = 0;
1100		vfap->f_capabilities.valid[VOL_CAPABILITIES_RESERVED2] = 0;
1101		VFSATTR_SET_SUPPORTED(vfap, f_capabilities);
1102	}
1103
1104	if (VFSATTR_IS_ACTIVE(vfap, f_attributes)) {
1105		vfap->f_attributes.validattr.commonattr =
1106			ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_FSID |
1107			ATTR_CMN_OBJTYPE | ATTR_CMN_OBJTAG | ATTR_CMN_OBJID |
1108			ATTR_CMN_PAROBJID |
1109			ATTR_CMN_MODTIME | ATTR_CMN_CHGTIME | ATTR_CMN_ACCTIME |
1110			ATTR_CMN_OWNERID | ATTR_CMN_GRPID | ATTR_CMN_ACCESSMASK |
1111			ATTR_CMN_FLAGS | ATTR_CMN_USERACCESS | ATTR_CMN_FILEID;
1112		vfap->f_attributes.validattr.volattr =
1113			ATTR_VOL_MOUNTPOINT | ATTR_VOL_MOUNTFLAGS |
1114			ATTR_VOL_MOUNTEDDEVICE | ATTR_VOL_CAPABILITIES |
1115			ATTR_VOL_ATTRIBUTES;
1116		vfap->f_attributes.validattr.dirattr =
1117			ATTR_DIR_LINKCOUNT | ATTR_DIR_MOUNTSTATUS;
1118		vfap->f_attributes.validattr.fileattr =
1119			ATTR_FILE_LINKCOUNT | ATTR_FILE_TOTALSIZE |
1120			ATTR_FILE_IOBLOCKSIZE | ATTR_FILE_DEVTYPE |
1121			ATTR_FILE_DATALENGTH;
1122		vfap->f_attributes.validattr.forkattr = 0;
1123
1124		vfap->f_attributes.nativeattr.commonattr =
1125			ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_FSID |
1126			ATTR_CMN_OBJTYPE | ATTR_CMN_OBJTAG | ATTR_CMN_OBJID |
1127			ATTR_CMN_PAROBJID |
1128			ATTR_CMN_MODTIME | ATTR_CMN_CHGTIME | ATTR_CMN_ACCTIME |
1129			ATTR_CMN_OWNERID | ATTR_CMN_GRPID | ATTR_CMN_ACCESSMASK |
1130			ATTR_CMN_FLAGS | ATTR_CMN_USERACCESS | ATTR_CMN_FILEID;
1131		vfap->f_attributes.nativeattr.volattr =
1132			ATTR_VOL_OBJCOUNT |
1133			ATTR_VOL_MOUNTPOINT | ATTR_VOL_MOUNTFLAGS |
1134			ATTR_VOL_MOUNTEDDEVICE | ATTR_VOL_CAPABILITIES |
1135			ATTR_VOL_ATTRIBUTES;
1136		vfap->f_attributes.nativeattr.dirattr =
1137			ATTR_DIR_MOUNTSTATUS;
1138		vfap->f_attributes.nativeattr.fileattr =
1139			ATTR_FILE_LINKCOUNT | ATTR_FILE_TOTALSIZE |
1140			ATTR_FILE_IOBLOCKSIZE | ATTR_FILE_DEVTYPE |
1141			ATTR_FILE_DATALENGTH;
1142		vfap->f_attributes.nativeattr.forkattr = 0;
1143
1144		VFSATTR_SET_SUPPORTED(vfap, f_attributes);
1145	}
1146
1147	return (0);
1148}
1149
1150/*
1151 * autofs doesn't have any data or backing store and you can't write into
1152 * any of the autofs structures, so don't do anything.
1153 */
1154static int
1155auto_sync(__unused mount_t mp, __unused int waitfor,
1156    __unused vfs_context_t context)
1157{
1158	return (0);
1159}
1160
1161/*
1162 * Look up a autofs node by node number.
1163 * Currently not supported.
1164 */
1165static int
1166auto_vget(__unused mount_t mp, __unused ino64_t ino, __unused vnode_t *vpp,
1167    __unused vfs_context_t context)
1168{
1169	return (ENOTSUP);
1170}
1171
1172static int
1173autofs_sysctl(int *name, u_int namelen, __unused user_addr_t oldp,
1174    __unused size_t *oldlenp, __unused user_addr_t newp,
1175    __unused size_t newlen, __unused vfs_context_t context)
1176{
1177	int error;
1178#ifdef DEBUG
1179	struct sysctl_req *req = NULL;
1180	uint32_t debug;
1181#endif
1182
1183	/*
1184	 * All names at this level are terminal
1185	 */
1186	if (namelen > 1)
1187		return ENOTDIR;		/* overloaded error code */
1188
1189	error = 0;
1190
1191#ifdef DEBUG
1192	req = CAST_DOWN(struct sysctl_req *, oldp);
1193#endif
1194
1195	AUTOFS_DPRINT((4, "autofs_sysctl called.\n"));
1196	switch (name[0]) {
1197#ifdef DEBUG
1198	case AUTOFS_CTL_DEBUG:
1199		error = SYSCTL_IN(req, &debug, sizeof(debug));
1200		if (error)
1201			break;
1202		auto_debug_set(debug);
1203		break;
1204#endif
1205	default:
1206		error = ENOTSUP;
1207		break;
1208	}
1209
1210	return (error);
1211}
1212
1213/*
1214 * Opening /dev/autofs when nobody else has it open makes you the
1215 * automounter, so you and all your children never trigger mounts and
1216 * never get stuck waiting for a mount to complete (as it's your job
1217 * to complete the mount.
1218 *
1219 * Opening /dev/autofs when somebody else has it open fails with EBUSY.
1220 *
1221 * Closing /dev/autofs clears automounter_pid, so nobody's the automounter;
1222 * that means if the automounter exits, it ceases to be the automounter.
1223 */
1224static d_open_t	 autofs_dev_open;
1225static d_close_t autofs_dev_close;
1226static d_ioctl_t autofs_ioctl;
1227
1228static struct cdevsw autofs_cdevsw = {
1229	autofs_dev_open,
1230	autofs_dev_close,
1231	eno_rdwrt,	/* d_read */
1232	eno_rdwrt,	/* d_write */
1233	autofs_ioctl,
1234	eno_stop,
1235	eno_reset,
1236	0,		/* struct tty ** d_ttys */
1237	eno_select,
1238	eno_mmap,
1239	eno_strat,
1240	eno_getc,
1241	eno_putc,
1242	0		/* d_type */
1243};
1244
1245static int	autofs_major = -1;
1246static void	*autofs_devfs;
1247
1248static int	automounter_pid;
1249static lck_rw_t *autofs_automounter_pid_rwlock;
1250
1251static int
1252autofs_dev_open(__unused dev_t dev, __unused int oflags, __unused int devtype,
1253    struct proc *p)
1254{
1255	lck_rw_lock_exclusive(autofs_automounter_pid_rwlock);
1256	if (automounter_pid != 0) {
1257		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
1258		return (EBUSY);
1259	}
1260	automounter_pid = proc_pid(p);
1261	lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
1262	return (0);
1263}
1264
1265static int
1266autofs_dev_close(__unused dev_t dev, __unused int flag, __unused int fmt,
1267    __unused struct proc *p)
1268{
1269	lck_rw_lock_exclusive(autofs_automounter_pid_rwlock);
1270	automounter_pid = 0;
1271	lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
1272	return (0);
1273}
1274
1275static int
1276autofs_ioctl(__unused dev_t dev, u_long cmd, __unused caddr_t data,
1277    __unused int flag, __unused struct proc *p)
1278{
1279	struct autofs_globals *fngp;
1280	fnnode_t *fnp;
1281	vnode_t vp;
1282	fninfo_t *fnip;
1283	struct timeval now;
1284	struct vnode_attr vattr;
1285	int error;
1286
1287	lck_mtx_lock(autofs_global_lock);
1288	if ((fngp = autofs_zone_get_globals()) == NULL) {
1289		fngp = autofs_zone_init();
1290		autofs_zone_set_globals(fngp);
1291	}
1292	lck_mtx_unlock(autofs_global_lock);
1293	if (fngp == NULL)
1294		return (ENOMEM);
1295
1296	switch (cmd) {
1297
1298	case AUTOFS_NOTIFYCHANGE:
1299		if (fngp != NULL) {
1300			/*
1301			 * Post a flush notification, to wake up the
1302			 * thread waiting for the flush notification.
1303			 */
1304			lck_mtx_lock(fngp->fng_flush_notification_lock);
1305			fngp->fng_flush_notification_pending = 1;
1306			wakeup(&fngp->fng_flush_notification_pending);
1307			lck_mtx_unlock(fngp->fng_flush_notification_lock);
1308
1309			/*
1310			 * Now update the mod times of the root directories
1311			 * of all indirect map mounts, and deliver change
1312			 * notifications for them if their contents can
1313			 * reflect information from Open Directory (i.e.,
1314			 * they're not nobrowse), as the information
1315			 * we're getting from Open Directory might now
1316			 * be different.
1317			 */
1318			microtime(&now);
1319			lck_rw_lock_shared(fngp->fng_rootfnnodep->fn_rwlock);
1320			for (fnp = fngp->fng_rootfnnodep->fn_dirents;
1321			    fnp != NULL; fnp = fnp->fn_next) {
1322				vp = fntovn(fnp);
1323			    	fnip = vfstofni(vnode_mount(vp));
1324			    	if (fnip->fi_flags & MF_DIRECT)
1325			    		continue;	/* direct map */
1326				fnp->fn_mtime = now;
1327				if (vnode_ismonitored(vp) &&
1328				    !auto_nobrowse(vp)) {
1329					vfs_get_notify_attributes(&vattr);
1330					auto_get_attributes(vp, &vattr);
1331					vnode_notify(vp, VNODE_EVENT_WRITE,
1332					    &vattr);
1333				}
1334			}
1335			lck_rw_unlock_shared(fngp->fng_rootfnnodep->fn_rwlock);
1336		}
1337		error = 0;
1338		break;
1339
1340	case AUTOFS_WAITFORFLUSH:
1341		lck_mtx_lock(autofs_global_lock);
1342		if (fngp == NULL)
1343			fngp = autofs_zone_init();
1344		lck_mtx_unlock(autofs_global_lock);
1345		if (fngp == NULL)
1346			return (ENOMEM);
1347
1348		/*
1349		 * Block until there's a flush notification pending or we
1350		 * get a signal.
1351		 */
1352		error = 0;
1353		lck_mtx_lock(fngp->fng_flush_notification_lock);
1354		while (error == 0 && !fngp->fng_flush_notification_pending) {
1355			error = msleep(&fngp->fng_flush_notification_pending,
1356			    fngp->fng_flush_notification_lock, PWAIT | PCATCH,
1357			    "flush notification", NULL);
1358		}
1359		fngp->fng_flush_notification_pending = 0;
1360		lck_mtx_unlock(fngp->fng_flush_notification_lock);
1361		break;
1362
1363	default:
1364		error = EINVAL;
1365		break;
1366	}
1367
1368	return (error);
1369}
1370
1371/*
1372 * Check whether this process is a automounter or an inferior of a automounter.
1373 */
1374int
1375auto_is_automounter(int pid)
1376{
1377	int is_automounter;
1378
1379	lck_rw_lock_shared(autofs_automounter_pid_rwlock);
1380	is_automounter = (automounter_pid != 0 &&
1381	    (pid == automounter_pid || proc_isinferior(pid, automounter_pid)));
1382	lck_rw_unlock_shared(autofs_automounter_pid_rwlock);
1383	return (is_automounter);
1384}
1385
1386/*
1387 * Opening /dev/autofs_nowait makes you (but not your children) a
1388 * nowait process; those processes trigger mounts, but don't wait
1389 * for them to finish - instead, they return ENOENT.  This is used
1390 * by launchd.
1391 *
1392 * Closing /dev/autofs_nowait makes you no longer a nowait process;
1393 * it's closed on exit, so if you exit, you cease to be a nowait process.
1394 */
1395static d_open_t	 autofs_nowait_dev_open;
1396static d_close_t autofs_nowait_dev_close;
1397
1398static struct cdevsw autofs_nowait_cdevsw = {
1399	autofs_nowait_dev_open,
1400	autofs_nowait_dev_close,
1401	eno_rdwrt,	/* d_read */
1402	eno_rdwrt,	/* d_write */
1403	eno_ioctl,
1404	eno_stop,
1405	eno_reset,
1406	0,		/* struct tty ** d_ttys */
1407	eno_select,
1408	eno_mmap,
1409	eno_strat,
1410	eno_getc,
1411	eno_putc,
1412	0		/* d_type */
1413};
1414
1415static int	autofs_nowait_major = -1;
1416static void	*autofs_nowait_devfs;
1417
1418/*
1419 * Structure representing a process that has registered itself as an
1420 * nowait process by opening a cloning autofs device.
1421 */
1422struct nowait_process {
1423	LIST_ENTRY(nowait_process) entries;
1424	int	pid;			/* PID of the nowait process */
1425	int	minor;			/* minor device they opened */
1426};
1427
1428static LIST_HEAD(nowaitproclist, nowait_process) nowait_processes;
1429static lck_rw_t *autofs_nowait_processes_rwlock;
1430
1431/*
1432 * Given the dev entry that's being opened, we clone the device.  This driver
1433 * doesn't actually use the dev entry, since we alreaqdy know who we are by
1434 * being called from this code.  This routine is a callback registered from
1435 * devfs_make_node_clone() in autofs_init(); its purpose is to provide a new
1436 * minor number, or to return -1, if one can't be provided.
1437 *
1438 * Parameters:	dev			The device we are cloning from
1439 *
1440 * Returns:	>= 0			A new minor device number
1441 *		-1			Error: ENOMEM ("Can't alloc device")
1442 *
1443 * NOTE:	Called with DEVFS_LOCK() held
1444 */
1445static int
1446autofs_nowait_dev_clone(__unused dev_t dev, int action)
1447{
1448	int minor;
1449	struct nowait_process *nowait_process;
1450
1451	if (action == DEVFS_CLONE_ALLOC) {
1452		minor = 0;	/* tentative choice of minor */
1453		lck_rw_lock_exclusive(autofs_nowait_processes_rwlock);
1454		LIST_FOREACH(nowait_process, &nowait_processes, entries) {
1455			if (minor < nowait_process->minor) {
1456				/*
1457				 * None of the nowait processes we've looked
1458				 * at so far have this minor, and this
1459				 * minor is less than all of the minors
1460				 * later in the list (which is always
1461				 * sorted in increasing order by minor
1462				 * device number).
1463				 *
1464				 * Therefore, it's not in use.
1465				 */
1466				break;
1467			}
1468
1469			/*
1470			 * All minors <= nowait_process->minor are in use.
1471			 * Try the next one after nowait_process->minor.
1472			 */
1473			minor = nowait_process->minor + 1;
1474		}
1475		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
1476		return (minor);
1477	}
1478
1479	return (-1);
1480}
1481
1482static int
1483autofs_nowait_dev_open(dev_t dev, __unused int oflags, __unused int devtype,
1484    struct proc *p)
1485{
1486	struct nowait_process *newnowait_process, *nowait_process, *lastnowait_process;
1487
1488	MALLOC(newnowait_process, struct nowait_process *, sizeof(*newnowait_process),
1489	    M_AUTOFS, M_WAITOK);
1490	if (newnowait_process == NULL)
1491		return (ENOMEM);
1492	newnowait_process->pid = proc_pid(p);
1493	newnowait_process->minor = minor(dev);
1494	/*
1495	 * Insert the structure in the list of nowait processes in order by
1496	 * minor device number.
1497	 */
1498	lck_rw_lock_exclusive(autofs_nowait_processes_rwlock);
1499	if (LIST_EMPTY(&nowait_processes)) {
1500		/*
1501		 * List is empty, insert at the head.
1502		 */
1503		LIST_INSERT_HEAD(&nowait_processes, newnowait_process, entries);
1504	} else {
1505		/*
1506		 * List isn't empty, insert in front of the first entry
1507		 * with a larger minor device number.
1508		 */
1509		LIST_FOREACH(nowait_process, &nowait_processes, entries) {
1510			if (newnowait_process->minor < nowait_process->minor) {
1511				/*
1512				 * This entry is the first one with a larger
1513				 * minor device number.
1514				 */
1515				LIST_INSERT_BEFORE(nowait_process,
1516				    newnowait_process, entries);
1517				goto done;
1518			}
1519			lastnowait_process = nowait_process;
1520		}
1521		/*
1522		 * lastnowait_process is the last entry in the list, and it
1523		 * doesn't have a larger minor device number than the new
1524		 * entry; insert the new entry after it.
1525		 */
1526		LIST_INSERT_AFTER(lastnowait_process, newnowait_process,
1527		    entries);
1528	}
1529done:
1530	lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
1531	return (0);
1532}
1533
1534static int
1535autofs_nowait_dev_close(dev_t dev, __unused int flag, __unused int fmt,
1536    __unused struct proc *p)
1537{
1538	struct nowait_process *nowait_process;
1539
1540	/*
1541	 * Remove the nowait_process structure for this device from the
1542	 * list of nowait processes.
1543	 */
1544	lck_rw_lock_exclusive(autofs_nowait_processes_rwlock);
1545	LIST_FOREACH(nowait_process, &nowait_processes, entries) {
1546		if (minor(dev) == nowait_process->minor) {
1547			LIST_REMOVE(nowait_process, entries);
1548			FREE(nowait_process, M_AUTOFS);
1549			break;
1550		}
1551	}
1552	lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
1553	return (0);
1554}
1555
1556/*
1557 * Check whether this process is a nowait process.
1558 */
1559int
1560auto_is_nowait_process(int pid)
1561{
1562	struct nowait_process *nowait_process;
1563
1564	lck_rw_lock_shared(autofs_nowait_processes_rwlock);
1565	LIST_FOREACH(nowait_process, &nowait_processes, entries) {
1566		if (pid == nowait_process->pid) {
1567			lck_rw_unlock_shared(autofs_nowait_processes_rwlock);
1568			return (1);
1569		}
1570	}
1571	lck_rw_unlock_shared(autofs_nowait_processes_rwlock);
1572	return (0);
1573}
1574
1575/*
1576 * Opening /dev/autofs_notrigger makes you (but not your children) a
1577 * notrigger process; those do not trigger mounts.  This is used
1578 * by srvsvc.
1579 *
1580 * Closing /dev/autofs_notrigger makes you no longer a notrigger process;
1581 * it's closed on exit, so if you exit, you cease to be a notrigger process.
1582 */
1583static d_open_t	 autofs_notrigger_dev_open;
1584static d_close_t autofs_notrigger_dev_close;
1585
1586static struct cdevsw autofs_notrigger_cdevsw = {
1587	autofs_notrigger_dev_open,
1588	autofs_notrigger_dev_close,
1589	eno_rdwrt,	/* d_read */
1590	eno_rdwrt,	/* d_write */
1591	eno_ioctl,
1592	eno_stop,
1593	eno_reset,
1594	0,		/* struct tty ** d_ttys */
1595	eno_select,
1596	eno_mmap,
1597	eno_strat,
1598	eno_getc,
1599	eno_putc,
1600	0		/* d_type */
1601};
1602
1603static int	autofs_notrigger_major = -1;
1604static void	*autofs_notrigger_devfs;
1605
1606/*
1607 * Structure representing a process that has registered itself as an
1608 * notrigger process by opening a cloning autofs device.
1609 */
1610struct notrigger_process {
1611	LIST_ENTRY(notrigger_process) entries;
1612	int	pid;			/* PID of the notrigger process */
1613	int	minor;			/* minor device they opened */
1614};
1615
1616static LIST_HEAD(notriggerproclist, notrigger_process) notrigger_processes;
1617static lck_rw_t *autofs_notrigger_processes_rwlock;
1618
1619/*
1620 * Given the dev entry that's being opened, we clone the device.  This driver
1621 * doesn't actually use the dev entry, since we alreaqdy know who we are by
1622 * being called from this code.  This routine is a callback registered from
1623 * devfs_make_node_clone() in autofs_init(); its purpose is to provide a new
1624 * minor number, or to return -1, if one can't be provided.
1625 *
1626 * Parameters:	dev			The device we are cloning from
1627 *
1628 * Returns:	>= 0			A new minor device number
1629 *		-1			Error: ENOMEM ("Can't alloc device")
1630 *
1631 * NOTE:	Called with DEVFS_LOCK() held
1632 */
1633static int
1634autofs_notrigger_dev_clone(__unused dev_t dev, int action)
1635{
1636	int minor;
1637	struct notrigger_process *notrigger_process;
1638
1639	if (action == DEVFS_CLONE_ALLOC) {
1640		minor = 0;	/* tentative choice of minor */
1641		lck_rw_lock_exclusive(autofs_notrigger_processes_rwlock);
1642		LIST_FOREACH(notrigger_process, &notrigger_processes, entries) {
1643			if (minor < notrigger_process->minor) {
1644				/*
1645				 * None of the notrigger processes we've looked
1646				 * at so far have this minor, and this
1647				 * minor is less than all of the minors
1648				 * later in the list (which is always
1649				 * sorted in increasing order by minor
1650				 * device number).
1651				 *
1652				 * Therefore, it's not in use.
1653				 */
1654				break;
1655			}
1656
1657			/*
1658			 * All minors <= notrigger_process->minor are in use.
1659			 * Try the next one after notrigger_process->minor.
1660			 */
1661			minor = notrigger_process->minor + 1;
1662		}
1663		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
1664		return (minor);
1665	}
1666
1667	return (-1);
1668}
1669
1670static int
1671autofs_notrigger_dev_open(dev_t dev, __unused int oflags, __unused int devtype,
1672    struct proc *p)
1673{
1674	struct notrigger_process *newnotrigger_process, *notrigger_process, *lastnotrigger_process;
1675
1676	MALLOC(newnotrigger_process, struct notrigger_process *, sizeof(*newnotrigger_process),
1677	    M_AUTOFS, M_WAITOK);
1678	if (newnotrigger_process == NULL)
1679		return (ENOMEM);
1680	newnotrigger_process->pid = proc_pid(p);
1681	newnotrigger_process->minor = minor(dev);
1682	/*
1683	 * Insert the structure in the list of notrigger processes in order by
1684	 * minor device number.
1685	 */
1686	lck_rw_lock_exclusive(autofs_notrigger_processes_rwlock);
1687	if (LIST_EMPTY(&notrigger_processes)) {
1688		/*
1689		 * List is empty, insert at the head.
1690		 */
1691		LIST_INSERT_HEAD(&notrigger_processes, newnotrigger_process, entries);
1692	} else {
1693		/*
1694		 * List isn't empty, insert in front of the first entry
1695		 * with a larger minor device number.
1696		 */
1697		LIST_FOREACH(notrigger_process, &notrigger_processes, entries) {
1698			if (newnotrigger_process->minor < notrigger_process->minor) {
1699				/*
1700				 * This entry is the first one with a larger
1701				 * minor device number.
1702				 */
1703				LIST_INSERT_BEFORE(notrigger_process,
1704				    newnotrigger_process, entries);
1705				goto done;
1706			}
1707			lastnotrigger_process = notrigger_process;
1708		}
1709		/*
1710		 * lastnotrigger_process is the last entry in the list, and it
1711		 * doesn't have a larger minor device number than the new
1712		 * entry; insert the new entry after it.
1713		 */
1714		LIST_INSERT_AFTER(lastnotrigger_process, newnotrigger_process,
1715		    entries);
1716	}
1717done:
1718	lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
1719	return (0);
1720}
1721
1722static int
1723autofs_notrigger_dev_close(dev_t dev, __unused int flag, __unused int fmt,
1724    __unused struct proc *p)
1725{
1726	struct notrigger_process *notrigger_process;
1727
1728	/*
1729	 * Remove the notrigger_process structure for this device from the
1730	 * list of notrigger processes.
1731	 */
1732	lck_rw_lock_exclusive(autofs_notrigger_processes_rwlock);
1733	LIST_FOREACH(notrigger_process, &notrigger_processes, entries) {
1734		if (minor(dev) == notrigger_process->minor) {
1735			LIST_REMOVE(notrigger_process, entries);
1736			FREE(notrigger_process, M_AUTOFS);
1737			break;
1738		}
1739	}
1740	lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
1741	return (0);
1742}
1743
1744/*
1745 * Check whether this process is a notrigger process.
1746 */
1747int
1748auto_is_notrigger_process(int pid)
1749{
1750	struct notrigger_process *notrigger_process;
1751
1752	/*
1753	 * automountd, and anything it runs, is a notrigger process.
1754	 */
1755	if (auto_is_automounter(pid))
1756		return (1);
1757
1758	lck_rw_lock_shared(autofs_notrigger_processes_rwlock);
1759	LIST_FOREACH(notrigger_process, &notrigger_processes, entries) {
1760		if (pid == notrigger_process->pid) {
1761			lck_rw_unlock_shared(autofs_notrigger_processes_rwlock);
1762			return (1);
1763		}
1764	}
1765	lck_rw_unlock_shared(autofs_notrigger_processes_rwlock);
1766	return (0);
1767}
1768
1769/*
1770 * Opening /dev/autofs_homedirmounter makes you (but not your children) a
1771 * homedirmounter process; those processes can perform an fcntl() to arrange
1772 * that a given autofs vnode not trigger a mount.  This is used by code
1773 * that remounts home directories, so that nobody does any automounts
1774 * while they're in the process of doing a remount.
1775 *
1776 * Closing /dev/autofs_homedirmounter makes you no longer a homedirmounter
1777 * process, which means that whatever vnode you set not to trigger a mount
1778 * reverts to triggering mounts; it's closed on exit, so if you exit, you
1779 * cease to be a homedirmounter process.
1780 */
1781static d_open_t	 autofs_homedirmounter_dev_open;
1782static d_close_t autofs_homedirmounter_dev_close;
1783
1784static struct cdevsw autofs_homedirmounter_cdevsw = {
1785	autofs_homedirmounter_dev_open,
1786	autofs_homedirmounter_dev_close,
1787	eno_rdwrt,	/* d_read */
1788	eno_rdwrt,	/* d_write */
1789	eno_ioctl,
1790	eno_stop,
1791	eno_reset,
1792	0,		/* struct tty ** d_ttys */
1793	eno_select,
1794	eno_mmap,
1795	eno_strat,
1796	eno_getc,
1797	eno_putc,
1798	0		/* d_type */
1799};
1800
1801static int	autofs_homedirmounter_major = -1;
1802static void	*autofs_homedirmounter_devfs;
1803
1804/*
1805 * Structure representing a process that has registered itself as an
1806 * homedirmounter process by opening a cloning autofs device.
1807 */
1808struct homedirmounter_process {
1809	LIST_ENTRY(homedirmounter_process) entries;
1810	int	pid;		/* PID of the homedirmounter process */
1811	int	minor;		/* minor device they opened */
1812	vnode_t	mount_point;	/* autofs vnode on which they're doing a mount, if any */
1813};
1814
1815static LIST_HEAD(homedirmounterproclist, homedirmounter_process) homedirmounter_processes;
1816static lck_rw_t *autofs_homedirmounter_processes_rwlock;
1817
1818/*
1819 * Given the dev entry that's being opened, we clone the device.  This driver
1820 * doesn't actually use the dev entry, since we alreaqdy know who we are by
1821 * being called from this code.  This routine is a callback registered from
1822 * devfs_make_node_clone() in autofs_init(); its purpose is to provide a new
1823 * minor number, or to return -1, if one can't be provided.
1824 *
1825 * Parameters:	dev			The device we are cloning from
1826 *
1827 * Returns:	>= 0			A new minor device number
1828 *		-1			Error: ENOMEM ("Can't alloc device")
1829 *
1830 * NOTE:	Called with DEVFS_LOCK() held
1831 */
1832static int
1833autofs_homedirmounter_dev_clone(__unused dev_t dev, int action)
1834{
1835	int minor;
1836	struct homedirmounter_process *homedirmounter_process;
1837
1838	if (action == DEVFS_CLONE_ALLOC) {
1839		minor = 0;	/* tentative choice of minor */
1840		lck_rw_lock_exclusive(autofs_homedirmounter_processes_rwlock);
1841		LIST_FOREACH(homedirmounter_process, &homedirmounter_processes,
1842		    entries) {
1843			if (minor < homedirmounter_process->minor) {
1844				/*
1845				 * None of the homedirmounter processes
1846				 * we've looked at so far have this minor,
1847				 * and this minor is less than all of the
1848				 * minors later in the list (which is always
1849				 * sorted in increasing order by minor
1850				 * device number).
1851				 *
1852				 * Therefore, it's not in use.
1853				 */
1854				break;
1855			}
1856
1857			/*
1858			 * All minors <= homedirmounter_process->minor are
1859			 * in use.  Try the next one after
1860			 * homedirmounter_process->minor.
1861			 */
1862			minor = homedirmounter_process->minor + 1;
1863		}
1864		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
1865		return (minor);
1866	}
1867
1868	return (-1);
1869}
1870
1871static int
1872autofs_homedirmounter_dev_open(dev_t dev, __unused int oflags, __unused int devtype,
1873    struct proc *p)
1874{
1875	struct homedirmounter_process *newhomedirmounter_process, *homedirmounter_process, *lasthomedirmounter_process;
1876
1877	MALLOC(newhomedirmounter_process, struct homedirmounter_process *,
1878	    sizeof(*newhomedirmounter_process), M_AUTOFS, M_WAITOK);
1879	if (newhomedirmounter_process == NULL)
1880		return (ENOMEM);
1881	newhomedirmounter_process->pid = proc_pid(p);
1882	newhomedirmounter_process->minor = minor(dev);
1883	newhomedirmounter_process->mount_point = NULL;
1884	/*
1885	 * Insert the structure in the list of homedirmounter processes in
1886	 * order by minor device number.
1887	 */
1888	lck_rw_lock_exclusive(autofs_homedirmounter_processes_rwlock);
1889	if (LIST_EMPTY(&homedirmounter_processes)) {
1890		/*
1891		 * List is empty, insert at the head.
1892		 */
1893		LIST_INSERT_HEAD(&homedirmounter_processes,
1894		    newhomedirmounter_process, entries);
1895	} else {
1896		/*
1897		 * List isn't empty, insert in front of the first entry
1898		 * with a larger minor device number.
1899		 */
1900		LIST_FOREACH(homedirmounter_process, &homedirmounter_processes,
1901		    entries) {
1902			if (newhomedirmounter_process->minor < homedirmounter_process->minor) {
1903				/*
1904				 * This entry is the first one with a larger
1905				 * minor device number.
1906				 */
1907				LIST_INSERT_BEFORE(homedirmounter_process,
1908				    newhomedirmounter_process, entries);
1909				goto done;
1910			}
1911			lasthomedirmounter_process = homedirmounter_process;
1912		}
1913		/*
1914		 * lasthomedirmounter_process is the last entry in the list,
1915		 * and it doesn't have a larger minor device number than the
1916		 * new entry; insert the new entry after it.
1917		 */
1918		LIST_INSERT_AFTER(lasthomedirmounter_process,
1919		    newhomedirmounter_process, entries);
1920	}
1921done:
1922	lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
1923	return (0);
1924}
1925
1926static int
1927autofs_homedirmounter_dev_close(dev_t dev, __unused int flag, __unused int fmt,
1928    __unused struct proc *p)
1929{
1930	struct homedirmounter_process *homedirmounter_process;
1931	vnode_t vp;
1932	fnnode_t *fnp;
1933
1934	/*
1935	 * Remove the homedirmounter_process structure for this device from the
1936	 * list of homedirmounter processes.
1937	 *
1938	 * If it's holding onto a mount point fnnode, mark it as no
1939	 * longer having a home directory mount in progress on it,
1940	 * and release it.
1941	 */
1942	lck_rw_lock_exclusive(autofs_homedirmounter_processes_rwlock);
1943	LIST_FOREACH(homedirmounter_process, &homedirmounter_processes,
1944	    entries) {
1945		if (minor(dev) == homedirmounter_process->minor) {
1946			LIST_REMOVE(homedirmounter_process, entries);
1947			vp = homedirmounter_process->mount_point;
1948			if (vp != NULL) {
1949				fnp = vntofn(vp);
1950
1951                                /*
1952                                 * <13595777> Keep from racing with
1953                                 * homedirmounter
1954                                 */
1955                                if (fnp->fn_flags & MF_HOMEDIRMOUNT_LOCKED) {
1956                                        lck_mtx_unlock(fnp->fn_mnt_lock);
1957                                }
1958
1959				lck_mtx_lock(fnp->fn_lock);
1960				fnp->fn_flags &= ~(MF_HOMEDIRMOUNT |
1961                                                   MF_HOMEDIRMOUNT_LOCKED);
1962				lck_mtx_unlock(fnp->fn_lock);
1963
1964				vnode_rele(vp);
1965			}
1966			FREE(homedirmounter_process, M_AUTOFS);
1967			break;
1968		}
1969	}
1970	lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
1971	return (0);
1972}
1973
1974/*
1975 * Check whether this process is a homedirmounter process and, if we are,
1976 * and we were passed a vnode_t, also check whether we're the homedirmounter
1977 * for that vnode.
1978 */
1979int
1980auto_is_homedirmounter_process(vnode_t vp, int pid)
1981{
1982	struct homedirmounter_process *homedirmounter_process;
1983	int ret;
1984
1985	lck_rw_lock_shared(autofs_homedirmounter_processes_rwlock);
1986	LIST_FOREACH(homedirmounter_process, &homedirmounter_processes,
1987	    entries) {
1988		if (pid == homedirmounter_process->pid) {
1989			ret = (vp == NULL) || (vp == homedirmounter_process->mount_point);
1990			lck_rw_unlock_shared(autofs_homedirmounter_processes_rwlock);
1991			return (ret);
1992		}
1993	}
1994	lck_rw_unlock_shared(autofs_homedirmounter_processes_rwlock);
1995	return (0);
1996}
1997
1998/*
1999 * If this is a home directory mounter process:
2000 *
2001 *	if we haven't already marked a vnode as having a home directory
2002 *	mount in progress, mark the specified vnode and remember it, and
2003 *	return 0 (unless we couldn't grab a reference to it, in which
2004 *	case we return the error);
2005 *
2006 *	if we've already marked this vnode as having a home directory
2007 *	mount in progress, return 0;
2008 *
2009 *	if we have already marked some other vnode as having a home
2010 *	directory mount in progress, return EBUSY.
2011 *
2012 * Otherwise, return EINVAL.
2013 */
2014int
2015auto_mark_vnode_homedirmount(vnode_t vp, int pid, int need_lock)
2016{
2017	struct homedirmounter_process *homedirmounter_process;
2018	int error;
2019	fnnode_t *fnp = NULL;
2020
2021	lck_rw_lock_shared(autofs_homedirmounter_processes_rwlock);
2022	LIST_FOREACH(homedirmounter_process, &homedirmounter_processes,
2023	    entries) {
2024		if (pid == homedirmounter_process->pid) {
2025			if (homedirmounter_process->mount_point != NULL) {
2026				/*
2027				 * We're already a home directory mounter
2028				 * for some mount point.  Is this that
2029				 * mount point?
2030				 */
2031				if (homedirmounter_process->mount_point == vp) {
2032					/*
2033					 * Yes - not an error.  (That means
2034					 * we don't have to avoid doing the
2035					 * "make me the home directory
2036					 * mounter" fsctl if we already
2037					 * became the home directory mounter
2038					 * as a result of doing an unmount.)
2039					 */
2040					error = 0;
2041				} else {
2042					/*
2043					 * No - that's an error, as we can
2044					 * only be the home directory mounter
2045					 * for one vnode at a time.
2046					 */
2047					error = EBUSY;
2048				}
2049			} else {
2050				/*
2051				 * We're not a home directory mounter for
2052				 * a mount point.  Make us the home
2053				 * directory mounter for this vnode.
2054				 * Attempt to grab a reference to it,
2055				 * as we'll be storing a pointer to it.
2056				 */
2057				error = vnode_ref(vp);
2058				if (error == 0) {
2059					/*
2060					 * We succeeded.
2061					 */
2062					fnp = vntofn(vp);
2063					lck_mtx_lock(fnp->fn_lock);
2064					fnp->fn_flags |= MF_HOMEDIRMOUNT;
2065					lck_mtx_unlock(fnp->fn_lock);
2066					homedirmounter_process->mount_point = vp;
2067				}
2068			}
2069			lck_rw_unlock_shared(autofs_homedirmounter_processes_rwlock);
2070			return (error);
2071		}
2072	}
2073	lck_rw_unlock_shared(autofs_homedirmounter_processes_rwlock);
2074
2075        if ((fnp != NULL) && (need_lock)) {
2076                /*
2077                 * <13595777> homedirmounter is getting ready to do a
2078                 * mount. To keep from racing with an autofs mount already
2079                 * in progress, take the fn_mnt_lock. This lock will be freed
2080                 * in autofs_homedirmounter_dev_close(). Its expected that
2081                 * homedirmounter will open the magic autofs dev, do the magic
2082                 * fsctl, then close the magic autofs dev.
2083                 */
2084                lck_mtx_lock(fnp->fn_mnt_lock);
2085
2086                lck_mtx_lock(fnp->fn_lock);
2087                fnp->fn_flags |= MF_HOMEDIRMOUNT_LOCKED;
2088                lck_mtx_unlock(fnp->fn_lock);
2089        }
2090
2091	return (EINVAL);
2092}
2093
2094/*
2095 * Opening /dev/autofs_control when nobody else has it open lets you perform
2096 * various ioctls to control autofs.
2097 *
2098 * Opening /dev/autofs when somebody else has it open fails with EBUSY.
2099 * This is used to ensure that only one instance of the automount command
2100 * is running at a time.
2101 */
2102static d_open_t	 auto_control_dev_open;
2103static d_close_t auto_control_dev_close;
2104static d_ioctl_t auto_control_ioctl;
2105
2106static struct cdevsw autofs_control_cdevsw = {
2107	auto_control_dev_open,
2108	auto_control_dev_close,
2109	eno_rdwrt,	/* d_read */
2110	eno_rdwrt,	/* d_write */
2111	auto_control_ioctl,
2112	eno_stop,
2113	eno_reset,
2114	0,		/* struct tty ** d_ttys */
2115	eno_select,
2116	eno_mmap,
2117	eno_strat,
2118	eno_getc,
2119	eno_putc,
2120	0		/* d_type */
2121};
2122
2123static int	autofs_control_major = -1;
2124static void	*autofs_control_devfs;
2125
2126static int	autofs_control_isopen;
2127static lck_mtx_t *autofs_control_isopen_lock;
2128
2129static int
2130auto_control_dev_open(__unused dev_t dev, __unused int oflags,
2131    __unused int devtype, __unused struct proc *p)
2132{
2133	lck_mtx_lock(autofs_control_isopen_lock);
2134	if (autofs_control_isopen) {
2135		lck_mtx_unlock(autofs_control_isopen_lock);
2136		return (EBUSY);
2137	}
2138	autofs_control_isopen = 1;
2139	lck_mtx_unlock(autofs_control_isopen_lock);
2140	return (0);
2141}
2142
2143static int
2144auto_control_dev_close(__unused dev_t dev, __unused int flag,
2145    __unused int fmt, __unused struct proc *p)
2146{
2147	lck_mtx_lock(autofs_control_isopen_lock);
2148	autofs_control_isopen = 0;
2149	lck_mtx_unlock(autofs_control_isopen_lock);
2150	return (0);
2151}
2152
2153static int
2154auto_control_ioctl(__unused dev_t dev, u_long cmd, caddr_t data,
2155    __unused int flag, __unused proc_t p)
2156{
2157	struct autofs_globals *fngp;
2158	int error;
2159	struct autofs_update_args_32 *update_argsp_32;
2160	struct autofs_update_args_64 update_args;
2161	mount_t mp;
2162	fninfo_t *fnip;
2163
2164	lck_mtx_lock(autofs_global_lock);
2165	if ((fngp = autofs_zone_get_globals()) == NULL) {
2166		fngp = autofs_zone_init();
2167		autofs_zone_set_globals(fngp);
2168	}
2169	lck_mtx_unlock(autofs_global_lock);
2170	if (fngp == NULL)
2171		return (ENOMEM);
2172
2173	switch (cmd) {
2174
2175	case AUTOFS_SET_MOUNT_TO:
2176		trigger_set_mount_to(*(int *)data);
2177		error = 0;
2178		break;
2179
2180	case AUTOFS_UPDATE_OPTIONS_32:
2181		update_argsp_32 = (struct autofs_update_args_32 *)data;
2182		update_args.fsid = update_argsp_32->fsid;
2183		update_args.opts = CAST_USER_ADDR_T(update_argsp_32->opts);
2184		update_args.map = CAST_USER_ADDR_T(update_argsp_32->map);
2185		update_args.mntflags = update_argsp_32->mntflags;
2186		update_args.direct = update_argsp_32->direct;
2187		update_args.node_type = update_argsp_32->node_type;
2188		error = auto_update_options(&update_args);
2189		break;
2190
2191	case AUTOFS_UPDATE_OPTIONS_64:
2192		error = auto_update_options((struct autofs_update_args_64 *)data);
2193		break;
2194
2195	case AUTOFS_NOTIFYCHANGE:
2196		if (fngp != NULL) {
2197			/*
2198			 * Post a flush notification, to provoke the
2199			 * automounter to flush its cache.
2200			 */
2201			lck_mtx_lock(fngp->fng_flush_notification_lock);
2202			fngp->fng_flush_notification_pending = 1;
2203			wakeup(&fngp->fng_flush_notification_pending);
2204			lck_mtx_unlock(fngp->fng_flush_notification_lock);
2205		}
2206		error = 0;
2207		break;
2208
2209	case AUTOFS_UNMOUNT:
2210		/*
2211		 * Mark this as being unmounted, so that we return ENOENT
2212		 * for any lookups under it (for an indirect map), and
2213		 * then try to unmount it.
2214		 *
2215		 * We fail lookups under it so that nobody creates
2216		 * triggers under us while we're being unmounted,
2217		 * as that can cause the root vnode of the indirect
2218		 * map to have links to it while it's being removed
2219		 * from the list of autofs mounts, causing 6491044.
2220		 * and to prevent the deadlock described below.
2221		 *
2222		 * Given that this trigger isn't supposed to be
2223		 * there in the first place, lookups under it
2224		 * should fail in any case.
2225		 *
2226		 * XXX - does that still apply?
2227		 */
2228		error = 0;
2229		if (fngp != NULL) {
2230			mp = vfs_getvfs((fsid_t *)data);
2231			if (mp == NULL) {
2232				error = ENOENT;
2233				break;
2234			}
2235			if (!auto_is_autofs(mp)) {
2236				error = EINVAL;
2237				break;
2238			}
2239			fnip = vfstofni(mp);
2240
2241			/*
2242			 * Mark this as being unmounted.
2243			 */
2244			lck_rw_lock_exclusive(fnip->fi_rwlock);
2245			fnip->fi_flags |= MF_UNMOUNTING;
2246			lck_rw_unlock_exclusive(fnip->fi_rwlock);
2247
2248			/*
2249			 * Unmount the file system with the specified
2250			 * fsid; that will provoke an unmount of
2251			 * all triggered mounts below it.
2252			 */
2253			error = vfs_unmountbyfsid((fsid_t *)data, MNT_NOBLOCK,
2254			    vfs_context_current());
2255
2256			/*
2257			 * If that failed, we're no longer in the middle
2258			 * of unmounting it.  (If it succeeded, it no
2259			 * longer exists, so we can't unmark it as being
2260			 * in the middle of being unmounted.)
2261			 */
2262			if (error != 0) {
2263				lck_rw_lock_exclusive(fnip->fi_rwlock);
2264				fnip->fi_flags &= ~MF_UNMOUNTING;
2265				lck_rw_unlock_exclusive(fnip->fi_rwlock);
2266			}
2267		}
2268		break;
2269
2270	case AUTOFS_UNMOUNT_TRIGGERED:
2271		unmount_triggered_mounts(1);
2272		error = 0;
2273		break;
2274
2275	default:
2276		error = EINVAL;
2277		break;
2278	}
2279
2280	return (error);
2281}
2282
2283/*
2284 * Initialize the filesystem
2285 */
2286__private_extern__ int
2287auto_module_start(__unused kmod_info_t *ki, __unused void *data)
2288{
2289	errno_t error;
2290
2291	/*
2292	 * Set up the lock group and the global locks.
2293	 */
2294	autofs_lck_grp = lck_grp_alloc_init("autofs", NULL);
2295	if (autofs_lck_grp == NULL) {
2296		IOLog("auto_module_start: Couldn't create autofs lock group\n");
2297		goto fail;
2298	}
2299	autofs_global_lock = lck_mtx_alloc_init(autofs_lck_grp, LCK_ATTR_NULL);
2300	if (autofs_global_lock == NULL) {
2301		IOLog("auto_module_start: Couldn't create autofs global lock\n");
2302		goto fail;
2303	}
2304	autofs_nodeid_lock = lck_mtx_alloc_init(autofs_lck_grp, LCK_ATTR_NULL);
2305	if (autofs_nodeid_lock == NULL) {
2306		IOLog("auto_module_start: Couldn't create autofs node ID lock\n");
2307		goto fail;
2308	}
2309	autofs_automounter_pid_rwlock = lck_rw_alloc_init(autofs_lck_grp, NULL);
2310	if (autofs_automounter_pid_rwlock == NULL) {
2311		IOLog("auto_module_start: Couldn't create autofs automounter pid lock\n");
2312		goto fail;
2313	}
2314	autofs_nowait_processes_rwlock = lck_rw_alloc_init(autofs_lck_grp, NULL);
2315	if (autofs_nowait_processes_rwlock == NULL) {
2316		IOLog("auto_module_start: Couldn't create autofs nowait_processes list lock\n");
2317		goto fail;
2318	}
2319	autofs_notrigger_processes_rwlock = lck_rw_alloc_init(autofs_lck_grp, NULL);
2320	if (autofs_notrigger_processes_rwlock == NULL) {
2321		IOLog("auto_module_start: Couldn't create autofs notrigger_processes list lock\n");
2322		goto fail;
2323	}
2324	autofs_homedirmounter_processes_rwlock = lck_rw_alloc_init(autofs_lck_grp, NULL);
2325	if (autofs_homedirmounter_processes_rwlock == NULL) {
2326		IOLog("auto_module_start: Couldn't create autofs homedirmounter_processes list lock\n");
2327		goto fail;
2328	}
2329	autofs_control_isopen_lock = lck_mtx_alloc_init(autofs_lck_grp, LCK_ATTR_NULL);
2330	if (autofs_control_isopen_lock == NULL) {
2331		IOLog("auto_module_start: Couldn't create autofs control device lock\n");
2332		goto fail;
2333	}
2334
2335	/*
2336	 * Add the autofs device.
2337	 */
2338	autofs_major = cdevsw_add(-1, &autofs_cdevsw);
2339	if (autofs_major == -1) {
2340		IOLog("auto_module_start: cdevsw_add failed on autofs device\n");
2341		goto fail;
2342	}
2343	autofs_devfs = devfs_make_node(makedev(autofs_major, 0),
2344	    DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0600, AUTOFS_DEVICE);
2345	if (autofs_devfs == NULL) {
2346		IOLog("auto_module_start: devfs_make_node failed on autofs device\n");
2347		goto fail;
2348	}
2349
2350	/*
2351	 * Add the autofs nowait device.  Everybody's allowed to open it.
2352	 */
2353	autofs_nowait_major = cdevsw_add(-1, &autofs_nowait_cdevsw);
2354	if (autofs_nowait_major == -1) {
2355		IOLog("auto_module_start: cdevsw_add failed on autofs_nowait device\n");
2356		goto fail;
2357	}
2358	autofs_nowait_devfs = devfs_make_node_clone(makedev(autofs_nowait_major, 0),
2359	    DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, autofs_nowait_dev_clone,
2360	    AUTOFS_NOWAIT_DEVICE);
2361	if (autofs_nowait_devfs == NULL) {
2362		IOLog("auto_module_start: devfs_make_node failed on autofs nowait device\n");
2363		goto fail;
2364	}
2365
2366	/*
2367	 * Add the autofs notrigger device.  Everybody's allowed to open it.
2368	 */
2369	autofs_notrigger_major = cdevsw_add(-1, &autofs_notrigger_cdevsw);
2370	if (autofs_notrigger_major == -1) {
2371		IOLog("auto_module_start: cdevsw_add failed on autofs_notrigger device\n");
2372		goto fail;
2373	}
2374	autofs_notrigger_devfs = devfs_make_node_clone(makedev(autofs_notrigger_major, 0),
2375	    DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, autofs_notrigger_dev_clone,
2376	    AUTOFS_NOTRIGGER_DEVICE);
2377	if (autofs_notrigger_devfs == NULL) {
2378		IOLog("auto_module_start: devfs_make_node failed on autofs notrigger device\n");
2379		goto fail;
2380	}
2381
2382	/*
2383	 * Add the autofs homedirmounter device.
2384	 */
2385	autofs_homedirmounter_major = cdevsw_add(-1, &autofs_homedirmounter_cdevsw);
2386	if (autofs_homedirmounter_major == -1) {
2387		IOLog("auto_module_start: cdevsw_add failed on autofs_homedirmounter device\n");
2388		goto fail;
2389	}
2390	autofs_homedirmounter_devfs = devfs_make_node_clone(makedev(autofs_homedirmounter_major, 0),
2391	    DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, autofs_homedirmounter_dev_clone,
2392	    AUTOFS_HOMEDIRMOUNTER_DEVICE);
2393	if (autofs_homedirmounter_devfs == NULL) {
2394		IOLog("auto_module_start: devfs_make_node failed on autofs homedirmounter device\n");
2395		goto fail;
2396	}
2397
2398	/*
2399	 * Add the autofs control device.
2400	 */
2401	autofs_control_major = cdevsw_add(-1, &autofs_control_cdevsw);
2402	if (autofs_control_major == -1) {
2403		IOLog("auto_module_start: cdevsw_add failed on autofs control device\n");
2404		goto fail;
2405	}
2406	autofs_control_devfs = devfs_make_node(makedev(autofs_control_major, 0),
2407	    DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0600, AUTOFS_CONTROL_DEVICE);
2408	if (autofs_control_devfs == NULL) {
2409		IOLog("auto_module_start: devfs_make_node failed on autofs control device\n");
2410		goto fail;
2411	}
2412
2413	/*
2414	 * Register the file system.
2415	 */
2416	error = vfs_fsadd(&autofs_fsentry, &auto_vfsconf);
2417	if (error != 0) {
2418		IOLog("auto_module_start: Error %d from vfs_fsadd\n",
2419		    error);
2420		goto fail;
2421	}
2422
2423	return (KERN_SUCCESS);
2424
2425fail:
2426	if (autofs_control_devfs != NULL)
2427		devfs_remove(autofs_control_devfs);
2428	if (autofs_control_major != -1) {
2429		if (cdevsw_remove(autofs_control_major, &autofs_control_cdevsw) == -1)
2430			panic("auto_module_start: can't remove autofs control device from cdevsw");
2431	}
2432	if (autofs_nowait_devfs != NULL)
2433		devfs_remove(autofs_nowait_devfs);
2434	if (autofs_nowait_major != -1) {
2435		if (cdevsw_remove(autofs_nowait_major, &autofs_nowait_cdevsw) == -1)
2436			panic("auto_module_start: can't remove autofs nowait device from cdevsw");
2437	}
2438	if (autofs_notrigger_devfs != NULL)
2439		devfs_remove(autofs_notrigger_devfs);
2440	if (autofs_notrigger_major != -1) {
2441		if (cdevsw_remove(autofs_notrigger_major, &autofs_notrigger_cdevsw) == -1)
2442			panic("auto_module_start: can't remove autofs notrigger device from cdevsw");
2443	}
2444	if (autofs_homedirmounter_devfs != NULL)
2445		devfs_remove(autofs_homedirmounter_devfs);
2446	if (autofs_homedirmounter_major != -1) {
2447		if (cdevsw_remove(autofs_homedirmounter_major, &autofs_homedirmounter_cdevsw) == -1)
2448			panic("auto_module_start: can't remove autofs homedirmounter device from cdevsw");
2449	}
2450	if (autofs_devfs != NULL)
2451		devfs_remove(autofs_devfs);
2452	if (autofs_major != -1) {
2453		if (cdevsw_remove(autofs_major, &autofs_cdevsw) == -1)
2454			panic("auto_module_start: can't remove autofs device from cdevsw");
2455	}
2456	if (autofs_control_isopen_lock != NULL)
2457		lck_mtx_free(autofs_control_isopen_lock, autofs_lck_grp);
2458	if (autofs_nowait_processes_rwlock != NULL)
2459		lck_rw_free(autofs_nowait_processes_rwlock, autofs_lck_grp);
2460	if (autofs_notrigger_processes_rwlock != NULL)
2461		lck_rw_free(autofs_notrigger_processes_rwlock, autofs_lck_grp);
2462	if (autofs_homedirmounter_processes_rwlock != NULL)
2463		lck_rw_free(autofs_homedirmounter_processes_rwlock, autofs_lck_grp);
2464	if (autofs_automounter_pid_rwlock != NULL)
2465		lck_rw_free(autofs_automounter_pid_rwlock, autofs_lck_grp);
2466	if (autofs_nodeid_lock != NULL)
2467		lck_mtx_free(autofs_nodeid_lock, autofs_lck_grp);
2468	if (autofs_global_lock != NULL)
2469		lck_mtx_free(autofs_global_lock, autofs_lck_grp);
2470	if (autofs_lck_grp != NULL)
2471		lck_grp_free(autofs_lck_grp);
2472	return (KERN_FAILURE);
2473}
2474
2475__private_extern__ int
2476auto_module_stop(__unused kmod_info_t *ki, __unused void *data)
2477{
2478	struct autofs_globals *fngp;
2479	int error;
2480
2481	lck_mtx_lock(autofs_global_lock);
2482	lck_mtx_lock(autofs_nodeid_lock);
2483	lck_rw_lock_exclusive(autofs_automounter_pid_rwlock);
2484	lck_rw_lock_exclusive(autofs_nowait_processes_rwlock);
2485	lck_rw_lock_exclusive(autofs_notrigger_processes_rwlock);
2486	lck_rw_lock_exclusive(autofs_homedirmounter_processes_rwlock);
2487	lck_mtx_lock(autofs_control_isopen_lock);
2488	if (autofs_mounts != 0) {
2489		AUTOFS_DPRINT((2, "auto_module_stop: Can't remove, still %u mounts active\n", autofs_mounts));
2490		lck_mtx_unlock(autofs_control_isopen_lock);
2491		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2492		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2493		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2494		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2495		lck_mtx_unlock(autofs_nodeid_lock);
2496		lck_mtx_unlock(autofs_global_lock);
2497		return (KERN_NO_ACCESS);
2498	}
2499	if (automounter_pid != 0) {
2500		AUTOFS_DPRINT((2, "auto_module_stop: Can't remove, automounter still running\n"));
2501		lck_mtx_unlock(autofs_control_isopen_lock);
2502		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2503		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2504		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2505		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2506		lck_mtx_unlock(autofs_nodeid_lock);
2507		lck_mtx_unlock(autofs_global_lock);
2508		return (KERN_NO_ACCESS);
2509	}
2510	if (!LIST_EMPTY(&nowait_processes)) {
2511		AUTOFS_DPRINT((2, "auto_module_stop: Can't remove, still nowait processes running\n"));
2512		lck_mtx_unlock(autofs_control_isopen_lock);
2513		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2514		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2515		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2516		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2517		lck_mtx_unlock(autofs_nodeid_lock);
2518		lck_mtx_unlock(autofs_global_lock);
2519		return (KERN_NO_ACCESS);
2520	}
2521	if (!LIST_EMPTY(&notrigger_processes)) {
2522		AUTOFS_DPRINT((2, "auto_module_stop: Can't remove, still notrigger processes running\n"));
2523		lck_mtx_unlock(autofs_control_isopen_lock);
2524		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2525		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2526		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2527		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2528		lck_mtx_unlock(autofs_nodeid_lock);
2529		lck_mtx_unlock(autofs_global_lock);
2530		return (KERN_NO_ACCESS);
2531	}
2532	if (!LIST_EMPTY(&homedirmounter_processes)) {
2533		AUTOFS_DPRINT((2, "auto_module_stop: Can't remove, still homedirmounter processes running\n"));
2534		lck_mtx_unlock(autofs_control_isopen_lock);
2535		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2536		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2537		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2538		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2539		lck_mtx_unlock(autofs_nodeid_lock);
2540		lck_mtx_unlock(autofs_global_lock);
2541		return (KERN_NO_ACCESS);
2542	}
2543	if (autofs_control_isopen) {
2544		AUTOFS_DPRINT((2, "auto_module_stop: Can't remove, automount command is running\n"));
2545		lck_mtx_unlock(autofs_control_isopen_lock);
2546		lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2547		lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2548		lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2549		lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2550		lck_mtx_unlock(autofs_nodeid_lock);
2551		lck_mtx_unlock(autofs_global_lock);
2552		return (KERN_NO_ACCESS);
2553	}
2554	AUTOFS_DPRINT((10, "auto_module_stop: removing autofs from vfs conf. list...\n"));
2555
2556	fngp = autofs_zone_get_globals();
2557	if (fngp) {
2558		assert(fngp->fng_fnnode_count == 1);
2559	}
2560
2561	error = vfs_fsremove(auto_vfsconf);
2562	if (error) {
2563		IOLog("auto_module_stop: Error %d from vfs_remove\n",
2564		    error);
2565		return (KERN_FAILURE);
2566	}
2567
2568	devfs_remove(autofs_devfs);
2569	autofs_devfs = NULL;
2570	if (cdevsw_remove(autofs_major, &autofs_cdevsw) == -1)
2571		panic("auto_module_stop: can't remove autofs device from cdevsw");
2572	autofs_major = -1;
2573	devfs_remove(autofs_nowait_devfs);
2574	autofs_nowait_devfs = NULL;
2575	if (cdevsw_remove(autofs_nowait_major, &autofs_nowait_cdevsw) == -1)
2576		panic("auto_module_stop: can't remove autofs nowait device from cdevsw");
2577	autofs_nowait_major = -1;
2578	devfs_remove(autofs_notrigger_devfs);
2579	autofs_notrigger_devfs = NULL;
2580	if (cdevsw_remove(autofs_notrigger_major, &autofs_notrigger_cdevsw) == -1)
2581		panic("auto_module_stop: can't remove autofs notrigger device from cdevsw");
2582	autofs_notrigger_major = -1;
2583	devfs_remove(autofs_homedirmounter_devfs);
2584	autofs_homedirmounter_devfs = NULL;
2585	if (cdevsw_remove(autofs_homedirmounter_major, &autofs_homedirmounter_cdevsw) == -1)
2586		panic("auto_module_stop: can't remove autofs homedirmounter device from cdevsw");
2587	autofs_homedirmounter_major = -1;
2588	devfs_remove(autofs_control_devfs);
2589	autofs_control_devfs = NULL;
2590	if (cdevsw_remove(autofs_control_major, &autofs_control_cdevsw) == -1)
2591		panic("auto_module_start: can't remove autofs control device from cdevsw");
2592
2593	if (fngp) {
2594		/*
2595		 * Free up the root fnnode.
2596		 */
2597		FREE(fngp->fng_rootfnnodep->fn_name, M_AUTOFS);
2598		FREE(fngp->fng_rootfnnodep, M_AUTOFS);
2599
2600		lck_mtx_free(fngp->fng_flush_notification_lock, autofs_lck_grp);
2601
2602                if (fngp->fng_rootfnnodep->fn_mnt_lock != NULL)
2603			lck_mtx_free(fngp->fng_rootfnnodep->fn_mnt_lock, autofs_lck_grp);
2604		if (fngp->fng_rootfnnodep->fn_rwlock != NULL)
2605			lck_rw_free(fngp->fng_rootfnnodep->fn_rwlock, autofs_lck_grp);
2606		if (fngp->fng_rootfnnodep->fn_lock != NULL)
2607			lck_mtx_free(fngp->fng_rootfnnodep->fn_lock, autofs_lck_grp);
2608
2609                FREE(fngp, M_AUTOFS);
2610        }
2611
2612	lck_mtx_unlock(autofs_nodeid_lock);
2613	lck_mtx_free(autofs_nodeid_lock, autofs_lck_grp);
2614	lck_mtx_unlock(autofs_global_lock);
2615	lck_mtx_free(autofs_global_lock, autofs_lck_grp);
2616	lck_rw_unlock_exclusive(autofs_automounter_pid_rwlock);
2617	lck_rw_free(autofs_automounter_pid_rwlock, autofs_lck_grp);
2618	lck_rw_unlock_exclusive(autofs_nowait_processes_rwlock);
2619	lck_rw_free(autofs_nowait_processes_rwlock, autofs_lck_grp);
2620	lck_rw_unlock_exclusive(autofs_notrigger_processes_rwlock);
2621	lck_rw_free(autofs_notrigger_processes_rwlock, autofs_lck_grp);
2622	lck_rw_unlock_exclusive(autofs_homedirmounter_processes_rwlock);
2623	lck_rw_free(autofs_homedirmounter_processes_rwlock, autofs_lck_grp);
2624	lck_mtx_unlock(autofs_control_isopen_lock);
2625	lck_mtx_free(autofs_control_isopen_lock, autofs_lck_grp);
2626	lck_grp_free(autofs_lck_grp);
2627
2628	return (KERN_SUCCESS);
2629}
2630