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