pseudofs_vncache.c revision 84246
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 84246 2001-10-01 04:22:20Z des $
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/mount.h>
37#include <sys/mutex.h>
38#include <sys/proc.h>
39#include <sys/sbuf.h>
40#include <sys/sysctl.h>
41#include <sys/vnode.h>
42
43#include <fs/pseudofs/pseudofs.h>
44#include <fs/pseudofs/pseudofs_internal.h>
45
46static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache");
47
48static struct mtx pfs_vncache_mutex;
49struct pfs_vdata *pfs_vncache;
50static void pfs_exit(struct proc *p);
51
52SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0,
53    "pseudofs vnode cache");
54
55static int pfs_vncache_entries;
56SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD,
57    &pfs_vncache_entries, 0,
58    "number of entries in the vnode cache");
59
60static int pfs_vncache_maxentries;
61SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD,
62    &pfs_vncache_maxentries, 0,
63    "highest number of entries in the vnode cache");
64
65static int pfs_vncache_hits;
66SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD,
67    &pfs_vncache_hits, 0,
68    "number of cache hits since initialization");
69
70static int pfs_vncache_misses;
71SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD,
72    &pfs_vncache_misses, 0,
73    "number of cache misses since initialization");
74
75extern vop_t **pfs_vnodeop_p;
76
77/*
78 * Initialize vnode cache
79 */
80void
81pfs_vncache_load(void)
82{
83	mtx_init(&pfs_vncache_mutex, "pseudofs_vncache", MTX_DEF|MTX_RECURSE);
84	/* XXX at_exit() can fail with ENOMEN */
85	at_exit(pfs_exit);
86}
87
88/*
89 * Tear down vnode cache
90 */
91void
92pfs_vncache_unload(void)
93{
94	rm_at_exit(pfs_exit);
95	mtx_destroy(&pfs_vncache_mutex);
96}
97
98/*
99 * Allocate a vnode
100 */
101int
102pfs_vncache_alloc(struct mount *mp, struct vnode **vpp,
103		  struct pfs_node *pn, pid_t pid)
104{
105	struct pfs_vdata *pvd;
106	int error;
107
108	/* see if the vnode is in the cache */
109	/* XXX linear search... not very efficient */
110	mtx_lock(&pfs_vncache_mutex);
111	for (pvd = pfs_vncache; pvd; pvd = pvd->pvd_next) {
112		if (pvd->pvd_pn == pn && pvd->pvd_pid == pid) {
113			if (vget(pvd->pvd_vnode, 0, curthread) == 0) {
114				++pfs_vncache_hits;
115				*vpp = pvd->pvd_vnode;
116				mtx_unlock(&pfs_vncache_mutex);
117				return (0);
118			}
119			/* XXX if this can happen, we're in trouble */
120			break;
121		}
122	}
123	mtx_unlock(&pfs_vncache_mutex);
124	++pfs_vncache_misses;
125
126	/* nope, get a new one */
127	MALLOC(pvd, struct pfs_vdata *, sizeof *pvd, M_PFSVNCACHE, M_WAITOK);
128	if (++pfs_vncache_entries > pfs_vncache_maxentries)
129		pfs_vncache_maxentries = pfs_vncache_entries;
130	error = getnewvnode(VT_PSEUDOFS, mp, pfs_vnodeop_p, vpp);
131	if (error)
132		return (error);
133	pvd->pvd_pn = pn;
134	pvd->pvd_pid = pid;
135	(*vpp)->v_data = pvd;
136	switch (pn->pn_type) {
137	case pfstype_root:
138		(*vpp)->v_flag = VROOT;
139#if 0
140		printf("root vnode allocated\n");
141#endif
142		/* fall through */
143	case pfstype_dir:
144	case pfstype_this:
145	case pfstype_parent:
146	case pfstype_procdir:
147		(*vpp)->v_type = VDIR;
148		break;
149	case pfstype_file:
150		(*vpp)->v_type = VREG;
151		break;
152	case pfstype_symlink:
153		(*vpp)->v_type = VLNK;
154		break;
155	case pfstype_none:
156		KASSERT(0, ("pfs_vncache_alloc called for null node\n"));
157	default:
158		panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type);
159	}
160	pvd->pvd_vnode = *vpp;
161	mtx_lock(&pfs_vncache_mutex);
162	pvd->pvd_prev = NULL;
163	pvd->pvd_next = pfs_vncache;
164	if (pvd->pvd_next)
165		pvd->pvd_next->pvd_prev = pvd;
166	pfs_vncache = pvd;
167	mtx_unlock(&pfs_vncache_mutex);
168	return (0);
169}
170
171/*
172 * Free a vnode
173 */
174int
175pfs_vncache_free(struct vnode *vp)
176{
177	struct pfs_vdata *pvd;
178
179	mtx_lock(&pfs_vncache_mutex);
180	pvd = (struct pfs_vdata *)vp->v_data;
181	KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n"));
182	if (pvd->pvd_next)
183		pvd->pvd_next->pvd_prev = pvd->pvd_prev;
184	if (pvd->pvd_prev)
185		pvd->pvd_prev->pvd_next = pvd->pvd_next;
186	else
187		pfs_vncache = pvd->pvd_next;
188	mtx_unlock(&pfs_vncache_mutex);
189
190	--pfs_vncache_entries;
191	FREE(pvd, M_PFSVNCACHE);
192	vp->v_data = NULL;
193	return (0);
194}
195
196/*
197 * Free all vnodes associated with a defunct process
198 */
199static void
200pfs_exit(struct proc *p)
201{
202	struct pfs_vdata *pvd, *prev;
203
204	mtx_lock(&pfs_vncache_mutex);
205	/*
206	 * The double loop is necessary because vgone() indirectly
207	 * calls pfs_vncache_free() which frees pvd, so we have to
208	 * backtrace one step every time we free a vnode.
209	 */
210	/* XXX linear search... not very efficient */
211	for (pvd = pfs_vncache; pvd != NULL; pvd = pvd->pvd_next) {
212		while (pvd != NULL && pvd->pvd_pid == p->p_pid) {
213			prev = pvd->pvd_prev;
214			vgone(pvd->pvd_vnode);
215			pvd = prev ? prev->pvd_next : pfs_vncache;
216		}
217	}
218	mtx_unlock(&pfs_vncache_mutex);
219}
220