2 * Copyright (c) 2016 The DragonFly Project
3 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * 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 * 3. 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/kernel.h>
65 #include <sys/module.h>
66 #include <sys/sysctl.h>
67 #include <sys/queue.h>
68 #include <sys/signalvar.h>
69 #include <sys/refcount.h>
70 #include <sys/kern_syscall.h>
73 #include "autofs_ioctl.h"
75 MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
77 struct objcache *autofs_request_objcache = NULL;
78 struct objcache *autofs_node_objcache = NULL;
80 static int autofs_open(struct dev_open_args *ap);
81 static int autofs_close(struct dev_close_args *ap);
82 static int autofs_ioctl(struct dev_ioctl_args *ap);
84 struct dev_ops autofs_ops = {
86 .d_open = autofs_open,
87 .d_close = autofs_close,
88 .d_ioctl = autofs_ioctl,
92 * List of signals that can interrupt an autofs trigger.
94 static int autofs_sig_set[] = {
102 struct autofs_softc *autofs_softc = NULL;
104 SYSCTL_NODE(_vfs, OID_AUTO, autofs, CTLFLAG_RD, 0, "Automounter filesystem");
105 int autofs_debug = 1;
106 TUNABLE_INT("vfs.autofs.debug", &autofs_debug);
107 SYSCTL_INT(_vfs_autofs, OID_AUTO, debug, CTLFLAG_RW,
108 &autofs_debug, 1, "Enable debug messages");
109 int autofs_mount_on_stat = 0; /* XXX: Not supported on DragonFly */
110 TUNABLE_INT("vfs.autofs.mount_on_stat", &autofs_mount_on_stat);
111 SYSCTL_INT(_vfs_autofs, OID_AUTO, mount_on_stat, CTLFLAG_RW,
112 &autofs_mount_on_stat, 0, "Trigger mount on stat(2) on mountpoint "
113 "(not supported on DragonFly)");
114 static int autofs_timeout = 30;
115 TUNABLE_INT("vfs.autofs.timeout", &autofs_timeout);
116 SYSCTL_INT(_vfs_autofs, OID_AUTO, timeout, CTLFLAG_RW,
117 &autofs_timeout, 30, "Number of seconds to wait for automountd(8)");
118 static int autofs_cache = 600;
119 TUNABLE_INT("vfs.autofs.cache", &autofs_cache);
120 SYSCTL_INT(_vfs_autofs, OID_AUTO, cache, CTLFLAG_RW,
121 &autofs_cache, 600, "Number of seconds to wait before reinvoking "
122 "automountd(8) for any given file or directory");
123 static int autofs_retry_attempts = 3;
124 TUNABLE_INT("vfs.autofs.retry_attempts", &autofs_retry_attempts);
125 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_attempts, CTLFLAG_RW,
126 &autofs_retry_attempts, 3, "Number of attempts before failing mount");
127 static int autofs_retry_delay = 1;
128 TUNABLE_INT("vfs.autofs.retry_delay", &autofs_retry_delay);
129 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_delay, CTLFLAG_RW,
130 &autofs_retry_delay, 1, "Number of seconds before retrying");
131 static int autofs_interruptible = 1;
132 TUNABLE_INT("vfs.autofs.interruptible", &autofs_interruptible);
133 SYSCTL_INT(_vfs_autofs, OID_AUTO, interruptible, CTLFLAG_RW,
134 &autofs_interruptible, 1, "Allow requests to be interrupted by signal");
136 static __inline pid_t
137 proc_pgid(const struct proc *p)
139 return (p->p_pgrp->pg_id);
143 autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
145 return (strcmp(a->an_name, b->an_name));
148 RB_GENERATE(autofs_node_tree, autofs_node, an_link, autofs_node_cmp);
151 autofs_ignore_thread(void)
153 struct proc *curp = curproc;
155 if (autofs_softc->sc_dev_opened == false)
158 lwkt_gettoken(&curp->p_token);
159 if (autofs_softc->sc_dev_sid == proc_pgid(curp)) {
160 lwkt_reltoken(&curp->p_token);
163 lwkt_reltoken(&curp->p_token);
169 autofs_path(struct autofs_node *anp)
171 struct autofs_mount *amp = anp->an_mount;
174 path = kstrdup("", M_AUTOFS);
175 for (; anp->an_parent != NULL; anp = anp->an_parent) {
176 tmp = kmalloc(strlen(anp->an_name) + strlen(path) + 2,
178 strcpy(tmp, anp->an_name);
181 kfree(path, M_AUTOFS);
185 tmp = kmalloc(strlen(amp->am_on) + strlen(path) + 2,
187 strcpy(tmp, amp->am_on);
190 kfree(path, M_AUTOFS);
197 autofs_task(void *context, int pending)
199 struct autofs_request *ar = context;
201 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
202 AUTOFS_WARN("request %d for %s timed out after %d seconds",
203 ar->ar_id, ar->ar_path, autofs_timeout);
205 ar->ar_error = ETIMEDOUT;
206 ar->ar_wildcards = true;
208 ar->ar_in_progress = false;
209 cv_broadcast(&autofs_softc->sc_cv);
210 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
214 autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
216 struct autofs_mount *amp = anp->an_mount;
218 KKASSERT(mtx_notlocked(&->am_lock));
221 * For root node we need to request automountd(8) assistance even
222 * if the node is marked as cached, but the requested top-level
223 * directory does not exist. This is necessary for wildcard indirect
224 * map keys to work. We don't do this if we know that there are
227 if (anp->an_parent == NULL && componentlen != 0 && anp->an_wildcards) {
229 KKASSERT(amp->am_root == anp);
230 mtx_lock_sh_quick(&->am_lock);
231 error = autofs_node_find(anp, component, componentlen, NULL);
232 mtx_unlock_sh(&->am_lock);
237 return (anp->an_cached);
241 autofs_cache_callout(void *context)
243 struct autofs_node *anp = context;
245 autofs_node_uncache(anp);
249 autofs_flush(struct autofs_mount *amp)
251 struct autofs_node *anp = amp->am_root;
252 struct autofs_node *child;
254 mtx_lock_ex_quick(&->am_lock);
255 RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
256 autofs_node_uncache(child);
258 autofs_node_uncache(amp->am_root);
259 mtx_unlock_ex(&->am_lock);
261 AUTOFS_DEBUG("%s flushed", amp->am_on);
265 * The set/restore sigmask functions are used to (temporarily) overwrite
266 * the thread sigmask during triggering.
269 autofs_set_sigmask(sigset_t *oldset)
271 struct lwp *lp = curthread->td_lwp;
276 /* Remove the autofs set of signals from newset */
277 lwkt_gettoken(&lp->lwp_token);
278 for (i = 0; i < nitems(autofs_sig_set); i++) {
280 * But make sure we leave the ones already masked
281 * by the process, i.e. remove the signal from the
282 * temporary signalmask only if it wasn't already
285 if (!SIGISMEMBER(lp->lwp_sigmask, autofs_sig_set[i]) &&
286 !SIGISMEMBER(lp->lwp_proc->p_sigacts->ps_sigignore,
287 autofs_sig_set[i])) {
288 SIGDELSET(newset, autofs_sig_set[i]);
291 kern_sigprocmask(SIG_SETMASK, &newset, oldset);
292 lwkt_reltoken(&lp->lwp_token);
296 autofs_restore_sigmask(sigset_t *set)
298 kern_sigprocmask(SIG_SETMASK, set, NULL);
302 autofs_trigger_one(struct autofs_node *anp,
303 const char *component, int componentlen)
305 #define _taskqueue_thread (taskqueue_thread[mycpuid])
306 struct autofs_mount *amp = anp->an_mount;
307 struct autofs_request *ar;
309 int error = 0, request_error;
312 KKASSERT(lockstatus(&autofs_softc->sc_lock, curthread) == LK_EXCLUSIVE);
314 if (anp->an_parent == NULL) {
315 key = kstrndup(component, componentlen, M_AUTOFS);
317 struct autofs_node *firstanp;
318 for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
319 firstanp = firstanp->an_parent)
321 key = kstrdup(firstanp->an_name, M_AUTOFS);
324 path = autofs_path(anp);
326 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
327 if (strcmp(ar->ar_path, path))
329 if (strcmp(ar->ar_key, key))
332 KASSERT(strcmp(ar->ar_from, amp->am_from) == 0,
333 ("from changed; %s != %s", ar->ar_from, amp->am_from));
334 KASSERT(strcmp(ar->ar_prefix, amp->am_prefix) == 0,
335 ("prefix changed; %s != %s",
336 ar->ar_prefix, amp->am_prefix));
337 KASSERT(strcmp(ar->ar_options, amp->am_options) == 0,
338 ("options changed; %s != %s",
339 ar->ar_options, amp->am_options));
344 refcount_acquire(&ar->ar_refcount);
346 ar = objcache_get(autofs_request_objcache, M_WAITOK);
348 ar->ar_id = autofs_softc->sc_last_request_id++;
351 ar->ar_wildcards = false;
352 ar->ar_in_progress = false;
353 strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
354 strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
355 strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
356 strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
357 strlcpy(ar->ar_options,
358 amp->am_options, sizeof(ar->ar_options));
359 TIMEOUT_TASK_INIT(_taskqueue_thread, &ar->ar_task, 0,
361 taskqueue_enqueue_timeout(_taskqueue_thread, &ar->ar_task,
362 autofs_timeout * hz);
363 refcount_init(&ar->ar_refcount, 1);
364 TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
367 cv_broadcast(&autofs_softc->sc_cv);
368 while (ar->ar_done == false) {
369 if (autofs_interruptible) {
371 autofs_set_sigmask(&oldset);
372 error = cv_wait_sig(&autofs_softc->sc_cv,
373 &autofs_softc->sc_lock);
374 autofs_restore_sigmask(&oldset);
376 AUTOFS_WARN("cv_wait_sig for %s failed "
377 "with error %d", ar->ar_path, error);
381 cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
385 request_error = ar->ar_error;
387 AUTOFS_WARN("request for %s completed with error %d",
388 ar->ar_path, request_error);
390 wildcards = ar->ar_wildcards;
393 * Check if this is the last reference.
395 if (refcount_release(&ar->ar_refcount)) {
396 TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
397 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
398 taskqueue_cancel_timeout(_taskqueue_thread, &ar->ar_task, NULL);
399 taskqueue_drain_timeout(_taskqueue_thread, &ar->ar_task);
400 objcache_put(autofs_request_objcache, ar);
401 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
405 * Note that we do not do negative caching on purpose. This
406 * way the user can retry access at any time, e.g. after fixing
407 * the failure reason, without waiting for cache timer to expire.
409 if (error == 0 && request_error == 0 && autofs_cache > 0) {
410 autofs_node_cache(anp);
411 anp->an_wildcards = wildcards;
412 callout_reset(&anp->an_callout, autofs_cache * hz,
413 autofs_cache_callout, anp);
416 kfree(key, M_AUTOFS);
417 kfree(path, M_AUTOFS);
421 return (request_error);
425 autofs_trigger(struct autofs_node *anp,
426 const char *component, int componentlen)
431 error = autofs_trigger_one(anp, component, componentlen);
436 if (error == EINTR || error == ERESTART) {
437 AUTOFS_DEBUG("trigger interrupted by signal, "
443 if (anp->an_retries >= autofs_retry_attempts) {
444 AUTOFS_DEBUG("trigger failed %d times; returning "
445 "error %d", anp->an_retries, error);
450 AUTOFS_DEBUG("trigger failed with error %d; will retry in "
451 "%d seconds, %d attempts left", error, autofs_retry_delay,
452 autofs_retry_attempts - anp->an_retries);
453 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
454 tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz);
455 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
460 autofs_ioctl_request(struct autofs_daemon_request *adr)
462 struct proc *curp = curproc;
463 struct autofs_request *ar;
465 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
468 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
471 if (ar->ar_in_progress)
479 error = cv_wait_sig(&autofs_softc->sc_cv,
480 &autofs_softc->sc_lock);
482 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
487 ar->ar_in_progress = true;
489 adr->adr_id = ar->ar_id;
490 strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
491 strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
492 strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
493 strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
494 strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
496 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
498 lwkt_gettoken(&curp->p_token);
499 autofs_softc->sc_dev_sid = proc_pgid(curp);
500 lwkt_reltoken(&curp->p_token);
506 autofs_ioctl_done(struct autofs_daemon_done *add)
508 struct autofs_request *ar;
510 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
511 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
512 if (ar->ar_id == add->add_id)
517 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
518 AUTOFS_DEBUG("id %d not found", add->add_id);
522 ar->ar_error = add->add_error;
523 ar->ar_wildcards = add->add_wildcards;
525 ar->ar_in_progress = false;
526 cv_broadcast(&autofs_softc->sc_cv);
528 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
534 autofs_open(struct dev_open_args *ap)
536 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
538 * We must never block automountd(8) and its descendants, and we use
539 * session ID to determine that: we store session id of the process
540 * that opened the device, and then compare it with session ids
541 * of triggering processes. This means running a second automountd(8)
542 * instance would break the previous one. The check below prevents
545 if (autofs_softc->sc_dev_opened) {
546 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
550 autofs_softc->sc_dev_opened = true;
551 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
557 autofs_close(struct dev_close_args *ap)
559 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
560 KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
561 autofs_softc->sc_dev_opened = false;
562 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
568 autofs_ioctl(struct dev_ioctl_args *ap)
570 u_long cmd = ap->a_cmd;
571 void *arg = ap->a_data;
573 KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
577 return (autofs_ioctl_request(
578 (struct autofs_daemon_request *)arg));
580 return (autofs_ioctl_done(
581 (struct autofs_daemon_done *)arg));
583 AUTOFS_DEBUG("invalid cmd %lx", cmd);