autofs.c revision 274234
1/*-
2 * Copyright (c) 2014 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30/*-
31 * Copyright (c) 1989, 1991, 1993, 1995
32 *	The Regents of the University of California.  All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley by
35 * Rick Macklem at The University of Guelph.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 4. Neither the name of the University nor the names of its contributors
46 *    may be used to endorse or promote products derived from this software
47 *    without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 */
62
63#include <sys/cdefs.h>
64 __FBSDID("$FreeBSD: stable/10/sys/fs/autofs/autofs.c 274234 2014-11-07 15:44:03Z trasz $");
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/buf.h>
69#include <sys/conf.h>
70#include <sys/dirent.h>
71#include <sys/ioccom.h>
72#include <sys/kernel.h>
73#include <sys/module.h>
74#include <sys/mount.h>
75#include <sys/refcount.h>
76#include <sys/sx.h>
77#include <sys/sysctl.h>
78#include <sys/syscallsubr.h>
79#include <sys/taskqueue.h>
80#include <sys/vnode.h>
81#include <machine/atomic.h>
82#include <vm/uma.h>
83
84#include <fs/autofs/autofs.h>
85#include <fs/autofs/autofs_ioctl.h>
86
87MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
88
89uma_zone_t autofs_request_zone;
90uma_zone_t autofs_node_zone;
91
92static int	autofs_open(struct cdev *dev, int flags, int fmt,
93		    struct thread *td);
94static int	autofs_close(struct cdev *dev, int flag, int fmt,
95		    struct thread *td);
96static int	autofs_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
97		    int mode, struct thread *td);
98
99static struct cdevsw autofs_cdevsw = {
100     .d_version = D_VERSION,
101     .d_open   = autofs_open,
102     .d_close   = autofs_close,
103     .d_ioctl   = autofs_ioctl,
104     .d_name    = "autofs",
105};
106
107/*
108 * List of signals that can interrupt an autofs trigger.  Might be a good
109 * idea to keep it synchronised with list in sys/fs/nfs/nfs_commonkrpc.c.
110 */
111int autofs_sig_set[] = {
112	SIGINT,
113	SIGTERM,
114	SIGHUP,
115	SIGKILL,
116	SIGQUIT
117};
118
119struct autofs_softc	*autofs_softc;
120
121SYSCTL_NODE(_vfs, OID_AUTO, autofs, CTLFLAG_RD, 0, "Automounter filesystem");
122int autofs_debug = 1;
123TUNABLE_INT("vfs.autofs.debug", &autofs_debug);
124SYSCTL_INT(_vfs_autofs, OID_AUTO, debug, CTLFLAG_RWTUN,
125    &autofs_debug, 1, "Enable debug messages");
126int autofs_mount_on_stat = 0;
127TUNABLE_INT("vfs.autofs.mount_on_stat", &autofs_mount_on_stat);
128SYSCTL_INT(_vfs_autofs, OID_AUTO, mount_on_stat, CTLFLAG_RWTUN,
129    &autofs_mount_on_stat, 0, "Trigger mount on stat(2) on mountpoint");
130int autofs_timeout = 30;
131TUNABLE_INT("vfs.autofs.timeout", &autofs_timeout);
132SYSCTL_INT(_vfs_autofs, OID_AUTO, timeout, CTLFLAG_RWTUN,
133    &autofs_timeout, 30, "Number of seconds to wait for automountd(8)");
134int autofs_cache = 600;
135TUNABLE_INT("vfs.autofs.cache", &autofs_cache);
136SYSCTL_INT(_vfs_autofs, OID_AUTO, cache, CTLFLAG_RWTUN,
137    &autofs_cache, 600, "Number of seconds to wait before reinvoking "
138    "automountd(8) for any given file or directory");
139int autofs_retry_attempts = 3;
140TUNABLE_INT("vfs.autofs.retry_attempts", &autofs_retry_attempts);
141SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_attempts, CTLFLAG_RWTUN,
142    &autofs_retry_attempts, 3, "Number of attempts before failing mount");
143int autofs_retry_delay = 1;
144TUNABLE_INT("vfs.autofs.retry_delay", &autofs_retry_delay);
145SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_delay, CTLFLAG_RWTUN,
146    &autofs_retry_delay, 1, "Number of seconds before retrying");
147int autofs_interruptible = 1;
148TUNABLE_INT("vfs.autofs.interruptible", &autofs_interruptible);
149SYSCTL_INT(_vfs_autofs, OID_AUTO, interruptible, CTLFLAG_RWTUN,
150    &autofs_interruptible, 1, "Allow requests to be interrupted by signal");
151
152int
153autofs_init(struct vfsconf *vfsp)
154{
155	int error;
156
157	KASSERT(autofs_softc == NULL,
158	    ("softc %p, should be NULL", autofs_softc));
159
160	autofs_softc = malloc(sizeof(*autofs_softc), M_AUTOFS,
161	    M_WAITOK | M_ZERO);
162
163	autofs_request_zone = uma_zcreate("autofs_request",
164	    sizeof(struct autofs_request), NULL, NULL, NULL, NULL,
165	    UMA_ALIGN_PTR, 0);
166	autofs_node_zone = uma_zcreate("autofs_node",
167	    sizeof(struct autofs_node), NULL, NULL, NULL, NULL,
168	    UMA_ALIGN_PTR, 0);
169
170	TAILQ_INIT(&autofs_softc->sc_requests);
171	cv_init(&autofs_softc->sc_cv, "autofscv");
172	sx_init(&autofs_softc->sc_lock, "autofslk");
173
174	error = make_dev_p(MAKEDEV_CHECKNAME, &autofs_softc->sc_cdev,
175	    &autofs_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "autofs");
176	if (error != 0) {
177		AUTOFS_WARN("failed to create device node, error %d", error);
178		uma_zdestroy(autofs_request_zone);
179		uma_zdestroy(autofs_node_zone);
180		free(autofs_softc, M_AUTOFS);
181
182		return (error);
183	}
184	autofs_softc->sc_cdev->si_drv1 = autofs_softc;
185
186	return (0);
187}
188
189int
190autofs_uninit(struct vfsconf *vfsp)
191{
192
193	sx_xlock(&autofs_softc->sc_lock);
194	if (autofs_softc->sc_dev_opened) {
195		sx_xunlock(&autofs_softc->sc_lock);
196		return (EBUSY);
197	}
198	if (autofs_softc->sc_cdev != NULL)
199		destroy_dev(autofs_softc->sc_cdev);
200
201	uma_zdestroy(autofs_request_zone);
202	uma_zdestroy(autofs_node_zone);
203
204	sx_xunlock(&autofs_softc->sc_lock);
205	/*
206	 * XXX: Race with open?
207	 */
208	free(autofs_softc, M_AUTOFS);
209
210	return (0);
211}
212
213bool
214autofs_ignore_thread(const struct thread *td)
215{
216	struct proc *p;
217
218	p = td->td_proc;
219
220	if (autofs_softc->sc_dev_opened == false)
221		return (false);
222
223	PROC_LOCK(p);
224	if (p->p_session->s_sid == autofs_softc->sc_dev_sid) {
225		PROC_UNLOCK(p);
226		return (true);
227	}
228	PROC_UNLOCK(p);
229
230	return (false);
231}
232
233static char *
234autofs_path(struct autofs_node *anp)
235{
236	struct autofs_mount *amp;
237	char *path, *tmp;
238
239	amp = anp->an_mount;
240
241	path = strdup("", M_AUTOFS);
242	for (; anp->an_parent != NULL; anp = anp->an_parent) {
243		tmp = malloc(strlen(anp->an_name) + strlen(path) + 2,
244		    M_AUTOFS, M_WAITOK);
245		strcpy(tmp, anp->an_name);
246		strcat(tmp, "/");
247		strcat(tmp, path);
248		free(path, M_AUTOFS);
249		path = tmp;
250	}
251
252	tmp = malloc(strlen(amp->am_mountpoint) + strlen(path) + 2,
253	    M_AUTOFS, M_WAITOK);
254	strcpy(tmp, amp->am_mountpoint);
255	strcat(tmp, "/");
256	strcat(tmp, path);
257	free(path, M_AUTOFS);
258	path = tmp;
259
260	return (path);
261}
262
263static void
264autofs_task(void *context, int pending)
265{
266	struct autofs_request *ar;
267
268	ar = context;
269
270	sx_xlock(&autofs_softc->sc_lock);
271	AUTOFS_WARN("request %d for %s timed out after %d seconds",
272	    ar->ar_id, ar->ar_path, autofs_timeout);
273	/*
274	 * XXX: EIO perhaps?
275	 */
276	ar->ar_error = ETIMEDOUT;
277	ar->ar_done = true;
278	ar->ar_in_progress = false;
279	cv_broadcast(&autofs_softc->sc_cv);
280	sx_xunlock(&autofs_softc->sc_lock);
281}
282
283bool
284autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
285{
286	int error;
287	struct autofs_mount *amp;
288
289	amp = anp->an_mount;
290
291	AUTOFS_ASSERT_UNLOCKED(amp);
292
293	/*
294	 * For top-level nodes we need to request automountd(8)
295	 * assistance even if the node is marked as cached,
296	 * but the requested subdirectory does not exist.  This
297	 * is necessary for wildcard indirect map keys to work.
298	 */
299	if (anp->an_parent == NULL && componentlen != 0) {
300		AUTOFS_SLOCK(amp);
301		error = autofs_node_find(anp, component, componentlen, NULL);
302		AUTOFS_SUNLOCK(amp);
303		if (error != 0)
304			return (false);
305	}
306
307	return (anp->an_cached);
308}
309
310static void
311autofs_cache_callout(void *context)
312{
313	struct autofs_node *anp;
314
315	anp = context;
316	anp->an_cached = false;
317}
318
319/*
320 * The set/restore sigmask functions are used to (temporarily) overwrite
321 * the thread td_sigmask during triggering.
322 */
323static void
324autofs_set_sigmask(sigset_t *oldset)
325{
326	sigset_t newset;
327	int i;
328
329	SIGFILLSET(newset);
330	/* Remove the autofs set of signals from newset */
331	PROC_LOCK(curproc);
332	mtx_lock(&curproc->p_sigacts->ps_mtx);
333	for (i = 0 ; i < sizeof(autofs_sig_set)/sizeof(int) ; i++) {
334		/*
335		 * But make sure we leave the ones already masked
336		 * by the process, i.e. remove the signal from the
337		 * temporary signalmask only if it wasn't already
338		 * in p_sigmask.
339		 */
340		if (!SIGISMEMBER(curthread->td_sigmask, autofs_sig_set[i]) &&
341		    !SIGISMEMBER(curproc->p_sigacts->ps_sigignore,
342		    autofs_sig_set[i])) {
343			SIGDELSET(newset, autofs_sig_set[i]);
344		}
345	}
346	mtx_unlock(&curproc->p_sigacts->ps_mtx);
347	kern_sigprocmask(curthread, SIG_SETMASK, &newset, oldset,
348	    SIGPROCMASK_PROC_LOCKED);
349	PROC_UNLOCK(curproc);
350}
351
352static void
353autofs_restore_sigmask(sigset_t *set)
354{
355
356	kern_sigprocmask(curthread, SIG_SETMASK, set, NULL, 0);
357}
358
359static int
360autofs_trigger_one(struct autofs_node *anp,
361    const char *component, int componentlen)
362{
363	sigset_t oldset;
364	struct autofs_mount *amp;
365	struct autofs_node *firstanp;
366	struct autofs_request *ar;
367	char *key, *path;
368	int error = 0, request_error, last;
369
370	amp = VFSTOAUTOFS(anp->an_vnode->v_mount);
371
372	sx_assert(&autofs_softc->sc_lock, SA_XLOCKED);
373
374	if (anp->an_parent == NULL) {
375		key = strndup(component, componentlen, M_AUTOFS);
376	} else {
377		for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
378		    firstanp = firstanp->an_parent)
379			continue;
380		key = strdup(firstanp->an_name, M_AUTOFS);
381	}
382
383	path = autofs_path(anp);
384
385	TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
386		if (strcmp(ar->ar_path, path) != 0)
387			continue;
388		if (strcmp(ar->ar_key, key) != 0)
389			continue;
390
391		KASSERT(strcmp(ar->ar_from, amp->am_from) == 0,
392		    ("from changed; %s != %s", ar->ar_from, amp->am_from));
393		KASSERT(strcmp(ar->ar_prefix, amp->am_prefix) == 0,
394		    ("prefix changed; %s != %s",
395		     ar->ar_prefix, amp->am_prefix));
396		KASSERT(strcmp(ar->ar_options, amp->am_options) == 0,
397		    ("options changed; %s != %s",
398		     ar->ar_options, amp->am_options));
399
400		break;
401	}
402
403	if (ar != NULL) {
404		refcount_acquire(&ar->ar_refcount);
405	} else {
406		ar = uma_zalloc(autofs_request_zone, M_WAITOK | M_ZERO);
407		ar->ar_mount = amp;
408
409		ar->ar_id =
410		    atomic_fetchadd_int(&autofs_softc->sc_last_request_id, 1);
411		strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
412		strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
413		strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
414		strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
415		strlcpy(ar->ar_options,
416		    amp->am_options, sizeof(ar->ar_options));
417
418		TIMEOUT_TASK_INIT(taskqueue_thread, &ar->ar_task, 0,
419		    autofs_task, ar);
420		error = taskqueue_enqueue_timeout(taskqueue_thread,
421		    &ar->ar_task, autofs_timeout * hz);
422		if (error != 0) {
423			AUTOFS_WARN("taskqueue_enqueue_timeout() failed "
424			    "with error %d", error);
425		}
426		refcount_init(&ar->ar_refcount, 1);
427		TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
428	}
429
430	cv_broadcast(&autofs_softc->sc_cv);
431	while (ar->ar_done == false) {
432		if (autofs_interruptible != 0) {
433			autofs_set_sigmask(&oldset);
434			error = cv_wait_sig(&autofs_softc->sc_cv,
435			    &autofs_softc->sc_lock);
436			autofs_restore_sigmask(&oldset);
437			if (error != 0) {
438				/*
439				 * XXX: For some reson this returns -1
440				 *	instead of EINTR, wtf?!
441				 */
442				error = EINTR;
443				AUTOFS_WARN("cv_wait_sig for %s failed "
444				    "with error %d", ar->ar_path, error);
445				break;
446			}
447		} else {
448			cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
449		}
450	}
451
452	request_error = ar->ar_error;
453	if (request_error != 0) {
454		AUTOFS_WARN("request for %s completed with error %d",
455		    ar->ar_path, request_error);
456	}
457
458	last = refcount_release(&ar->ar_refcount);
459	if (last) {
460		TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
461		/*
462		 * Unlock the sc_lock, so that autofs_task() can complete.
463		 */
464		sx_xunlock(&autofs_softc->sc_lock);
465		taskqueue_cancel_timeout(taskqueue_thread, &ar->ar_task, NULL);
466		taskqueue_drain_timeout(taskqueue_thread, &ar->ar_task);
467		uma_zfree(autofs_request_zone, ar);
468		sx_xlock(&autofs_softc->sc_lock);
469	}
470
471	/*
472	 * Note that we do not do negative caching on purpose.  This
473	 * way the user can retry access at any time, e.g. after fixing
474	 * the failure reason, without waiting for cache timer to expire.
475	 */
476	if (error == 0 && request_error == 0 && autofs_cache > 0) {
477		anp->an_cached = true;
478		callout_reset(&anp->an_callout, autofs_cache * hz,
479		    autofs_cache_callout, anp);
480	}
481
482	free(key, M_AUTOFS);
483	free(path, M_AUTOFS);
484
485	if (error != 0)
486		return (error);
487	return (request_error);
488}
489
490/*
491 * Send request to automountd(8) and wait for completion.
492 */
493int
494autofs_trigger(struct autofs_node *anp,
495    const char *component, int componentlen)
496{
497	int error;
498
499	for (;;) {
500		error = autofs_trigger_one(anp, component, componentlen);
501		if (error == 0) {
502			anp->an_retries = 0;
503			return (0);
504		}
505		if (error == EINTR) {
506			AUTOFS_DEBUG("trigger interrupted by signal, "
507			    "not retrying");
508			anp->an_retries = 0;
509			return (error);
510		}
511		anp->an_retries++;
512		if (anp->an_retries >= autofs_retry_attempts) {
513			AUTOFS_DEBUG("trigger failed %d times; returning "
514			    "error %d", anp->an_retries, error);
515			anp->an_retries = 0;
516			return (error);
517
518		}
519		AUTOFS_DEBUG("trigger failed with error %d; will retry in "
520		    "%d seconds, %d attempts left", error, autofs_retry_delay,
521		    autofs_retry_attempts - anp->an_retries);
522		sx_xunlock(&autofs_softc->sc_lock);
523		pause("autofs_retry", autofs_retry_delay * hz);
524		sx_xlock(&autofs_softc->sc_lock);
525	}
526}
527
528static int
529autofs_ioctl_request(struct autofs_daemon_request *adr)
530{
531	struct autofs_request *ar;
532	int error;
533
534	sx_xlock(&autofs_softc->sc_lock);
535	for (;;) {
536		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
537			if (ar->ar_done)
538				continue;
539			if (ar->ar_in_progress)
540				continue;
541
542			break;
543		}
544
545		if (ar != NULL)
546			break;
547
548		error = cv_wait_sig(&autofs_softc->sc_cv,
549		    &autofs_softc->sc_lock);
550		if (error != 0) {
551			/*
552			 * XXX: For some reson this returns -1 instead
553			 * 	of EINTR, wtf?!
554			 */
555			error = EINTR;
556			sx_xunlock(&autofs_softc->sc_lock);
557			AUTOFS_DEBUG("failed with error %d", error);
558			return (error);
559		}
560	}
561
562	ar->ar_in_progress = true;
563	sx_xunlock(&autofs_softc->sc_lock);
564
565	adr->adr_id = ar->ar_id;
566	strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
567	strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
568	strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
569	strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
570	strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
571
572	PROC_LOCK(curproc);
573	autofs_softc->sc_dev_sid = curproc->p_session->s_sid;
574	PROC_UNLOCK(curproc);
575
576	return (0);
577}
578
579static int
580autofs_ioctl_done(struct autofs_daemon_done *add)
581{
582	struct autofs_request *ar;
583
584	sx_xlock(&autofs_softc->sc_lock);
585	TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
586		if (ar->ar_id == add->add_id)
587			break;
588	}
589
590	if (ar == NULL) {
591		sx_xunlock(&autofs_softc->sc_lock);
592		AUTOFS_DEBUG("id %d not found", add->add_id);
593		return (ESRCH);
594	}
595
596	ar->ar_error = add->add_error;
597	ar->ar_done = true;
598	ar->ar_in_progress = false;
599	cv_broadcast(&autofs_softc->sc_cv);
600
601	sx_xunlock(&autofs_softc->sc_lock);
602
603	return (0);
604}
605
606static int
607autofs_open(struct cdev *dev, int flags, int fmt, struct thread *td)
608{
609
610	sx_xlock(&autofs_softc->sc_lock);
611	/*
612	 * We must never block automountd(8) and its descendants, and we use
613	 * session ID to determine that: we store session id of the process
614	 * that opened the device, and then compare it with session ids
615	 * of triggering processes.  This means running a second automountd(8)
616	 * instance would break the previous one.  The check below prevents
617	 * it from happening.
618	 */
619	if (autofs_softc->sc_dev_opened) {
620		sx_xunlock(&autofs_softc->sc_lock);
621		return (EBUSY);
622	}
623
624	autofs_softc->sc_dev_opened = true;
625	sx_xunlock(&autofs_softc->sc_lock);
626
627	return (0);
628}
629
630static int
631autofs_close(struct cdev *dev, int flag, int fmt, struct thread *td)
632{
633
634	sx_xlock(&autofs_softc->sc_lock);
635	KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
636	autofs_softc->sc_dev_opened = false;
637	sx_xunlock(&autofs_softc->sc_lock);
638
639	return (0);
640}
641
642static int
643autofs_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
644    struct thread *td)
645{
646
647	KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
648
649	switch (cmd) {
650	case AUTOFSREQUEST:
651		return (autofs_ioctl_request(
652		    (struct autofs_daemon_request *)arg));
653	case AUTOFSDONE:
654		return (autofs_ioctl_done(
655		    (struct autofs_daemon_done *)arg));
656	default:
657		AUTOFS_DEBUG("invalid cmd %lx", cmd);
658		return (EINVAL);
659	}
660}
661