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);
117 pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
118 struct pfs_node *pn, pid_t pid)
120 struct pfs_vncache_head *hash;
121 struct pfs_vdata *pvd, *pvd2;
126 * See if the vnode is in the cache.
128 hash = PFS_VNCACHE_HASH(pid);
129 if (SLIST_EMPTY(hash))
132 mtx_lock(&pfs_vncache_mutex);
133 SLIST_FOREACH(pvd, hash, pvd_hash) {
134 if (pvd->pvd_pn == pn && pvd->pvd_pid == pid &&
135 pvd->pvd_vnode->v_mount == mp) {
138 mtx_unlock(&pfs_vncache_mutex);
139 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) {
143 * Some callers cache_enter(vp) later, so
144 * we have to make sure it's not in the
145 * VFS cache so it doesn't get entered
146 * twice. A better solution would be to
147 * make pfs_vncache_alloc() responsible
148 * for entering the vnode in the VFS
157 mtx_unlock(&pfs_vncache_mutex);
159 /* nope, get a new one */
160 pvd = malloc(sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
161 error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
163 free(pvd, M_PFSVNCACHE);
168 (*vpp)->v_data = pvd;
169 switch (pn->pn_type) {
171 (*vpp)->v_vflag = VV_ROOT;
173 printf("root vnode allocated\n");
179 case pfstype_procdir:
180 (*vpp)->v_type = VDIR;
183 (*vpp)->v_type = VREG;
185 case pfstype_symlink:
186 (*vpp)->v_type = VLNK;
189 KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
191 panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
194 * Propagate flag through to vnode so users know it can change
195 * if the process changes (i.e. execve)
197 if ((pn->pn_flags & PFS_PROCDEP) != 0)
198 (*vpp)->v_vflag |= VV_PROCDEP;
199 pvd->pvd_vnode = *vpp;
200 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
202 error = insmntque(*vpp, mp);
204 free(pvd, M_PFSVNCACHE);
209 mtx_lock(&pfs_vncache_mutex);
211 * Other thread may race with us, creating the entry we are
212 * going to insert into the cache. Recheck after
213 * pfs_vncache_mutex is reacquired.
215 SLIST_FOREACH(pvd2, hash, pvd_hash) {
216 if (pvd2->pvd_pn == pn && pvd2->pvd_pid == pid &&
217 pvd2->pvd_vnode->v_mount == mp) {
218 vp = pvd2->pvd_vnode;
220 mtx_unlock(&pfs_vncache_mutex);
221 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) {
232 ++pfs_vncache_misses;
233 if (++pfs_vncache_entries > pfs_vncache_maxentries)
234 pfs_vncache_maxentries = pfs_vncache_entries;
235 SLIST_INSERT_HEAD(hash, pvd, pvd_hash);
236 mtx_unlock(&pfs_vncache_mutex);
244 pfs_vncache_free(struct vnode *vp)
246 struct pfs_vdata *pvd, *pvd2;
248 mtx_lock(&pfs_vncache_mutex);
249 pvd = (struct pfs_vdata *)vp->v_data;
250 KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
251 SLIST_FOREACH(pvd2, PFS_VNCACHE_HASH(pvd->pvd_pid), pvd_hash) {
254 SLIST_REMOVE(PFS_VNCACHE_HASH(pvd->pvd_pid), pvd, pfs_vdata, pvd_hash);
255 --pfs_vncache_entries;
258 mtx_unlock(&pfs_vncache_mutex);
260 free(pvd, M_PFSVNCACHE);
266 * Purge the cache of dead entries
268 * The code is not very efficient and this perhaps can be addressed without
269 * a complete rewrite. Previous iteration was walking a linked list from
270 * scratch every time. This code only walks the relevant hash chain (if pid
271 * is provided), but still resorts to scanning the entire cache at least twice
272 * if a specific component is to be removed which is slower. This can be
273 * augmented with resizing the hash.
275 * Explanation of the previous state:
277 * This is extremely inefficient due to the fact that vgone() not only
278 * indirectly modifies the vnode cache, but may also sleep. We can
279 * neither hold pfs_vncache_mutex across a vgone() call, nor make any
280 * assumptions about the state of the cache after vgone() returns. In
281 * consequence, we must start over after every vgone() call, and keep
282 * trying until we manage to traverse the entire cache.
284 * The only way to improve this situation is to change the data structure
285 * used to implement the cache.
289 pfs_purge_one(struct vnode *vnp)
292 VOP_LOCK(vnp, LK_EXCLUSIVE);
299 pfs_purge(struct pfs_node *pn)
301 struct pfs_vdata *pvd;
305 mtx_lock(&pfs_vncache_mutex);
308 for (i = 0; i < pfs_vncache_hash; i++) {
310 SLIST_FOREACH(pvd, &pfs_vncache_hashtbl[i], pvd_hash) {
311 if (pn != NULL && pvd->pvd_pn != pn)
313 vnp = pvd->pvd_vnode;
315 mtx_unlock(&pfs_vncache_mutex);
318 mtx_lock(&pfs_vncache_mutex);
324 mtx_unlock(&pfs_vncache_mutex);
335 * Free all vnodes associated with a defunct process
338 pfs_exit(void *arg, struct proc *p)
340 struct pfs_vncache_head *hash;
341 struct pfs_vdata *pvd;
346 hash = PFS_VNCACHE_HASH(pid);
347 if (SLIST_EMPTY(hash))
350 mtx_lock(&pfs_vncache_mutex);
351 SLIST_FOREACH(pvd, hash, pvd_hash) {
352 if (pvd->pvd_pid != pid)
354 vnp = pvd->pvd_vnode;
356 mtx_unlock(&pfs_vncache_mutex);
360 mtx_unlock(&pfs_vncache_mutex);