xref: /freebsd/sys/fs/pseudofs/pseudofs_internal.h (revision f126890a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Dag-Erling 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 
31 #ifndef _PSEUDOFS_INTERNAL_H_INCLUDED
32 #define _PSEUDOFS_INTERNAL_H_INCLUDED
33 
34 /*
35  * Sysctl subtree
36  */
37 SYSCTL_DECL(_vfs_pfs);
38 
39 /*
40  * Vnode data
41  */
42 struct pfs_vdata {
43 	struct pfs_node	*pvd_pn;
44 	pid_t		 pvd_pid;
45 	struct vnode	*pvd_vnode;
46 	SLIST_ENTRY(pfs_vdata) pvd_hash;
47 };
48 
49 /*
50  * Vnode cache
51  */
52 void	 pfs_vncache_load	(void);
53 void	 pfs_vncache_unload	(void);
54 int	 pfs_vncache_alloc	(struct mount *, struct vnode **,
55 				 struct pfs_node *, pid_t pid);
56 int	 pfs_vncache_free	(struct vnode *);
57 
58 /*
59  * File number bitmap
60  */
61 void	 pfs_fileno_init	(struct pfs_info *);
62 void	 pfs_fileno_uninit	(struct pfs_info *);
63 void	 pfs_fileno_alloc	(struct pfs_node *);
64 void	 pfs_fileno_free	(struct pfs_node *);
65 
66 /*
67  * Debugging
68  */
69 #ifdef PSEUDOFS_TRACE
70 extern int pfs_trace;
71 
72 #define PFS_TRACE(foo) \
73 	do { \
74 		if (pfs_trace) { \
75 			printf("%s(): line %d: ", __func__, __LINE__); \
76 			printf foo ; \
77 			printf("\n"); \
78 		} \
79 	} while (0)
80 #define PFS_RETURN(err) \
81 	do { \
82 		if (pfs_trace) { \
83 			printf("%s(): line %d: returning %d\n", \
84 			    __func__, __LINE__, err); \
85 		} \
86 		return (err); \
87 	} while (0)
88 #else
89 #define PFS_TRACE(foo) \
90 	do { /* nothing */ } while (0)
91 #define PFS_RETURN(err) \
92 	return (err)
93 #endif
94 
95 /*
96  * Inline helpers for locking
97  */
98 static inline void
99 pfs_lock(struct pfs_node *pn)
100 {
101 
102 	mtx_lock(&pn->pn_mutex);
103 }
104 
105 static inline void
106 pfs_unlock(struct pfs_node *pn)
107 {
108 
109 	mtx_unlock(&pn->pn_mutex);
110 }
111 
112 static inline void
113 pfs_assert_owned(struct pfs_node *pn)
114 {
115 
116 	mtx_assert(&pn->pn_mutex, MA_OWNED);
117 }
118 
119 static inline void
120 pfs_assert_not_owned(struct pfs_node *pn)
121 {
122 
123 	mtx_assert(&pn->pn_mutex, MA_NOTOWNED);
124 }
125 
126 static inline int
127 pn_fill(PFS_FILL_ARGS)
128 {
129 
130 	PFS_TRACE(("%s", pn->pn_name));
131 	KASSERT(pn->pn_fill != NULL, ("%s(): no callback", __func__));
132 	if (p != NULL) {
133 		PROC_LOCK_ASSERT(p, MA_NOTOWNED);
134 		PROC_ASSERT_HELD(p);
135 	}
136 	pfs_assert_not_owned(pn);
137 	return ((pn->pn_fill)(PFS_FILL_ARGNAMES));
138 }
139 
140 static inline int
141 pn_attr(PFS_ATTR_ARGS)
142 {
143 
144 	PFS_TRACE(("%s", pn->pn_name));
145 	KASSERT(pn->pn_attr != NULL, ("%s(): no callback", __func__));
146 	if (p != NULL)
147 		PROC_LOCK_ASSERT(p, MA_OWNED);
148 	pfs_assert_not_owned(pn);
149 	return ((pn->pn_attr)(PFS_ATTR_ARGNAMES));
150 }
151 
152 static inline int
153 pn_vis(PFS_VIS_ARGS)
154 {
155 
156 	PFS_TRACE(("%s", pn->pn_name));
157 	if (pn->pn_vis == NULL)
158 		return (1);
159 	if (p != NULL)
160 		PROC_LOCK_ASSERT(p, MA_OWNED);
161 	pfs_assert_not_owned(pn);
162 	return ((pn->pn_vis)(PFS_VIS_ARGNAMES));
163 }
164 
165 static inline int
166 pn_ioctl(PFS_IOCTL_ARGS)
167 {
168 
169 	PFS_TRACE(("%s", pn->pn_name));
170 	KASSERT(pn->pn_ioctl != NULL, ("%s(): no callback", __func__));
171 	if (p != NULL)
172 		PROC_LOCK_ASSERT(p, MA_OWNED);
173 	pfs_assert_not_owned(pn);
174 	return ((pn->pn_ioctl)(PFS_IOCTL_ARGNAMES));
175 }
176 
177 static inline int
178 pn_getextattr(PFS_GETEXTATTR_ARGS)
179 {
180 
181 	PFS_TRACE(("%s", pn->pn_name));
182 	KASSERT(pn->pn_getextattr != NULL, ("%s(): no callback", __func__));
183 	if (p != NULL)
184 		PROC_LOCK_ASSERT(p, MA_OWNED);
185 	pfs_assert_not_owned(pn);
186 	return ((pn->pn_getextattr)(PFS_GETEXTATTR_ARGNAMES));
187 }
188 
189 static inline int
190 pn_close(PFS_CLOSE_ARGS)
191 {
192 
193 	PFS_TRACE(("%s", pn->pn_name));
194 	KASSERT(pn->pn_close != NULL, ("%s(): no callback", __func__));
195 	if (p != NULL)
196 		PROC_LOCK_ASSERT(p, MA_OWNED);
197 	pfs_assert_not_owned(pn);
198 	return ((pn->pn_close)(PFS_CLOSE_ARGNAMES));
199 }
200 
201 static inline int
202 pn_destroy(PFS_DESTROY_ARGS)
203 {
204 
205 	PFS_TRACE(("%s", pn->pn_name));
206 	KASSERT(pn->pn_destroy != NULL, ("%s(): no callback", __func__));
207 	pfs_assert_not_owned(pn);
208 	return ((pn->pn_destroy)(PFS_DESTROY_ARGNAMES));
209 }
210 
211 #endif
212