autofs.c revision 1.4 1 /* $NetBSD: autofs.c,v 1.4 2019/12/14 12:01:13 tkusumi Exp $ */
2
3 /*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * Copyright (c) 2016 The DragonFly Project
6 * Copyright (c) 2014 The FreeBSD Foundation
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Tomohiro Kusumi <kusumi.tomohiro (at) gmail.com>.
11 *
12 * This software was developed by Edward Tomasz Napierala under sponsorship
13 * from the FreeBSD Foundation.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 */
37 /*-
38 * Copyright (c) 1989, 1991, 1993, 1995
39 * The Regents of the University of California. All rights reserved.
40 *
41 * This code is derived from software contributed to Berkeley by
42 * Rick Macklem at The University of Guelph.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: autofs.c,v 1.4 2019/12/14 12:01:13 tkusumi Exp $");
72
73 #include "autofs.h"
74
75 #include "ioconf.h"
76
77 #include <sys/atomic.h>
78 #include <sys/queue.h>
79 #include <sys/signalvar.h>
80
81 static dev_type_open(autofs_open);
82 static dev_type_close(autofs_close);
83 static dev_type_ioctl(autofs_ioctl);
84
85 const struct cdevsw autofs_cdevsw = {
86 .d_open = autofs_open,
87 .d_close = autofs_close,
88 .d_read = noread,
89 .d_write = nowrite,
90 .d_ioctl = autofs_ioctl,
91 .d_stop = nostop,
92 .d_tty = notty,
93 .d_poll = nopoll,
94 .d_mmap = nommap,
95 .d_kqfilter = nokqfilter,
96 .d_discard = nodiscard,
97 .d_flag = D_OTHER,
98 };
99
100 /*
101 * List of signals that can interrupt an autofs trigger.
102 */
103 static int autofs_sig_set[] = {
104 SIGINT,
105 SIGTERM,
106 SIGHUP,
107 SIGKILL,
108 SIGQUIT,
109 };
110
111 struct pool autofs_request_pool;
112 struct pool autofs_node_pool;
113 struct autofs_softc *autofs_softc = NULL;
114 struct workqueue *autofs_tmo_wq = NULL;
115
116 int autofs_debug = 1;
117 int autofs_mount_on_stat = 0;
118 int autofs_timeout = 30;
119 int autofs_cache = 600;
120 int autofs_retry_attempts = 3;
121 int autofs_retry_delay = 1;
122 int autofs_interruptible = 1;
123
124 void
125 autofsattach(int n)
126 {
127 }
128
129 static int
130 autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
131 {
132
133 return strcmp(a->an_name, b->an_name);
134 }
135
136 RB_GENERATE(autofs_node_tree, autofs_node, an_entry, autofs_node_cmp);
137
138 bool
139 autofs_ignore_thread(void)
140 {
141
142 if (autofs_softc->sc_dev_opened == false)
143 return false;
144
145 mutex_enter(proc_lock);
146 if (autofs_softc->sc_dev_sid == curproc->p_pgrp->pg_id) {
147 mutex_exit(proc_lock);
148 return true;
149 }
150 mutex_exit(proc_lock);
151
152 return false;
153 }
154
155 static char *
156 autofs_path(struct autofs_node *anp)
157 {
158 struct autofs_mount *amp = anp->an_mount;
159 size_t len;
160 char *path, *tmp;
161
162 path = kmem_strdup("", KM_SLEEP);
163 for (; anp->an_parent; anp = anp->an_parent) {
164 len = strlen(anp->an_name) + strlen(path) + 2;
165 tmp = kmem_alloc(len, KM_SLEEP);
166 snprintf(tmp, len, "%s/%s", anp->an_name, path);
167 kmem_strfree(path);
168 path = tmp;
169 }
170
171 len = strlen(amp->am_on) + strlen(path) + 2;
172 tmp = kmem_alloc(len, KM_SLEEP);
173 snprintf(tmp, len, "%s/%s", amp->am_on, path);
174 kmem_strfree(path);
175
176 return tmp;
177 }
178
179 void
180 autofs_timeout_wq(struct work *wk, void *arg)
181 {
182 struct autofs_request *ar = (void *)wk;
183
184 mutex_enter(&autofs_softc->sc_lock);
185 AUTOFS_WARN("request %d for %s timed out after %d seconds",
186 ar->ar_id, ar->ar_path, autofs_timeout);
187
188 ar->ar_error = ETIMEDOUT;
189 ar->ar_wildcards = true;
190 ar->ar_done = true;
191 ar->ar_in_progress = false;
192 cv_broadcast(&autofs_softc->sc_cv);
193 mutex_exit(&autofs_softc->sc_lock);
194 }
195
196 static void
197 autofs_timeout_callout(void *context)
198 {
199 struct autofs_request *ar = context;
200
201 workqueue_enqueue(autofs_tmo_wq, &ar->ar_wk, NULL);
202 }
203
204 bool
205 autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
206 {
207 struct autofs_mount *amp = anp->an_mount;
208
209 KASSERT(!mutex_owned(&->am_lock));
210
211 /*
212 * For root node we need to request automountd(8) assistance even
213 * if the node is marked as cached, but the requested top-level
214 * directory does not exist. This is necessary for wildcard indirect
215 * map keys to work. We don't do this if we know that there are
216 * no wildcards.
217 */
218 if (!anp->an_parent && componentlen && anp->an_wildcards) {
219 int error;
220 KASSERT(amp->am_root == anp);
221 mutex_enter(&->am_lock);
222 error = autofs_node_find(anp, component, componentlen, NULL);
223 mutex_exit(&->am_lock);
224 if (error)
225 return false;
226 }
227
228 return anp->an_cached;
229 }
230
231 static void
232 autofs_cache_callout(void *context)
233 {
234 struct autofs_node *anp = context;
235
236 autofs_node_uncache(anp);
237 }
238
239 void
240 autofs_flush(struct autofs_mount *amp)
241 {
242 struct autofs_node *anp = amp->am_root;
243 struct autofs_node *child;
244
245 mutex_enter(&->am_lock);
246 RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
247 autofs_node_uncache(child);
248 }
249 autofs_node_uncache(amp->am_root);
250 mutex_exit(&->am_lock);
251
252 AUTOFS_DEBUG("%s flushed", amp->am_on);
253 }
254
255 /*
256 * The set/restore sigmask functions are used to (temporarily) overwrite
257 * the thread sigmask during triggering.
258 */
259 static void
260 autofs_set_sigmask(sigset_t *oldset)
261 {
262 sigset_t newset;
263 int i;
264
265 sigfillset(&newset);
266 /* Remove the autofs set of signals from newset */
267 mutex_enter(proc_lock);
268 mutex_enter(curproc->p_lock);
269
270 for (i = 0; i < __arraycount(autofs_sig_set); i++) {
271 /*
272 * But make sure we leave the ones already masked
273 * by the process, i.e. remove the signal from the
274 * temporary signalmask only if it wasn't already
275 * in sigmask.
276 */
277 if (!sigismasked(curlwp, autofs_sig_set[i]))
278 sigdelset(&newset, autofs_sig_set[i]);
279 }
280 sigprocmask1(curlwp, SIG_SETMASK, &newset, oldset);
281
282 mutex_exit(curproc->p_lock);
283 mutex_exit(proc_lock);
284 }
285
286 static void
287 autofs_restore_sigmask(sigset_t *set)
288 {
289
290 mutex_enter(proc_lock);
291 mutex_enter(curproc->p_lock);
292
293 sigprocmask1(curlwp, SIG_SETMASK, set, NULL);
294
295 mutex_exit(curproc->p_lock);
296 mutex_exit(proc_lock);
297 }
298
299 static int
300 autofs_trigger_one(struct autofs_node *anp, const char *component,
301 int componentlen)
302 {
303 struct autofs_mount *amp = anp->an_mount;
304 struct autofs_request *ar;
305 char *key, *path;
306 int error = 0, request_error;
307 bool wildcards;
308
309 KASSERT(mutex_owned(&autofs_softc->sc_lock));
310
311 if (!anp->an_parent) {
312 key = autofs_strndup(component, componentlen, KM_SLEEP);
313 } else {
314 struct autofs_node *firstanp;
315 for (firstanp = anp; firstanp->an_parent->an_parent;
316 firstanp = firstanp->an_parent)
317 continue;
318 key = kmem_strdup(firstanp->an_name, KM_SLEEP);
319 }
320
321 path = autofs_path(anp);
322
323 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
324 if (strcmp(ar->ar_path, path))
325 continue;
326 if (strcmp(ar->ar_key, key))
327 continue;
328
329 KASSERT(!strcmp(ar->ar_from, amp->am_from));
330 KASSERT(!strcmp(ar->ar_prefix, amp->am_prefix));
331 KASSERT(!strcmp(ar->ar_options, amp->am_options));
332 break;
333 }
334
335 if (ar) {
336 atomic_add_int(&ar->ar_refcount, 1);
337 } else {
338 ar = pool_get(&autofs_request_pool, PR_WAITOK);
339 ar->ar_mount = amp;
340 ar->ar_id = autofs_softc->sc_last_request_id++;
341 ar->ar_done = false;
342 ar->ar_error = 0;
343 ar->ar_wildcards = false;
344 ar->ar_in_progress = false;
345 strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
346 strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
347 strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
348 strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
349 strlcpy(ar->ar_options,
350 amp->am_options, sizeof(ar->ar_options));
351
352 callout_init(&ar->ar_callout, 0);
353 callout_reset(&ar->ar_callout, autofs_timeout * hz,
354 autofs_timeout_callout, ar);
355 ar->ar_refcount = 1;
356 TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
357 }
358
359 cv_broadcast(&autofs_softc->sc_cv);
360 while (ar->ar_done == false) {
361 if (autofs_interruptible) {
362 sigset_t oldset;
363 autofs_set_sigmask(&oldset);
364 error = cv_wait_sig(&autofs_softc->sc_cv,
365 &autofs_softc->sc_lock);
366 autofs_restore_sigmask(&oldset);
367 if (error) {
368 AUTOFS_WARN("cv_wait_sig for %s failed "
369 "with error %d", ar->ar_path, error);
370 break;
371 }
372 } else {
373 cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
374 }
375 }
376
377 request_error = ar->ar_error;
378 if (request_error)
379 AUTOFS_WARN("request for %s completed with error %d",
380 ar->ar_path, request_error);
381
382 wildcards = ar->ar_wildcards;
383
384 /*
385 * Check if this is the last reference.
386 */
387 if (!atomic_add_int_nv(&ar->ar_refcount, -1)) {
388 TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
389 mutex_exit(&autofs_softc->sc_lock);
390 callout_halt(&ar->ar_callout, NULL);
391 pool_put(&autofs_request_pool, ar);
392 mutex_enter(&autofs_softc->sc_lock);
393 }
394
395 /*
396 * Note that we do not do negative caching on purpose. This
397 * way the user can retry access at any time, e.g. after fixing
398 * the failure reason, without waiting for cache timer to expire.
399 */
400 if (!error && !request_error && autofs_cache > 0) {
401 autofs_node_cache(anp);
402 anp->an_wildcards = wildcards;
403 callout_reset(&anp->an_callout, autofs_cache * hz,
404 autofs_cache_callout, anp);
405 }
406
407 kmem_strfree(key);
408 kmem_strfree(path);
409
410 if (error)
411 return error;
412 return request_error;
413 }
414
415 int
416 autofs_trigger(struct autofs_node *anp, const char *component,
417 int componentlen)
418 {
419 for (;;) {
420 int error, dummy;
421
422 error = autofs_trigger_one(anp, component, componentlen);
423 if (!error) {
424 anp->an_retries = 0;
425 return 0;
426 }
427 if (error == EINTR || error == ERESTART) {
428 AUTOFS_DEBUG("trigger interrupted by signal, "
429 "not retrying");
430 anp->an_retries = 0;
431 return error;
432 }
433 anp->an_retries++;
434 if (anp->an_retries >= autofs_retry_attempts) {
435 AUTOFS_DEBUG("trigger failed %d times; returning "
436 "error %d", anp->an_retries, error);
437 anp->an_retries = 0;
438 return error;
439
440 }
441 AUTOFS_DEBUG("trigger failed with error %d; will retry in "
442 "%d seconds, %d attempts left", error, autofs_retry_delay,
443 autofs_retry_attempts - anp->an_retries);
444 mutex_exit(&autofs_softc->sc_lock);
445 tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz);
446 mutex_enter(&autofs_softc->sc_lock);
447 }
448 }
449
450 static int
451 autofs_ioctl_request(struct autofs_daemon_request *adr)
452 {
453 struct autofs_request *ar;
454
455 mutex_enter(&autofs_softc->sc_lock);
456 for (;;) {
457 int error;
458 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
459 if (ar->ar_done)
460 continue;
461 if (ar->ar_in_progress)
462 continue;
463 break;
464 }
465
466 if (ar)
467 break;
468
469 error = cv_wait_sig(&autofs_softc->sc_cv,
470 &autofs_softc->sc_lock);
471 if (error) {
472 mutex_exit(&autofs_softc->sc_lock);
473 return error;
474 }
475 }
476
477 ar->ar_in_progress = true;
478 mutex_exit(&autofs_softc->sc_lock);
479
480 adr->adr_id = ar->ar_id;
481 strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
482 strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
483 strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
484 strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
485 strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
486
487 mutex_enter(proc_lock);
488 autofs_softc->sc_dev_sid = curproc->p_pgrp->pg_id;
489 mutex_exit(proc_lock);
490
491 return 0;
492 }
493
494 static int
495 autofs_ioctl_done(struct autofs_daemon_done *add)
496 {
497 struct autofs_request *ar;
498
499 mutex_enter(&autofs_softc->sc_lock);
500 TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
501 if (ar->ar_id == add->add_id)
502 break;
503 }
504
505 if (!ar) {
506 mutex_exit(&autofs_softc->sc_lock);
507 AUTOFS_DEBUG("id %d not found", add->add_id);
508 return ESRCH;
509 }
510
511 ar->ar_error = add->add_error;
512 ar->ar_wildcards = add->add_wildcards;
513 ar->ar_done = true;
514 ar->ar_in_progress = false;
515 cv_broadcast(&autofs_softc->sc_cv);
516
517 mutex_exit(&autofs_softc->sc_lock);
518
519 return 0;
520 }
521
522 static int
523 autofs_open(dev_t dev, int flags, int mode, struct lwp *l)
524 {
525
526 mutex_enter(&autofs_softc->sc_lock);
527 /*
528 * We must never block automountd(8) and its descendants, and we use
529 * session ID to determine that: we store session id of the process
530 * that opened the device, and then compare it with session ids
531 * of triggering processes. This means running a second automountd(8)
532 * instance would break the previous one. The check below prevents
533 * it from happening.
534 */
535 if (autofs_softc->sc_dev_opened) {
536 mutex_exit(&autofs_softc->sc_lock);
537 return EBUSY;
538 }
539
540 autofs_softc->sc_dev_opened = true;
541 mutex_exit(&autofs_softc->sc_lock);
542
543 return 0;
544 }
545
546 static int
547 autofs_close(dev_t dev, int flags, int mode, struct lwp *l)
548 {
549
550 mutex_enter(&autofs_softc->sc_lock);
551 KASSERT(autofs_softc->sc_dev_opened);
552 autofs_softc->sc_dev_opened = false;
553 mutex_exit(&autofs_softc->sc_lock);
554
555 return 0;
556 }
557
558 static int
559 autofs_ioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
560 {
561
562 KASSERT(autofs_softc->sc_dev_opened);
563
564 switch (cmd) {
565 case AUTOFSREQUEST:
566 return autofs_ioctl_request(
567 (struct autofs_daemon_request *)data);
568 case AUTOFSDONE:
569 return autofs_ioctl_done(
570 (struct autofs_daemon_done *)data);
571 default:
572 AUTOFS_DEBUG("invalid cmd %lx", cmd);
573 return EINVAL;
574 }
575 return EINVAL;
576 }
577