pseudofs_vncache.c revision 167497
1/*-
2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_vncache.c 167497 2007-03-13 01:50:27Z tegge $");
31
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 struct pfs_vdata *pfs_vncache;
52static eventhandler_tag pfs_exit_tag;
53static void pfs_exit(void *arg, struct proc *p);
54
55SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 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
80/*
81 * Initialize vnode cache
82 */
83void
84pfs_vncache_load(void)
85{
86	mtx_init(&pfs_vncache_mutex, "pseudofs_vncache", NULL, MTX_DEF);
87	pfs_exit_tag = EVENTHANDLER_REGISTER(process_exit, pfs_exit, NULL,
88	    EVENTHANDLER_PRI_ANY);
89}
90
91/*
92 * Tear down vnode cache
93 */
94void
95pfs_vncache_unload(void)
96{
97	EVENTHANDLER_DEREGISTER(process_exit, pfs_exit_tag);
98	if (pfs_vncache_entries != 0)
99		printf("pfs_vncache_unload(): %d entries remaining\n",
100		    pfs_vncache_entries);
101	mtx_destroy(&pfs_vncache_mutex);
102}
103
104/*
105 * Allocate a vnode
106 */
107int
108pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
109		  struct pfs_node *pn, pid_t pid)
110{
111	struct pfs_vdata *pvd;
112	struct vnode *vp;
113	int error;
114
115	/*
116	 * See if the vnode is in the cache.
117	 * XXX linear search is not very efficient.
118	 */
119retry:
120	mtx_lock(&pfs_vncache_mutex);
121	for (pvd = pfs_vncache; pvd; pvd = pvd->pvd_next) {
122		if (pvd->pvd_pn == pn && pvd->pvd_pid == pid &&
123		    pvd->pvd_vnode->v_mount == mp) {
124			vp = pvd->pvd_vnode;
125			VI_LOCK(vp);
126			mtx_unlock(&pfs_vncache_mutex);
127			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread) == 0) {
128				++pfs_vncache_hits;
129				*vpp = vp;
130				/* XXX see comment at top of pfs_lookup() */
131				cache_purge(vp);
132				return (0);
133			}
134			goto retry;
135		}
136	}
137	mtx_unlock(&pfs_vncache_mutex);
138	++pfs_vncache_misses;
139
140	/* nope, get a new one */
141	MALLOC(pvd, struct pfs_vdata *, sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
142	if (++pfs_vncache_entries > pfs_vncache_maxentries)
143		pfs_vncache_maxentries = pfs_vncache_entries;
144	error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp);
145	if (error) {
146		FREE(pvd, M_PFSVNCACHE);
147		return (error);
148	}
149	pvd->pvd_pn = pn;
150	pvd->pvd_pid = pid;
151	(*vpp)->v_data = pvd;
152	switch (pn->pn_type) {
153	case pfstype_root:
154		(*vpp)->v_vflag = VV_ROOT;
155#if 0
156		printf("root vnode allocated\n");
157#endif
158		/* fall through */
159	case pfstype_dir:
160	case pfstype_this:
161	case pfstype_parent:
162	case pfstype_procdir:
163		(*vpp)->v_type = VDIR;
164		break;
165	case pfstype_file:
166		(*vpp)->v_type = VREG;
167		break;
168	case pfstype_symlink:
169		(*vpp)->v_type = VLNK;
170		break;
171	case pfstype_none:
172		KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
173	default:
174		panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
175	}
176	/*
177	 * Propagate flag through to vnode so users know it can change
178	 * if the process changes (i.e. execve)
179	 */
180	if ((pn->pn_flags & PFS_PROCDEP) != 0)
181		(*vpp)->v_vflag |= VV_PROCDEP;
182	pvd->pvd_vnode = *vpp;
183	(*vpp)->v_vnlock->lk_flags |= LK_CANRECURSE;
184	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, curthread);
185	error = insmntque(*vpp, mp);
186	if (error != 0) {
187		FREE(pvd, M_PFSVNCACHE);
188		*vpp = NULLVP;
189		return (error);
190	}
191	mtx_lock(&pfs_vncache_mutex);
192	pvd->pvd_prev = NULL;
193	pvd->pvd_next = pfs_vncache;
194	if (pvd->pvd_next)
195		pvd->pvd_next->pvd_prev = pvd;
196	pfs_vncache = pvd;
197	mtx_unlock(&pfs_vncache_mutex);
198	return (0);
199}
200
201/*
202 * Free a vnode
203 */
204int
205pfs_vncache_free(struct vnode *vp)
206{
207	struct pfs_vdata *pvd;
208
209	mtx_lock(&pfs_vncache_mutex);
210	pvd = (struct pfs_vdata *)vp->v_data;
211	KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
212	if (pvd->pvd_next)
213		pvd->pvd_next->pvd_prev = pvd->pvd_prev;
214	if (pvd->pvd_prev)
215		pvd->pvd_prev->pvd_next = pvd->pvd_next;
216	else
217		pfs_vncache = pvd->pvd_next;
218	mtx_unlock(&pfs_vncache_mutex);
219
220	--pfs_vncache_entries;
221	FREE(pvd, M_PFSVNCACHE);
222	vp->v_data = NULL;
223	return (0);
224}
225
226/*
227 * Free all vnodes associated with a defunct process
228 *
229 * XXXRW: It is unfortunate that pfs_exit() always acquires and releases two
230 * mutexes (one of which is Giant) for every process exit, even if procfs
231 * isn't mounted.
232 */
233static void
234pfs_exit(void *arg, struct proc *p)
235{
236	struct pfs_vdata *pvd;
237	struct vnode *vnp;
238
239	if (pfs_vncache == NULL)
240		return;
241	mtx_lock(&Giant);
242	/*
243	 * This is extremely inefficient due to the fact that vgone() not
244	 * only indirectly modifies the vnode cache, but may also sleep.
245	 * We can neither hold pfs_vncache_mutex across a vgone() call,
246	 * nor make any assumptions about the state of the cache after
247	 * vgone() returns.  In consequence, we must start over after
248	 * every vgone() call, and keep trying until we manage to traverse
249	 * the entire cache.
250	 *
251	 * The only way to improve this situation is to change the data
252	 * structure used to implement the cache.  An obvious choice in
253	 * this particular case would be a BST sorted by PID.
254	 */
255	mtx_lock(&pfs_vncache_mutex);
256	pvd = pfs_vncache;
257	while (pvd != NULL) {
258		if (pvd->pvd_pid == p->p_pid) {
259			vnp = pvd->pvd_vnode;
260			vhold(vnp);
261			mtx_unlock(&pfs_vncache_mutex);
262			VOP_LOCK(vnp, LK_EXCLUSIVE, curthread);
263			vgone(vnp);
264			VOP_UNLOCK(vnp, 0, curthread);
265			vdrop(vnp);
266			mtx_lock(&pfs_vncache_mutex);
267			pvd = pfs_vncache;
268		} else {
269			pvd = pvd->pvd_next;
270		}
271	}
272	mtx_unlock(&pfs_vncache_mutex);
273	mtx_unlock(&Giant);
274}
275
276/*
277 * Disable a pseudofs node, and free all vnodes associated with it
278 */
279int
280pfs_disable(struct pfs_node *pn)
281{
282	struct pfs_vdata *pvd;
283	struct vnode *vnp;
284
285	if (pn->pn_flags & PFS_DISABLED)
286		return (0);
287	pn->pn_flags |= PFS_DISABLED;
288	/* XXX see comment above nearly identical code in pfs_exit() */
289	mtx_lock(&pfs_vncache_mutex);
290	pvd = pfs_vncache;
291	while (pvd != NULL) {
292		if (pvd->pvd_pn == pn) {
293			vnp = pvd->pvd_vnode;
294			vhold(vnp);
295			mtx_unlock(&pfs_vncache_mutex);
296			VOP_LOCK(vnp, LK_EXCLUSIVE, curthread);
297			vgone(vnp);
298			VOP_UNLOCK(vnp, 0, curthread);
299			vdrop(vnp);
300			mtx_lock(&pfs_vncache_mutex);
301			pvd = pfs_vncache;
302		} else {
303			pvd = pvd->pvd_next;
304		}
305	}
306	mtx_unlock(&pfs_vncache_mutex);
307	return (0);
308}
309
310/*
311 * Re-enable a disabled pseudofs node
312 */
313int
314pfs_enable(struct pfs_node *pn)
315{
316	pn->pn_flags &= ~PFS_DISABLED;
317	return (0);
318}
319