xref: /freebsd/sys/fs/pseudofs/pseudofs_vncache.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_pseudofs.h"
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/eventhandler.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
46 
47 #include <fs/pseudofs/pseudofs.h>
48 #include <fs/pseudofs/pseudofs_internal.h>
49 
50 static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
51 
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);
56 
57 static SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
58     "pseudofs vnode cache");
59 
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");
64 
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");
69 
70 static int pfs_vncache_hits;
71 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
72     &pfs_vncache_hits, 0,
73     "number of cache hits since initialization");
74 
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");
79 
80 extern struct vop_vector pfs_vnodeops;	/* XXX -> .h file */
81 
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])
85 
86 /*
87  * Initialize vnode cache
88  */
89 void
90 pfs_vncache_load(void)
91 {
92 
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);
97 }
98 
99 /*
100  * Tear down vnode cache
101  */
102 void
103 pfs_vncache_unload(void)
104 {
105 
106 	EVENTHANDLER_DEREGISTER(process_exit, pfs_exit_tag);
107 	pfs_purge_all();
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);
112 }
113 
114 /*
115  * Allocate a vnode
116  */
117 int
118 pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
119 		  struct pfs_node *pn, pid_t pid)
120 {
121 	struct pfs_vncache_head *hash;
122 	struct pfs_vdata *pvd, *pvd2;
123 	struct vnode *vp;
124 	enum vgetstate vs;
125 	int error;
126 
127 	/*
128 	 * See if the vnode is in the cache.
129 	 */
130 	hash = PFS_VNCACHE_HASH(pid);
131 	if (SLIST_EMPTY(hash))
132 		goto alloc;
133 retry:
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) {
138 			vp = pvd->pvd_vnode;
139 			vs = vget_prep(vp);
140 			mtx_unlock(&pfs_vncache_mutex);
141 			if (vget_finish(vp, LK_EXCLUSIVE, vs) == 0) {
142 				++pfs_vncache_hits;
143 				*vpp = vp;
144 				/*
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
151 				 * cache.
152 				 */
153 				cache_purge(vp);
154 				return (0);
155 			}
156 			goto retry;
157 		}
158 	}
159 	mtx_unlock(&pfs_vncache_mutex);
160 alloc:
161 	/* nope, get a new one */
162 	pvd = malloc(sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
163 	error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
164 	if (error) {
165 		free(pvd, M_PFSVNCACHE);
166 		return (error);
167 	}
168 	pvd->pvd_pn = pn;
169 	pvd->pvd_pid = pid;
170 	(*vpp)->v_data = pvd;
171 	switch (pn->pn_type) {
172 	case pfstype_root:
173 		(*vpp)->v_vflag = VV_ROOT;
174 #if 0
175 		printf("root vnode allocated\n");
176 #endif
177 		/* fall through */
178 	case pfstype_dir:
179 	case pfstype_this:
180 	case pfstype_parent:
181 	case pfstype_procdir:
182 		(*vpp)->v_type = VDIR;
183 		break;
184 	case pfstype_file:
185 		(*vpp)->v_type = VREG;
186 		break;
187 	case pfstype_symlink:
188 		(*vpp)->v_type = VLNK;
189 		break;
190 	case pfstype_none:
191 		KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
192 	default:
193 		panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
194 	}
195 	/*
196 	 * Propagate flag through to vnode so users know it can change
197 	 * if the process changes (i.e. execve)
198 	 */
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);
203 	VN_LOCK_AREC(*vpp);
204 	error = insmntque(*vpp, mp);
205 	if (error != 0) {
206 		free(pvd, M_PFSVNCACHE);
207 		*vpp = NULLVP;
208 		return (error);
209 	}
210 retry2:
211 	mtx_lock(&pfs_vncache_mutex);
212 	/*
213 	 * Other thread may race with us, creating the entry we are
214 	 * going to insert into the cache. Recheck after
215 	 * pfs_vncache_mutex is reacquired.
216 	 */
217 	SLIST_FOREACH(pvd2, hash, pvd_hash) {
218 		if (pvd2->pvd_pn == pn && pvd2->pvd_pid == pid &&
219 		    pvd2->pvd_vnode->v_mount == mp) {
220 			vp = pvd2->pvd_vnode;
221 			VI_LOCK(vp);
222 			mtx_unlock(&pfs_vncache_mutex);
223 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) {
224 				++pfs_vncache_hits;
225 				vgone(*vpp);
226 				vput(*vpp);
227 				*vpp = vp;
228 				cache_purge(vp);
229 				return (0);
230 			}
231 			goto retry2;
232 		}
233 	}
234 	++pfs_vncache_misses;
235 	if (++pfs_vncache_entries > pfs_vncache_maxentries)
236 		pfs_vncache_maxentries = pfs_vncache_entries;
237 	SLIST_INSERT_HEAD(hash, pvd, pvd_hash);
238 	mtx_unlock(&pfs_vncache_mutex);
239 	return (0);
240 }
241 
242 /*
243  * Free a vnode
244  */
245 int
246 pfs_vncache_free(struct vnode *vp)
247 {
248 	struct pfs_vdata *pvd, *pvd2;
249 
250 	mtx_lock(&pfs_vncache_mutex);
251 	pvd = (struct pfs_vdata *)vp->v_data;
252 	KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
253 	SLIST_FOREACH(pvd2, PFS_VNCACHE_HASH(pvd->pvd_pid), pvd_hash) {
254 		if (pvd2 != pvd)
255 			continue;
256 		SLIST_REMOVE(PFS_VNCACHE_HASH(pvd->pvd_pid), pvd, pfs_vdata, pvd_hash);
257 		--pfs_vncache_entries;
258 		break;
259 	}
260 	mtx_unlock(&pfs_vncache_mutex);
261 
262 	free(pvd, M_PFSVNCACHE);
263 	vp->v_data = NULL;
264 	return (0);
265 }
266 
267 /*
268  * Purge the cache of dead entries
269  *
270  * The code is not very efficient and this perhaps can be addressed without
271  * a complete rewrite. Previous iteration was walking a linked list from
272  * scratch every time. This code only walks the relevant hash chain (if pid
273  * is provided), but still resorts to scanning the entire cache at least twice
274  * if a specific component is to be removed which is slower. This can be
275  * augmented with resizing the hash.
276  *
277  * Explanation of the previous state:
278  *
279  * This is extremely inefficient due to the fact that vgone() not only
280  * indirectly modifies the vnode cache, but may also sleep.  We can
281  * neither hold pfs_vncache_mutex across a vgone() call, nor make any
282  * assumptions about the state of the cache after vgone() returns.  In
283  * consequence, we must start over after every vgone() call, and keep
284  * trying until we manage to traverse the entire cache.
285  *
286  * The only way to improve this situation is to change the data structure
287  * used to implement the cache.
288  */
289 
290 static void
291 pfs_purge_one(struct vnode *vnp)
292 {
293 
294 	VOP_LOCK(vnp, LK_EXCLUSIVE);
295 	vgone(vnp);
296 	VOP_UNLOCK(vnp);
297 	vdrop(vnp);
298 }
299 
300 void
301 pfs_purge(struct pfs_node *pn)
302 {
303 	struct pfs_vdata *pvd;
304 	struct vnode *vnp;
305 	u_long i, removed;
306 
307 	mtx_lock(&pfs_vncache_mutex);
308 restart:
309 	removed = 0;
310 	for (i = 0; i < pfs_vncache_hash; i++) {
311 restart_chain:
312 		SLIST_FOREACH(pvd, &pfs_vncache_hashtbl[i], pvd_hash) {
313 			if (pn != NULL && pvd->pvd_pn != pn)
314 				continue;
315 			vnp = pvd->pvd_vnode;
316 			vhold(vnp);
317 			mtx_unlock(&pfs_vncache_mutex);
318 			pfs_purge_one(vnp);
319 			removed++;
320 			mtx_lock(&pfs_vncache_mutex);
321 			goto restart_chain;
322 		}
323 	}
324 	if (removed > 0)
325 		goto restart;
326 	mtx_unlock(&pfs_vncache_mutex);
327 }
328 
329 static void
330 pfs_purge_all(void)
331 {
332 
333 	pfs_purge(NULL);
334 }
335 
336 /*
337  * Free all vnodes associated with a defunct process
338  */
339 static void
340 pfs_exit(void *arg, struct proc *p)
341 {
342 	struct pfs_vncache_head *hash;
343 	struct pfs_vdata *pvd;
344 	struct vnode *vnp;
345 	int pid;
346 
347 	pid = p->p_pid;
348 	hash = PFS_VNCACHE_HASH(pid);
349 	if (SLIST_EMPTY(hash))
350 		return;
351 restart:
352 	mtx_lock(&pfs_vncache_mutex);
353 	SLIST_FOREACH(pvd, hash, pvd_hash) {
354 		if (pvd->pvd_pid != pid)
355 			continue;
356 		vnp = pvd->pvd_vnode;
357 		vhold(vnp);
358 		mtx_unlock(&pfs_vncache_mutex);
359 		pfs_purge_one(vnp);
360 		goto restart;
361 	}
362 	mtx_unlock(&pfs_vncache_mutex);
363 }
364