xref: /freebsd/sys/fs/pseudofs/pseudofs_vncache.c (revision 81ad6265)
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 	vn_set_state(*vpp, VSTATE_CONSTRUCTED);
211 retry2:
212 	mtx_lock(&pfs_vncache_mutex);
213 	/*
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.
217 	 */
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;
222 			vs = vget_prep(vp);
223 			mtx_unlock(&pfs_vncache_mutex);
224 			if (vget_finish(vp, LK_EXCLUSIVE, vs) == 0) {
225 				++pfs_vncache_hits;
226 				vgone(*vpp);
227 				vput(*vpp);
228 				*vpp = vp;
229 				cache_purge(vp);
230 				return (0);
231 			}
232 			goto retry2;
233 		}
234 	}
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);
240 	return (0);
241 }
242 
243 /*
244  * Free a vnode
245  */
246 int
247 pfs_vncache_free(struct vnode *vp)
248 {
249 	struct pfs_vdata *pvd, *pvd2;
250 
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) {
255 		if (pvd2 != pvd)
256 			continue;
257 		SLIST_REMOVE(PFS_VNCACHE_HASH(pvd->pvd_pid), pvd, pfs_vdata, pvd_hash);
258 		--pfs_vncache_entries;
259 		break;
260 	}
261 	mtx_unlock(&pfs_vncache_mutex);
262 
263 	free(pvd, M_PFSVNCACHE);
264 	vp->v_data = NULL;
265 	return (0);
266 }
267 
268 /*
269  * Purge the cache of dead entries
270  *
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.
277  *
278  * Explanation of the previous state:
279  *
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.
286  *
287  * The only way to improve this situation is to change the data structure
288  * used to implement the cache.
289  */
290 
291 static void
292 pfs_purge_one(struct vnode *vnp)
293 {
294 
295 	VOP_LOCK(vnp, LK_EXCLUSIVE);
296 	vgone(vnp);
297 	VOP_UNLOCK(vnp);
298 	vdrop(vnp);
299 }
300 
301 void
302 pfs_purge(struct pfs_node *pn)
303 {
304 	struct pfs_vdata *pvd;
305 	struct vnode *vnp;
306 	u_long i, removed;
307 
308 	mtx_lock(&pfs_vncache_mutex);
309 restart:
310 	removed = 0;
311 	for (i = 0; i < pfs_vncache_hash; i++) {
312 restart_chain:
313 		SLIST_FOREACH(pvd, &pfs_vncache_hashtbl[i], pvd_hash) {
314 			if (pn != NULL && pvd->pvd_pn != pn)
315 				continue;
316 			vnp = pvd->pvd_vnode;
317 			vhold(vnp);
318 			mtx_unlock(&pfs_vncache_mutex);
319 			pfs_purge_one(vnp);
320 			removed++;
321 			mtx_lock(&pfs_vncache_mutex);
322 			goto restart_chain;
323 		}
324 	}
325 	if (removed > 0)
326 		goto restart;
327 	mtx_unlock(&pfs_vncache_mutex);
328 }
329 
330 static void
331 pfs_purge_all(void)
332 {
333 
334 	pfs_purge(NULL);
335 }
336 
337 /*
338  * Free all vnodes associated with a defunct process
339  */
340 static void
341 pfs_exit(void *arg, struct proc *p)
342 {
343 	struct pfs_vncache_head *hash;
344 	struct pfs_vdata *pvd;
345 	struct vnode *vnp;
346 	int pid;
347 
348 	pid = p->p_pid;
349 	hash = PFS_VNCACHE_HASH(pid);
350 	if (SLIST_EMPTY(hash))
351 		return;
352 restart:
353 	mtx_lock(&pfs_vncache_mutex);
354 	SLIST_FOREACH(pvd, hash, pvd_hash) {
355 		if (pvd->pvd_pid != pid)
356 			continue;
357 		vnp = pvd->pvd_vnode;
358 		vhold(vnp);
359 		mtx_unlock(&pfs_vncache_mutex);
360 		pfs_purge_one(vnp);
361 		goto restart;
362 	}
363 	mtx_unlock(&pfs_vncache_mutex);
364 }
365