pseudofs_vncache.c revision 77965
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 77965 2001-06-10 10:36:16Z des $ 29 */ 30 31#include <sys/param.h> 32#include <sys/kernel.h> 33#include <sys/systm.h> 34#include <sys/malloc.h> 35#include <sys/mount.h> 36#include <sys/mutex.h> 37#include <sys/sbuf.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, "pseudofs_vncache", "pseudofs vnode cache"); 45 46static struct mtx pfs_vncache_mutex; 47 48struct pfs_vnode { 49 struct vnode *pv_vnode; 50 struct pfs_vnode *pv_next; 51} *pfs_vncache; 52 53SYSCTL_NODE(_vfs_pfs, OID_AUTO, vncache, CTLFLAG_RW, 0, 54 "pseudofs vnode cache"); 55 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