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$"); 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 48 static MALLOC_DEFINE(M_PFSVNCACHE, "pfs_vncache", "pseudofs vnode cache"); 49 50 static struct mtx pfs_vncache_mutex; 51 static struct pfs_vdata *pfs_vncache; 52 static eventhandler_tag pfs_exit_tag; 53 static void pfs_exit(void *arg, struct proc *p); 54 55 static SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0, 56 "pseudofs vnode cache"); 57 58 static int pfs_vncache_entries; 59 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, entries, CTLFLAG_RD, 60 &pfs_vncache_entries, 0, 61 "number of entries in the vnode cache"); 62 63 static int pfs_vncache_maxentries; 64 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, maxentries, CTLFLAG_RD, 65 &pfs_vncache_maxentries, 0, 66 "highest number of entries in the vnode cache"); 67 68 static int pfs_vncache_hits; 69 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD, 70 &pfs_vncache_hits, 0, 71 "number of cache hits since initialization"); 72 73 static int pfs_vncache_misses; 74 SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD, 75 &pfs_vncache_misses, 0, 76 "number of cache misses since initialization"); 77 78 extern struct vop_vector pfs_vnodeops; /* XXX -> .h file */ 79 80 /* 81 * Initialize vnode cache 82 */ 83 void 84 pfs_vncache_load(void) 85 { 86 87 mtx_init(&pfs_vncache_mutex, "pfs_vncache", NULL, MTX_DEF); 88 pfs_exit_tag = EVENTHANDLER_REGISTER(process_exit, pfs_exit, NULL, 89 EVENTHANDLER_PRI_ANY); 90 } 91 92 /* 93 * Tear down vnode cache 94 */ 95 void 96 pfs_vncache_unload(void) 97 { 98 99 EVENTHANDLER_DEREGISTER(process_exit, pfs_exit_tag); 100 KASSERT(pfs_vncache_entries == 0, 101 ("%d vncache entries remaining", pfs_vncache_entries)); 102 mtx_destroy(&pfs_vncache_mutex); 103 } 104 105 /* 106 * Allocate a vnode 107 */ 108 int 109 pfs_vncache_alloc(struct mount *mp, struct vnode **vpp, 110 struct pfs_node *pn, pid_t pid) 111 { 112 struct pfs_vdata *pvd, *pvd2; 113 struct vnode *vp; 114 int error; 115 116 /* 117 * See if the vnode is in the cache. 118 * XXX linear search is not very efficient. 119 */ 120 retry: 121 mtx_lock(&pfs_vncache_mutex); 122 for (pvd = pfs_vncache; pvd; pvd = pvd->pvd_next) { 123 if (pvd->pvd_pn == pn && pvd->pvd_pid == pid && 124 pvd->pvd_vnode->v_mount == mp) { 125 vp = pvd->pvd_vnode; 126 VI_LOCK(vp); 127 mtx_unlock(&pfs_vncache_mutex); 128 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread) == 0) { 129 ++pfs_vncache_hits; 130 *vpp = vp; 131 /* 132 * Some callers cache_enter(vp) later, so 133 * we have to make sure it's not in the 134 * VFS cache so it doesn't get entered 135 * twice. A better solution would be to 136 * make pfs_vncache_alloc() responsible 137 * for entering the vnode in the VFS 138 * cache. 139 */ 140 cache_purge(vp); 141 return (0); 142 } 143 goto retry; 144 } 145 } 146 mtx_unlock(&pfs_vncache_mutex); 147 148 /* nope, get a new one */ 149 pvd = malloc(sizeof *pvd, M_PFSVNCACHE, M_WAITOK); 150 pvd->pvd_next = pvd->pvd_prev = NULL; 151 error = getnewvnode("pseudofs", mp, &pfs_vnodeops, vpp); 152 if (error) { 153 free(pvd, M_PFSVNCACHE); 154 return (error); 155 } 156 pvd->pvd_pn = pn; 157 pvd->pvd_pid = pid; 158 (*vpp)->v_data = pvd; 159 switch (pn->pn_type) { 160 case pfstype_root: 161 (*vpp)->v_vflag = VV_ROOT; 162 #if 0 163 printf("root vnode allocated\n"); 164 #endif 165 /* fall through */ 166 case pfstype_dir: 167 case pfstype_this: 168 case pfstype_parent: 169 case pfstype_procdir: 170 (*vpp)->v_type = VDIR; 171 break; 172 case pfstype_file: 173 (*vpp)->v_type = VREG; 174 break; 175 case pfstype_symlink: 176 (*vpp)->v_type = VLNK; 177 break; 178 case pfstype_none: 179 KASSERT(0, ("pfs_vncache_alloc called for null node\n")); 180 default: 181 panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type); 182 } 183 /* 184 * Propagate flag through to vnode so users know it can change 185 * if the process changes (i.e. execve) 186 */ 187 if ((pn->pn_flags & PFS_PROCDEP) != 0) 188 (*vpp)->v_vflag |= VV_PROCDEP; 189 pvd->pvd_vnode = *vpp; 190 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 191 VN_LOCK_AREC(*vpp); 192 error = insmntque(*vpp, mp); 193 if (error != 0) { 194 free(pvd, M_PFSVNCACHE); 195 *vpp = NULLVP; 196 return (error); 197 } 198 retry2: 199 mtx_lock(&pfs_vncache_mutex); 200 /* 201 * Other thread may race with us, creating the entry we are 202 * going to insert into the cache. Recheck after 203 * pfs_vncache_mutex is reacquired. 204 */ 205 for (pvd2 = pfs_vncache; pvd2; pvd2 = pvd2->pvd_next) { 206 if (pvd2->pvd_pn == pn && pvd2->pvd_pid == pid && 207 pvd2->pvd_vnode->v_mount == mp) { 208 vp = pvd2->pvd_vnode; 209 VI_LOCK(vp); 210 mtx_unlock(&pfs_vncache_mutex); 211 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, curthread) == 0) { 212 ++pfs_vncache_hits; 213 vgone(*vpp); 214 vput(*vpp); 215 *vpp = vp; 216 cache_purge(vp); 217 return (0); 218 } 219 goto retry2; 220 } 221 } 222 ++pfs_vncache_misses; 223 if (++pfs_vncache_entries > pfs_vncache_maxentries) 224 pfs_vncache_maxentries = pfs_vncache_entries; 225 pvd->pvd_prev = NULL; 226 pvd->pvd_next = pfs_vncache; 227 if (pvd->pvd_next) 228 pvd->pvd_next->pvd_prev = pvd; 229 pfs_vncache = pvd; 230 mtx_unlock(&pfs_vncache_mutex); 231 return (0); 232 } 233 234 /* 235 * Free a vnode 236 */ 237 int 238 pfs_vncache_free(struct vnode *vp) 239 { 240 struct pfs_vdata *pvd; 241 242 mtx_lock(&pfs_vncache_mutex); 243 pvd = (struct pfs_vdata *)vp->v_data; 244 KASSERT(pvd != NULL, ("pfs_vncache_free(): no vnode data\n")); 245 if (pvd->pvd_next) 246 pvd->pvd_next->pvd_prev = pvd->pvd_prev; 247 if (pvd->pvd_prev) { 248 pvd->pvd_prev->pvd_next = pvd->pvd_next; 249 --pfs_vncache_entries; 250 } else if (pfs_vncache == pvd) { 251 pfs_vncache = pvd->pvd_next; 252 --pfs_vncache_entries; 253 } 254 mtx_unlock(&pfs_vncache_mutex); 255 256 free(pvd, M_PFSVNCACHE); 257 vp->v_data = NULL; 258 return (0); 259 } 260 261 /* 262 * Purge the cache of dead entries 263 * 264 * This is extremely inefficient due to the fact that vgone() not only 265 * indirectly modifies the vnode cache, but may also sleep. We can 266 * neither hold pfs_vncache_mutex across a vgone() call, nor make any 267 * assumptions about the state of the cache after vgone() returns. In 268 * consequence, we must start over after every vgone() call, and keep 269 * trying until we manage to traverse the entire cache. 270 * 271 * The only way to improve this situation is to change the data structure 272 * used to implement the cache. 273 */ 274 static void 275 pfs_purge_locked(struct pfs_node *pn) 276 { 277 struct pfs_vdata *pvd; 278 struct vnode *vnp; 279 280 mtx_assert(&pfs_vncache_mutex, MA_OWNED); 281 pvd = pfs_vncache; 282 while (pvd != NULL) { 283 if (pvd->pvd_dead || (pn != NULL && pvd->pvd_pn == pn)) { 284 vnp = pvd->pvd_vnode; 285 vhold(vnp); 286 mtx_unlock(&pfs_vncache_mutex); 287 VOP_LOCK(vnp, LK_EXCLUSIVE); 288 vgone(vnp); 289 VOP_UNLOCK(vnp, 0); 290 mtx_lock(&pfs_vncache_mutex); 291 vdrop(vnp); 292 pvd = pfs_vncache; 293 } else { 294 pvd = pvd->pvd_next; 295 } 296 } 297 } 298 299 void 300 pfs_purge(struct pfs_node *pn) 301 { 302 303 mtx_lock(&pfs_vncache_mutex); 304 pfs_purge_locked(pn); 305 mtx_unlock(&pfs_vncache_mutex); 306 } 307 308 /* 309 * Free all vnodes associated with a defunct process 310 */ 311 static void 312 pfs_exit(void *arg, struct proc *p) 313 { 314 struct pfs_vdata *pvd; 315 int dead; 316 317 if (pfs_vncache == NULL) 318 return; 319 mtx_lock(&pfs_vncache_mutex); 320 for (pvd = pfs_vncache, dead = 0; pvd != NULL; pvd = pvd->pvd_next) 321 if (pvd->pvd_pid == p->p_pid) 322 dead = pvd->pvd_dead = 1; 323 if (dead) 324 pfs_purge_locked(NULL); 325 mtx_unlock(&pfs_vncache_mutex); 326 } 327