2 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_pseudofs.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/eventhandler.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 #include <sys/vnode.h>
45 #include <fs/pseudofs/pseudofs.h>
46 #include <fs/pseudofs/pseudofs_internal.h>
48 static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
50 static struct mtx pfs_vncache_mutex;
51 static struct pfs_vdata *pfs_vncache;
52 static eventhandler_tag pfs_exit_tag;
53 static void pfs_exit(void *arg, struct proc *p);
55 SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0,
56 "pseudofs vnode cache");
58 static int pfs_vncache_entries;
59 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD,
60 &pfs_vncache_entries, 0,
61 "number of entries in the vnode cache");
63 static int pfs_vncache_maxentries;
64 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD,
65 &pfs_vncache_maxentries, 0,
66 "highest number of entries in the vnode cache");
68 static int pfs_vncache_hits;
69 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
71 "number of cache hits since initialization");
73 static int pfs_vncache_misses;
74 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD,
75 &pfs_vncache_misses, 0,
76 "number of cache misses since initialization");
78 extern struct vop_vector pfs_vnodeops; /* XXX -> .h file */
81 * Initialize vnode cache
84 pfs_vncache_load(void)
86 mtx_init(&pfs_vncache_mutex, "pseudofs_vncache", NULL, MTX_DEF);
87 pfs_exit_tag = EVENTHANDLER_REGISTER(process_exit, pfs_exit, NULL,
88 EVENTHANDLER_PRI_ANY);
92 * Tear down vnode cache
95 pfs_vncache_unload(void)
97 EVENTHANDLER_DEREGISTER(process_exit, pfs_exit_tag);
98 if (pfs_vncache_entries != 0)
99 printf("pfs_vncache_unload(): %d entries remaining\n",
100 pfs_vncache_entries);
101 mtx_destroy(&pfs_vncache_mutex);
108 pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
109 struct pfs_node *pn, pid_t pid)
111 struct pfs_vdata *pvd;
115 * See if the vnode is in the cache.
116 * XXX linear search is not very efficient.
118 mtx_lock(&pfs_vncache_mutex);
119 for (pvd = pfs_vncache; pvd; pvd = pvd->pvd_next) {
120 if (pvd->pvd_pn == pn && pvd->pvd_pid == pid &&
121 pvd->pvd_vnode->v_mount == mp) {
122 if (vget(pvd->pvd_vnode, 0, curthread) == 0) {
124 *vpp = pvd->pvd_vnode;
125 mtx_unlock(&pfs_vncache_mutex);
126 /* XXX see comment at top of pfs_lookup() */
128 vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE,
132 /* XXX if this can happen, we're in trouble */
136 mtx_unlock(&pfs_vncache_mutex);
137 ++pfs_vncache_misses;
139 /* nope, get a new one */
140 MALLOC(pvd, struct pfs_vdata *, sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
141 if (++pfs_vncache_entries > pfs_vncache_maxentries)
142 pfs_vncache_maxentries = pfs_vncache_entries;
143 error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
145 FREE(pvd, M_PFSVNCACHE);
150 (*vpp)->v_data = pvd;
151 switch (pn->pn_type) {
153 (*vpp)->v_vflag = VV_ROOT;
155 printf("root vnode allocated\n");
161 case pfstype_procdir:
162 (*vpp)->v_type = VDIR;
165 (*vpp)->v_type = VREG;
167 case pfstype_symlink:
168 (*vpp)->v_type = VLNK;
171 KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
173 panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
176 * Propagate flag through to vnode so users know it can change
177 * if the process changes (i.e. execve)
179 if ((pn->pn_flags & PFS_PROCDEP) != 0)
180 (*vpp)->v_vflag |= VV_PROCDEP;
181 pvd->pvd_vnode = *vpp;
182 mtx_lock(&pfs_vncache_mutex);
183 pvd->pvd_prev = NULL;
184 pvd->pvd_next = pfs_vncache;
186 pvd->pvd_next->pvd_prev = pvd;
188 mtx_unlock(&pfs_vncache_mutex);
189 (*vpp)->v_vnlock->lk_flags |= LK_CANRECURSE;
190 vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
198 pfs_vncache_free(struct vnode *vp)
200 struct pfs_vdata *pvd;
202 mtx_lock(&pfs_vncache_mutex);
203 pvd = (struct pfs_vdata *)vp->v_data;
204 KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
206 pvd->pvd_next->pvd_prev = pvd->pvd_prev;
208 pvd->pvd_prev->pvd_next = pvd->pvd_next;
210 pfs_vncache = pvd->pvd_next;
211 mtx_unlock(&pfs_vncache_mutex);
213 --pfs_vncache_entries;
214 FREE(pvd, M_PFSVNCACHE);
220 * Free all vnodes associated with a defunct process
222 * XXXRW: It is unfortunate that pfs_exit() always acquires and releases two
223 * mutexes (one of which is Giant) for every process exit, even if procfs
227 pfs_exit(void *arg, struct proc *p)
229 struct pfs_vdata *pvd;
232 if (pfs_vncache == NULL)
236 * This is extremely inefficient due to the fact that vgone() not
237 * only indirectly modifies the vnode cache, but may also sleep.
238 * We can neither hold pfs_vncache_mutex across a vgone() call,
239 * nor make any assumptions about the state of the cache after
240 * vgone() returns. In consequence, we must start over after
241 * every vgone() call, and keep trying until we manage to traverse
244 * The only way to improve this situation is to change the data
245 * structure used to implement the cache. An obvious choice in
246 * this particular case would be a BST sorted by PID.
248 mtx_lock(&pfs_vncache_mutex);
250 while (pvd != NULL) {
251 if (pvd->pvd_pid == p->p_pid) {
252 vnp = pvd->pvd_vnode;
254 mtx_unlock(&pfs_vncache_mutex);
255 VOP_LOCK(vnp, LK_EXCLUSIVE, curthread);
257 VOP_UNLOCK(vnp, 0, curthread);
259 mtx_lock(&pfs_vncache_mutex);
265 mtx_unlock(&pfs_vncache_mutex);
270 * Disable a pseudofs node, and free all vnodes associated with it
273 pfs_disable(struct pfs_node *pn)
275 struct pfs_vdata *pvd;
278 if (pn->pn_flags & PFS_DISABLED)
280 pn->pn_flags |= PFS_DISABLED;
281 /* XXX see comment above nearly identical code in pfs_exit() */
282 mtx_lock(&pfs_vncache_mutex);
284 while (pvd != NULL) {
285 if (pvd->pvd_pn == pn) {
286 vnp = pvd->pvd_vnode;
288 mtx_unlock(&pfs_vncache_mutex);
289 VOP_LOCK(vnp, LK_EXCLUSIVE, curthread);
291 VOP_UNLOCK(vnp, 0, curthread);
293 mtx_lock(&pfs_vncache_mutex);
299 mtx_unlock(&pfs_vncache_mutex);
304 * Re-enable a disabled pseudofs node
307 pfs_enable(struct pfs_node *pn)
309 pn->pn_flags &= ~PFS_DISABLED;