xref: /freebsd/sys/fs/pseudofs/pseudofs.c (revision b331ec01c48f4ad8f999782d1b1766516add6118)
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 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mount.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/sbuf.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 
44 #include <fs/pseudofs/pseudofs.h>
45 #include <fs/pseudofs/pseudofs_internal.h>
46 
47 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");
48 
49 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0,
50     "pseudofs");
51 
52 #if PFS_FSNAMELEN != MFSNAMELEN
53 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
54 #endif
55 
56 /*
57  * Add a node to a directory
58  */
59 static int
60 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node)
61 {
62 	KASSERT(parent != NULL,
63 	    ("%s(): parent is NULL", __func__));
64 	KASSERT(parent->pn_info != NULL,
65 	    ("%s(): parent has no pn_info", __func__));
66 	KASSERT(parent->pn_type == pfstype_dir ||
67 	    parent->pn_type == pfstype_procdir ||
68 	    parent->pn_type == pfstype_root,
69 	    ("%s(): parent is not a directory", __func__));
70 
71 	/* XXX should check for duplicate names etc. */
72 
73 	mtx_lock(&parent->pn_info->pi_mutex);
74 	node->pn_info = parent->pn_info;
75 	node->pn_parent = parent;
76 	node->pn_next = parent->pn_nodes;
77 	parent->pn_nodes = node;
78 	/* Propagate flag to all child nodes (and thus their vnodes) */
79 	if ((parent->pn_flags & PFS_PROCDEP) != 0)
80 		node->pn_flags |= PFS_PROCDEP;
81 	mtx_unlock(&parent->pn_info->pi_mutex);
82 
83 	return (0);
84 }
85 
86 /*
87  * Add . and .. to a directory
88  */
89 static int
90 _pfs_fixup_dir(struct pfs_node *parent)
91 {
92 	struct pfs_node *dir;
93 
94 	MALLOC(dir, struct pfs_node *, sizeof *dir,
95 	    M_PFSNODES, M_WAITOK|M_ZERO);
96 	dir->pn_name[0] = '.';
97 	dir->pn_type = pfstype_this;
98 
99 	if (_pfs_add_node(parent, dir) != 0) {
100 		FREE(dir, M_PFSNODES);
101 		return (-1);
102 	}
103 
104 	MALLOC(dir, struct pfs_node *, sizeof *dir,
105 	    M_PFSNODES, M_WAITOK|M_ZERO);
106 	dir->pn_name[0] = dir->pn_name[1] = '.';
107 	dir->pn_type = pfstype_parent;
108 
109 	if (_pfs_add_node(parent, dir) != 0) {
110 		FREE(dir, M_PFSNODES);
111 		return (-1);
112 	}
113 
114 	return (0);
115 }
116 
117 /*
118  * Create a directory
119  */
120 struct pfs_node	*
121 pfs_create_dir(struct pfs_node *parent, const char *name,
122 	       pfs_attr_t attr, pfs_vis_t vis, int flags)
123 {
124 	struct pfs_node *dir;
125 
126 	KASSERT(strlen(name) < PFS_NAMELEN,
127 	    ("%s(): node name is too long", __func__));
128 
129 	MALLOC(dir, struct pfs_node *, sizeof *dir,
130 	    M_PFSNODES, M_WAITOK|M_ZERO);
131 	strcpy(dir->pn_name, name);
132 	dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir;
133 	dir->pn_attr = attr;
134 	dir->pn_vis = vis;
135 	dir->pn_flags = flags;
136 
137 	if (_pfs_add_node(parent, dir) != 0) {
138 		FREE(dir, M_PFSNODES);
139 		return (NULL);
140 	}
141 
142 	if (_pfs_fixup_dir(dir) != 0) {
143 		pfs_destroy(dir);
144 		return (NULL);
145 	}
146 
147 	return (dir);
148 }
149 
150 /*
151  * Create a file
152  */
153 struct pfs_node	*
154 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
155 		pfs_attr_t attr, pfs_vis_t vis, int flags)
156 {
157 	struct pfs_node *node;
158 
159 	KASSERT(strlen(name) < PFS_NAMELEN,
160 	    ("%s(): node name is too long", __func__));
161 
162 	MALLOC(node, struct pfs_node *, sizeof *node,
163 	    M_PFSNODES, M_WAITOK|M_ZERO);
164 	strcpy(node->pn_name, name);
165 	node->pn_type = pfstype_file;
166 	node->pn_func = fill;
167 	node->pn_attr = attr;
168 	node->pn_vis = vis;
169 	node->pn_flags = flags;
170 
171 	if (_pfs_add_node(parent, node) != 0) {
172 		FREE(node, M_PFSNODES);
173 		return (NULL);
174 	}
175 
176 	return (node);
177 }
178 
179 /*
180  * Create a symlink
181  */
182 struct pfs_node	*
183 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
184 		pfs_attr_t attr, pfs_vis_t vis, int flags)
185 {
186 	struct pfs_node *node;
187 
188 	node = pfs_create_file(parent, name, fill, attr, vis, flags);
189 	if (node == NULL)
190 		return (NULL);
191 	node->pn_type = pfstype_symlink;
192 	return (node);
193 }
194 
195 /*
196  * Locate a node by name
197  */
198 struct pfs_node *
199 pfs_find_node(struct pfs_node *parent, const char *name)
200 {
201 	struct pfs_node *node;
202 
203 	for (node = parent->pn_nodes; node != NULL; node = node->pn_next)
204 		if (strcmp(node->pn_name, name) == 0)
205 			return (node);
206 	return (NULL);
207 }
208 
209 /*
210  * Destroy a node or a tree of nodes
211  */
212 int
213 pfs_destroy(struct pfs_node *node)
214 {
215 	struct pfs_node *parent, *rover;
216 
217 	KASSERT(node != NULL,
218 	    ("%s(): node is NULL", __func__));
219 	KASSERT(node->pn_info != NULL,
220 	    ("%s(): node has no pn_info", __func__));
221 
222 	/* destroy children */
223 	if (node->pn_type == pfstype_dir ||
224 	    node->pn_type == pfstype_procdir ||
225 	    node->pn_type == pfstype_root)
226 		while (node->pn_nodes != NULL)
227 			pfs_destroy(node->pn_nodes);
228 
229 	/* unlink from parent */
230 	if ((parent = node->pn_parent) != NULL) {
231 		KASSERT(parent->pn_info == node->pn_info,
232 		    ("%s(): parent has different pn_info", __func__));
233 		mtx_lock(&node->pn_info->pi_mutex);
234 		if (parent->pn_nodes == node) {
235 			parent->pn_nodes = node->pn_next;
236 		} else {
237 			rover = parent->pn_nodes;
238 			while (rover->pn_next != NULL) {
239 				if (rover->pn_next == node) {
240 					rover->pn_next = node->pn_next;
241 					break;
242 				}
243 				rover = rover->pn_next;
244 			}
245 		}
246 		mtx_unlock(&node->pn_info->pi_mutex);
247 	}
248 
249 	/* revoke vnodes and release memory */
250 	pfs_disable(node);
251 	FREE(node, M_PFSNODES);
252 
253 	return (0);
254 }
255 
256 /*
257  * Mount a pseudofs instance
258  */
259 int
260 pfs_mount(struct pfs_info *pi, struct mount *mp, struct nameidata *ndp,
261 	  struct thread *td)
262 {
263 	struct statfs *sbp;
264 
265 	if (mp->mnt_flag & MNT_UPDATE)
266 		return (EOPNOTSUPP);
267 
268 	mp->mnt_flag |= MNT_LOCAL;
269 	mp->mnt_data = (qaddr_t)pi;
270 	vfs_getnewfsid(mp);
271 
272 	sbp = &mp->mnt_stat;
273 	bcopy(pi->pi_name, sbp->f_mntfromname, sizeof pi->pi_name);
274 	sbp->f_bsize = PAGE_SIZE;
275 	sbp->f_iosize = PAGE_SIZE;
276 	sbp->f_blocks = 1;
277 	sbp->f_bfree = 0;
278 	sbp->f_bavail = 0;
279 	sbp->f_files = 1;
280 	sbp->f_ffree = 0;
281 
282 	return (0);
283 }
284 
285 /*
286  * Unmount a pseudofs instance
287  */
288 int
289 pfs_unmount(struct mount *mp, int mntflags, struct thread *td)
290 {
291 	struct pfs_info *pi;
292 	int error;
293 
294 	pi = (struct pfs_info *)mp->mnt_data;
295 
296 	/* XXX do stuff with pi... */
297 
298 	error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0);
299 	return (error);
300 }
301 
302 /*
303  * Return a root vnode
304  */
305 int
306 pfs_root(struct mount *mp, struct vnode **vpp)
307 {
308 	struct pfs_info *pi;
309 
310 	pi = (struct pfs_info *)mp->mnt_data;
311 	return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID);
312 }
313 
314 /*
315  * Return filesystem stats
316  */
317 int
318 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
319 {
320 	bcopy(&mp->mnt_stat, sbp, sizeof *sbp);
321 	return (0);
322 }
323 
324 /*
325  * Initialize a pseudofs instance
326  */
327 int
328 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
329 {
330 	struct pfs_node *root;
331 	int error;
332 
333 	mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF);
334 
335 	/* set up the root diretory */
336 	MALLOC(root, struct pfs_node *, sizeof *root,
337 	    M_PFSNODES, M_WAITOK|M_ZERO);
338 	root->pn_type = pfstype_root;
339 	root->pn_name[0] = '/';
340 	root->pn_info = pi;
341 	if (_pfs_fixup_dir(root) != 0) {
342 		FREE(root, M_PFSNODES);
343 		return (ENODEV); /* XXX not really the right errno */
344 	}
345 	pi->pi_root = root;
346 
347 	/* construct file hierarchy */
348 	error = (pi->pi_init)(pi, vfc);
349 	if (error) {
350 		pfs_destroy(root);
351 		pi->pi_root = NULL;
352 		mtx_destroy(&pi->pi_mutex);
353 		return (error);
354 	}
355 
356 	pfs_fileno_init(pi);
357 	if (bootverbose)
358 		printf("%s registered\n", pi->pi_name);
359 	return (0);
360 }
361 
362 /*
363  * Destroy a pseudofs instance
364  */
365 int
366 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
367 {
368 	int error;
369 
370 	pfs_fileno_uninit(pi);
371 	pfs_destroy(pi->pi_root);
372 	pi->pi_root = NULL;
373 	mtx_destroy(&pi->pi_mutex);
374 	if (bootverbose)
375 		printf("%s unregistered\n", pi->pi_name);
376 	error = (pi->pi_uninit)(pi, vfc);
377 	return (error);
378 }
379 
380 /*
381  * Handle load / unload events
382  */
383 static int
384 pfs_modevent(module_t mod, int evt, void *arg)
385 {
386 	switch (evt) {
387 	case MOD_LOAD:
388 		pfs_fileno_load();
389 		pfs_vncache_load();
390 		break;
391 	case MOD_UNLOAD:
392 	case MOD_SHUTDOWN:
393 		pfs_vncache_unload();
394 		pfs_fileno_unload();
395 		break;
396 	default:
397 		printf("pseudofs: unexpected event type %d\n", evt);
398 		break;
399 	}
400 	return 0;
401 }
402 
403 /*
404  * Module declaration
405  */
406 static moduledata_t pseudofs_data = {
407 	"pseudofs",
408 	pfs_modevent,
409 	NULL
410 };
411 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
412 MODULE_VERSION(pseudofs, 1);
413