xref: /freebsd/sys/fs/pseudofs/pseudofs_internal.h (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      $FreeBSD$
31  */
32 
33 #ifndef _PSEUDOFS_INTERNAL_H_INCLUDED
34 #define _PSEUDOFS_INTERNAL_H_INCLUDED
35 
36 /*
37  * Sysctl subtree
38  */
39 SYSCTL_DECL(_vfs_pfs);
40 
41 /*
42  * Vnode data
43  */
44 struct pfs_vdata {
45 	struct pfs_node	*pvd_pn;
46 	pid_t		 pvd_pid;
47 	struct vnode	*pvd_vnode;
48 	SLIST_ENTRY(pfs_vdata) pvd_hash;
49 };
50 
51 /*
52  * Vnode cache
53  */
54 void	 pfs_vncache_load	(void);
55 void	 pfs_vncache_unload	(void);
56 int	 pfs_vncache_alloc	(struct mount *, struct vnode **,
57 				 struct pfs_node *, pid_t pid);
58 int	 pfs_vncache_free	(struct vnode *);
59 
60 /*
61  * File number bitmap
62  */
63 void	 pfs_fileno_init	(struct pfs_info *);
64 void	 pfs_fileno_uninit	(struct pfs_info *);
65 void	 pfs_fileno_alloc	(struct pfs_node *);
66 void	 pfs_fileno_free	(struct pfs_node *);
67 
68 /*
69  * Debugging
70  */
71 #ifdef PSEUDOFS_TRACE
72 extern int pfs_trace;
73 
74 #define PFS_TRACE(foo) \
75 	do { \
76 		if (pfs_trace) { \
77 			printf("%s(): line %d: ", __func__, __LINE__); \
78 			printf foo ; \
79 			printf("\n"); \
80 		} \
81 	} while (0)
82 #define PFS_RETURN(err) \
83 	do { \
84 		if (pfs_trace) { \
85 			printf("%s(): line %d: returning %d\n", \
86 			    __func__, __LINE__, err); \
87 		} \
88 		return (err); \
89 	} while (0)
90 #else
91 #define PFS_TRACE(foo) \
92 	do { /* nothing */ } while (0)
93 #define PFS_RETURN(err) \
94 	return (err)
95 #endif
96 
97 /*
98  * Inline helpers for locking
99  */
100 static inline void
101 pfs_lock(struct pfs_node *pn)
102 {
103 
104 	mtx_lock(&pn->pn_mutex);
105 }
106 
107 static inline void
108 pfs_unlock(struct pfs_node *pn)
109 {
110 
111 	mtx_unlock(&pn->pn_mutex);
112 }
113 
114 static inline void
115 pfs_assert_owned(struct pfs_node *pn)
116 {
117 
118 	mtx_assert(&pn->pn_mutex, MA_OWNED);
119 }
120 
121 static inline void
122 pfs_assert_not_owned(struct pfs_node *pn)
123 {
124 
125 	mtx_assert(&pn->pn_mutex, MA_NOTOWNED);
126 }
127 
128 static inline int
129 pn_fill(PFS_FILL_ARGS)
130 {
131 
132 	PFS_TRACE(("%s", pn->pn_name));
133 	KASSERT(pn->pn_fill != NULL, ("%s(): no callback", __func__));
134 	if (p != NULL) {
135 		PROC_LOCK_ASSERT(p, MA_NOTOWNED);
136 		PROC_ASSERT_HELD(p);
137 	}
138 	pfs_assert_not_owned(pn);
139 	return ((pn->pn_fill)(PFS_FILL_ARGNAMES));
140 }
141 
142 static inline int
143 pn_attr(PFS_ATTR_ARGS)
144 {
145 
146 	PFS_TRACE(("%s", pn->pn_name));
147 	KASSERT(pn->pn_attr != NULL, ("%s(): no callback", __func__));
148 	if (p != NULL)
149 		PROC_LOCK_ASSERT(p, MA_OWNED);
150 	pfs_assert_not_owned(pn);
151 	return ((pn->pn_attr)(PFS_ATTR_ARGNAMES));
152 }
153 
154 static inline int
155 pn_vis(PFS_VIS_ARGS)
156 {
157 
158 	PFS_TRACE(("%s", pn->pn_name));
159 	if (pn->pn_vis == NULL)
160 		return (1);
161 	if (p != NULL)
162 		PROC_LOCK_ASSERT(p, MA_OWNED);
163 	pfs_assert_not_owned(pn);
164 	return ((pn->pn_vis)(PFS_VIS_ARGNAMES));
165 }
166 
167 static inline int
168 pn_ioctl(PFS_IOCTL_ARGS)
169 {
170 
171 	PFS_TRACE(("%s", pn->pn_name));
172 	KASSERT(pn->pn_ioctl != NULL, ("%s(): no callback", __func__));
173 	if (p != NULL)
174 		PROC_LOCK_ASSERT(p, MA_OWNED);
175 	pfs_assert_not_owned(pn);
176 	return ((pn->pn_ioctl)(PFS_IOCTL_ARGNAMES));
177 }
178 
179 static inline int
180 pn_getextattr(PFS_GETEXTATTR_ARGS)
181 {
182 
183 	PFS_TRACE(("%s", pn->pn_name));
184 	KASSERT(pn->pn_getextattr != NULL, ("%s(): no callback", __func__));
185 	if (p != NULL)
186 		PROC_LOCK_ASSERT(p, MA_OWNED);
187 	pfs_assert_not_owned(pn);
188 	return ((pn->pn_getextattr)(PFS_GETEXTATTR_ARGNAMES));
189 }
190 
191 static inline int
192 pn_close(PFS_CLOSE_ARGS)
193 {
194 
195 	PFS_TRACE(("%s", pn->pn_name));
196 	KASSERT(pn->pn_close != NULL, ("%s(): no callback", __func__));
197 	if (p != NULL)
198 		PROC_LOCK_ASSERT(p, MA_OWNED);
199 	pfs_assert_not_owned(pn);
200 	return ((pn->pn_close)(PFS_CLOSE_ARGNAMES));
201 }
202 
203 static inline int
204 pn_destroy(PFS_DESTROY_ARGS)
205 {
206 
207 	PFS_TRACE(("%s", pn->pn_name));
208 	KASSERT(pn->pn_destroy != NULL, ("%s(): no callback", __func__));
209 	pfs_assert_not_owned(pn);
210 	return ((pn->pn_destroy)(PFS_DESTROY_ARGNAMES));
211 }
212 
213 #endif
214