xref: /freebsd/sys/fs/pseudofs/pseudofs.h (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*-
2  * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *      $FreeBSD$
29  */
30 
31 #ifndef _PSEUDOFS_H_INCLUDED
32 #define _PSEUDOFS_H_INCLUDED
33 
34 /*
35  * Limits and constants
36  */
37 #define PFS_NAMELEN		24
38 #define PFS_DELEN		(8 + PFS_NAMELEN)
39 
40 typedef enum {
41 	pfstype_none = 0,
42 	pfstype_root,
43 	pfstype_dir,
44 	pfstype_this,
45 	pfstype_parent,
46 	pfstype_file,
47 	pfstype_symlink,
48 	pfstype_procdir
49 } pfs_type_t;
50 
51 /*
52  * Data structures
53  */
54 struct pfs_info;
55 struct pfs_node;
56 struct pfs_bitmap;
57 
58 #define PFS_FILL_ARGS \
59 	struct proc *curp, struct proc *p, struct pfs_node *pn, struct sbuf *sb
60 #define PFS_FILL_PROTO(name) \
61 	int name(PFS_FILL_ARGS);
62 typedef int (*pfs_fill_t)(PFS_FILL_ARGS);
63 
64 struct pfs_bitmap;		/* opaque */
65 
66 /*
67  * pfs_info: describes a pseudofs instance
68  */
69 struct pfs_info {
70 	char			 pi_name[MFSNAMELEN];
71 	struct pfs_node		*pi_root;
72 	/* members below this line aren't initialized */
73 	/* currently, the mutex is only used to protect the bitmap */
74 	struct mtx		 pi_mutex;
75 	struct pfs_bitmap	*pi_bitmap;
76 };
77 
78 /*
79  * pfs_node: describes a node (file or directory) within a pseudofs
80  */
81 struct pfs_node {
82 	char			 pn_name[PFS_NAMELEN];
83 	pfs_type_t		 pn_type;
84 	int			 pn_flags;
85 	uid_t			 pn_uid;
86 	gid_t			 pn_gid;
87 	mode_t			 pn_mode;
88 	union {
89 		void		*_pn_data;
90 		pfs_fill_t	 _pn_func;
91 		struct pfs_node	*_pn_nodes;
92 	} u1;
93 #define pn_data		u1._pn_data
94 #define pn_func		u1._pn_func
95 #define pn_nodes	u1._pn_nodes
96 	/* members below this line aren't initialized */
97 	struct pfs_node		*pn_parent;
98 	u_int32_t		 pn_fileno;
99 };
100 
101 #define PFS_NODE(name, type, flags, uid, gid, mode, data) \
102         { (name), (type), (flags), (uid), (gid), (mode), { (data) } }
103 #define PFS_DIR(name, flags, uid, gid, mode, nodes) \
104         PFS_NODE(name, pfstype_dir, flags, uid, gid, mode, nodes)
105 #define PFS_ROOT(nodes) \
106         PFS_NODE("/", pfstype_root, 0, 0, 0, 0555, nodes)
107 #define PFS_THIS \
108 	PFS_NODE(".", pfstype_this, 0, 0, 0, 0, NULL)
109 #define PFS_PARENT \
110 	PFS_NODE("..", pfstype_parent, 0, 0, 0, 0, NULL)
111 #define PFS_FILE(name, flags, uid, gid, mode, func) \
112 	PFS_NODE(name, pfstype_file, flags, uid, gid, mode, func)
113 #define PFS_SYMLINK(name, flags, uid, gid, mode, func) \
114 	PFS_NODE(name, pfstype_symlink, flags, uid, gid, mode, func)
115 #define PFS_PROCDIR(flags, uid, gid, mode, nodes) \
116         PFS_NODE("", pfstype_procdir, flags, uid, gid, mode, nodes)
117 #define PFS_LASTNODE \
118 	PFS_NODE("", pfstype_none, 0, 0, 0, 0, NULL)
119 
120 /*
121  * VFS interface
122  */
123 int	 pfs_mount		(struct pfs_info *pi,
124 				 struct mount *mp, char *path, caddr_t data,
125 				 struct nameidata *ndp, struct proc *p);
126 int	 pfs_unmount		(struct mount *mp, int mntflags,
127 				 struct proc *p);
128 int	 pfs_root		(struct mount *mp, struct vnode **vpp);
129 int	 pfs_statfs		(struct mount *mp, struct statfs *sbp,
130 				 struct proc *p);
131 int	 pfs_init		(struct pfs_info *pi, struct vfsconf *vfc);
132 int	 pfs_uninit		(struct pfs_info *pi, struct vfsconf *vfc);
133 
134 /*
135  * Now for some initialization magic...
136  */
137 #define PSEUDOFS(name, root)						\
138 									\
139 static struct pfs_info name##_info = {					\
140 	#name,								\
141 	&(root)								\
142 };									\
143 									\
144 static int								\
145 _##name##_mount(struct mount *mp, char *path, caddr_t data,		\
146 	     struct nameidata *ndp, struct proc *p) {			\
147         return pfs_mount(&name##_info, mp, path, data, ndp, p);		\
148 }									\
149 									\
150 static int								\
151 _##name##_init(struct vfsconf *vfc) {					\
152         return pfs_init(&name##_info, vfc);				\
153 }									\
154 									\
155 static int								\
156 _##name##_uninit(struct vfsconf *vfc) {					\
157         return pfs_uninit(&name##_info, vfc);				\
158 }									\
159 									\
160 static struct vfsops name##_vfsops = {					\
161 	_##name##_mount,						\
162 	vfs_stdstart,							\
163 	pfs_unmount,							\
164 	pfs_root,							\
165 	vfs_stdquotactl,						\
166 	pfs_statfs,							\
167 	vfs_stdsync,							\
168 	vfs_stdvget,							\
169 	vfs_stdfhtovp,							\
170 	vfs_stdcheckexp,						\
171 	vfs_stdvptofh,							\
172 	_##name##_init,							\
173 	_##name##_uninit,						\
174 	vfs_stdextattrctl,						\
175 };									\
176 VFS_SET(name##_vfsops, name, VFCF_SYNTHETIC);				\
177 MODULE_DEPEND(name, pseudofs, 1, 1, 1);
178 
179 #endif
180