pseudofs_vncache.c revision 77965
1130803Smarcel/*- 2130803Smarcel * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3130803Smarcel * All rights reserved. 4130803Smarcel * 5130803Smarcel * Redistribution and use in source and binary forms, with or without 6130803Smarcel * modification, are permitted provided that the following conditions 7130803Smarcel * are met: 8130803Smarcel * 1. Redistributions of source code must retain the above copyright 9130803Smarcel * notice, this list of conditions and the following disclaimer 10130803Smarcel * in this position and unchanged. 11130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12130803Smarcel * notice, this list of conditions and the following disclaimer in the 13130803Smarcel * documentation and/or other materials provided with the distribution. 14130803Smarcel * 3. The name of the author may not be used to endorse or promote products 15130803Smarcel * derived from this software without specific prior written permission. 16130803Smarcel * 17130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18130803Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19130803Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20130803Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21130803Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22130803Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23130803Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24130803Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25130803Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26130803Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27130803Smarcel * 28130803Smarcel * $FreeBSD: head/sys/fs/pseudofs/pseudofs_vncache.c 77965 2001-06-10 10:36:16Z des $ 29130803Smarcel */ 30130803Smarcel 31130803Smarcel#include <sys/param.h> 32130803Smarcel#include <sys/kernel.h> 33130803Smarcel#include <sys/systm.h> 34130803Smarcel#include <sys/malloc.h> 35130803Smarcel#include <sys/mount.h> 36130803Smarcel#include <sys/mutex.h> 37130803Smarcel#include <sys/sbuf.h> 38130803Smarcel#include <sys/sysctl.h> 39130803Smarcel#include <sys/vnode.h> 40130803Smarcel 41130803Smarcel#include <fs/pseudofs/pseudofs.h> 42130803Smarcel#include <fs/pseudofs/pseudofs_internal.h> 43130803Smarcel 44130803Smarcelstatic MALLOC_DEFINE(M_PFSVNCACHE, "pseudofs_vncache", "pseudofs vnode cache"); 45130803Smarcel 46130803Smarcelstatic struct mtx pfs_vncache_mutex; 47130803Smarcel 48130803Smarcelstruct pfs_vnode { 49130803Smarcel struct vnode *pv_vnode; 50130803Smarcel struct pfs_vnode *pv_next; 51130803Smarcel} *pfs_vncache; 52130803Smarcel 53130803SmarcelSYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0, 54130803Smarcel "pseudofs vnode cache"); 55130803Smarcel 56static int pfs_vncache_hits; 57SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, hits, CTLFLAG_RD, &pfs_vncache_hits, 0, 58 "number of cache hits since initialization"); 59 60static int pfs_vncache_misses; 61SYSCTL_INT(_vfs_pfs_vncache, OID_AUTO, misses, CTLFLAG_RD, &pfs_vncache_misses, 0, 62 "number of cache misses since initialization"); 63 64extern vop_t **pfs_vnodeop_p; 65 66/* 67 * Initialize vnode cache 68 */ 69void 70pfs_vncache_load(void) 71{ 72 mtx_init(&pfs_vncache_mutex, "pseudofs_vncache", MTX_DEF); 73} 74 75/* 76 * Tear down vnode cache 77 */ 78void 79pfs_vncache_unload(void) 80{ 81 mtx_destroy(&pfs_vncache_mutex); 82} 83 84/* 85 * Allocate a vnode 86 */ 87int 88pfs_vncache_alloc(struct mount *mp, struct vnode **vpp, struct pfs_node *pn) 89{ 90 struct pfs_vnode *pv; 91 int error; 92 93 mtx_lock(&pfs_vncache_mutex); 94 95 /* see if the vnode is in the cache */ 96 for (pv = pfs_vncache; pv; pv = pv->pv_next) 97 if (pv->pv_vnode->v_data == pn) 98 if (vget(pv->pv_vnode, 0, curproc) == 0) { 99 ++pfs_vncache_hits; 100 *vpp = pv->pv_vnode; 101 mtx_unlock(&pfs_vncache_mutex); 102 return (0); 103 } 104 ++pfs_vncache_misses; 105 106 /* nope, get a new one */ 107 MALLOC(pv, struct pfs_vnode *, sizeof *pv, M_PFSVNCACHE, M_WAITOK); 108 error = getnewvnode(VT_PSEUDOFS, mp, pfs_vnodeop_p, vpp); 109 if (error) { 110 mtx_unlock(&pfs_vncache_mutex); 111 return (error); 112 } 113 (*vpp)->v_data = pn; 114 switch (pn->pn_type) { 115 case pfstype_root: 116 (*vpp)->v_flag = VROOT; 117#if 0 118 printf("root vnode allocated\n"); 119#endif 120 case pfstype_dir: 121 case pfstype_this: 122 case pfstype_parent: 123 (*vpp)->v_type = VDIR; 124 break; 125 case pfstype_file: 126 (*vpp)->v_type = VREG; 127 break; 128 case pfstype_symlink: 129 (*vpp)->v_type = VLNK; 130 break; 131 default: 132 panic("%s has unexpected type: %d", pn->pn_name, pn->pn_type); 133 } 134 pv->pv_vnode = *vpp; 135 pv->pv_next = pfs_vncache; 136 pfs_vncache = pv; 137 mtx_unlock(&pfs_vncache_mutex); 138 return (0); 139} 140 141/* 142 * Free a vnode 143 */ 144int 145pfs_vncache_free(struct vnode *vp) 146{ 147 struct pfs_vnode *prev, *pv; 148 149 mtx_lock(&pfs_vncache_mutex); 150 for (prev = NULL, pv = pfs_vncache; pv; prev = pv, pv = pv->pv_next) 151 if (pv->pv_vnode == vp) 152 break; 153 if (!pv) 154 printf("pfs_vncache_free(): not in cache\n"); /* it should be! */ 155#if 0 156 if (vp->v_data == ((struct pfs_info *)vp->v_mount->mnt_data)->pi_root) 157 printf("root vnode reclaimed\n"); 158#endif 159 vp->v_data = NULL; 160 if (pv) { 161 if (prev) 162 prev->pv_next = pv->pv_next; 163 else 164 pfs_vncache = pv->pv_next; 165 FREE(pv, M_PFSVNCACHE); 166 } 167 mtx_unlock(&pfs_vncache_mutex); 168 return (0); 169} 170