sys/vfs/autofs: Add Copyright
[dragonfly.git] / sys / vfs / autofs / autofs.c
1 /*-
2  * Copyright (c) 2016 Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
3  * Copyright (c) 2016 The DragonFly Project
4  * Copyright (c) 2014 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Edward Tomasz Napierala under sponsorship
8  * from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  *
31  */
32 /*-
33  * Copyright (c) 1989, 1991, 1993, 1995
34  *      The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * Rick Macklem at The University of Guelph.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
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.
50  *
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
61  * SUCH DAMAGE.
62  *
63  */
64
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>
72
73 #include "autofs.h"
74 #include "autofs_ioctl.h"
75
76 MALLOC_DEFINE(M_AUTOFS, "autofs", "Automounter filesystem");
77
78 struct objcache *autofs_request_objcache = NULL;
79 struct objcache *autofs_node_objcache = NULL;
80
81 static d_open_t         autofs_open;
82 static d_close_t        autofs_close;
83 static d_ioctl_t        autofs_ioctl;
84
85 struct dev_ops autofs_ops = {
86         { "autofs", 0, 0 },
87         .d_open         = autofs_open,
88         .d_close        = autofs_close,
89         .d_ioctl        = autofs_ioctl,
90 };
91
92 /*
93  * List of signals that can interrupt an autofs trigger.
94  */
95 static int autofs_sig_set[] = {
96         SIGINT,
97         SIGTERM,
98         SIGHUP,
99         SIGKILL,
100         SIGQUIT
101 };
102
103 struct autofs_softc     *autofs_softc = NULL;
104
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");
110 #if 0
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");
115 #endif
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");
137
138 static __inline pid_t
139 proc_pgid(const struct proc *p)
140 {
141         return (p->p_pgrp->pg_id);
142 }
143
144 static int
145 autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
146 {
147         return (strcmp(a->an_name, b->an_name));
148 }
149
150 RB_GENERATE(autofs_node_tree, autofs_node, an_link, autofs_node_cmp);
151
152 bool
153 autofs_ignore_thread(void)
154 {
155         struct proc *curp = curproc;
156
157         if (autofs_softc->sc_dev_opened == false)
158                 return (false);
159
160         lwkt_gettoken(&curp->p_token);
161         if (autofs_softc->sc_dev_sid == proc_pgid(curp)) {
162                 lwkt_reltoken(&curp->p_token);
163                 return (true);
164         }
165         lwkt_reltoken(&curp->p_token);
166
167         return (false);
168 }
169
170 char *
171 autofs_path(struct autofs_node *anp)
172 {
173         struct autofs_mount *amp = anp->an_mount;
174         size_t len;
175         char *path, *tmp;
176
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);
183                 path = tmp;
184         }
185
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);
190         path = tmp;
191
192         return (path);
193 }
194
195 static void
196 autofs_task(void *context, int pending)
197 {
198         struct autofs_request *ar = context;
199
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);
203
204         ar->ar_error = ETIMEDOUT;
205         ar->ar_wildcards = true;
206         ar->ar_done = true;
207         ar->ar_in_progress = false;
208         cv_broadcast(&autofs_softc->sc_cv);
209         lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
210 }
211
212 bool
213 autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
214 {
215         struct autofs_mount *amp = anp->an_mount;
216
217         KKASSERT(mtx_notlocked(&amp->am_lock));
218
219         /*
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
224          * no wildcards.
225          */
226         if (anp->an_parent == NULL && componentlen != 0 && anp->an_wildcards) {
227                 int error;
228                 KKASSERT(amp->am_root == anp);
229                 mtx_lock_sh_quick(&amp->am_lock);
230                 error = autofs_node_find(anp, component, componentlen, NULL);
231                 mtx_unlock_sh(&amp->am_lock);
232                 if (error)
233                         return (false);
234         }
235
236         return (anp->an_cached);
237 }
238
239 static void
240 autofs_cache_callout(void *context)
241 {
242         struct autofs_node *anp = context;
243
244         autofs_node_uncache(anp);
245 }
246
247 void
248 autofs_flush(struct autofs_mount *amp)
249 {
250         struct autofs_node *anp = amp->am_root;
251         struct autofs_node *child;
252
253         mtx_lock_ex_quick(&amp->am_lock);
254         RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
255                 autofs_node_uncache(child);
256         }
257         autofs_node_uncache(amp->am_root);
258         mtx_unlock_ex(&amp->am_lock);
259
260         AUTOFS_DEBUG("%s flushed", amp->am_on);
261 }
262
263 /*
264  * The set/restore sigmask functions are used to (temporarily) overwrite
265  * the thread sigmask during triggering.
266  */
267 static void
268 autofs_set_sigmask(sigset_t *oldset)
269 {
270         struct lwp *lp = curthread->td_lwp;
271         sigset_t newset;
272         int i;
273
274         SIGFILLSET(newset);
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++) {
278                 /*
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
282                  * in sigmask.
283                  */
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]);
288                 }
289         }
290         kern_sigprocmask(SIG_SETMASK, &newset, oldset);
291         lwkt_reltoken(&lp->lwp_token);
292 }
293
294 static void
295 autofs_restore_sigmask(sigset_t *set)
296 {
297         kern_sigprocmask(SIG_SETMASK, set, NULL);
298 }
299
300 static int
301 autofs_trigger_one(struct autofs_node *anp,
302     const char *component, int componentlen)
303 {
304 #define _taskqueue_thread (taskqueue_thread[mycpuid])
305         struct autofs_mount *amp = anp->an_mount;
306         struct autofs_request *ar;
307         char *key, *path;
308         int error = 0, request_error;
309         bool wildcards;
310
311         KKASSERT(lockstatus(&autofs_softc->sc_lock, curthread) == LK_EXCLUSIVE);
312
313         if (anp->an_parent == NULL) {
314                 key = kstrndup(component, componentlen, M_AUTOFS);
315         } else {
316                 struct autofs_node *firstanp;
317                 for (firstanp = anp; firstanp->an_parent->an_parent != NULL;
318                     firstanp = firstanp->an_parent)
319                         continue;
320                 key = kstrdup(firstanp->an_name, M_AUTOFS);
321         }
322
323         path = autofs_path(anp);
324
325         TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
326                 if (strcmp(ar->ar_path, path))
327                         continue;
328                 if (strcmp(ar->ar_key, key))
329                         continue;
330
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));
339                 break;
340         }
341
342         if (ar != NULL) {
343                 refcount_acquire(&ar->ar_refcount);
344         } else {
345                 /*
346                  * All struct fields must be initialized.
347                  */
348                 ar = objcache_get(autofs_request_objcache, M_WAITOK);
349                 ar->ar_mount = amp;
350                 ar->ar_id = autofs_softc->sc_last_request_id++;
351                 ar->ar_done = false;
352                 ar->ar_error = 0;
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,
362                     autofs_task, ar);
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);
367         }
368
369         cv_broadcast(&autofs_softc->sc_cv);
370         while (ar->ar_done == false) {
371                 if (autofs_interruptible) {
372                         sigset_t oldset;
373                         autofs_set_sigmask(&oldset);
374                         error = cv_wait_sig(&autofs_softc->sc_cv,
375                             &autofs_softc->sc_lock);
376                         autofs_restore_sigmask(&oldset);
377                         if (error) {
378                                 AUTOFS_WARN("cv_wait_sig for %s failed "
379                                     "with error %d", ar->ar_path, error);
380                                 break;
381                         }
382                 } else {
383                         cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
384                 }
385         }
386
387         request_error = ar->ar_error;
388         if (request_error)
389                 AUTOFS_WARN("request for %s completed with error %d",
390                     ar->ar_path, request_error);
391
392         wildcards = ar->ar_wildcards;
393
394         /*
395          * Check if this is the last reference.
396          */
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);
404         }
405
406         /*
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.
410          */
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);
416         }
417
418         kfree(key, M_AUTOFS);
419         kfree(path, M_AUTOFS);
420
421         if (error)
422                 return (error);
423         return (request_error);
424 }
425
426 int
427 autofs_trigger(struct autofs_node *anp,
428     const char *component, int componentlen)
429 {
430         for (;;) {
431                 int error, dummy;
432
433                 error = autofs_trigger_one(anp, component, componentlen);
434                 if (error == 0) {
435                         anp->an_retries = 0;
436                         return (0);
437                 }
438                 if (error == EINTR || error == ERESTART) {
439                         AUTOFS_DEBUG("trigger interrupted by signal, "
440                             "not retrying");
441                         anp->an_retries = 0;
442                         return (error);
443                 }
444                 anp->an_retries++;
445                 if (anp->an_retries >= autofs_retry_attempts) {
446                         AUTOFS_DEBUG("trigger failed %d times; returning "
447                             "error %d", anp->an_retries, error);
448                         anp->an_retries = 0;
449                         return (error);
450
451                 }
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);
458         }
459 }
460
461 static int
462 autofs_ioctl_request(struct autofs_daemon_request *adr)
463 {
464         struct proc *curp = curproc;
465         struct autofs_request *ar;
466
467         lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
468         for (;;) {
469                 int error;
470                 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
471                         if (ar->ar_done)
472                                 continue;
473                         if (ar->ar_in_progress)
474                                 continue;
475                         break;
476                 }
477
478                 if (ar != NULL)
479                         break;
480
481                 error = cv_wait_sig(&autofs_softc->sc_cv,
482                     &autofs_softc->sc_lock);
483                 if (error) {
484                         lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
485                         return (error);
486                 }
487         }
488
489         ar->ar_in_progress = true;
490
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));
497
498         lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
499
500         lwkt_gettoken(&curp->p_token);
501         autofs_softc->sc_dev_sid = proc_pgid(curp);
502         lwkt_reltoken(&curp->p_token);
503
504         return (0);
505 }
506
507 static int
508 autofs_ioctl_done(struct autofs_daemon_done *add)
509 {
510         struct autofs_request *ar;
511
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)
515                         break;
516         }
517
518         if (ar == NULL) {
519                 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
520                 AUTOFS_DEBUG("id %d not found", add->add_id);
521                 return (ESRCH);
522         }
523
524         ar->ar_error = add->add_error;
525         ar->ar_wildcards = add->add_wildcards;
526         ar->ar_done = true;
527         ar->ar_in_progress = false;
528         cv_broadcast(&autofs_softc->sc_cv);
529
530         lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
531
532         return (0);
533 }
534
535 static int
536 autofs_open(struct dev_open_args *ap)
537 {
538         lockmgr(&autofs_softc->sc_lock, LK_EXCLUSIVE);
539         /*
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
545          * it from happening.
546          */
547         if (autofs_softc->sc_dev_opened) {
548                 lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
549                 return (EBUSY);
550         }
551
552         autofs_softc->sc_dev_opened = true;
553         lockmgr(&autofs_softc->sc_lock, LK_RELEASE);
554
555         return (0);
556 }
557
558 static int
559 autofs_close(struct dev_close_args *ap)
560 {
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);
565
566         return (0);
567 }
568
569 static int
570 autofs_ioctl(struct dev_ioctl_args *ap)
571 {
572         u_long cmd = ap->a_cmd;
573         void *arg = ap->a_data;
574
575         KASSERT(autofs_softc->sc_dev_opened, ("not opened?"));
576
577         switch (cmd) {
578         case AUTOFSREQUEST:
579                 return (autofs_ioctl_request(
580                     (struct autofs_daemon_request *)arg));
581         case AUTOFSDONE:
582                 return (autofs_ioctl_done(
583                     (struct autofs_daemon_done *)arg));
584         default:
585                 AUTOFS_DEBUG("invalid cmd %lx", cmd);
586                 return (EINVAL);
587         }
588         return (EINVAL);
589 }