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