2 * Copyright (c) 2016 Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
3 * Copyright (c) 2016 The DragonFly Project
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1989, 1991, 1993, 1995
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * Rick Macklem at The University of Guelph.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/kernel.h>
66 #include <sys/module.h>
67 #include <sys/sysctl.h>
68 #include <sys/queue.h>
69 #include <sys/signalvar.h>
70 #include <sys/refcount.h>
71 #include <sys/kern_syscall.h>
74 #include "autofs_ioctl.h"
76 MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
78 struct objcache *autofs_request_objcache = NULL;
79 struct objcache *autofs_node_objcache = NULL;
81 static d_open_t autofs_open;
82 static d_close_t autofs_close;
83 static d_ioctl_t autofs_ioctl;
85 struct dev_ops autofs_ops = {
87 .d_open = autofs_open,
88 .d_close = autofs_close,
89 .d_ioctl = autofs_ioctl,
93 * List of signals that can interrupt an autofs trigger.
95 static int autofs_sig_set[] = {
103 struct autofs_softc *autofs_softc = NULL;
105 SYSCTL_NODE(_vfs, OID_AUTO, autofs, CTLFLAG_RD, 0, "Automounter filesystem");
106 int autofs_debug = 1;
107 TUNABLE_INT("vfs.autofs.debug", &autofs_debug);
108 SYSCTL_INT(_vfs_autofs, OID_AUTO, debug, CTLFLAG_RW,
109 &autofs_debug, 1, "Enable debug messages");
111 int autofs_mount_on_stat = 0;
112 TUNABLE_INT("vfs.autofs.mount_on_stat", &autofs_mount_on_stat);
113 SYSCTL_INT(_vfs_autofs, OID_AUTO, mount_on_stat, CTLFLAG_RW,
114 &autofs_mount_on_stat, 0, "Trigger mount on stat(2) on mountpoint");
116 static int autofs_timeout = 30;
117 TUNABLE_INT("vfs.autofs.timeout", &autofs_timeout);
118 SYSCTL_INT(_vfs_autofs, OID_AUTO, timeout, CTLFLAG_RW,
119 &autofs_timeout, 30, "Number of seconds to wait for automountd(8)");
120 static int autofs_cache = 600;
121 TUNABLE_INT("vfs.autofs.cache", &autofs_cache);
122 SYSCTL_INT(_vfs_autofs, OID_AUTO, cache, CTLFLAG_RW,
123 &autofs_cache, 600, "Number of seconds to wait before reinvoking "
124 "automountd(8) for any given file or directory");
125 static int autofs_retry_attempts = 3;
126 TUNABLE_INT("vfs.autofs.retry_attempts", &autofs_retry_attempts);
127 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_attempts, CTLFLAG_RW,
128 &autofs_retry_attempts, 3, "Number of attempts before failing mount");
129 static int autofs_retry_delay = 1;
130 TUNABLE_INT("vfs.autofs.retry_delay", &autofs_retry_delay);
131 SYSCTL_INT(_vfs_autofs, OID_AUTO, retry_delay, CTLFLAG_RW,
132 &autofs_retry_delay, 1, "Number of seconds before retrying");
133 static int autofs_interruptible = 1;
134 TUNABLE_INT("vfs.autofs.interruptible", &autofs_interruptible);
135 SYSCTL_INT(_vfs_autofs, OID_AUTO, interruptible, CTLFLAG_RW,
136 &autofs_interruptible, 1, "Allow requests to be interrupted by signal");
138 static __inline pid_t
139 proc_pgid(const struct proc *p)
141 return (p->p_pgrp->pg_id);
145 autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
147 return (strcmp(a->an_name, b->an_name));
150 RB_GENERATE(autofs_node_tree, autofs_node, an_link, autofs_node_cmp);
153 autofs_ignore_thread(void)
155 struct proc *curp = curproc;
157 if (autofs_softc->sc_dev_opened == false)
160 lwkt_gettoken(&curp->p_token);
161 if (autofs_softc->sc_dev_sid == proc_pgid(curp)) {
162 lwkt_reltoken(&curp->p_token);
165 lwkt_reltoken(&curp->p_token);
171 autofs_path(struct autofs_node *anp)
173 struct autofs_mount *amp = anp->an_mount;
177 path = kstrdup("", M_AUTOFS);
178 for (; anp->an_parent != NULL; anp = anp->an_parent) {
179 len = strlen(anp->an_name) + strlen(path) + 2;
180 tmp = kmalloc(len, M_AUTOFS, M_WAITOK);
181 ksnprintf(tmp, len, "%s/%s", anp->an_name, path);
182 kfree(path, M_AUTOFS);
186 len = strlen(amp->am_on) + strlen(path) + 2;
187 tmp = kmalloc(len, M_AUTOFS, M_WAITOK);
188 ksnprintf(tmp, len, "%s/%s", amp->am_on, path);
189 kfree(path, M_AUTOFS);
196 autofs_task(void *context, int pending)
198 struct autofs_request *ar = context;
200 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
201 AUTOFS_WARN("request %d for %s timed out after %d seconds",
202 ar->ar_id, ar->ar_path, autofs_timeout);
204 ar->ar_error = ETIMEDOUT;
205 ar->ar_wildcards = true;
207 ar->ar_in_progress = false;
208 cv_broadcast(&autofs_softc->sc_cv);
209 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
213 autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
215 struct autofs_mount *amp = anp->an_mount;
217 KKASSERT(mtx_notlocked(&->am_lock));
220 * For root node we need to request automountd(8) assistance even
221 * if the node is marked as cached, but the requested top-level
222 * directory does not exist. This is necessary for wildcard indirect
223 * map keys to work. We don't do this if we know that there are
226 if (anp->an_parent == NULL && componentlen != 0 && anp->an_wildcards) {
228 KKASSERT(amp->am_root == anp);
229 mtx_lock_sh_quick(&->am_lock);
230 error = autofs_node_find(anp, component, componentlen, NULL);
231 mtx_unlock_sh(&->am_lock);
236 return (anp->an_cached);
240 autofs_cache_callout(void *context)
242 struct autofs_node *anp = context;
244 autofs_node_uncache(anp);
248 autofs_flush(struct autofs_mount *amp)
250 struct autofs_node *anp = amp->am_root;
251 struct autofs_node *child;
253 mtx_lock_ex_quick(&->am_lock);
254 RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
255 autofs_node_uncache(child);
257 autofs_node_uncache(amp->am_root);
258 mtx_unlock_ex(&->am_lock);
260 AUTOFS_DEBUG("%s flushed", amp->am_on);
264 * The set/restore sigmask functions are used to (temporarily) overwrite
265 * the thread sigmask during triggering.
268 autofs_set_sigmask(sigset_t *oldset)
270 struct lwp *lp = curthread->td_lwp;
275 /* Remove the autofs set of signals from newset */
276 lwkt_gettoken(&lp->lwp_token);
277 for (i = 0; i < nitems(autofs_sig_set); i++) {
279 * But make sure we leave the ones already masked
280 * by the process, i.e. remove the signal from the
281 * temporary signalmask only if it wasn't already
284 if (!SIGISMEMBER(lp->lwp_sigmask, autofs_sig_set[i]) &&
285 !SIGISMEMBER(lp->lwp_proc->p_sigacts->ps_sigignore,
286 autofs_sig_set[i])) {
287 SIGDELSET(newset, autofs_sig_set[i]);
290 kern_sigprocmask(SIG_SETMASK, &newset, oldset);
291 lwkt_reltoken(&lp->lwp_token);
295 autofs_restore_sigmask(sigset_t *set)
297 kern_sigprocmask(SIG_SETMASK, set, NULL);
301 autofs_trigger_one(struct autofs_node *anp,
302 const char *component, int componentlen)
304 #define _taskqueue_thread (taskqueue_thread[mycpuid])
305 struct autofs_mount *amp = anp->an_mount;
306 struct autofs_request *ar;
308 int error = 0, request_error;
311 KKASSERT(lockstatus(&autofs_softc->sc_lock, curthread) == LK_EXCLUSIVE);
313 if (anp->an_parent == NULL) {
314 key = kstrndup(component, componentlen, M_AUTOFS);
316 struct autofs_node *firstanp;
317 for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
318 firstanp = firstanp->an_parent)
320 key = kstrdup(firstanp->an_name, M_AUTOFS);
323 path = autofs_path(anp);
325 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
326 if (strcmp(ar->ar_path, path))
328 if (strcmp(ar->ar_key, key))
331 KASSERT(strcmp(ar->ar_from, amp->am_from) == 0,
332 ("from changed; %s != %s", ar->ar_from, amp->am_from));
333 KASSERT(strcmp(ar->ar_prefix, amp->am_prefix) == 0,
334 ("prefix changed; %s != %s",
335 ar->ar_prefix, amp->am_prefix));
336 KASSERT(strcmp(ar->ar_options, amp->am_options) == 0,
337 ("options changed; %s != %s",
338 ar->ar_options, amp->am_options));
343 refcount_acquire(&ar->ar_refcount);
346 * All struct fields must be initialized.
348 ar = objcache_get(autofs_request_objcache, M_WAITOK);
350 ar->ar_id = autofs_softc->sc_last_request_id++;
353 ar->ar_wildcards = false;
354 ar->ar_in_progress = false;
355 strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
356 strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
357 strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
358 strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
359 strlcpy(ar->ar_options,
360 amp->am_options, sizeof(ar->ar_options));
361 TIMEOUT_TASK_INIT(_taskqueue_thread, &ar->ar_task, 0,
363 taskqueue_enqueue_timeout(_taskqueue_thread, &ar->ar_task,
364 autofs_timeout * hz);
365 refcount_init(&ar->ar_refcount, 1);
366 TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
369 cv_broadcast(&autofs_softc->sc_cv);
370 while (ar->ar_done == false) {
371 if (autofs_interruptible) {
373 autofs_set_sigmask(&oldset);
374 error = cv_wait_sig(&autofs_softc->sc_cv,
375 &autofs_softc->sc_lock);
376 autofs_restore_sigmask(&oldset);
378 AUTOFS_WARN("cv_wait_sig for %s failed "
379 "with error %d", ar->ar_path, error);
383 cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
387 request_error = ar->ar_error;
389 AUTOFS_WARN("request for %s completed with error %d",
390 ar->ar_path, request_error);
392 wildcards = ar->ar_wildcards;
395 * Check if this is the last reference.
397 if (refcount_release(&ar->ar_refcount)) {
398 TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
399 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
400 taskqueue_cancel_timeout(_taskqueue_thread, &ar->ar_task, NULL);
401 taskqueue_drain_timeout(_taskqueue_thread, &ar->ar_task);
402 objcache_put(autofs_request_objcache, ar);
403 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
407 * Note that we do not do negative caching on purpose. This
408 * way the user can retry access at any time, e.g. after fixing
409 * the failure reason, without waiting for cache timer to expire.
411 if (error == 0 && request_error == 0 && autofs_cache > 0) {
412 autofs_node_cache(anp);
413 anp->an_wildcards = wildcards;
414 callout_reset(&anp->an_callout, autofs_cache * hz,
415 autofs_cache_callout, anp);
418 kfree(key, M_AUTOFS);
419 kfree(path, M_AUTOFS);
423 return (request_error);
427 autofs_trigger(struct autofs_node *anp,
428 const char *component, int componentlen)
433 error = autofs_trigger_one(anp, component, componentlen);
438 if (error == EINTR || error == ERESTART) {
439 AUTOFS_DEBUG("trigger interrupted by signal, "
445 if (anp->an_retries >= autofs_retry_attempts) {
446 AUTOFS_DEBUG("trigger failed %d times; returning "
447 "error %d", anp->an_retries, error);
452 AUTOFS_DEBUG("trigger failed with error %d; will retry in "
453 "%d seconds, %d attempts left", error, autofs_retry_delay,
454 autofs_retry_attempts - anp->an_retries);
455 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
456 tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz);
457 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
462 autofs_ioctl_request(struct autofs_daemon_request *adr)
464 struct proc *curp = curproc;
465 struct autofs_request *ar;
467 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
470 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
473 if (ar->ar_in_progress)
481 error = cv_wait_sig(&autofs_softc->sc_cv,
482 &autofs_softc->sc_lock);
484 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
489 ar->ar_in_progress = true;
491 adr->adr_id = ar->ar_id;
492 strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
493 strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
494 strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
495 strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
496 strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
498 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
500 lwkt_gettoken(&curp->p_token);
501 autofs_softc->sc_dev_sid = proc_pgid(curp);
502 lwkt_reltoken(&curp->p_token);
508 autofs_ioctl_done(struct autofs_daemon_done *add)
510 struct autofs_request *ar;
512 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
513 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
514 if (ar->ar_id == add->add_id)
519 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
520 AUTOFS_DEBUG("id %d not found", add->add_id);
524 ar->ar_error = add->add_error;
525 ar->ar_wildcards = add->add_wildcards;
527 ar->ar_in_progress = false;
528 cv_broadcast(&autofs_softc->sc_cv);
530 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
536 autofs_open(struct dev_open_args *ap)
538 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
540 * We must never block automountd(8) and its descendants, and we use
541 * session ID to determine that: we store session id of the process
542 * that opened the device, and then compare it with session ids
543 * of triggering processes. This means running a second automountd(8)
544 * instance would break the previous one. The check below prevents
547 if (autofs_softc->sc_dev_opened) {
548 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
552 autofs_softc->sc_dev_opened = true;
553 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
559 autofs_close(struct dev_close_args *ap)
561 lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
562 KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
563 autofs_softc->sc_dev_opened = false;
564 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
570 autofs_ioctl(struct dev_ioctl_args *ap)
572 u_long cmd = ap->a_cmd;
573 void *arg = ap->a_data;
575 KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
579 return (autofs_ioctl_request(
580 (struct autofs_daemon_request *)arg));
582 return (autofs_ioctl_done(
583 (struct autofs_daemon_done *)arg));
585 AUTOFS_DEBUG("invalid cmd %lx", cmd);