2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 * Copyright (c) 1989, 1991, 1993, 1995
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
35 * Rick Macklem at The University of Guelph.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
67 #include <sys/systm.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>
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>
84 #include <fs/autofs/autofs.h>
85 #include <fs/autofs/autofs_ioctl.h>
87 MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
89 uma_zone_t autofs_request_zone;
90 uma_zone_t autofs_node_zone;
92 static int autofs_open(struct cdev *dev, int flags, int fmt,
94 static int autofs_close(struct cdev *dev, int flag, int fmt,
96 static int autofs_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
97 int mode, struct thread *td);
99 static struct cdevsw autofs_cdevsw = {
100 .d_version = D_VERSION,
101 .d_open = autofs_open,
102 .d_close = autofs_close,
103 .d_ioctl = autofs_ioctl,
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.
111 int autofs_sig_set[] = {
119 struct autofs_softc *autofs_softc;
121 SYSCTL_NODE(_vfs, OID_AUTO, autofs, CTLFLAG_RD, 0, "Automounter filesystem");
122 int autofs_debug = 1;
123 TUNABLE_INT("vfs.autofs.debug", &autofs_debug);
124 SYSCTL_INT(_vfs_autofs, OID_AUTO, debug, CTLFLAG_RWTUN,
125 &autofs_debug, 1, "Enable debug messages");
126 int autofs_mount_on_stat = 0;
127 TUNABLE_INT("vfs.autofs.mount_on_stat", &autofs_mount_on_stat);
128 SYSCTL_INT(_vfs_autofs, OID_AUTO, mount_on_stat, CTLFLAG_RWTUN,
129 &autofs_mount_on_stat, 0, "Trigger mount on stat(2) on mountpoint");
130 int autofs_timeout = 30;
131 TUNABLE_INT("vfs.autofs.timeout", &autofs_timeout);
132 SYSCTL_INT(_vfs_autofs, OID_AUTO, timeout, CTLFLAG_RWTUN,
133 &autofs_timeout, 30, "Number of seconds to wait for automountd(8)");
134 int autofs_cache = 600;
135 TUNABLE_INT("vfs.autofs.cache", &autofs_cache);
136 SYSCTL_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");
139 int autofs_retry_attempts = 3;
140 TUNABLE_INT("vfs.autofs.retry_attempts", &autofs_retry_attempts);
141 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_attempts, CTLFLAG_RWTUN,
142 &autofs_retry_attempts, 3, "Number of attempts before failing mount");
143 int autofs_retry_delay = 1;
144 TUNABLE_INT("vfs.autofs.retry_delay", &autofs_retry_delay);
145 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_delay, CTLFLAG_RWTUN,
146 &autofs_retry_delay, 1, "Number of seconds before retrying");
147 int autofs_interruptible = 1;
148 TUNABLE_INT("vfs.autofs.interruptible", &autofs_interruptible);
149 SYSCTL_INT(_vfs_autofs, OID_AUTO, interruptible, CTLFLAG_RWTUN,
150 &autofs_interruptible, 1, "Allow requests to be interrupted by signal");
153 autofs_init(struct vfsconf *vfsp)
157 KASSERT(autofs_softc == NULL,
158 ("softc %p, should be NULL", autofs_softc));
160 autofs_softc = malloc(sizeof(*autofs_softc), M_AUTOFS,
163 autofs_request_zone = uma_zcreate("autofs_request",
164 sizeof(struct autofs_request), NULL, NULL, NULL, NULL,
166 autofs_node_zone = uma_zcreate("autofs_node",
167 sizeof(struct autofs_node), NULL, NULL, NULL, NULL,
170 TAILQ_INIT(&autofs_softc->sc_requests);
171 cv_init(&autofs_softc->sc_cv, "autofscv");
172 sx_init(&autofs_softc->sc_lock, "autofslk");
174 error = make_dev_p(MAKEDEV_CHECKNAME, &autofs_softc->sc_cdev,
175 &autofs_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "autofs");
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);
184 autofs_softc->sc_cdev->si_drv1 = autofs_softc;
190 autofs_uninit(struct vfsconf *vfsp)
193 sx_xlock(&autofs_softc->sc_lock);
194 if (autofs_softc->sc_dev_opened) {
195 sx_xunlock(&autofs_softc->sc_lock);
198 if (autofs_softc->sc_cdev != NULL)
199 destroy_dev(autofs_softc->sc_cdev);
201 uma_zdestroy(autofs_request_zone);
202 uma_zdestroy(autofs_node_zone);
204 sx_xunlock(&autofs_softc->sc_lock);
206 * XXX: Race with open?
208 free(autofs_softc, M_AUTOFS);
214 autofs_ignore_thread(const struct thread *td)
220 if (autofs_softc->sc_dev_opened == false)
224 if (p->p_session->s_sid == autofs_softc->sc_dev_sid) {
234 autofs_path(struct autofs_node *anp)
236 struct autofs_mount *amp;
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,
245 strcpy(tmp, anp->an_name);
248 free(path, M_AUTOFS);
252 tmp = malloc(strlen(amp->am_mountpoint) + strlen(path) + 2,
254 strcpy(tmp, amp->am_mountpoint);
257 free(path, M_AUTOFS);
264 autofs_task(void *context, int pending)
266 struct autofs_request *ar;
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);
276 ar->ar_error = ETIMEDOUT;
277 ar->ar_wildcards = true;
279 ar->ar_in_progress = false;
280 cv_broadcast(&autofs_softc->sc_cv);
281 sx_xunlock(&autofs_softc->sc_lock);
285 autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
288 struct autofs_mount *amp;
292 AUTOFS_ASSERT_UNLOCKED(amp);
295 * For root node we need to request automountd(8) assistance even
296 * if the node is marked as cached, but the requested top-level
297 * directory does not exist. This is necessary for wildcard indirect
298 * map keys to work. We don't do this if we know that there are
301 if (anp->an_parent == NULL && componentlen != 0 && anp->an_wildcards) {
303 error = autofs_node_find(anp, component, componentlen, NULL);
309 return (anp->an_cached);
313 autofs_cache_callout(void *context)
315 struct autofs_node *anp;
318 anp->an_cached = false;
322 * The set/restore sigmask functions are used to (temporarily) overwrite
323 * the thread td_sigmask during triggering.
326 autofs_set_sigmask(sigset_t *oldset)
332 /* Remove the autofs set of signals from newset */
334 mtx_lock(&curproc->p_sigacts->ps_mtx);
335 for (i = 0 ; i < sizeof(autofs_sig_set)/sizeof(int) ; i++) {
337 * But make sure we leave the ones already masked
338 * by the process, i.e. remove the signal from the
339 * temporary signalmask only if it wasn't already
342 if (!SIGISMEMBER(curthread->td_sigmask, autofs_sig_set[i]) &&
343 !SIGISMEMBER(curproc->p_sigacts->ps_sigignore,
344 autofs_sig_set[i])) {
345 SIGDELSET(newset, autofs_sig_set[i]);
348 mtx_unlock(&curproc->p_sigacts->ps_mtx);
349 kern_sigprocmask(curthread, SIG_SETMASK, &newset, oldset,
350 SIGPROCMASK_PROC_LOCKED);
351 PROC_UNLOCK(curproc);
355 autofs_restore_sigmask(sigset_t *set)
358 kern_sigprocmask(curthread, SIG_SETMASK, set, NULL, 0);
362 autofs_trigger_one(struct autofs_node *anp,
363 const char *component, int componentlen)
366 struct autofs_mount *amp;
367 struct autofs_node *firstanp;
368 struct autofs_request *ar;
370 int error = 0, request_error, last;
375 sx_assert(&autofs_softc->sc_lock, SA_XLOCKED);
377 if (anp->an_parent == NULL) {
378 key = strndup(component, componentlen, M_AUTOFS);
380 for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
381 firstanp = firstanp->an_parent)
383 key = strdup(firstanp->an_name, M_AUTOFS);
386 path = autofs_path(anp);
388 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
389 if (strcmp(ar->ar_path, path) != 0)
391 if (strcmp(ar->ar_key, key) != 0)
394 KASSERT(strcmp(ar->ar_from, amp->am_from) == 0,
395 ("from changed; %s != %s", ar->ar_from, amp->am_from));
396 KASSERT(strcmp(ar->ar_prefix, amp->am_prefix) == 0,
397 ("prefix changed; %s != %s",
398 ar->ar_prefix, amp->am_prefix));
399 KASSERT(strcmp(ar->ar_options, amp->am_options) == 0,
400 ("options changed; %s != %s",
401 ar->ar_options, amp->am_options));
407 refcount_acquire(&ar->ar_refcount);
409 ar = uma_zalloc(autofs_request_zone, M_WAITOK | M_ZERO);
413 atomic_fetchadd_int(&autofs_softc->sc_last_request_id, 1);
414 strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
415 strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
416 strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
417 strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
418 strlcpy(ar->ar_options,
419 amp->am_options, sizeof(ar->ar_options));
421 TIMEOUT_TASK_INIT(taskqueue_thread, &ar->ar_task, 0,
423 error = taskqueue_enqueue_timeout(taskqueue_thread,
424 &ar->ar_task, autofs_timeout * hz);
426 AUTOFS_WARN("taskqueue_enqueue_timeout() failed "
427 "with error %d", error);
429 refcount_init(&ar->ar_refcount, 1);
430 TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
433 cv_broadcast(&autofs_softc->sc_cv);
434 while (ar->ar_done == false) {
435 if (autofs_interruptible != 0) {
436 autofs_set_sigmask(&oldset);
437 error = cv_wait_sig(&autofs_softc->sc_cv,
438 &autofs_softc->sc_lock);
439 autofs_restore_sigmask(&oldset);
442 * XXX: For some reson this returns -1
443 * instead of EINTR, wtf?!
446 AUTOFS_WARN("cv_wait_sig for %s failed "
447 "with error %d", ar->ar_path, error);
451 cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
455 request_error = ar->ar_error;
456 if (request_error != 0) {
457 AUTOFS_WARN("request for %s completed with error %d",
458 ar->ar_path, request_error);
461 wildcards = ar->ar_wildcards;
463 last = refcount_release(&ar->ar_refcount);
465 TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
467 * Unlock the sc_lock, so that autofs_task() can complete.
469 sx_xunlock(&autofs_softc->sc_lock);
470 taskqueue_cancel_timeout(taskqueue_thread, &ar->ar_task, NULL);
471 taskqueue_drain_timeout(taskqueue_thread, &ar->ar_task);
472 uma_zfree(autofs_request_zone, ar);
473 sx_xlock(&autofs_softc->sc_lock);
477 * Note that we do not do negative caching on purpose. This
478 * way the user can retry access at any time, e.g. after fixing
479 * the failure reason, without waiting for cache timer to expire.
481 if (error == 0 && request_error == 0 && autofs_cache > 0) {
482 anp->an_cached = true;
483 anp->an_wildcards = wildcards;
484 callout_reset(&anp->an_callout, autofs_cache * hz,
485 autofs_cache_callout, anp);
489 free(path, M_AUTOFS);
493 return (request_error);
497 * Send request to automountd(8) and wait for completion.
500 autofs_trigger(struct autofs_node *anp,
501 const char *component, int componentlen)
506 error = autofs_trigger_one(anp, component, componentlen);
511 if (error == EINTR) {
512 AUTOFS_DEBUG("trigger interrupted by signal, "
518 if (anp->an_retries >= autofs_retry_attempts) {
519 AUTOFS_DEBUG("trigger failed %d times; returning "
520 "error %d", anp->an_retries, error);
525 AUTOFS_DEBUG("trigger failed with error %d; will retry in "
526 "%d seconds, %d attempts left", error, autofs_retry_delay,
527 autofs_retry_attempts - anp->an_retries);
528 sx_xunlock(&autofs_softc->sc_lock);
529 pause("autofs_retry", autofs_retry_delay * hz);
530 sx_xlock(&autofs_softc->sc_lock);
535 autofs_ioctl_request(struct autofs_daemon_request *adr)
537 struct autofs_request *ar;
540 sx_xlock(&autofs_softc->sc_lock);
542 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
545 if (ar->ar_in_progress)
554 error = cv_wait_sig(&autofs_softc->sc_cv,
555 &autofs_softc->sc_lock);
558 * XXX: For some reson this returns -1 instead
562 sx_xunlock(&autofs_softc->sc_lock);
563 AUTOFS_DEBUG("failed with error %d", error);
568 ar->ar_in_progress = true;
569 sx_xunlock(&autofs_softc->sc_lock);
571 adr->adr_id = ar->ar_id;
572 strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
573 strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
574 strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
575 strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
576 strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
579 autofs_softc->sc_dev_sid = curproc->p_session->s_sid;
580 PROC_UNLOCK(curproc);
586 autofs_ioctl_done_101(struct autofs_daemon_done_101 *add)
588 struct autofs_request *ar;
590 sx_xlock(&autofs_softc->sc_lock);
591 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
592 if (ar->ar_id == add->add_id)
597 sx_xunlock(&autofs_softc->sc_lock);
598 AUTOFS_DEBUG("id %d not found", add->add_id);
602 ar->ar_error = add->add_error;
603 ar->ar_wildcards = true;
605 ar->ar_in_progress = false;
606 cv_broadcast(&autofs_softc->sc_cv);
608 sx_xunlock(&autofs_softc->sc_lock);
614 autofs_ioctl_done(struct autofs_daemon_done *add)
616 struct autofs_request *ar;
618 sx_xlock(&autofs_softc->sc_lock);
619 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
620 if (ar->ar_id == add->add_id)
625 sx_xunlock(&autofs_softc->sc_lock);
626 AUTOFS_DEBUG("id %d not found", add->add_id);
630 ar->ar_error = add->add_error;
631 ar->ar_wildcards = add->add_wildcards;
633 ar->ar_in_progress = false;
634 cv_broadcast(&autofs_softc->sc_cv);
636 sx_xunlock(&autofs_softc->sc_lock);
642 autofs_open(struct cdev *dev, int flags, int fmt, struct thread *td)
645 sx_xlock(&autofs_softc->sc_lock);
647 * We must never block automountd(8) and its descendants, and we use
648 * session ID to determine that: we store session id of the process
649 * that opened the device, and then compare it with session ids
650 * of triggering processes. This means running a second automountd(8)
651 * instance would break the previous one. The check below prevents
654 if (autofs_softc->sc_dev_opened) {
655 sx_xunlock(&autofs_softc->sc_lock);
659 autofs_softc->sc_dev_opened = true;
660 sx_xunlock(&autofs_softc->sc_lock);
666 autofs_close(struct cdev *dev, int flag, int fmt, struct thread *td)
669 sx_xlock(&autofs_softc->sc_lock);
670 KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
671 autofs_softc->sc_dev_opened = false;
672 sx_xunlock(&autofs_softc->sc_lock);
678 autofs_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
682 KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
686 return (autofs_ioctl_request(
687 (struct autofs_daemon_request *)arg));
689 return (autofs_ioctl_done_101(
690 (struct autofs_daemon_done_101 *)arg));
692 return (autofs_ioctl_done(
693 (struct autofs_daemon_done *)arg));
695 AUTOFS_DEBUG("invalid cmd %lx", cmd);