1*0eaaa024Sad /* $NetBSD: autofs.c,v 1.6 2020/05/23 23:42:43 ad Exp $ */
2b985414bSchristos
3b985414bSchristos /*-
4b985414bSchristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5b985414bSchristos * Copyright (c) 2016 The DragonFly Project
6b985414bSchristos * Copyright (c) 2014 The FreeBSD Foundation
7b985414bSchristos * All rights reserved.
8b985414bSchristos *
9b985414bSchristos * This code is derived from software contributed to The NetBSD Foundation
10b985414bSchristos * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11b985414bSchristos *
12b985414bSchristos * This software was developed by Edward Tomasz Napierala under sponsorship
13b985414bSchristos * from the FreeBSD Foundation.
14b985414bSchristos *
15b985414bSchristos * Redistribution and use in source and binary forms, with or without
16b985414bSchristos * modification, are permitted provided that the following conditions
17b985414bSchristos * are met:
18b985414bSchristos * 1. Redistributions of source code must retain the above copyright
19b985414bSchristos * notice, this list of conditions and the following disclaimer.
20b985414bSchristos * 2. Redistributions in binary form must reproduce the above copyright
21b985414bSchristos * notice, this list of conditions and the following disclaimer in the
22b985414bSchristos * documentation and/or other materials provided with the distribution.
23b985414bSchristos *
24b985414bSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25b985414bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b985414bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b985414bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28b985414bSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29b985414bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30b985414bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31b985414bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32b985414bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33b985414bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34b985414bSchristos * SUCH DAMAGE.
35b985414bSchristos *
36b985414bSchristos */
37b985414bSchristos /*-
38b985414bSchristos * Copyright (c) 1989, 1991, 1993, 1995
39b985414bSchristos * The Regents of the University of California. All rights reserved.
40b985414bSchristos *
41b985414bSchristos * This code is derived from software contributed to Berkeley by
42b985414bSchristos * Rick Macklem at The University of Guelph.
43b985414bSchristos *
44b985414bSchristos * Redistribution and use in source and binary forms, with or without
45b985414bSchristos * modification, are permitted provided that the following conditions
46b985414bSchristos * are met:
47b985414bSchristos * 1. Redistributions of source code must retain the above copyright
48b985414bSchristos * notice, this list of conditions and the following disclaimer.
49b985414bSchristos * 2. Redistributions in binary form must reproduce the above copyright
50b985414bSchristos * notice, this list of conditions and the following disclaimer in the
51b985414bSchristos * documentation and/or other materials provided with the distribution.
52b985414bSchristos * 3. Neither the name of the University nor the names of its contributors
53b985414bSchristos * may be used to endorse or promote products derived from this software
54b985414bSchristos * without specific prior written permission.
55b985414bSchristos *
56b985414bSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57b985414bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58b985414bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59b985414bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60b985414bSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61b985414bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62b985414bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63b985414bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64b985414bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65b985414bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66b985414bSchristos * SUCH DAMAGE.
67b985414bSchristos *
68b985414bSchristos */
69b985414bSchristos
70b985414bSchristos #include <sys/cdefs.h>
71*0eaaa024Sad __KERNEL_RCSID(0, "$NetBSD: autofs.c,v 1.6 2020/05/23 23:42:43 ad Exp $");
72b985414bSchristos
73b985414bSchristos #include "autofs.h"
74b985414bSchristos
750d40f566Schristos #include "ioconf.h"
760d40f566Schristos
77602913a7Smartin #include <sys/atomic.h>
78b985414bSchristos #include <sys/queue.h>
79b985414bSchristos #include <sys/signalvar.h>
80b985414bSchristos
81d3d26a32Stkusumi static dev_type_open(autofs_open);
82d3d26a32Stkusumi static dev_type_close(autofs_close);
83d3d26a32Stkusumi static dev_type_ioctl(autofs_ioctl);
84b985414bSchristos
850d40f566Schristos const struct cdevsw autofs_cdevsw = {
86b985414bSchristos .d_open = autofs_open,
87b985414bSchristos .d_close = autofs_close,
880d40f566Schristos .d_read = noread,
890d40f566Schristos .d_write = nowrite,
90b985414bSchristos .d_ioctl = autofs_ioctl,
910d40f566Schristos .d_stop = nostop,
920d40f566Schristos .d_tty = notty,
930d40f566Schristos .d_poll = nopoll,
940d40f566Schristos .d_mmap = nommap,
950d40f566Schristos .d_kqfilter = nokqfilter,
960d40f566Schristos .d_discard = nodiscard,
970d40f566Schristos .d_flag = D_OTHER,
98b985414bSchristos };
99b985414bSchristos
100b985414bSchristos /*
101b985414bSchristos * List of signals that can interrupt an autofs trigger.
102b985414bSchristos */
103b985414bSchristos static int autofs_sig_set[] = {
104b985414bSchristos SIGINT,
105b985414bSchristos SIGTERM,
106b985414bSchristos SIGHUP,
107b985414bSchristos SIGKILL,
108b985414bSchristos SIGQUIT,
109b985414bSchristos };
110b985414bSchristos
111b985414bSchristos struct pool autofs_request_pool;
112b985414bSchristos struct pool autofs_node_pool;
113b985414bSchristos struct autofs_softc *autofs_softc = NULL;
114b985414bSchristos struct workqueue *autofs_tmo_wq = NULL;
115b985414bSchristos
116b985414bSchristos int autofs_debug = 1;
117b985414bSchristos int autofs_mount_on_stat = 0;
118b985414bSchristos int autofs_timeout = 30;
119b985414bSchristos int autofs_cache = 600;
120b985414bSchristos int autofs_retry_attempts = 3;
121b985414bSchristos int autofs_retry_delay = 1;
122b985414bSchristos int autofs_interruptible = 1;
123b985414bSchristos
1240d40f566Schristos void
autofsattach(int n)1250d40f566Schristos autofsattach(int n)
1260d40f566Schristos {
1270d40f566Schristos }
1280d40f566Schristos
129b985414bSchristos static int
autofs_node_cmp(const struct autofs_node * a,const struct autofs_node * b)130b985414bSchristos autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
131b985414bSchristos {
132b985414bSchristos
133b985414bSchristos return strcmp(a->an_name, b->an_name);
134b985414bSchristos }
135b985414bSchristos
136b985414bSchristos RB_GENERATE(autofs_node_tree, autofs_node, an_entry, autofs_node_cmp);
137b985414bSchristos
138b985414bSchristos bool
autofs_ignore_thread(void)139b985414bSchristos autofs_ignore_thread(void)
140b985414bSchristos {
141b985414bSchristos
142b985414bSchristos if (autofs_softc->sc_dev_opened == false)
143b985414bSchristos return false;
144b985414bSchristos
145*0eaaa024Sad mutex_enter(&proc_lock);
146b985414bSchristos if (autofs_softc->sc_dev_sid == curproc->p_pgrp->pg_id) {
147*0eaaa024Sad mutex_exit(&proc_lock);
148b985414bSchristos return true;
149b985414bSchristos }
150*0eaaa024Sad mutex_exit(&proc_lock);
151b985414bSchristos
152b985414bSchristos return false;
153b985414bSchristos }
154b985414bSchristos
155b985414bSchristos static char *
autofs_path(struct autofs_node * anp)156b985414bSchristos autofs_path(struct autofs_node *anp)
157b985414bSchristos {
158b985414bSchristos struct autofs_mount *amp = anp->an_mount;
159b985414bSchristos size_t len;
160b985414bSchristos char *path, *tmp;
161b985414bSchristos
162b985414bSchristos path = kmem_strdup("", KM_SLEEP);
163b985414bSchristos for (; anp->an_parent; anp = anp->an_parent) {
164b985414bSchristos len = strlen(anp->an_name) + strlen(path) + 2;
165b985414bSchristos tmp = kmem_alloc(len, KM_SLEEP);
166b985414bSchristos snprintf(tmp, len, "%s/%s", anp->an_name, path);
167b985414bSchristos kmem_strfree(path);
168b985414bSchristos path = tmp;
169b985414bSchristos }
170b985414bSchristos
171b985414bSchristos len = strlen(amp->am_on) + strlen(path) + 2;
172b985414bSchristos tmp = kmem_alloc(len, KM_SLEEP);
173b985414bSchristos snprintf(tmp, len, "%s/%s", amp->am_on, path);
174b985414bSchristos kmem_strfree(path);
175b985414bSchristos
176b985414bSchristos return tmp;
177b985414bSchristos }
178b985414bSchristos
179b985414bSchristos void
autofs_timeout_wq(struct work * wk,void * arg)180b985414bSchristos autofs_timeout_wq(struct work *wk, void *arg)
181b985414bSchristos {
182b985414bSchristos struct autofs_request *ar = (void *)wk;
183b985414bSchristos
184b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
185b985414bSchristos AUTOFS_WARN("request %d for %s timed out after %d seconds",
186b985414bSchristos ar->ar_id, ar->ar_path, autofs_timeout);
187b985414bSchristos
188b985414bSchristos ar->ar_error = ETIMEDOUT;
189b985414bSchristos ar->ar_wildcards = true;
190b985414bSchristos ar->ar_done = true;
191b985414bSchristos ar->ar_in_progress = false;
192b985414bSchristos cv_broadcast(&autofs_softc->sc_cv);
193b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
194b985414bSchristos }
195b985414bSchristos
196b985414bSchristos static void
autofs_timeout_callout(void * context)197b985414bSchristos autofs_timeout_callout(void *context)
198b985414bSchristos {
199b985414bSchristos struct autofs_request *ar = context;
200b985414bSchristos
201b985414bSchristos workqueue_enqueue(autofs_tmo_wq, &ar->ar_wk, NULL);
202b985414bSchristos }
203b985414bSchristos
204b985414bSchristos bool
autofs_cached(struct autofs_node * anp,const char * component,int componentlen)205b985414bSchristos autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
206b985414bSchristos {
207b985414bSchristos struct autofs_mount *amp = anp->an_mount;
208b985414bSchristos
209b985414bSchristos KASSERT(!mutex_owned(&->am_lock));
210b985414bSchristos
211b985414bSchristos /*
212b985414bSchristos * For root node we need to request automountd(8) assistance even
213b985414bSchristos * if the node is marked as cached, but the requested top-level
214b985414bSchristos * directory does not exist. This is necessary for wildcard indirect
215b985414bSchristos * map keys to work. We don't do this if we know that there are
216b985414bSchristos * no wildcards.
217b985414bSchristos */
218b985414bSchristos if (!anp->an_parent && componentlen && anp->an_wildcards) {
219b985414bSchristos int error;
220b985414bSchristos KASSERT(amp->am_root == anp);
221b985414bSchristos mutex_enter(&->am_lock);
222b985414bSchristos error = autofs_node_find(anp, component, componentlen, NULL);
223b985414bSchristos mutex_exit(&->am_lock);
224b985414bSchristos if (error)
225b985414bSchristos return false;
226b985414bSchristos }
227b985414bSchristos
228b985414bSchristos return anp->an_cached;
229b985414bSchristos }
230b985414bSchristos
231b985414bSchristos static void
autofs_cache_callout(void * context)232b985414bSchristos autofs_cache_callout(void *context)
233b985414bSchristos {
234b985414bSchristos struct autofs_node *anp = context;
235b985414bSchristos
236b985414bSchristos autofs_node_uncache(anp);
237b985414bSchristos }
238b985414bSchristos
239b985414bSchristos void
autofs_flush(struct autofs_mount * amp)240b985414bSchristos autofs_flush(struct autofs_mount *amp)
241b985414bSchristos {
242b985414bSchristos struct autofs_node *anp = amp->am_root;
243b985414bSchristos struct autofs_node *child;
244b985414bSchristos
245b985414bSchristos mutex_enter(&->am_lock);
246b985414bSchristos RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
247b985414bSchristos autofs_node_uncache(child);
248b985414bSchristos }
249b985414bSchristos autofs_node_uncache(amp->am_root);
250b985414bSchristos mutex_exit(&->am_lock);
251b985414bSchristos
252b985414bSchristos AUTOFS_DEBUG("%s flushed", amp->am_on);
253b985414bSchristos }
254b985414bSchristos
255b985414bSchristos /*
256b985414bSchristos * The set/restore sigmask functions are used to (temporarily) overwrite
257b985414bSchristos * the thread sigmask during triggering.
258b985414bSchristos */
259b985414bSchristos static void
autofs_set_sigmask(sigset_t * oldset)260b985414bSchristos autofs_set_sigmask(sigset_t *oldset)
261b985414bSchristos {
262b985414bSchristos sigset_t newset;
263b985414bSchristos int i;
264b985414bSchristos
265b985414bSchristos sigfillset(&newset);
266b985414bSchristos /* Remove the autofs set of signals from newset */
267*0eaaa024Sad mutex_enter(&proc_lock);
268b985414bSchristos mutex_enter(curproc->p_lock);
269b985414bSchristos
270b985414bSchristos for (i = 0; i < __arraycount(autofs_sig_set); i++) {
271b985414bSchristos /*
272b985414bSchristos * But make sure we leave the ones already masked
273b985414bSchristos * by the process, i.e. remove the signal from the
274b985414bSchristos * temporary signalmask only if it wasn't already
275b985414bSchristos * in sigmask.
276b985414bSchristos */
277b985414bSchristos if (!sigismasked(curlwp, autofs_sig_set[i]))
278b985414bSchristos sigdelset(&newset, autofs_sig_set[i]);
279b985414bSchristos }
280b985414bSchristos sigprocmask1(curlwp, SIG_SETMASK, &newset, oldset);
281b985414bSchristos
282b985414bSchristos mutex_exit(curproc->p_lock);
283*0eaaa024Sad mutex_exit(&proc_lock);
284b985414bSchristos }
285b985414bSchristos
286b985414bSchristos static void
autofs_restore_sigmask(sigset_t * set)287b985414bSchristos autofs_restore_sigmask(sigset_t *set)
288b985414bSchristos {
289b985414bSchristos
290*0eaaa024Sad mutex_enter(&proc_lock);
291b985414bSchristos mutex_enter(curproc->p_lock);
292b985414bSchristos
293b985414bSchristos sigprocmask1(curlwp, SIG_SETMASK, set, NULL);
294b985414bSchristos
295b985414bSchristos mutex_exit(curproc->p_lock);
296*0eaaa024Sad mutex_exit(&proc_lock);
297b985414bSchristos }
298b985414bSchristos
299b985414bSchristos static int
autofs_trigger_one(struct autofs_node * anp,const char * component,int componentlen)300b985414bSchristos autofs_trigger_one(struct autofs_node *anp, const char *component,
301b985414bSchristos int componentlen)
302b985414bSchristos {
303b985414bSchristos struct autofs_mount *amp = anp->an_mount;
304b985414bSchristos struct autofs_request *ar;
305b985414bSchristos char *key, *path;
306b985414bSchristos int error = 0, request_error;
307b985414bSchristos bool wildcards;
308b985414bSchristos
309b985414bSchristos KASSERT(mutex_owned(&autofs_softc->sc_lock));
310b985414bSchristos
311b985414bSchristos if (!anp->an_parent) {
312b985414bSchristos key = autofs_strndup(component, componentlen, KM_SLEEP);
313b985414bSchristos } else {
314b985414bSchristos struct autofs_node *firstanp;
315b985414bSchristos for (firstanp = anp; firstanp->an_parent->an_parent;
316b985414bSchristos firstanp = firstanp->an_parent)
317b985414bSchristos continue;
318b985414bSchristos key = kmem_strdup(firstanp->an_name, KM_SLEEP);
319b985414bSchristos }
320b985414bSchristos
321b985414bSchristos path = autofs_path(anp);
322b985414bSchristos
323b985414bSchristos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
324b985414bSchristos if (strcmp(ar->ar_path, path))
325b985414bSchristos continue;
326b985414bSchristos if (strcmp(ar->ar_key, key))
327b985414bSchristos continue;
328b985414bSchristos
329b985414bSchristos KASSERT(!strcmp(ar->ar_from, amp->am_from));
330b985414bSchristos KASSERT(!strcmp(ar->ar_prefix, amp->am_prefix));
331b985414bSchristos KASSERT(!strcmp(ar->ar_options, amp->am_options));
332b985414bSchristos break;
333b985414bSchristos }
334b985414bSchristos
335b985414bSchristos if (ar) {
336b985414bSchristos atomic_add_int(&ar->ar_refcount, 1);
337b985414bSchristos } else {
338b985414bSchristos ar = pool_get(&autofs_request_pool, PR_WAITOK);
339b985414bSchristos ar->ar_mount = amp;
340b985414bSchristos ar->ar_id = autofs_softc->sc_last_request_id++;
341b985414bSchristos ar->ar_done = false;
342b985414bSchristos ar->ar_error = 0;
343b985414bSchristos ar->ar_wildcards = false;
344b985414bSchristos ar->ar_in_progress = false;
345b985414bSchristos strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
346b985414bSchristos strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
347b985414bSchristos strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
348b985414bSchristos strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
349b985414bSchristos strlcpy(ar->ar_options,
350b985414bSchristos amp->am_options, sizeof(ar->ar_options));
351b985414bSchristos
352b985414bSchristos callout_init(&ar->ar_callout, 0);
353b985414bSchristos callout_reset(&ar->ar_callout, autofs_timeout * hz,
354b985414bSchristos autofs_timeout_callout, ar);
355b985414bSchristos ar->ar_refcount = 1;
356b985414bSchristos TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
357b985414bSchristos }
358b985414bSchristos
359b985414bSchristos cv_broadcast(&autofs_softc->sc_cv);
360b985414bSchristos while (ar->ar_done == false) {
361b985414bSchristos if (autofs_interruptible) {
362b985414bSchristos sigset_t oldset;
363b985414bSchristos autofs_set_sigmask(&oldset);
364b985414bSchristos error = cv_wait_sig(&autofs_softc->sc_cv,
365b985414bSchristos &autofs_softc->sc_lock);
366b985414bSchristos autofs_restore_sigmask(&oldset);
367b985414bSchristos if (error) {
368b985414bSchristos AUTOFS_WARN("cv_wait_sig for %s failed "
369b985414bSchristos "with error %d", ar->ar_path, error);
370b985414bSchristos break;
371b985414bSchristos }
372b985414bSchristos } else {
373b985414bSchristos cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
374b985414bSchristos }
375b985414bSchristos }
376b985414bSchristos
377b985414bSchristos request_error = ar->ar_error;
378b985414bSchristos if (request_error)
379819d266eStkusumi AUTOFS_WARN("request for %s completed with error %d, "
380819d266eStkusumi "pid %d (%s)", ar->ar_path, request_error,
381819d266eStkusumi curproc->p_pid, curproc->p_comm);
382b985414bSchristos
383b985414bSchristos wildcards = ar->ar_wildcards;
384b985414bSchristos
385b985414bSchristos /*
386b985414bSchristos * Check if this is the last reference.
387b985414bSchristos */
388b985414bSchristos if (!atomic_add_int_nv(&ar->ar_refcount, -1)) {
389b985414bSchristos TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
390b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
391b985414bSchristos callout_halt(&ar->ar_callout, NULL);
392b985414bSchristos pool_put(&autofs_request_pool, ar);
393b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
394b985414bSchristos }
395b985414bSchristos
396b985414bSchristos /*
397b985414bSchristos * Note that we do not do negative caching on purpose. This
398b985414bSchristos * way the user can retry access at any time, e.g. after fixing
399b985414bSchristos * the failure reason, without waiting for cache timer to expire.
400b985414bSchristos */
401b985414bSchristos if (!error && !request_error && autofs_cache > 0) {
402b985414bSchristos autofs_node_cache(anp);
403b985414bSchristos anp->an_wildcards = wildcards;
404b985414bSchristos callout_reset(&anp->an_callout, autofs_cache * hz,
405b985414bSchristos autofs_cache_callout, anp);
406b985414bSchristos }
407b985414bSchristos
408b985414bSchristos kmem_strfree(key);
409b985414bSchristos kmem_strfree(path);
410b985414bSchristos
411b985414bSchristos if (error)
412b985414bSchristos return error;
413b985414bSchristos return request_error;
414b985414bSchristos }
415b985414bSchristos
416b985414bSchristos int
autofs_trigger(struct autofs_node * anp,const char * component,int componentlen)417b985414bSchristos autofs_trigger(struct autofs_node *anp, const char *component,
418b985414bSchristos int componentlen)
419b985414bSchristos {
420b985414bSchristos for (;;) {
421b985414bSchristos int error, dummy;
422b985414bSchristos
423b985414bSchristos error = autofs_trigger_one(anp, component, componentlen);
424b985414bSchristos if (!error) {
425b985414bSchristos anp->an_retries = 0;
426b985414bSchristos return 0;
427b985414bSchristos }
428b985414bSchristos if (error == EINTR || error == ERESTART) {
429b985414bSchristos AUTOFS_DEBUG("trigger interrupted by signal, "
430b985414bSchristos "not retrying");
431b985414bSchristos anp->an_retries = 0;
432b985414bSchristos return error;
433b985414bSchristos }
434b985414bSchristos anp->an_retries++;
435b985414bSchristos if (anp->an_retries >= autofs_retry_attempts) {
436b985414bSchristos AUTOFS_DEBUG("trigger failed %d times; returning "
437b985414bSchristos "error %d", anp->an_retries, error);
438b985414bSchristos anp->an_retries = 0;
439b985414bSchristos return error;
440b985414bSchristos
441b985414bSchristos }
442b985414bSchristos AUTOFS_DEBUG("trigger failed with error %d; will retry in "
443b985414bSchristos "%d seconds, %d attempts left", error, autofs_retry_delay,
444b985414bSchristos autofs_retry_attempts - anp->an_retries);
445b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
446b985414bSchristos tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz);
447b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
448b985414bSchristos }
449b985414bSchristos }
450b985414bSchristos
451b985414bSchristos static int
autofs_ioctl_request(struct autofs_daemon_request * adr)452b985414bSchristos autofs_ioctl_request(struct autofs_daemon_request *adr)
453b985414bSchristos {
454b985414bSchristos struct autofs_request *ar;
455b985414bSchristos
456b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
457b985414bSchristos for (;;) {
458b985414bSchristos int error;
459b985414bSchristos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
460b985414bSchristos if (ar->ar_done)
461b985414bSchristos continue;
462b985414bSchristos if (ar->ar_in_progress)
463b985414bSchristos continue;
464b985414bSchristos break;
465b985414bSchristos }
466b985414bSchristos
467b985414bSchristos if (ar)
468b985414bSchristos break;
469b985414bSchristos
470b985414bSchristos error = cv_wait_sig(&autofs_softc->sc_cv,
471b985414bSchristos &autofs_softc->sc_lock);
472b985414bSchristos if (error) {
473b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
474b985414bSchristos return error;
475b985414bSchristos }
476b985414bSchristos }
477b985414bSchristos
478b985414bSchristos ar->ar_in_progress = true;
479b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
480b985414bSchristos
481b985414bSchristos adr->adr_id = ar->ar_id;
482b985414bSchristos strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
483b985414bSchristos strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
484b985414bSchristos strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
485b985414bSchristos strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
486b985414bSchristos strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
487b985414bSchristos
488*0eaaa024Sad mutex_enter(&proc_lock);
489b985414bSchristos autofs_softc->sc_dev_sid = curproc->p_pgrp->pg_id;
490*0eaaa024Sad mutex_exit(&proc_lock);
491b985414bSchristos
492b985414bSchristos return 0;
493b985414bSchristos }
494b985414bSchristos
495b985414bSchristos static int
autofs_ioctl_done(struct autofs_daemon_done * add)496b985414bSchristos autofs_ioctl_done(struct autofs_daemon_done *add)
497b985414bSchristos {
498b985414bSchristos struct autofs_request *ar;
499b985414bSchristos
500b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
501b985414bSchristos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
502b985414bSchristos if (ar->ar_id == add->add_id)
503b985414bSchristos break;
504b985414bSchristos }
505b985414bSchristos
506b985414bSchristos if (!ar) {
507b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
508b985414bSchristos AUTOFS_DEBUG("id %d not found", add->add_id);
509b985414bSchristos return ESRCH;
510b985414bSchristos }
511b985414bSchristos
512b985414bSchristos ar->ar_error = add->add_error;
513b985414bSchristos ar->ar_wildcards = add->add_wildcards;
514b985414bSchristos ar->ar_done = true;
515b985414bSchristos ar->ar_in_progress = false;
516b985414bSchristos cv_broadcast(&autofs_softc->sc_cv);
517b985414bSchristos
518b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
519b985414bSchristos
520b985414bSchristos return 0;
521b985414bSchristos }
522b985414bSchristos
523d3d26a32Stkusumi static int
autofs_open(dev_t dev,int flags,int mode,struct lwp * l)524b985414bSchristos autofs_open(dev_t dev, int flags, int mode, struct lwp *l)
525b985414bSchristos {
526b985414bSchristos
527b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
528b985414bSchristos /*
529b985414bSchristos * We must never block automountd(8) and its descendants, and we use
530b985414bSchristos * session ID to determine that: we store session id of the process
531b985414bSchristos * that opened the device, and then compare it with session ids
532b985414bSchristos * of triggering processes. This means running a second automountd(8)
533b985414bSchristos * instance would break the previous one. The check below prevents
534b985414bSchristos * it from happening.
535b985414bSchristos */
536b985414bSchristos if (autofs_softc->sc_dev_opened) {
537b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
538b985414bSchristos return EBUSY;
539b985414bSchristos }
540b985414bSchristos
541b985414bSchristos autofs_softc->sc_dev_opened = true;
542b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
543b985414bSchristos
544b985414bSchristos return 0;
545b985414bSchristos }
546b985414bSchristos
547d3d26a32Stkusumi static int
autofs_close(dev_t dev,int flags,int mode,struct lwp * l)548b985414bSchristos autofs_close(dev_t dev, int flags, int mode, struct lwp *l)
549b985414bSchristos {
550b985414bSchristos
551b985414bSchristos mutex_enter(&autofs_softc->sc_lock);
552b985414bSchristos KASSERT(autofs_softc->sc_dev_opened);
553b985414bSchristos autofs_softc->sc_dev_opened = false;
554b985414bSchristos mutex_exit(&autofs_softc->sc_lock);
555b985414bSchristos
556b985414bSchristos return 0;
557b985414bSchristos }
558b985414bSchristos
559d3d26a32Stkusumi static int
autofs_ioctl(dev_t dev,const u_long cmd,void * data,int flag,struct lwp * l)560b985414bSchristos autofs_ioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
561b985414bSchristos {
562b985414bSchristos
563b985414bSchristos KASSERT(autofs_softc->sc_dev_opened);
564b985414bSchristos
565b985414bSchristos switch (cmd) {
566b985414bSchristos case AUTOFSREQUEST:
567b985414bSchristos return autofs_ioctl_request(
568b985414bSchristos (struct autofs_daemon_request *)data);
569b985414bSchristos case AUTOFSDONE:
570b985414bSchristos return autofs_ioctl_done(
571b985414bSchristos (struct autofs_daemon_done *)data);
572b985414bSchristos default:
573b985414bSchristos AUTOFS_DEBUG("invalid cmd %lx", cmd);
574b985414bSchristos return EINVAL;
575b985414bSchristos }
576b985414bSchristos return EINVAL;
577b985414bSchristos }
578