xref: /dflybsd-src/sys/vfs/autofs/autofs.c (revision 54368b50e649680db895957c3cbb7581cc3a6ed0)
1e2950f41STomohiro Kusumi /*-
249837aefSTomohiro Kusumi  * Copyright (c) 2016 Tomohiro Kusumi <tkusumi@netbsd.org>
3e2950f41STomohiro Kusumi  * Copyright (c) 2016 The DragonFly Project
4e2950f41STomohiro Kusumi  * Copyright (c) 2014 The FreeBSD Foundation
5e2950f41STomohiro Kusumi  * All rights reserved.
6e2950f41STomohiro Kusumi  *
7e2950f41STomohiro Kusumi  * This software was developed by Edward Tomasz Napierala under sponsorship
8e2950f41STomohiro Kusumi  * from the FreeBSD Foundation.
9e2950f41STomohiro Kusumi  *
10e2950f41STomohiro Kusumi  * Redistribution and use in source and binary forms, with or without
11e2950f41STomohiro Kusumi  * modification, are permitted provided that the following conditions
12e2950f41STomohiro Kusumi  * are met:
13e2950f41STomohiro Kusumi  * 1. Redistributions of source code must retain the above copyright
14e2950f41STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer.
15e2950f41STomohiro Kusumi  * 2. Redistributions in binary form must reproduce the above copyright
16e2950f41STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer in the
17e2950f41STomohiro Kusumi  *    documentation and/or other materials provided with the distribution.
18e2950f41STomohiro Kusumi  *
19e2950f41STomohiro Kusumi  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20e2950f41STomohiro Kusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21e2950f41STomohiro Kusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e2950f41STomohiro Kusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23e2950f41STomohiro Kusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e2950f41STomohiro Kusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25e2950f41STomohiro Kusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26e2950f41STomohiro Kusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27e2950f41STomohiro Kusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28e2950f41STomohiro Kusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29e2950f41STomohiro Kusumi  * SUCH DAMAGE.
30e2950f41STomohiro Kusumi  *
31e2950f41STomohiro Kusumi  */
32e2950f41STomohiro Kusumi /*-
33e2950f41STomohiro Kusumi  * Copyright (c) 1989, 1991, 1993, 1995
34e2950f41STomohiro Kusumi  *	The Regents of the University of California.  All rights reserved.
35e2950f41STomohiro Kusumi  *
36e2950f41STomohiro Kusumi  * This code is derived from software contributed to Berkeley by
37e2950f41STomohiro Kusumi  * Rick Macklem at The University of Guelph.
38e2950f41STomohiro Kusumi  *
39e2950f41STomohiro Kusumi  * Redistribution and use in source and binary forms, with or without
40e2950f41STomohiro Kusumi  * modification, are permitted provided that the following conditions
41e2950f41STomohiro Kusumi  * are met:
42e2950f41STomohiro Kusumi  * 1. Redistributions of source code must retain the above copyright
43e2950f41STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer.
44e2950f41STomohiro Kusumi  * 2. Redistributions in binary form must reproduce the above copyright
45e2950f41STomohiro Kusumi  *    notice, this list of conditions and the following disclaimer in the
46e2950f41STomohiro Kusumi  *    documentation and/or other materials provided with the distribution.
473948dfa0STomohiro Kusumi  * 3. Neither the name of the University nor the names of its contributors
48e2950f41STomohiro Kusumi  *    may be used to endorse or promote products derived from this software
49e2950f41STomohiro Kusumi  *    without specific prior written permission.
50e2950f41STomohiro Kusumi  *
51e2950f41STomohiro Kusumi  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52e2950f41STomohiro Kusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53e2950f41STomohiro Kusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54e2950f41STomohiro Kusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55e2950f41STomohiro Kusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56e2950f41STomohiro Kusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57e2950f41STomohiro Kusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58e2950f41STomohiro Kusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59e2950f41STomohiro Kusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60e2950f41STomohiro Kusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61e2950f41STomohiro Kusumi  * SUCH DAMAGE.
62e2950f41STomohiro Kusumi  *
63e2950f41STomohiro Kusumi  */
64e2950f41STomohiro Kusumi 
65e2950f41STomohiro Kusumi #include <sys/kernel.h>
66e2950f41STomohiro Kusumi #include <sys/module.h>
67e2950f41STomohiro Kusumi #include <sys/sysctl.h>
68e2950f41STomohiro Kusumi #include <sys/queue.h>
69e2950f41STomohiro Kusumi #include <sys/signalvar.h>
70e2950f41STomohiro Kusumi #include <sys/refcount.h>
712137724aSTomohiro Kusumi #include <sys/spinlock2.h>
72e2950f41STomohiro Kusumi #include <sys/kern_syscall.h>
73e2950f41STomohiro Kusumi 
74e2950f41STomohiro Kusumi #include "autofs.h"
75e2950f41STomohiro Kusumi #include "autofs_ioctl.h"
76e2950f41STomohiro Kusumi 
77e2950f41STomohiro Kusumi MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
78e2950f41STomohiro Kusumi 
79e2950f41STomohiro Kusumi struct objcache *autofs_request_objcache = NULL;
80e2950f41STomohiro Kusumi struct objcache *autofs_node_objcache = NULL;
81e2950f41STomohiro Kusumi 
8241c48124STomohiro Kusumi static d_open_t		autofs_open;
8341c48124STomohiro Kusumi static d_close_t	autofs_close;
8441c48124STomohiro Kusumi static d_ioctl_t	autofs_ioctl;
85e2950f41STomohiro Kusumi 
86e2950f41STomohiro Kusumi struct dev_ops autofs_ops = {
87cb953f03STomohiro Kusumi 	{ "autofs", 0, D_MPSAFE },
88e2950f41STomohiro Kusumi 	.d_open		= autofs_open,
89e2950f41STomohiro Kusumi 	.d_close	= autofs_close,
90e2950f41STomohiro Kusumi 	.d_ioctl	= autofs_ioctl,
91e2950f41STomohiro Kusumi };
92e2950f41STomohiro Kusumi 
93e2950f41STomohiro Kusumi /*
94e2950f41STomohiro Kusumi  * List of signals that can interrupt an autofs trigger.
95e2950f41STomohiro Kusumi  */
96add630f0STomohiro Kusumi static int autofs_sig_set[] = {
97e2950f41STomohiro Kusumi 	SIGINT,
98e2950f41STomohiro Kusumi 	SIGTERM,
99e2950f41STomohiro Kusumi 	SIGHUP,
100e2950f41STomohiro Kusumi 	SIGKILL,
101e2950f41STomohiro Kusumi 	SIGQUIT
102e2950f41STomohiro Kusumi };
103e2950f41STomohiro Kusumi 
104e2950f41STomohiro Kusumi struct autofs_softc	*autofs_softc = NULL;
105e2950f41STomohiro Kusumi 
106e2950f41STomohiro Kusumi SYSCTL_NODE(_vfs, OID_AUTO, autofs, CTLFLAG_RD, 0, "Automounter filesystem");
107e2950f41STomohiro Kusumi int autofs_debug = 1;
108e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.debug", &autofs_debug);
10974565ee0STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, debug, CTLFLAG_RW, &autofs_debug, 1,
11074565ee0STomohiro Kusumi     "Enable debug messages");
111888acc39STomohiro Kusumi #if 0
112888acc39STomohiro Kusumi int autofs_mount_on_stat = 0;
113e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.mount_on_stat", &autofs_mount_on_stat);
114e2950f41STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, mount_on_stat, CTLFLAG_RW,
115888acc39STomohiro Kusumi     &autofs_mount_on_stat, 0, "Trigger mount on stat(2) on mountpoint");
116888acc39STomohiro Kusumi #endif
117e2950f41STomohiro Kusumi static int autofs_timeout = 30;
118e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.timeout", &autofs_timeout);
11974565ee0STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, timeout, CTLFLAG_RW, &autofs_timeout, 30,
12074565ee0STomohiro Kusumi     "Number of seconds to wait for automountd(8)");
121e2950f41STomohiro Kusumi static int autofs_cache = 600;
122e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.cache", &autofs_cache);
12374565ee0STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, cache, CTLFLAG_RW, &autofs_cache, 600,
12474565ee0STomohiro Kusumi     "Number of seconds to wait before reinvoking automountd(8) for any given "
12574565ee0STomohiro Kusumi     "file or directory");
126e2950f41STomohiro Kusumi static int autofs_retry_attempts = 3;
127e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.retry_attempts", &autofs_retry_attempts);
128e2950f41STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_attempts, CTLFLAG_RW,
129e2950f41STomohiro Kusumi     &autofs_retry_attempts, 3, "Number of attempts before failing mount");
130e2950f41STomohiro Kusumi static int autofs_retry_delay = 1;
131e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.retry_delay", &autofs_retry_delay);
13274565ee0STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_delay, CTLFLAG_RW, &autofs_retry_delay,
13374565ee0STomohiro Kusumi     1, "Number of seconds before retrying");
134e2950f41STomohiro Kusumi static int autofs_interruptible = 1;
135e2950f41STomohiro Kusumi TUNABLE_INT("vfs.autofs.interruptible", &autofs_interruptible);
136e2950f41STomohiro Kusumi SYSCTL_INT(_vfs_autofs, OID_AUTO, interruptible, CTLFLAG_RW,
137e2950f41STomohiro Kusumi     &autofs_interruptible, 1, "Allow requests to be interrupted by signal");
138e2950f41STomohiro Kusumi 
139e2950f41STomohiro Kusumi static __inline pid_t
proc_pgid(const struct proc * p)140e2950f41STomohiro Kusumi proc_pgid(const struct proc *p)
141e2950f41STomohiro Kusumi {
142e2950f41STomohiro Kusumi 	return (p->p_pgrp->pg_id);
143e2950f41STomohiro Kusumi }
144e2950f41STomohiro Kusumi 
145e2950f41STomohiro Kusumi static int
autofs_node_cmp(const struct autofs_node * a,const struct autofs_node * b)146e2950f41STomohiro Kusumi autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
147e2950f41STomohiro Kusumi {
148e2950f41STomohiro Kusumi 	return (strcmp(a->an_name, b->an_name));
149e2950f41STomohiro Kusumi }
150e2950f41STomohiro Kusumi 
151e2950f41STomohiro Kusumi RB_GENERATE(autofs_node_tree, autofs_node, an_link, autofs_node_cmp);
152e2950f41STomohiro Kusumi 
153e2950f41STomohiro Kusumi bool
autofs_ignore_thread(void)154e2950f41STomohiro Kusumi autofs_ignore_thread(void)
155e2950f41STomohiro Kusumi {
156e2950f41STomohiro Kusumi 	struct proc *curp = curproc;
157e2950f41STomohiro Kusumi 
158e2950f41STomohiro Kusumi 	if (autofs_softc->sc_dev_opened == false)
159e2950f41STomohiro Kusumi 		return (false);
160e2950f41STomohiro Kusumi 
161e2950f41STomohiro Kusumi 	lwkt_gettoken(&curp->p_token);
162e2950f41STomohiro Kusumi 	if (autofs_softc->sc_dev_sid == proc_pgid(curp)) {
163e2950f41STomohiro Kusumi 		lwkt_reltoken(&curp->p_token);
164e2950f41STomohiro Kusumi 		return (true);
165e2950f41STomohiro Kusumi 	}
166e2950f41STomohiro Kusumi 	lwkt_reltoken(&curp->p_token);
167e2950f41STomohiro Kusumi 
168e2950f41STomohiro Kusumi 	return (false);
169e2950f41STomohiro Kusumi }
170e2950f41STomohiro Kusumi 
171e2950f41STomohiro Kusumi char *
autofs_path(struct autofs_node * anp)172e2950f41STomohiro Kusumi autofs_path(struct autofs_node *anp)
173e2950f41STomohiro Kusumi {
174e2950f41STomohiro Kusumi 	struct autofs_mount *amp = anp->an_mount;
175a5d18315STomohiro Kusumi 	size_t len;
176e2950f41STomohiro Kusumi 	char *path, *tmp;
177e2950f41STomohiro Kusumi 
178e2950f41STomohiro Kusumi 	path = kstrdup("", M_AUTOFS);
179e2950f41STomohiro Kusumi 	for (; anp->an_parent != NULL; anp = anp->an_parent) {
180a5d18315STomohiro Kusumi 		len = strlen(anp->an_name) + strlen(path) + 2;
181a5d18315STomohiro Kusumi 		tmp = kmalloc(len, M_AUTOFS, M_WAITOK);
182a5d18315STomohiro Kusumi 		ksnprintf(tmp, len, "%s/%s", anp->an_name, path);
183e2950f41STomohiro Kusumi 		kfree(path, M_AUTOFS);
184e2950f41STomohiro Kusumi 		path = tmp;
185e2950f41STomohiro Kusumi 	}
186e2950f41STomohiro Kusumi 
187a5d18315STomohiro Kusumi 	len = strlen(amp->am_on) + strlen(path) + 2;
188a5d18315STomohiro Kusumi 	tmp = kmalloc(len, M_AUTOFS, M_WAITOK);
189a5d18315STomohiro Kusumi 	ksnprintf(tmp, len, "%s/%s", amp->am_on, path);
190e2950f41STomohiro Kusumi 	kfree(path, M_AUTOFS);
191e2950f41STomohiro Kusumi 	path = tmp;
192e2950f41STomohiro Kusumi 
193e2950f41STomohiro Kusumi 	return (path);
194e2950f41STomohiro Kusumi }
195e2950f41STomohiro Kusumi 
196e2950f41STomohiro Kusumi static void
autofs_task(void * context,int pending)197e2950f41STomohiro Kusumi autofs_task(void *context, int pending)
198e2950f41STomohiro Kusumi {
199e2950f41STomohiro Kusumi 	struct autofs_request *ar = context;
200e2950f41STomohiro Kusumi 
2012137724aSTomohiro Kusumi 	mtx_lock_ex_quick(&autofs_softc->sc_lock);
202e2950f41STomohiro Kusumi 	AUTOFS_WARN("request %d for %s timed out after %d seconds",
203e2950f41STomohiro Kusumi 	    ar->ar_id, ar->ar_path, autofs_timeout);
204e2950f41STomohiro Kusumi 
205e2950f41STomohiro Kusumi 	ar->ar_error = ETIMEDOUT;
206e2950f41STomohiro Kusumi 	ar->ar_wildcards = true;
207e2950f41STomohiro Kusumi 	ar->ar_done = true;
208e2950f41STomohiro Kusumi 	ar->ar_in_progress = false;
209e2950f41STomohiro Kusumi 	cv_broadcast(&autofs_softc->sc_cv);
2102137724aSTomohiro Kusumi 	mtx_unlock_ex(&autofs_softc->sc_lock);
211e2950f41STomohiro Kusumi }
212e2950f41STomohiro Kusumi 
213e2950f41STomohiro Kusumi bool
autofs_cached(struct autofs_node * anp,const char * component,int componentlen)214e2950f41STomohiro Kusumi autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
215e2950f41STomohiro Kusumi {
216e2950f41STomohiro Kusumi 	struct autofs_mount *amp = anp->an_mount;
217e2950f41STomohiro Kusumi 
2186be174c1STomohiro Kusumi 	KKASSERT(mtx_notlocked(&amp->am_lock));
219e2950f41STomohiro Kusumi 
220e2950f41STomohiro Kusumi 	/*
221e2950f41STomohiro Kusumi 	 * For root node we need to request automountd(8) assistance even
222e2950f41STomohiro Kusumi 	 * if the node is marked as cached, but the requested top-level
223e2950f41STomohiro Kusumi 	 * directory does not exist.  This is necessary for wildcard indirect
224e2950f41STomohiro Kusumi 	 * map keys to work.  We don't do this if we know that there are
225e2950f41STomohiro Kusumi 	 * no wildcards.
226e2950f41STomohiro Kusumi 	 */
227e2950f41STomohiro Kusumi 	if (anp->an_parent == NULL && componentlen != 0 && anp->an_wildcards) {
228662e8088STomohiro Kusumi 		int error;
229e2950f41STomohiro Kusumi 		KKASSERT(amp->am_root == anp);
230bc6139d4STomohiro Kusumi 		mtx_lock_sh_quick(&amp->am_lock);
231e2950f41STomohiro Kusumi 		error = autofs_node_find(anp, component, componentlen, NULL);
232bc6139d4STomohiro Kusumi 		mtx_unlock_sh(&amp->am_lock);
233e2950f41STomohiro Kusumi 		if (error)
234e2950f41STomohiro Kusumi 			return (false);
235e2950f41STomohiro Kusumi 	}
236e2950f41STomohiro Kusumi 
237e2950f41STomohiro Kusumi 	return (anp->an_cached);
238e2950f41STomohiro Kusumi }
239e2950f41STomohiro Kusumi 
240e2950f41STomohiro Kusumi static void
autofs_cache_callout(void * context)241e2950f41STomohiro Kusumi autofs_cache_callout(void *context)
242e2950f41STomohiro Kusumi {
243e2950f41STomohiro Kusumi 	struct autofs_node *anp = context;
244e2950f41STomohiro Kusumi 
245e2950f41STomohiro Kusumi 	autofs_node_uncache(anp);
246e2950f41STomohiro Kusumi }
247e2950f41STomohiro Kusumi 
248e2950f41STomohiro Kusumi void
autofs_flush(struct autofs_mount * amp)249e2950f41STomohiro Kusumi autofs_flush(struct autofs_mount *amp)
250e2950f41STomohiro Kusumi {
251e2950f41STomohiro Kusumi 	struct autofs_node *anp = amp->am_root;
252e2950f41STomohiro Kusumi 	struct autofs_node *child;
253e2950f41STomohiro Kusumi 
254bc6139d4STomohiro Kusumi 	mtx_lock_ex_quick(&amp->am_lock);
25574565ee0STomohiro Kusumi 	RB_FOREACH(child, autofs_node_tree, &anp->an_children)
256e2950f41STomohiro Kusumi 		autofs_node_uncache(child);
257e2950f41STomohiro Kusumi 	autofs_node_uncache(amp->am_root);
258bc6139d4STomohiro Kusumi 	mtx_unlock_ex(&amp->am_lock);
259e2950f41STomohiro Kusumi 
260e2950f41STomohiro Kusumi 	AUTOFS_DEBUG("%s flushed", amp->am_on);
261e2950f41STomohiro Kusumi }
262e2950f41STomohiro Kusumi 
263e2950f41STomohiro Kusumi /*
264e2950f41STomohiro Kusumi  * The set/restore sigmask functions are used to (temporarily) overwrite
265e2950f41STomohiro Kusumi  * the thread sigmask during triggering.
266e2950f41STomohiro Kusumi  */
267e2950f41STomohiro Kusumi static void
autofs_set_sigmask(sigset_t * oldset)268e2950f41STomohiro Kusumi autofs_set_sigmask(sigset_t *oldset)
269e2950f41STomohiro Kusumi {
270e2950f41STomohiro Kusumi 	struct lwp *lp = curthread->td_lwp;
271e2950f41STomohiro Kusumi 	sigset_t newset;
272e2950f41STomohiro Kusumi 	int i;
273e2950f41STomohiro Kusumi 
274e2950f41STomohiro Kusumi 	SIGFILLSET(newset);
275e2950f41STomohiro Kusumi 	/* Remove the autofs set of signals from newset */
276e2950f41STomohiro Kusumi 	lwkt_gettoken(&lp->lwp_token);
277b112b669STomohiro Kusumi 	for (i = 0; i < nitems(autofs_sig_set); i++) {
278e2950f41STomohiro Kusumi 		/*
279e2950f41STomohiro Kusumi 		 * But make sure we leave the ones already masked
280e2950f41STomohiro Kusumi 		 * by the process, i.e. remove the signal from the
281e2950f41STomohiro Kusumi 		 * temporary signalmask only if it wasn't already
282e2950f41STomohiro Kusumi 		 * in sigmask.
283e2950f41STomohiro Kusumi 		 */
284e2950f41STomohiro Kusumi 		if (!SIGISMEMBER(lp->lwp_sigmask, autofs_sig_set[i]) &&
285e2950f41STomohiro Kusumi 		    !SIGISMEMBER(lp->lwp_proc->p_sigacts->ps_sigignore,
28674565ee0STomohiro Kusumi 		    autofs_sig_set[i]))
287e2950f41STomohiro Kusumi 			SIGDELSET(newset, autofs_sig_set[i]);
288e2950f41STomohiro Kusumi 	}
289e2950f41STomohiro Kusumi 	kern_sigprocmask(SIG_SETMASK, &newset, oldset);
290e2950f41STomohiro Kusumi 	lwkt_reltoken(&lp->lwp_token);
291e2950f41STomohiro Kusumi }
292e2950f41STomohiro Kusumi 
293e2950f41STomohiro Kusumi static void
autofs_restore_sigmask(sigset_t * set)294e2950f41STomohiro Kusumi autofs_restore_sigmask(sigset_t *set)
295e2950f41STomohiro Kusumi {
296e2950f41STomohiro Kusumi 	kern_sigprocmask(SIG_SETMASK, set, NULL);
297e2950f41STomohiro Kusumi }
298e2950f41STomohiro Kusumi 
299e2950f41STomohiro Kusumi static int
autofs_trigger_one(struct autofs_node * anp,const char * component,int componentlen)30074565ee0STomohiro Kusumi autofs_trigger_one(struct autofs_node *anp, const char *component,
30174565ee0STomohiro Kusumi     int componentlen)
302e2950f41STomohiro Kusumi {
303e2950f41STomohiro Kusumi #define _taskqueue_thread (taskqueue_thread[mycpuid])
304e2950f41STomohiro Kusumi 	struct autofs_mount *amp = anp->an_mount;
305e2950f41STomohiro Kusumi 	struct autofs_request *ar;
306e2950f41STomohiro Kusumi 	char *key, *path;
307662e8088STomohiro Kusumi 	int error = 0, request_error;
308e2950f41STomohiro Kusumi 	bool wildcards;
309e2950f41STomohiro Kusumi 
3102137724aSTomohiro Kusumi 	KKASSERT(mtx_islocked_ex(&autofs_softc->sc_lock));
311e2950f41STomohiro Kusumi 
312e2950f41STomohiro Kusumi 	if (anp->an_parent == NULL) {
313e2950f41STomohiro Kusumi 		key = kstrndup(component, componentlen, M_AUTOFS);
314e2950f41STomohiro Kusumi 	} else {
315662e8088STomohiro Kusumi 		struct autofs_node *firstanp;
316e2950f41STomohiro Kusumi 		for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
317e2950f41STomohiro Kusumi 		    firstanp = firstanp->an_parent)
318e2950f41STomohiro Kusumi 			continue;
319e2950f41STomohiro Kusumi 		key = kstrdup(firstanp->an_name, M_AUTOFS);
320e2950f41STomohiro Kusumi 	}
321e2950f41STomohiro Kusumi 
322e2950f41STomohiro Kusumi 	path = autofs_path(anp);
323e2950f41STomohiro Kusumi 
324e2950f41STomohiro Kusumi 	TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
3252f752b64STomohiro Kusumi 		if (strcmp(ar->ar_path, path) || strcmp(ar->ar_key, key))
326e2950f41STomohiro Kusumi 			continue;
327e2950f41STomohiro Kusumi 		KASSERT(strcmp(ar->ar_from, amp->am_from) == 0,
328e2950f41STomohiro Kusumi 		    ("from changed; %s != %s", ar->ar_from, amp->am_from));
329e2950f41STomohiro Kusumi 		KASSERT(strcmp(ar->ar_prefix, amp->am_prefix) == 0,
330e2950f41STomohiro Kusumi 		    ("prefix changed; %s != %s",
331e2950f41STomohiro Kusumi 		     ar->ar_prefix, amp->am_prefix));
332e2950f41STomohiro Kusumi 		KASSERT(strcmp(ar->ar_options, amp->am_options) == 0,
333e2950f41STomohiro Kusumi 		    ("options changed; %s != %s",
334e2950f41STomohiro Kusumi 		     ar->ar_options, amp->am_options));
335e2950f41STomohiro Kusumi 		break;
336e2950f41STomohiro Kusumi 	}
337e2950f41STomohiro Kusumi 
338e2950f41STomohiro Kusumi 	if (ar != NULL) {
339e2950f41STomohiro Kusumi 		refcount_acquire(&ar->ar_refcount);
340e2950f41STomohiro Kusumi 	} else {
341a5dfdd2bSTomohiro Kusumi 		/*
342a5dfdd2bSTomohiro Kusumi 		 * All struct fields must be initialized.
343a5dfdd2bSTomohiro Kusumi 		 */
344e2950f41STomohiro Kusumi 		ar = objcache_get(autofs_request_objcache, M_WAITOK);
345e2950f41STomohiro Kusumi 		ar->ar_mount = amp;
346e2950f41STomohiro Kusumi 		ar->ar_id = autofs_softc->sc_last_request_id++;
347e2950f41STomohiro Kusumi 		ar->ar_done = false;
348e2950f41STomohiro Kusumi 		ar->ar_error = 0;
349e2950f41STomohiro Kusumi 		ar->ar_wildcards = false;
350e2950f41STomohiro Kusumi 		ar->ar_in_progress = false;
351e2950f41STomohiro Kusumi 		strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
352e2950f41STomohiro Kusumi 		strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
353e2950f41STomohiro Kusumi 		strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
354e2950f41STomohiro Kusumi 		strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
3552f752b64STomohiro Kusumi 		strlcpy(ar->ar_options, amp->am_options,
3562f752b64STomohiro Kusumi 		    sizeof(ar->ar_options));
357e2950f41STomohiro Kusumi 		TIMEOUT_TASK_INIT(_taskqueue_thread, &ar->ar_task, 0,
358e2950f41STomohiro Kusumi 		    autofs_task, ar);
359102b6bbaSTomohiro Kusumi 		taskqueue_enqueue_timeout(_taskqueue_thread, &ar->ar_task,
360102b6bbaSTomohiro Kusumi 		    autofs_timeout * hz);
361e2950f41STomohiro Kusumi 		refcount_init(&ar->ar_refcount, 1);
362e2950f41STomohiro Kusumi 		TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
363e2950f41STomohiro Kusumi 	}
364e2950f41STomohiro Kusumi 
365e2950f41STomohiro Kusumi 	cv_broadcast(&autofs_softc->sc_cv);
366e2950f41STomohiro Kusumi 	while (ar->ar_done == false) {
367e2950f41STomohiro Kusumi 		if (autofs_interruptible) {
368662e8088STomohiro Kusumi 			sigset_t oldset;
369e2950f41STomohiro Kusumi 			autofs_set_sigmask(&oldset);
3702137724aSTomohiro Kusumi 			error = cv_mtx_wait_sig(&autofs_softc->sc_cv,
371e2950f41STomohiro Kusumi 			    &autofs_softc->sc_lock);
372e2950f41STomohiro Kusumi 			autofs_restore_sigmask(&oldset);
373e2950f41STomohiro Kusumi 			if (error) {
3742137724aSTomohiro Kusumi 				AUTOFS_WARN("cv_mtx_wait_sig for %s failed "
375e2950f41STomohiro Kusumi 				    "with error %d", ar->ar_path, error);
376e2950f41STomohiro Kusumi 				break;
377e2950f41STomohiro Kusumi 			}
378e2950f41STomohiro Kusumi 		} else {
3792137724aSTomohiro Kusumi 			cv_mtx_wait(&autofs_softc->sc_cv,
3802137724aSTomohiro Kusumi 			    &autofs_softc->sc_lock);
381e2950f41STomohiro Kusumi 		}
382e2950f41STomohiro Kusumi 	}
383e2950f41STomohiro Kusumi 
384e2950f41STomohiro Kusumi 	request_error = ar->ar_error;
385e2950f41STomohiro Kusumi 	if (request_error)
386*5bbad4bcSTomohiro Kusumi 		AUTOFS_WARN("request for %s completed with error %d, "
387*5bbad4bcSTomohiro Kusumi 		    "pid %d (%s)", ar->ar_path, request_error,
388*5bbad4bcSTomohiro Kusumi 		    curproc->p_pid, curproc->p_comm);
389e2950f41STomohiro Kusumi 
390e2950f41STomohiro Kusumi 	wildcards = ar->ar_wildcards;
391e2950f41STomohiro Kusumi 
392662e8088STomohiro Kusumi 	/*
393662e8088STomohiro Kusumi 	 * Check if this is the last reference.
394662e8088STomohiro Kusumi 	 */
395662e8088STomohiro Kusumi 	if (refcount_release(&ar->ar_refcount)) {
396e2950f41STomohiro Kusumi 		TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
3972137724aSTomohiro Kusumi 		mtx_unlock_ex(&autofs_softc->sc_lock);
398e2950f41STomohiro Kusumi 		taskqueue_cancel_timeout(_taskqueue_thread, &ar->ar_task, NULL);
399e2950f41STomohiro Kusumi 		taskqueue_drain_timeout(_taskqueue_thread, &ar->ar_task);
400e2950f41STomohiro Kusumi 		objcache_put(autofs_request_objcache, ar);
4012137724aSTomohiro Kusumi 		mtx_lock_ex_quick(&autofs_softc->sc_lock);
402e2950f41STomohiro Kusumi 	}
403e2950f41STomohiro Kusumi 
404e2950f41STomohiro Kusumi 	/*
405e2950f41STomohiro Kusumi 	 * Note that we do not do negative caching on purpose.  This
406e2950f41STomohiro Kusumi 	 * way the user can retry access at any time, e.g. after fixing
407e2950f41STomohiro Kusumi 	 * the failure reason, without waiting for cache timer to expire.
408e2950f41STomohiro Kusumi 	 */
409e2950f41STomohiro Kusumi 	if (error == 0 && request_error == 0 && autofs_cache > 0) {
410e2950f41STomohiro Kusumi 		autofs_node_cache(anp);
411e2950f41STomohiro Kusumi 		anp->an_wildcards = wildcards;
412e2950f41STomohiro Kusumi 		callout_reset(&anp->an_callout, autofs_cache * hz,
413e2950f41STomohiro Kusumi 		    autofs_cache_callout, anp);
414e2950f41STomohiro Kusumi 	}
415e2950f41STomohiro Kusumi 
416e2950f41STomohiro Kusumi 	kfree(key, M_AUTOFS);
417e2950f41STomohiro Kusumi 	kfree(path, M_AUTOFS);
418e2950f41STomohiro Kusumi 
419e2950f41STomohiro Kusumi 	if (error)
420e2950f41STomohiro Kusumi 		return (error);
421e2950f41STomohiro Kusumi 	return (request_error);
422e2950f41STomohiro Kusumi }
423e2950f41STomohiro Kusumi 
424e2950f41STomohiro Kusumi int
autofs_trigger(struct autofs_node * anp,const char * component,int componentlen)42574565ee0STomohiro Kusumi autofs_trigger(struct autofs_node *anp, const char *component, int componentlen)
426e2950f41STomohiro Kusumi {
427662e8088STomohiro Kusumi 	for (;;) {
428e2950f41STomohiro Kusumi 		int error, dummy;
429e2950f41STomohiro Kusumi 
430e2950f41STomohiro Kusumi 		error = autofs_trigger_one(anp, component, componentlen);
431e2950f41STomohiro Kusumi 		if (error == 0) {
432e2950f41STomohiro Kusumi 			anp->an_retries = 0;
433e2950f41STomohiro Kusumi 			return (0);
434e2950f41STomohiro Kusumi 		}
435e2950f41STomohiro Kusumi 		if (error == EINTR || error == ERESTART) {
436e2950f41STomohiro Kusumi 			AUTOFS_DEBUG("trigger interrupted by signal, "
437e2950f41STomohiro Kusumi 			    "not retrying");
438e2950f41STomohiro Kusumi 			anp->an_retries = 0;
439e2950f41STomohiro Kusumi 			return (error);
440e2950f41STomohiro Kusumi 		}
441e2950f41STomohiro Kusumi 		anp->an_retries++;
442e2950f41STomohiro Kusumi 		if (anp->an_retries >= autofs_retry_attempts) {
443e2950f41STomohiro Kusumi 			AUTOFS_DEBUG("trigger failed %d times; returning "
444e2950f41STomohiro Kusumi 			    "error %d", anp->an_retries, error);
445e2950f41STomohiro Kusumi 			anp->an_retries = 0;
446e2950f41STomohiro Kusumi 			return (error);
447e2950f41STomohiro Kusumi 		}
448e2950f41STomohiro Kusumi 		AUTOFS_DEBUG("trigger failed with error %d; will retry in "
449e2950f41STomohiro Kusumi 		    "%d seconds, %d attempts left", error, autofs_retry_delay,
450e2950f41STomohiro Kusumi 		    autofs_retry_attempts - anp->an_retries);
4512137724aSTomohiro Kusumi 		mtx_unlock_ex(&autofs_softc->sc_lock);
452e2950f41STomohiro Kusumi 		tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz);
4532137724aSTomohiro Kusumi 		mtx_lock_ex_quick(&autofs_softc->sc_lock);
454e2950f41STomohiro Kusumi 	}
455e2950f41STomohiro Kusumi }
456e2950f41STomohiro Kusumi 
457e2950f41STomohiro Kusumi static int
autofs_ioctl_request(struct autofs_daemon_request * adr)458e2950f41STomohiro Kusumi autofs_ioctl_request(struct autofs_daemon_request *adr)
459e2950f41STomohiro Kusumi {
460e2950f41STomohiro Kusumi 	struct proc *curp = curproc;
461e2950f41STomohiro Kusumi 	struct autofs_request *ar;
462e2950f41STomohiro Kusumi 
4632137724aSTomohiro Kusumi 	mtx_lock_ex_quick(&autofs_softc->sc_lock);
464e2950f41STomohiro Kusumi 	for (;;) {
465662e8088STomohiro Kusumi 		int error;
466e2950f41STomohiro Kusumi 		TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
46774565ee0STomohiro Kusumi 			if (ar->ar_done || ar->ar_in_progress)
468e2950f41STomohiro Kusumi 				continue;
469e2950f41STomohiro Kusumi 			break;
470e2950f41STomohiro Kusumi 		}
471e2950f41STomohiro Kusumi 		if (ar != NULL)
47274565ee0STomohiro Kusumi 			break; /* found (!done && !in_progress) */
473e2950f41STomohiro Kusumi 
4742137724aSTomohiro Kusumi 		error = cv_mtx_wait_sig(&autofs_softc->sc_cv,
475e2950f41STomohiro Kusumi 		    &autofs_softc->sc_lock);
476e2950f41STomohiro Kusumi 		if (error) {
4772137724aSTomohiro Kusumi 			mtx_unlock_ex(&autofs_softc->sc_lock);
478e2950f41STomohiro Kusumi 			return (error);
479e2950f41STomohiro Kusumi 		}
480e2950f41STomohiro Kusumi 	}
481e2950f41STomohiro Kusumi 
482e2950f41STomohiro Kusumi 	ar->ar_in_progress = true;
483e2950f41STomohiro Kusumi 
484e2950f41STomohiro Kusumi 	adr->adr_id = ar->ar_id;
485e2950f41STomohiro Kusumi 	strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
486e2950f41STomohiro Kusumi 	strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
487e2950f41STomohiro Kusumi 	strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
488e2950f41STomohiro Kusumi 	strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
489e2950f41STomohiro Kusumi 	strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
490e2950f41STomohiro Kusumi 
4912137724aSTomohiro Kusumi 	mtx_unlock_ex(&autofs_softc->sc_lock);
492e2950f41STomohiro Kusumi 
493e2950f41STomohiro Kusumi 	lwkt_gettoken(&curp->p_token);
494e2950f41STomohiro Kusumi 	autofs_softc->sc_dev_sid = proc_pgid(curp);
495e2950f41STomohiro Kusumi 	lwkt_reltoken(&curp->p_token);
496e2950f41STomohiro Kusumi 
497e2950f41STomohiro Kusumi 	return (0);
498e2950f41STomohiro Kusumi }
499e2950f41STomohiro Kusumi 
500e2950f41STomohiro Kusumi static int
autofs_ioctl_done(struct autofs_daemon_done * add)501e2950f41STomohiro Kusumi autofs_ioctl_done(struct autofs_daemon_done *add)
502e2950f41STomohiro Kusumi {
503e2950f41STomohiro Kusumi 	struct autofs_request *ar;
504e2950f41STomohiro Kusumi 
5052137724aSTomohiro Kusumi 	mtx_lock_ex_quick(&autofs_softc->sc_lock);
50674565ee0STomohiro Kusumi 	TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next)
507e2950f41STomohiro Kusumi 		if (ar->ar_id == add->add_id)
508e2950f41STomohiro Kusumi 			break;
509e2950f41STomohiro Kusumi 
510e2950f41STomohiro Kusumi 	if (ar == NULL) {
5112137724aSTomohiro Kusumi 		mtx_unlock_ex(&autofs_softc->sc_lock);
512e2950f41STomohiro Kusumi 		AUTOFS_DEBUG("id %d not found", add->add_id);
513e2950f41STomohiro Kusumi 		return (ESRCH);
514e2950f41STomohiro Kusumi 	}
515e2950f41STomohiro Kusumi 
516e2950f41STomohiro Kusumi 	ar->ar_error = add->add_error;
517e2950f41STomohiro Kusumi 	ar->ar_wildcards = add->add_wildcards;
518e2950f41STomohiro Kusumi 	ar->ar_done = true;
519e2950f41STomohiro Kusumi 	ar->ar_in_progress = false;
520e2950f41STomohiro Kusumi 	cv_broadcast(&autofs_softc->sc_cv);
521e2950f41STomohiro Kusumi 
5222137724aSTomohiro Kusumi 	mtx_unlock_ex(&autofs_softc->sc_lock);
523e2950f41STomohiro Kusumi 
524e2950f41STomohiro Kusumi 	return (0);
525e2950f41STomohiro Kusumi }
526e2950f41STomohiro Kusumi 
527e2950f41STomohiro Kusumi static int
autofs_open(struct dev_open_args * ap)528e2950f41STomohiro Kusumi autofs_open(struct dev_open_args *ap)
529e2950f41STomohiro Kusumi {
5302137724aSTomohiro Kusumi 	mtx_lock_ex_quick(&autofs_softc->sc_lock);
531e2950f41STomohiro Kusumi 	/*
532e2950f41STomohiro Kusumi 	 * We must never block automountd(8) and its descendants, and we use
533e2950f41STomohiro Kusumi 	 * session ID to determine that: we store session id of the process
534e2950f41STomohiro Kusumi 	 * that opened the device, and then compare it with session ids
535e2950f41STomohiro Kusumi 	 * of triggering processes.  This means running a second automountd(8)
536e2950f41STomohiro Kusumi 	 * instance would break the previous one.  The check below prevents
537e2950f41STomohiro Kusumi 	 * it from happening.
538e2950f41STomohiro Kusumi 	 */
539e2950f41STomohiro Kusumi 	if (autofs_softc->sc_dev_opened) {
5402137724aSTomohiro Kusumi 		mtx_unlock_ex(&autofs_softc->sc_lock);
541e2950f41STomohiro Kusumi 		return (EBUSY);
542e2950f41STomohiro Kusumi 	}
543e2950f41STomohiro Kusumi 
544e2950f41STomohiro Kusumi 	autofs_softc->sc_dev_opened = true;
5452137724aSTomohiro Kusumi 	mtx_unlock_ex(&autofs_softc->sc_lock);
546e2950f41STomohiro Kusumi 
547e2950f41STomohiro Kusumi 	return (0);
548e2950f41STomohiro Kusumi }
549e2950f41STomohiro Kusumi 
550e2950f41STomohiro Kusumi static int
autofs_close(struct dev_close_args * ap)551e2950f41STomohiro Kusumi autofs_close(struct dev_close_args *ap)
552e2950f41STomohiro Kusumi {
5532137724aSTomohiro Kusumi 	mtx_lock_ex_quick(&autofs_softc->sc_lock);
554e2950f41STomohiro Kusumi 	KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
555e2950f41STomohiro Kusumi 	autofs_softc->sc_dev_opened = false;
5562137724aSTomohiro Kusumi 	mtx_unlock_ex(&autofs_softc->sc_lock);
557e2950f41STomohiro Kusumi 
558e2950f41STomohiro Kusumi 	return (0);
559e2950f41STomohiro Kusumi }
560e2950f41STomohiro Kusumi 
561e2950f41STomohiro Kusumi static int
autofs_ioctl(struct dev_ioctl_args * ap)562e2950f41STomohiro Kusumi autofs_ioctl(struct dev_ioctl_args *ap)
563e2950f41STomohiro Kusumi {
5640b1d8a0dSTomohiro Kusumi 	unsigned long cmd = ap->a_cmd;
565e2950f41STomohiro Kusumi 	void *arg = ap->a_data;
566e2950f41STomohiro Kusumi 
567e2950f41STomohiro Kusumi 	KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
568e2950f41STomohiro Kusumi 
569e2950f41STomohiro Kusumi 	switch (cmd) {
570e2950f41STomohiro Kusumi 	case AUTOFSREQUEST:
571e2950f41STomohiro Kusumi 		return (autofs_ioctl_request(
572e2950f41STomohiro Kusumi 		    (struct autofs_daemon_request *)arg));
573e2950f41STomohiro Kusumi 	case AUTOFSDONE:
57474565ee0STomohiro Kusumi 		return (autofs_ioctl_done((struct autofs_daemon_done *)arg));
575e2950f41STomohiro Kusumi 	default:
576e2950f41STomohiro Kusumi 		AUTOFS_DEBUG("invalid cmd %lx", cmd);
577e2950f41STomohiro Kusumi 		return (EINVAL);
578e2950f41STomohiro Kusumi 	}
579e769d4a5STomohiro Kusumi 	return (EINVAL);
580e2950f41STomohiro Kusumi }
581