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