xref: /freebsd/sys/fs/autofs/autofs.h (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef AUTOFS_H
32 #define	AUTOFS_H
33 
34 #include <vm/uma.h>
35 
36 #define VFSTOAUTOFS(mp)    ((struct autofs_mount *)((mp)->mnt_data))
37 
38 MALLOC_DECLARE(M_AUTOFS);
39 
40 extern uma_zone_t autofs_request_zone;
41 extern uma_zone_t autofs_node_zone;
42 
43 extern int autofs_debug;
44 extern int autofs_mount_on_stat;
45 
46 #define	AUTOFS_DEBUG(X, ...)						\
47 	do {								\
48 		if (autofs_debug > 1)					\
49 			printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
50 	} while (0)
51 
52 #define	AUTOFS_WARN(X, ...)						\
53 	do {								\
54 		if (autofs_debug > 0) {					\
55 			printf("WARNING: %s: " X "\n",			\
56 		    	    __func__, ## __VA_ARGS__);			\
57 		}							\
58 	} while (0)
59 
60 #define AUTOFS_SLOCK(X)		sx_slock(&X->am_lock)
61 #define AUTOFS_XLOCK(X)		sx_xlock(&X->am_lock)
62 #define AUTOFS_SUNLOCK(X)	sx_sunlock(&X->am_lock)
63 #define AUTOFS_XUNLOCK(X)	sx_xunlock(&X->am_lock)
64 #define AUTOFS_ASSERT_LOCKED(X)		sx_assert(&X->am_lock, SA_LOCKED)
65 #define AUTOFS_ASSERT_XLOCKED(X)	sx_assert(&X->am_lock, SA_XLOCKED)
66 #define AUTOFS_ASSERT_UNLOCKED(X)	sx_assert(&X->am_lock, SA_UNLOCKED)
67 
68 struct autofs_node {
69 	RB_ENTRY(autofs_node)		an_link;
70 	char				*an_name;
71 	int				an_fileno;
72 	struct autofs_node		*an_parent;
73 	RB_HEAD(autofs_node_tree,
74 	    autofs_node)		an_children;
75 	struct autofs_mount		*an_mount;
76 	struct vnode			*an_vnode;
77 	struct sx			an_vnode_lock;
78 	bool				an_cached;
79 	bool				an_wildcards;
80 	struct callout			an_callout;
81 	int				an_retries;
82 	struct timespec			an_ctime;
83 };
84 
85 struct autofs_mount {
86 	TAILQ_ENTRY(autofs_mount)	am_next;
87 	struct autofs_node		*am_root;
88 	struct mount			*am_mp;
89 	struct sx			am_lock;
90 	char				am_from[MAXPATHLEN];
91 	char				am_mountpoint[MAXPATHLEN];
92 	char				am_options[MAXPATHLEN];
93 	char				am_prefix[MAXPATHLEN];
94 	int				am_last_fileno;
95 };
96 
97 struct autofs_request {
98 	TAILQ_ENTRY(autofs_request)	ar_next;
99 	struct autofs_mount		*ar_mount;
100 	int				ar_id;
101 	bool				ar_done;
102 	int				ar_error;
103 	bool				ar_wildcards;
104 	bool				ar_in_progress;
105 	char				ar_from[MAXPATHLEN];
106 	char				ar_path[MAXPATHLEN];
107 	char				ar_prefix[MAXPATHLEN];
108 	char				ar_key[MAXPATHLEN];
109 	char				ar_options[MAXPATHLEN];
110 	struct timeout_task		ar_task;
111 	volatile u_int			ar_refcount;
112 };
113 
114 struct autofs_softc {
115 	device_t			sc_dev;
116 	struct cdev			*sc_cdev;
117 	struct cv			sc_cv;
118 	struct sx			sc_lock;
119 	TAILQ_HEAD(, autofs_request)	sc_requests;
120 	bool				sc_dev_opened;
121 	pid_t				sc_dev_sid;
122 	int				sc_last_request_id;
123 };
124 
125 int	autofs_init(struct vfsconf *vfsp);
126 int	autofs_uninit(struct vfsconf *vfsp);
127 int	autofs_trigger(struct autofs_node *anp, const char *component,
128 	    int componentlen);
129 bool	autofs_cached(struct autofs_node *anp, const char *component,
130 	    int componentlen);
131 void	autofs_flush(struct autofs_mount *amp);
132 bool	autofs_ignore_thread(const struct thread *td);
133 int	autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
134 	    const char *name, int namelen, struct autofs_node **anpp);
135 int	autofs_node_find(struct autofs_node *parent,
136 	    const char *name, int namelen, struct autofs_node **anpp);
137 void	autofs_node_delete(struct autofs_node *anp);
138 int	autofs_node_vn(struct autofs_node *anp, struct mount *mp,
139 	    int flags, struct vnode **vpp);
140 
141 RB_PROTOTYPE(autofs_node_tree, autofs_node, an_link, autofs_node_cmp);
142 
143 #endif /* !AUTOFS_H */
144