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
50static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
51
52static struct mtx pfs_vncache_mutex;
53static eventhandler_tag pfs_exit_tag;
54static void pfs_exit(void *arg, struct proc *p);
55static void pfs_purge_all(void);
56
57static SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
58    "pseudofs vnode cache");
59
60static int pfs_vncache_entries;
61SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD,
62    &pfs_vncache_entries, 0,
63    "number of entries in the vnode cache");
64
65static int pfs_vncache_maxentries;
66SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD,
67    &pfs_vncache_maxentries, 0,
68    "highest number of entries in the vnode cache");
69
70static int pfs_vncache_hits;
71SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
72    &pfs_vncache_hits, 0,
73    "number of cache hits since initialization");
74
75static int pfs_vncache_misses;
76SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD,
77    &pfs_vncache_misses, 0,
78    "number of cache misses since initialization");
79
80extern struct vop_vector pfs_vnodeops;	/* XXX -> .h file */
81
82static SLIST_HEAD(pfs_vncache_head, pfs_vdata) *pfs_vncache_hashtbl;
83static 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 */
89void
90pfs_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 */
102void
103pfs_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}
112
113/*
114 * Allocate a vnode
115 */
116int
117pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
118		  struct pfs_node *pn, pid_t pid)
119{
120	struct pfs_vncache_head *hash;
121	struct pfs_vdata *pvd, *pvd2;
122	struct vnode *vp;
123	enum vgetstate vs;
124	int error;
125
126	/*
127	 * See if the vnode is in the cache.
128	 */
129	hash = PFS_VNCACHE_HASH(pid);
130	if (SLIST_EMPTY(hash))
131		goto alloc;
132retry:
133	mtx_lock(&pfs_vncache_mutex);
134	SLIST_FOREACH(pvd, hash, pvd_hash) {
135		if (pvd->pvd_pn == pn && pvd->pvd_pid == pid &&
136		    pvd->pvd_vnode->v_mount == mp) {
137			vp = pvd->pvd_vnode;
138			vs = vget_prep(vp);
139			mtx_unlock(&pfs_vncache_mutex);
140			if (vget_finish(vp, LK_EXCLUSIVE, vs) == 0) {
141				++pfs_vncache_hits;
142				*vpp = vp;
143				/*
144				 * Some callers cache_enter(vp) later, so
145				 * we have to make sure it's not in the
146				 * VFS cache so it doesn't get entered
147				 * twice.  A better solution would be to
148				 * make pfs_vncache_alloc() responsible
149				 * for entering the vnode in the VFS
150				 * cache.
151				 */
152				cache_purge(vp);
153				return (0);
154			}
155			goto retry;
156		}
157	}
158	mtx_unlock(&pfs_vncache_mutex);
159alloc:
160	/* nope, get a new one */
161	pvd = malloc(sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
162	error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
163	if (error) {
164		free(pvd, M_PFSVNCACHE);
165		return (error);
166	}
167	pvd->pvd_pn = pn;
168	pvd->pvd_pid = pid;
169	(*vpp)->v_data = pvd;
170	switch (pn->pn_type) {
171	case pfstype_root:
172		(*vpp)->v_vflag = VV_ROOT;
173#if 0
174		printf("root vnode allocated\n");
175#endif
176		/* fall through */
177	case pfstype_dir:
178	case pfstype_this:
179	case pfstype_parent:
180	case pfstype_procdir:
181		(*vpp)->v_type = VDIR;
182		break;
183	case pfstype_file:
184		(*vpp)->v_type = VREG;
185		break;
186	case pfstype_symlink:
187		(*vpp)->v_type = VLNK;
188		break;
189	case pfstype_none:
190		KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
191	default:
192		panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
193	}
194	/*
195	 * Propagate flag through to vnode so users know it can change
196	 * if the process changes (i.e. execve)
197	 */
198	if ((pn->pn_flags & PFS_PROCDEP) != 0)
199		(*vpp)->v_vflag |= VV_PROCDEP;
200	pvd->pvd_vnode = *vpp;
201	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
202	VN_LOCK_AREC(*vpp);
203	error = insmntque(*vpp, mp);
204	if (error != 0) {
205		free(pvd, M_PFSVNCACHE);
206		*vpp = NULLVP;
207		return (error);
208	}
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			VI_LOCK(vp);
221			mtx_unlock(&pfs_vncache_mutex);
222			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 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