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