2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #ifndef _PSEUDOFS_H_INCLUDED
34 #define _PSEUDOFS_H_INCLUDED
53 * Limits and constants
55 #define PFS_NAMELEN 128
56 #define PFS_FSNAMELEN 16 /* equal to MFSNAMELEN */
57 #define PFS_DELEN (offsetof(struct dirent, d_name) + PFS_NAMELEN)
73 #define PFS_RD 0x0001 /* readable */
74 #define PFS_WR 0x0002 /* writeable */
75 #define PFS_RDWR (PFS_RD|PFS_WR)
76 #define PFS_RAWRD 0x0004 /* raw reader */
77 #define PFS_RAWWR 0x0008 /* raw writer */
78 #define PFS_RAW (PFS_RAWRD|PFS_RAWWR)
79 #define PFS_PROCDEP 0x0010 /* process-dependent */
80 #define PFS_NOWAIT 0x0020 /* allow malloc to fail */
89 * Init / uninit callback
91 #define PFS_INIT_ARGS \
92 struct pfs_info *pi, struct vfsconf *vfc
93 #define PFS_INIT_ARGNAMES \
95 #define PFS_INIT_PROTO(name) \
96 int name(PFS_INIT_ARGS);
97 typedef int (*pfs_init_t)(PFS_INIT_ARGS);
101 * Called with proc held but unlocked
103 #define PFS_FILL_ARGS \
104 struct thread *td, struct proc *p, struct pfs_node *pn, \
105 struct sbuf *sb, struct uio *uio
106 #define PFS_FILL_ARGNAMES \
108 #define PFS_FILL_PROTO(name) \
109 int name(PFS_FILL_ARGS);
110 typedef int (*pfs_fill_t)(PFS_FILL_ARGS);
114 * Called with proc locked
117 #define PFS_ATTR_ARGS \
118 struct thread *td, struct proc *p, struct pfs_node *pn, \
120 #define PFS_ATTR_ARGNAMES \
122 #define PFS_ATTR_PROTO(name) \
123 int name(PFS_ATTR_ARGS);
124 typedef int (*pfs_attr_t)(PFS_ATTR_ARGS);
127 * Visibility callback
128 * Called with proc locked
130 #define PFS_VIS_ARGS \
131 struct thread *td, struct proc *p, struct pfs_node *pn
132 #define PFS_VIS_ARGNAMES \
134 #define PFS_VIS_PROTO(name) \
135 int name(PFS_VIS_ARGS);
136 typedef int (*pfs_vis_t)(PFS_VIS_ARGS);
140 * Called with proc locked
142 #define PFS_IOCTL_ARGS \
143 struct thread *td, struct proc *p, struct pfs_node *pn, \
144 unsigned long cmd, void *data
145 #define PFS_IOCTL_ARGNAMES \
147 #define PFS_IOCTL_PROTO(name) \
148 int name(PFS_IOCTL_ARGS);
149 typedef int (*pfs_ioctl_t)(PFS_IOCTL_ARGS);
152 * Getextattr callback
153 * Called with proc locked
155 #define PFS_GETEXTATTR_ARGS \
156 struct thread *td, struct proc *p, struct pfs_node *pn, \
157 int attrnamespace, const char *name, struct uio *uio, \
158 size_t *size, struct ucred *cred
159 #define PFS_GETEXTATTR_ARGNAMES \
160 td, p, pn, attrnamespace, name, uio, size, cred
161 #define PFS_GETEXTATTR_PROTO(name) \
162 int name(PFS_GETEXTATTR_ARGS);
164 typedef int (*pfs_getextattr_t)(PFS_GETEXTATTR_ARGS);
167 * Last-close callback
168 * Called with proc locked
170 #define PFS_CLOSE_ARGS \
171 struct thread *td, struct proc *p, struct pfs_node *pn
172 #define PFS_CLOSE_ARGNAMES \
174 #define PFS_CLOSE_PROTO(name) \
175 int name(PFS_CLOSE_ARGS);
176 typedef int (*pfs_close_t)(PFS_CLOSE_ARGS);
181 #define PFS_DESTROY_ARGS \
183 #define PFS_DESTROY_ARGNAMES \
185 #define PFS_DESTROY_PROTO(name) \
186 int name(PFS_DESTROY_ARGS);
187 typedef int (*pfs_destroy_t)(PFS_DESTROY_ARGS);
190 * pfs_info: describes a pseudofs instance
192 * The pi_mutex is only used to avoid using the global subr_unit lock
193 * for unrhdr. The rest of struct pfs_info is only modified during
194 * vfs_init() and vfs_uninit() of the consumer filesystem.
197 char pi_name[PFS_FSNAMELEN];
199 pfs_init_t pi_uninit;
201 /* members below this line are initialized at run time */
202 struct pfs_node *pi_root;
204 struct unrhdr *pi_unrhdr;
208 * pfs_node: describes a node (file or directory) within a pseudofs
210 * - Fields marked (o) are protected by the node's own mutex.
211 * - Fields marked (p) are protected by the node's parent's mutex.
212 * - Remaining fields are not protected by any lock and are assumed to be
213 * immutable once the node has been created.
215 * To prevent deadlocks, if a node's mutex is to be held at the same time
216 * as its parent's (e.g. when adding or removing nodes to a directory),
217 * the parent's mutex must always be acquired first. Unfortunately, this
218 * is not enforcable by WITNESS.
221 char pn_name[PFS_NAMELEN];
225 void *pn_data; /* (o) */
228 pfs_ioctl_t pn_ioctl;
229 pfs_close_t pn_close;
232 pfs_getextattr_t pn_getextattr;
233 pfs_destroy_t pn_destroy;
235 struct pfs_info *pn_info;
236 u_int32_t pn_fileno; /* (o) */
238 struct pfs_node *pn_parent; /* (o) */
239 struct pfs_node *pn_nodes; /* (o) */
240 struct pfs_node *pn_last_node; /* (o) */
241 struct pfs_node *pn_next; /* (p) */
247 int pfs_mount (struct pfs_info *pi, struct mount *mp);
248 int pfs_cmount (struct mntarg *ma, void *data, uint64_t flags);
249 int pfs_unmount (struct mount *mp, int mntflags);
250 int pfs_root (struct mount *mp, int flags,
252 int pfs_statfs (struct mount *mp, struct statfs *sbp);
253 int pfs_init (struct pfs_info *pi, struct vfsconf *vfc);
254 int pfs_uninit (struct pfs_info *pi, struct vfsconf *vfc);
257 * Directory structure construction and manipulation
259 struct pfs_node *pfs_create_dir (struct pfs_node *parent, const char *name,
260 pfs_attr_t attr, pfs_vis_t vis,
261 pfs_destroy_t destroy, int flags);
262 struct pfs_node *pfs_create_file(struct pfs_node *parent, const char *name,
263 pfs_fill_t fill, pfs_attr_t attr,
264 pfs_vis_t vis, pfs_destroy_t destroy,
266 struct pfs_node *pfs_create_link(struct pfs_node *parent, const char *name,
267 pfs_fill_t fill, pfs_attr_t attr,
268 pfs_vis_t vis, pfs_destroy_t destroy,
270 struct pfs_node *pfs_find_node (struct pfs_node *parent, const char *name);
271 void pfs_purge (struct pfs_node *pn);
272 int pfs_destroy (struct pfs_node *pn);
275 * Now for some initialization magic...
277 #define PSEUDOFS(name, version, flags) \
279 static struct pfs_info name##_info = { \
286 _##name##_mount(struct mount *mp) { \
287 return (pfs_mount(&name##_info, mp)); \
291 _##name##_init(struct vfsconf *vfc) { \
292 return (pfs_init(&name##_info, vfc)); \
296 _##name##_uninit(struct vfsconf *vfc) { \
297 return (pfs_uninit(&name##_info, vfc)); \
300 static struct vfsops name##_vfsops = { \
301 .vfs_cmount = pfs_cmount, \
302 .vfs_init = _##name##_init, \
303 .vfs_mount = _##name##_mount, \
304 .vfs_root = pfs_root, \
305 .vfs_statfs = pfs_statfs, \
306 .vfs_uninit = _##name##_uninit, \
307 .vfs_unmount = pfs_unmount, \
309 VFS_SET(name##_vfsops, name, VFCF_SYNTHETIC | flags); \
310 MODULE_VERSION(name, version); \
311 MODULE_DEPEND(name, pseudofs, 1, 1, 1);