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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_pseudofs.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/eventhandler.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
47 #include <fs/pseudofs/pseudofs.h>
48 #include <fs/pseudofs/pseudofs_internal.h>
50 static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
52 static struct mtx pfs_vncache_mutex;
53 static eventhandler_tag pfs_exit_tag;
54 static void pfs_exit(void *arg, struct proc *p);
55 static void pfs_purge_all(void);
57 static SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
58 "pseudofs vnode cache");
60 static int pfs_vncache_entries;
61 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD,
62 &pfs_vncache_entries, 0,
63 "number of entries in the vnode cache");
65 static int pfs_vncache_maxentries;
66 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD,
67 &pfs_vncache_maxentries, 0,
68 "highest number of entries in the vnode cache");
70 static int pfs_vncache_hits;
71 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
73 "number of cache hits since initialization");
75 static int pfs_vncache_misses;
76 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD,
77 &pfs_vncache_misses, 0,
78 "number of cache misses since initialization");
80 extern struct vop_vector pfs_vnodeops; /* XXX -> .h file */
82 static SLIST_HEAD(pfs_vncache_head, pfs_vdata) *pfs_vncache_hashtbl;
83 static u_long pfs_vncache_hash;
84 #define PFS_VNCACHE_HASH(pid) (&pfs_vncache_hashtbl[(pid) & pfs_vncache_hash])
87 * Initialize vnode cache
90 pfs_vncache_load(void)
93 mtx_init(&pfs_vncache_mutex, "pfs_vncache", NULL, MTX_DEF);
94 pfs_vncache_hashtbl = hashinit(maxproc / 4, M_PFSVNCACHE, &pfs_vncache_hash);
95 pfs_exit_tag = EVENTHANDLER_REGISTER(process_exit, pfs_exit, NULL,
96 EVENTHANDLER_PRI_ANY);
100 * Tear down vnode cache
103 pfs_vncache_unload(void)
106 EVENTHANDLER_DEREGISTER(process_exit, pfs_exit_tag);
108 KASSERT(pfs_vncache_entries == 0,
109 ("%d vncache entries remaining", pfs_vncache_entries));
110 mtx_destroy(&pfs_vncache_mutex);
111 hashdestroy(pfs_vncache_hashtbl, M_PFSVNCACHE, pfs_vncache_hash);
118 pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
119 struct pfs_node *pn, pid_t pid)
121 struct pfs_vncache_head *hash;
122 struct pfs_vdata *pvd, *pvd2;
128 * See if the vnode is in the cache.
130 hash = PFS_VNCACHE_HASH(pid);
131 if (SLIST_EMPTY(hash))
134 mtx_lock(&pfs_vncache_mutex);
135 SLIST_FOREACH(pvd, hash, pvd_hash) {
136 if (pvd->pvd_pn == pn && pvd->pvd_pid == pid &&
137 pvd->pvd_vnode->v_mount == mp) {
140 mtx_unlock(&pfs_vncache_mutex);
141 if (vget_finish(vp, LK_EXCLUSIVE, vs) == 0) {
145 * Some callers cache_enter(vp) later, so
146 * we have to make sure it's not in the
147 * VFS cache so it doesn't get entered
148 * twice. A better solution would be to
149 * make pfs_vncache_alloc() responsible
150 * for entering the vnode in the VFS
159 mtx_unlock(&pfs_vncache_mutex);
161 /* nope, get a new one */
162 pvd = malloc(sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
163 error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
165 free(pvd, M_PFSVNCACHE);
170 (*vpp)->v_data = pvd;
171 switch (pn->pn_type) {
173 (*vpp)->v_vflag = VV_ROOT;
175 printf("root vnode allocated\n");
181 case pfstype_procdir:
182 (*vpp)->v_type = VDIR;
185 (*vpp)->v_type = VREG;
187 case pfstype_symlink:
188 (*vpp)->v_type = VLNK;
191 KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
193 panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
196 * Propagate flag through to vnode so users know it can change
197 * if the process changes (i.e. execve)
199 if ((pn->pn_flags & PFS_PROCDEP) != 0)
200 (*vpp)->v_vflag |= VV_PROCDEP;
201 pvd->pvd_vnode = *vpp;
202 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
204 error = insmntque(*vpp, mp);
206 free(pvd, M_PFSVNCACHE);
210 vn_set_state(*vpp, VSTATE_CONSTRUCTED);
212 mtx_lock(&pfs_vncache_mutex);
214 * Other thread may race with us, creating the entry we are
215 * going to insert into the cache. Recheck after
216 * pfs_vncache_mutex is reacquired.
218 SLIST_FOREACH(pvd2, hash, pvd_hash) {
219 if (pvd2->pvd_pn == pn && pvd2->pvd_pid == pid &&
220 pvd2->pvd_vnode->v_mount == mp) {
221 vp = pvd2->pvd_vnode;
223 mtx_unlock(&pfs_vncache_mutex);
224 if (vget_finish(vp, LK_EXCLUSIVE, vs) == 0) {
235 ++pfs_vncache_misses;
236 if (++pfs_vncache_entries > pfs_vncache_maxentries)
237 pfs_vncache_maxentries = pfs_vncache_entries;
238 SLIST_INSERT_HEAD(hash, pvd, pvd_hash);
239 mtx_unlock(&pfs_vncache_mutex);
247 pfs_vncache_free(struct vnode *vp)
249 struct pfs_vdata *pvd, *pvd2;
251 mtx_lock(&pfs_vncache_mutex);
252 pvd = (struct pfs_vdata *)vp->v_data;
253 KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
254 SLIST_FOREACH(pvd2, PFS_VNCACHE_HASH(pvd->pvd_pid), pvd_hash) {
257 SLIST_REMOVE(PFS_VNCACHE_HASH(pvd->pvd_pid), pvd, pfs_vdata, pvd_hash);
258 --pfs_vncache_entries;
261 mtx_unlock(&pfs_vncache_mutex);
263 free(pvd, M_PFSVNCACHE);
269 * Purge the cache of dead entries
271 * The code is not very efficient and this perhaps can be addressed without
272 * a complete rewrite. Previous iteration was walking a linked list from
273 * scratch every time. This code only walks the relevant hash chain (if pid
274 * is provided), but still resorts to scanning the entire cache at least twice
275 * if a specific component is to be removed which is slower. This can be
276 * augmented with resizing the hash.
278 * Explanation of the previous state:
280 * This is extremely inefficient due to the fact that vgone() not only
281 * indirectly modifies the vnode cache, but may also sleep. We can
282 * neither hold pfs_vncache_mutex across a vgone() call, nor make any
283 * assumptions about the state of the cache after vgone() returns. In
284 * consequence, we must start over after every vgone() call, and keep
285 * trying until we manage to traverse the entire cache.
287 * The only way to improve this situation is to change the data structure
288 * used to implement the cache.
292 pfs_purge_one(struct vnode *vnp)
295 VOP_LOCK(vnp, LK_EXCLUSIVE);
302 pfs_purge(struct pfs_node *pn)
304 struct pfs_vdata *pvd;
308 mtx_lock(&pfs_vncache_mutex);
311 for (i = 0; i < pfs_vncache_hash; i++) {
313 SLIST_FOREACH(pvd, &pfs_vncache_hashtbl[i], pvd_hash) {
314 if (pn != NULL && pvd->pvd_pn != pn)
316 vnp = pvd->pvd_vnode;
318 mtx_unlock(&pfs_vncache_mutex);
321 mtx_lock(&pfs_vncache_mutex);
327 mtx_unlock(&pfs_vncache_mutex);
338 * Free all vnodes associated with a defunct process
341 pfs_exit(void *arg, struct proc *p)
343 struct pfs_vncache_head *hash;
344 struct pfs_vdata *pvd;
349 hash = PFS_VNCACHE_HASH(pid);
350 if (SLIST_EMPTY(hash))
353 mtx_lock(&pfs_vncache_mutex);
354 SLIST_FOREACH(pvd, hash, pvd_hash) {
355 if (pvd->pvd_pid != pid)
357 vnp = pvd->pvd_vnode;
359 mtx_unlock(&pfs_vncache_mutex);
363 mtx_unlock(&pfs_vncache_mutex);