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