xref: /freebsd/sys/fs/pseudofs/pseudofs_internal.h (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
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 	struct pfs_vdata*pvd_prev, *pvd_next;
49 	int		 pvd_dead:1;
50 };
51 
52 /*
53  * Vnode cache
54  */
55 void	 pfs_vncache_load	(void);
56 void	 pfs_vncache_unload	(void);
57 int	 pfs_vncache_alloc	(struct mount *, struct vnode **,
58 				 struct pfs_node *, pid_t pid);
59 int	 pfs_vncache_free	(struct vnode *);
60 
61 /*
62  * File number bitmap
63  */
64 void	 pfs_fileno_init	(struct pfs_info *);
65 void	 pfs_fileno_uninit	(struct pfs_info *);
66 void	 pfs_fileno_alloc	(struct pfs_node *);
67 void	 pfs_fileno_free	(struct pfs_node *);
68 
69 /*
70  * Debugging
71  */
72 #ifdef PSEUDOFS_TRACE
73 extern int pfs_trace;
74 
75 #define PFS_TRACE(foo) \
76 	do { \
77 		if (pfs_trace) { \
78 			printf("%s(): line %d: ", __func__, __LINE__); \
79 			printf foo ; \
80 			printf("\n"); \
81 		} \
82 	} while (0)
83 #define PFS_RETURN(err) \
84 	do { \
85 		if (pfs_trace) { \
86 			printf("%s(): line %d: returning %d\n", \
87 			    __func__, __LINE__, err); \
88 		} \
89 		return (err); \
90 	} while (0)
91 #else
92 #define PFS_TRACE(foo) \
93 	do { /* nothing */ } while (0)
94 #define PFS_RETURN(err) \
95 	return (err)
96 #endif
97 
98 /*
99  * Inline helpers for locking
100  */
101 static inline void
102 pfs_lock(struct pfs_node *pn)
103 {
104 
105 	mtx_lock(&pn->pn_mutex);
106 }
107 
108 static inline void
109 pfs_unlock(struct pfs_node *pn)
110 {
111 
112 	mtx_unlock(&pn->pn_mutex);
113 }
114 
115 static inline void
116 pfs_assert_owned(struct pfs_node *pn)
117 {
118 
119 	mtx_assert(&pn->pn_mutex, MA_OWNED);
120 }
121 
122 static inline void
123 pfs_assert_not_owned(struct pfs_node *pn)
124 {
125 
126 	mtx_assert(&pn->pn_mutex, MA_NOTOWNED);
127 }
128 
129 static inline int
130 pn_fill(PFS_FILL_ARGS)
131 {
132 
133 	PFS_TRACE(("%s", pn->pn_name));
134 	KASSERT(pn->pn_fill != NULL, ("%s(): no callback", __func__));
135 	if (p != NULL) {
136 		PROC_LOCK_ASSERT(p, MA_NOTOWNED);
137 		PROC_ASSERT_HELD(p);
138 	}
139 	pfs_assert_not_owned(pn);
140 	return ((pn->pn_fill)(PFS_FILL_ARGNAMES));
141 }
142 
143 static inline int
144 pn_attr(PFS_ATTR_ARGS)
145 {
146 
147 	PFS_TRACE(("%s", pn->pn_name));
148 	KASSERT(pn->pn_attr != NULL, ("%s(): no callback", __func__));
149 	if (p != NULL)
150 		PROC_LOCK_ASSERT(p, MA_OWNED);
151 	pfs_assert_not_owned(pn);
152 	return ((pn->pn_attr)(PFS_ATTR_ARGNAMES));
153 }
154 
155 static inline int
156 pn_vis(PFS_VIS_ARGS)
157 {
158 
159 	PFS_TRACE(("%s", pn->pn_name));
160 	KASSERT(pn->pn_vis != NULL, ("%s(): no callback", __func__));
161 	KASSERT(p != NULL, ("%s(): no process", __func__));
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