xref: /dragonfly/sys/vfs/autofs/autofs.h (revision d9d30518)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016 Tomohiro Kusumi <tkusumi@netbsd.org>
5  * Copyright (c) 2016 The DragonFly Project
6  * Copyright (c) 2014 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * This software was developed by Edward Tomasz Napierala under sponsorship
10  * from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #ifndef AUTOFS_H
37 #define AUTOFS_H
38 
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/objcache.h>
45 #include <sys/tree.h>
46 #include <sys/mutex2.h>
47 #include <sys/condvar.h>
48 #include <sys/mount.h>
49 #include <sys/vnode.h>
50 #include <sys/timespec.h>
51 #include <sys/callout.h>
52 #include <sys/taskqueue.h>
53 #include <sys/conf.h>
54 #include <sys/proc.h>
55 
56 #define AUTOFS_ROOTINO	((ino_t)1)
57 #define VFSTOAUTOFS(mp)	((struct autofs_mount *)((mp)->mnt_data))
58 #define VTOI(vp)	((struct autofs_node *)((vp)->v_data))
59 
60 #ifdef MALLOC_DECLARE
61 MALLOC_DECLARE(M_AUTOFS);
62 #endif
63 
64 extern struct objcache *autofs_request_objcache;
65 extern struct objcache *autofs_node_objcache;
66 extern struct autofs_softc *autofs_softc;
67 extern struct dev_ops autofs_ops;
68 
69 extern struct vop_ops autofs_vnode_vops;
70 
71 extern int autofs_debug;
72 //extern int autofs_mount_on_stat;
73 
74 #define AUTOFS_DEBUG(X, ...)				\
75 	do {						\
76 		if (autofs_debug > 1)			\
77 			kprintf("%s: " X "\n",		\
78 			    __func__, ## __VA_ARGS__);	\
79 	} while (0)
80 
81 #define AUTOFS_WARN(X, ...)				\
82 	do {						\
83 		if (autofs_debug > 0) {			\
84 			kprintf("WARNING: %s: " X "\n",	\
85 			    __func__, ## __VA_ARGS__);	\
86 		}					\
87 	} while (0)
88 
89 /*
90  * APRINTF is only for debugging.
91  */
92 #define APRINTF(X, ...)					\
93 	kprintf("### %s(%s): " X,			\
94 	    __func__, curproc ? curproc->p_comm : "-", ## __VA_ARGS__)
95 
96 struct autofs_node {
97 	RB_ENTRY(autofs_node)		an_link;
98 	char				*an_name;
99 	ino_t				an_ino;
100 	struct autofs_node		*an_parent;
101 	RB_HEAD(autofs_node_tree,
102 	    autofs_node)		an_children;
103 	struct autofs_mount		*an_mount;
104 	struct vnode			*an_vnode;
105 	mtx_t				an_vnode_lock;
106 	bool				an_cached;
107 	bool				an_wildcards;
108 	struct callout			an_callout;
109 	int				an_retries;
110 	struct timespec			an_ctime;
111 };
112 
113 struct autofs_mount {
114 	struct autofs_node		*am_root;
115 	mtx_t				am_lock;
116 	char				am_from[MAXPATHLEN];
117 	char				am_on[MAXPATHLEN];
118 	char				am_options[MAXPATHLEN];
119 	char				am_prefix[MAXPATHLEN];
120 	ino_t				am_last_ino;
121 };
122 
123 struct autofs_request {
124 	TAILQ_ENTRY(autofs_request)	ar_next;
125 	struct autofs_mount		*ar_mount;
126 	int				ar_id;
127 	bool				ar_done;
128 	int				ar_error;
129 	bool				ar_wildcards;
130 	bool				ar_in_progress;
131 	char				ar_from[MAXPATHLEN];
132 	char				ar_path[MAXPATHLEN];
133 	char				ar_prefix[MAXPATHLEN];
134 	char				ar_key[MAXPATHLEN];
135 	char				ar_options[MAXPATHLEN];
136 	struct timeout_task		ar_task;
137 	volatile unsigned int		ar_refcount;
138 };
139 
140 struct autofs_softc {
141 	struct cdev			*sc_cdev;
142 	struct cv			sc_cv;
143 	struct mtx			sc_lock;
144 	TAILQ_HEAD(, autofs_request)	sc_requests;
145 	bool				sc_dev_opened;
146 	pid_t				sc_dev_sid;
147 	int				sc_last_request_id;
148 };
149 
150 int	autofs_trigger(struct autofs_node *anp, const char *component,
151 	    int componentlen);
152 bool	autofs_cached(struct autofs_node *anp, const char *component,
153 	    int componentlen);
154 char*	autofs_path(struct autofs_node *anp);
155 void	autofs_flush(struct autofs_mount *amp);
156 bool	autofs_ignore_thread(void);
157 int	autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
158 	    const char *name, int namelen, struct autofs_node **anpp);
159 int	autofs_node_find(struct autofs_node *parent,
160 	    const char *name, int namelen, struct autofs_node **anpp);
161 void	autofs_node_delete(struct autofs_node *anp);
162 int	autofs_node_vn(struct autofs_node *anp, struct mount *mp,
163 	    int flags, struct vnode **vpp);
164 
165 static __inline void
166 autofs_node_cache(struct autofs_node *anp)
167 {
168 	anp->an_cached = true;
169 }
170 
171 static __inline void
172 autofs_node_uncache(struct autofs_node *anp)
173 {
174 	anp->an_cached = false;
175 }
176 
177 RB_PROTOTYPE(autofs_node_tree, autofs_node, an_link, autofs_node_cmp);
178 
179 #endif /* !AUTOFS_H */
180