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
32 * Copyright (c) 1989, 1991, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software contributed to Berkeley by
36 * Rick Macklem at The University of Guelph.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include <sys/param.h>
65 #include <sys/systm.h>
68 #include <sys/dirent.h>
69 #include <sys/ioccom.h>
70 #include <sys/kernel.h>
71 #include <sys/module.h>
72 #include <sys/mount.h>
73 #include <sys/refcount.h>
75 #include <sys/sysctl.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/vnode.h>
78 #include <machine/atomic.h>
82 #include "autofs_ioctl.h"
84 MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
86 uma_zone_t autofs_request_zone;
87 uma_zone_t autofs_node_zone;
89 static int autofs_open(struct cdev *dev, int flags, int fmt,
91 static int autofs_close(struct cdev *dev, int flag, int fmt,
93 static int autofs_ioctl(struct cdev *dev, u_long cmd, caddr_t arg,
94 int mode, struct thread *td);
96 static struct cdevsw autofs_cdevsw = {
97 .d_version = D_VERSION,
98 .d_open = autofs_open,
99 .d_close = autofs_close,
100 .d_ioctl = autofs_ioctl,
105 * List of signals that can interrupt an autofs trigger. Might be a good
106 * idea to keep it synchronised with list in sys/fs/nfs/nfs_commonkrpc.c.
108 int autofs_sig_set[] = {
116 struct autofs_softc *sc;
118 SYSCTL_NODE(_vfs, OID_AUTO, autofs, CTLFLAG_RD, 0, "Automounter filesystem");
119 int autofs_debug = 1;
120 TUNABLE_INT("vfs.autofs.debug", &autofs_debug);
121 SYSCTL_INT(_vfs_autofs, OID_AUTO, debug, CTLFLAG_RWTUN,
122 &autofs_debug, 1, "Enable debug messages");
123 int autofs_mount_on_stat = 0;
124 TUNABLE_INT("vfs.autofs.mount_on_stat", &autofs_mount_on_stat);
125 SYSCTL_INT(_vfs_autofs, OID_AUTO, mount_on_stat, CTLFLAG_RWTUN,
126 &autofs_mount_on_stat, 0, "Trigger mount on stat(2) on mountpoint");
127 int autofs_timeout = 30;
128 TUNABLE_INT("vfs.autofs.timeout", &autofs_timeout);
129 SYSCTL_INT(_vfs_autofs, OID_AUTO, timeout, CTLFLAG_RWTUN,
130 &autofs_timeout, 30, "Number of seconds to wait for automountd(8)");
131 int autofs_cache = 600;
132 TUNABLE_INT("vfs.autofs.cache", &autofs_cache);
133 SYSCTL_INT(_vfs_autofs, OID_AUTO, cache, CTLFLAG_RWTUN,
134 &autofs_cache, 600, "Number of seconds to wait before reinvoking "
135 "automountd(8) for any given file or directory");
136 int autofs_retry_attempts = 3;
137 TUNABLE_INT("vfs.autofs.retry_attempts", &autofs_retry_attempts);
138 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_attempts, CTLFLAG_RWTUN,
139 &autofs_retry_attempts, 3, "Number of attempts before failing mount");
140 int autofs_retry_delay = 1;
141 TUNABLE_INT("vfs.autofs.retry_delay", &autofs_retry_delay);
142 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_delay, CTLFLAG_RWTUN,
143 &autofs_retry_delay, 1, "Number of seconds before retrying");
144 int autofs_interruptible = 1;
145 TUNABLE_INT("vfs.autofs.interruptible", &autofs_interruptible);
146 SYSCTL_INT(_vfs_autofs, OID_AUTO, interruptible, CTLFLAG_RWTUN,
147 &autofs_interruptible, 1, "Allow requests to be interrupted by signal");
150 autofs_init(struct vfsconf *vfsp)
154 sc = malloc(sizeof(*sc), M_AUTOFS, M_WAITOK | M_ZERO);
156 autofs_request_zone = uma_zcreate("autofs_request",
157 sizeof(struct autofs_request), NULL, NULL, NULL, NULL,
159 autofs_node_zone = uma_zcreate("autofs_node",
160 sizeof(struct autofs_node), NULL, NULL, NULL, NULL,
163 TAILQ_INIT(&sc->sc_requests);
164 cv_init(&sc->sc_cv, "autofscv");
165 sx_init(&sc->sc_lock, "autofslk");
167 error = make_dev_p(MAKEDEV_CHECKNAME, &sc->sc_cdev, &autofs_cdevsw,
168 NULL, UID_ROOT, GID_WHEEL, 0600, "autofs");
170 AUTOFS_WARN("failed to create device node, error %d", error);
174 sc->sc_cdev->si_drv1 = sc;
180 autofs_uninit(struct vfsconf *vfsp)
183 sx_xlock(&sc->sc_lock);
184 if (sc->sc_dev_opened) {
185 sx_xunlock(&sc->sc_lock);
188 if (sc->sc_cdev != NULL)
189 destroy_dev(sc->sc_cdev);
191 uma_zdestroy(autofs_request_zone);
192 uma_zdestroy(autofs_node_zone);
194 sx_xunlock(&sc->sc_lock);
196 * XXX: Race with open?
204 autofs_ignore_thread(const struct thread *td)
210 if (sc->sc_dev_opened == false)
214 if (p->p_session->s_sid == sc->sc_dev_sid) {
224 autofs_path(struct autofs_node *anp)
226 struct autofs_mount *amp;
231 path = strdup("", M_AUTOFS);
232 for (; anp->an_parent != NULL; anp = anp->an_parent) {
233 tmp = malloc(strlen(anp->an_name) + strlen(path) + 2,
235 strcpy(tmp, anp->an_name);
238 free(path, M_AUTOFS);
242 tmp = malloc(strlen(amp->am_mountpoint) + strlen(path) + 2,
244 strcpy(tmp, amp->am_mountpoint);
247 free(path, M_AUTOFS);
254 autofs_callout(void *context)
256 struct autofs_request *ar;
257 struct autofs_softc *sc;
260 sc = ar->ar_mount->am_softc;
262 sx_xlock(&sc->sc_lock);
263 AUTOFS_WARN("request %d for %s timed out after %d seconds",
264 ar->ar_id, ar->ar_path, autofs_timeout);
268 ar->ar_error = ETIMEDOUT;
270 ar->ar_in_progress = false;
271 cv_broadcast(&sc->sc_cv);
272 sx_xunlock(&sc->sc_lock);
276 autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
279 struct autofs_mount *amp;
283 AUTOFS_ASSERT_UNLOCKED(amp);
286 * For top-level nodes we need to request automountd(8)
287 * assistance even if the node is marked as cached,
288 * but the requested subdirectory does not exist. This
289 * is necessary for wildcard indirect map keys to work.
291 if (anp->an_parent == NULL && componentlen != 0) {
293 error = autofs_node_find(anp, component, componentlen, NULL);
299 return (anp->an_cached);
303 autofs_cache_callout(void *context)
305 struct autofs_node *anp;
308 anp->an_cached = false;
312 * The set/restore sigmask functions are used to (temporarily) overwrite
313 * the thread td_sigmask during triggering.
316 autofs_set_sigmask(sigset_t *oldset)
322 /* Remove the autofs set of signals from newset */
324 mtx_lock(&curproc->p_sigacts->ps_mtx);
325 for (i = 0 ; i < sizeof(autofs_sig_set)/sizeof(int) ; i++) {
327 * But make sure we leave the ones already masked
328 * by the process, i.e. remove the signal from the
329 * temporary signalmask only if it wasn't already
332 if (!SIGISMEMBER(curthread->td_sigmask, autofs_sig_set[i]) &&
333 !SIGISMEMBER(curproc->p_sigacts->ps_sigignore,
334 autofs_sig_set[i])) {
335 SIGDELSET(newset, autofs_sig_set[i]);
338 mtx_unlock(&curproc->p_sigacts->ps_mtx);
339 kern_sigprocmask(curthread, SIG_SETMASK, &newset, oldset,
340 SIGPROCMASK_PROC_LOCKED);
341 PROC_UNLOCK(curproc);
345 autofs_restore_sigmask(sigset_t *set)
348 kern_sigprocmask(curthread, SIG_SETMASK, set, NULL, 0);
352 autofs_trigger_one(struct autofs_node *anp,
353 const char *component, int componentlen)
356 struct autofs_mount *amp;
357 struct autofs_softc *sc;
358 struct autofs_node *firstanp;
359 struct autofs_request *ar;
361 int error = 0, request_error, last;
363 amp = VFSTOAUTOFS(anp->an_vnode->v_mount);
366 sx_assert(&sc->sc_lock, SA_XLOCKED);
368 if (anp->an_parent == NULL) {
369 key = strndup(component, componentlen, M_AUTOFS);
371 for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
372 firstanp = firstanp->an_parent)
374 key = strdup(firstanp->an_name, M_AUTOFS);
377 path = autofs_path(anp);
379 TAILQ_FOREACH(ar, &sc->sc_requests, ar_next) {
380 if (strcmp(ar->ar_path, path) != 0)
382 if (strcmp(ar->ar_key, key) != 0)
385 KASSERT(strcmp(ar->ar_from, amp->am_from) == 0,
386 ("from changed; %s != %s", ar->ar_from, amp->am_from));
387 KASSERT(strcmp(ar->ar_prefix, amp->am_prefix) == 0,
388 ("prefix changed; %s != %s",
389 ar->ar_prefix, amp->am_prefix));
390 KASSERT(strcmp(ar->ar_options, amp->am_options) == 0,
391 ("options changed; %s != %s",
392 ar->ar_options, amp->am_options));
398 refcount_acquire(&ar->ar_refcount);
400 ar = uma_zalloc(autofs_request_zone, M_WAITOK | M_ZERO);
403 ar->ar_id = atomic_fetchadd_int(&sc->sc_last_request_id, 1);
404 strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
405 strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
406 strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
407 strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
408 strlcpy(ar->ar_options,
409 amp->am_options, sizeof(ar->ar_options));
411 callout_init(&ar->ar_callout, 1);
412 callout_reset(&ar->ar_callout,
413 autofs_timeout * hz, autofs_callout, ar);
414 refcount_init(&ar->ar_refcount, 1);
415 TAILQ_INSERT_TAIL(&sc->sc_requests, ar, ar_next);
418 cv_broadcast(&sc->sc_cv);
419 while (ar->ar_done == false) {
420 if (autofs_interruptible != 0) {
421 autofs_set_sigmask(&oldset);
422 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
423 autofs_restore_sigmask(&oldset);
426 * XXX: For some reson this returns -1
427 * instead of EINTR, wtf?!
430 AUTOFS_WARN("cv_wait_sig for %s failed "
431 "with error %d", ar->ar_path, error);
435 cv_wait(&sc->sc_cv, &sc->sc_lock);
439 request_error = ar->ar_error;
440 if (request_error != 0) {
441 AUTOFS_WARN("request for %s completed with error %d",
442 ar->ar_path, request_error);
445 last = refcount_release(&ar->ar_refcount);
447 TAILQ_REMOVE(&sc->sc_requests, ar, ar_next);
451 sx_xunlock(&sc->sc_lock);
452 callout_drain(&ar->ar_callout);
453 sx_xlock(&sc->sc_lock);
454 uma_zfree(autofs_request_zone, ar);
458 * Note that we do not do negative caching on purpose. This
459 * way the user can retry access at any time, e.g. after fixing
460 * the failure reason, without waiting for cache timer to expire.
462 if (error == 0 && request_error == 0 && autofs_cache > 0) {
463 anp->an_cached = true;
464 callout_reset(&anp->an_callout, autofs_cache * hz,
465 autofs_cache_callout, anp);
469 free(path, M_AUTOFS);
473 return (request_error);
477 * Send request to automountd(8) and wait for completion.
480 autofs_trigger(struct autofs_node *anp,
481 const char *component, int componentlen)
486 error = autofs_trigger_one(anp, component, componentlen);
491 if (error == EINTR) {
492 AUTOFS_DEBUG("trigger interrupted by signal, "
498 if (anp->an_retries >= autofs_retry_attempts) {
499 AUTOFS_DEBUG("trigger failed %d times; returning "
500 "error %d", anp->an_retries, error);
505 AUTOFS_DEBUG("trigger failed with error %d; will retry in "
506 "%d seconds, %d attempts left", error, autofs_retry_delay,
507 autofs_retry_attempts - anp->an_retries);
508 sx_xunlock(&sc->sc_lock);
509 pause("autofs_retry", autofs_retry_delay * hz);
510 sx_xlock(&sc->sc_lock);
515 autofs_ioctl_request(struct autofs_softc *sc, struct autofs_daemon_request *adr)
517 struct autofs_request *ar;
520 sx_xlock(&sc->sc_lock);
522 TAILQ_FOREACH(ar, &sc->sc_requests, ar_next) {
525 if (ar->ar_in_progress)
534 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
537 * XXX: For some reson this returns -1 instead
541 sx_xunlock(&sc->sc_lock);
542 AUTOFS_DEBUG("failed with error %d", error);
547 ar->ar_in_progress = true;
548 sx_xunlock(&sc->sc_lock);
550 adr->adr_id = ar->ar_id;
551 strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
552 strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
553 strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
554 strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
555 strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
558 sc->sc_dev_sid = curproc->p_session->s_sid;
559 PROC_UNLOCK(curproc);
565 autofs_ioctl_done(struct autofs_softc *sc, struct autofs_daemon_done *add)
567 struct autofs_request *ar;
569 sx_xlock(&sc->sc_lock);
570 TAILQ_FOREACH(ar, &sc->sc_requests, ar_next) {
571 if (ar->ar_id == add->add_id)
576 sx_xunlock(&sc->sc_lock);
577 AUTOFS_DEBUG("id %d not found", add->add_id);
581 ar->ar_error = add->add_error;
583 ar->ar_in_progress = false;
584 cv_broadcast(&sc->sc_cv);
586 sx_xunlock(&sc->sc_lock);
592 autofs_open(struct cdev *dev, int flags, int fmt, struct thread *td)
595 sx_xlock(&sc->sc_lock);
596 if (sc->sc_dev_opened) {
597 sx_xunlock(&sc->sc_lock);
601 sc->sc_dev_opened = true;
602 sx_xunlock(&sc->sc_lock);
608 autofs_close(struct cdev *dev, int flag, int fmt, struct thread *td)
611 sx_xlock(&sc->sc_lock);
612 KASSERT(sc->sc_dev_opened, ("not opened?"));
613 sc->sc_dev_opened = false;
614 sx_xunlock(&sc->sc_lock);
620 autofs_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
624 KASSERT(sc->sc_dev_opened, ("not opened?"));
628 return (autofs_ioctl_request(sc,
629 (struct autofs_daemon_request *)arg));
631 return (autofs_ioctl_done(sc,
632 (struct autofs_daemon_done *)arg));
634 AUTOFS_DEBUG("invalid cmd %lx", cmd);