pseudofs_vncache.c revision 103314
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 *      $FreeBSD: head/sys/fs/pseudofs/pseudofs_vncache.c 103314 2002-09-14 09:02:28Z njl $
29 */
30
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/systm.h>
34#include <sys/lock.h>
35#include <sys/malloc.h>
36#include <sys/mutex.h>
37#include <sys/proc.h>
38#include <sys/sysctl.h>
39#include <sys/vnode.h>
40
41#include <fs/pseudofs/pseudofs.h>
42#include <fs/pseudofs/pseudofs_internal.h>
43
44static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
45
46static struct mtx pfs_vncache_mutex;
47static struct pfs_vdata *pfs_vncache;
48static void pfs_exit(struct proc *p);
49
50SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0,
51    "pseudofs vnode cache");
52
53static int pfs_vncache_entries;
54SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD,
55    &pfs_vncache_entries, 0,
56    "number of entries in the vnode cache");
57
58static int pfs_vncache_maxentries;
59SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD,
60    &pfs_vncache_maxentries, 0,
61    "highest number of entries in the vnode cache");
62
63static int pfs_vncache_hits;
64SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
65    &pfs_vncache_hits, 0,
66    "number of cache hits since initialization");
67
68static int pfs_vncache_misses;
69SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD,
70    &pfs_vncache_misses, 0,
71    "number of cache misses since initialization");
72
73extern vop_t **pfs_vnodeop_p;
74
75/*
76 * Initialize vnode cache
77 */
78void
79pfs_vncache_load(void)
80{
81	mtx_init(&pfs_vncache_mutex, "pseudofs_vncache", NULL,
82	    MTX_DEF | MTX_RECURSE);
83	/* XXX at_exit() can fail with ENOMEN */
84	at_exit(pfs_exit);
85}
86
87/*
88 * Tear down vnode cache
89 */
90void
91pfs_vncache_unload(void)
92{
93	rm_at_exit(pfs_exit);
94	if (pfs_vncache_entries != 0)
95		printf("pfs_vncache_unload(): %d entries remaining\n",
96		    pfs_vncache_entries);
97	mtx_destroy(&pfs_vncache_mutex);
98}
99
100/*
101 * Allocate a vnode
102 */
103int
104pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
105		  struct pfs_node *pn, pid_t pid)
106{
107	struct pfs_vdata *pvd;
108	int error;
109
110	/*
111	 * See if the vnode is in the cache.
112	 * XXX linear search is not very efficient.
113	 */
114	mtx_lock(&pfs_vncache_mutex);
115	for (pvd = pfs_vncache; pvd; pvd = pvd->pvd_next) {
116		if (pvd->pvd_pn == pn && pvd->pvd_pid == pid) {
117			if (vget(pvd->pvd_vnode, 0, curthread) == 0) {
118				++pfs_vncache_hits;
119				*vpp = pvd->pvd_vnode;
120				mtx_unlock(&pfs_vncache_mutex);
121				/* XXX see comment at top of pfs_lookup() */
122				cache_purge(*vpp);
123				vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE,
124				    curthread);
125				return (0);
126			}
127			/* XXX if this can happen, we're in trouble */
128			break;
129		}
130	}
131	mtx_unlock(&pfs_vncache_mutex);
132	++pfs_vncache_misses;
133
134	/* nope, get a new one */
135	MALLOC(pvd, struct pfs_vdata *, sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
136	if (++pfs_vncache_entries > pfs_vncache_maxentries)
137		pfs_vncache_maxentries = pfs_vncache_entries;
138	error = getnewvnode("pseudofs", mp, pfs_vnodeop_p, vpp);
139	if (error)
140		return (error);
141	pvd->pvd_pn = pn;
142	pvd->pvd_pid = pid;
143	(*vpp)->v_data = pvd;
144	switch (pn->pn_type) {
145	case pfstype_root:
146		(*vpp)->v_vflag = VV_ROOT;
147#if 0
148		printf("root vnode allocated\n");
149#endif
150		/* fall through */
151	case pfstype_dir:
152	case pfstype_this:
153	case pfstype_parent:
154	case pfstype_procdir:
155		(*vpp)->v_type = VDIR;
156		break;
157	case pfstype_file:
158		(*vpp)->v_type = VREG;
159		break;
160	case pfstype_symlink:
161		(*vpp)->v_type = VLNK;
162		break;
163	case pfstype_none:
164		KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
165	default:
166		panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
167	}
168	/*
169	 * Propagate flag through to vnode so users know it can change
170	 * if the process changes (i.e. execve)
171	 */
172	if ((pn->pn_flags & PFS_PROCDEP) != 0)
173		(*vpp)->v_vflag |= VV_PROCDEP;
174	pvd->pvd_vnode = *vpp;
175	mtx_lock(&pfs_vncache_mutex);
176	pvd->pvd_prev = NULL;
177	pvd->pvd_next = pfs_vncache;
178	if (pvd->pvd_next)
179		pvd->pvd_next->pvd_prev = pvd;
180	pfs_vncache = pvd;
181	mtx_unlock(&pfs_vncache_mutex);
182        (*vpp)->v_vnlock = &(*vpp)->v_lock;
183        lockinit((*vpp)->v_vnlock, PINOD, "pfsnod", VLKTIMEOUT, LK_CANRECURSE);
184	vn_lock(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
185	return (0);
186}
187
188/*
189 * Free a vnode
190 */
191int
192pfs_vncache_free(struct vnode *vp)
193{
194	struct pfs_vdata *pvd;
195
196	cache_purge(vp);
197
198	mtx_lock(&pfs_vncache_mutex);
199	pvd = (struct pfs_vdata *)vp->v_data;
200	KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
201	if (pvd->pvd_next)
202		pvd->pvd_next->pvd_prev = pvd->pvd_prev;
203	if (pvd->pvd_prev)
204		pvd->pvd_prev->pvd_next = pvd->pvd_next;
205	else
206		pfs_vncache = pvd->pvd_next;
207	mtx_unlock(&pfs_vncache_mutex);
208
209	--pfs_vncache_entries;
210	FREE(pvd, M_PFSVNCACHE);
211	vp->v_data = NULL;
212	return (0);
213}
214
215/*
216 * Free all vnodes associated with a defunct process
217 */
218static void
219pfs_exit(struct proc *p)
220{
221	struct pfs_vdata *pvd, *prev;
222
223	mtx_lock(&pfs_vncache_mutex);
224	/*
225	 * The double loop is necessary because vgone() indirectly
226	 * calls pfs_vncache_free() which frees pvd, so we have to
227	 * backtrace one step every time we free a vnode.
228	 */
229	/* XXX linear search... not very efficient */
230	for (pvd = pfs_vncache; pvd != NULL; pvd = pvd->pvd_next) {
231		while (pvd != NULL && pvd->pvd_pid == p->p_pid) {
232			prev = pvd->pvd_prev;
233			vgone(pvd->pvd_vnode);
234			pvd = prev ? prev->pvd_next : pfs_vncache;
235		}
236		if (pvd == NULL)
237			break;
238	}
239	mtx_unlock(&pfs_vncache_mutex);
240}
241
242/*
243 * Disable a pseudofs node, and free all vnodes associated with it
244 */
245int
246pfs_disable(struct pfs_node *pn)
247{
248	struct pfs_vdata *pvd, *prev;
249
250	if (pn->pn_flags & PFS_DISABLED)
251		return (0);
252	mtx_lock(&pfs_vncache_mutex);
253	pn->pn_flags |= PFS_DISABLED;
254	/* see the comment about the double loop in pfs_exit() */
255	/* XXX linear search... not very efficient */
256	for (pvd = pfs_vncache; pvd != NULL; pvd = pvd->pvd_next) {
257		while (pvd != NULL && pvd->pvd_pn == pn) {
258			prev = pvd->pvd_prev;
259			vgone(pvd->pvd_vnode);
260			pvd = prev ? prev->pvd_next : pfs_vncache;
261		}
262		if (pvd == NULL)
263			break;
264	}
265	mtx_unlock(&pfs_vncache_mutex);
266	return (0);
267}
268
269/*
270 * Re-enable a disabled pseudofs node
271 */
272int
273pfs_enable(struct pfs_node *pn)
274{
275	pn->pn_flags &= ~PFS_DISABLED;
276	return (0);
277}
278