1 /* $NetBSD: vnode.c,v 1.3 2005/04/11 23:19:24 perseant Exp $ */ 2 /*- 3 * Copyright (c) 2003 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Konrad E. Schroder <perseant@hhhh.org>. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the NetBSD 20 * Foundation, Inc. and its contributors. 21 * 4. Neither the name of The NetBSD Foundation nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/types.h> 39 #include <sys/param.h> 40 #include <sys/time.h> 41 #include <sys/buf.h> 42 #include <sys/mount.h> 43 #include <sys/queue.h> 44 45 #include <ufs/ufs/inode.h> 46 #include <ufs/ufs/ufsmount.h> 47 #define vnode uvnode 48 #include <ufs/lfs/lfs.h> 49 #undef vnode 50 51 #include <assert.h> 52 #include <err.h> 53 #include <errno.h> 54 #include <stdarg.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 #include "bufcache.h" 61 #include "vnode.h" 62 63 struct uvnodelst vnodelist; 64 struct uvnodelst getvnodelist[VNODE_HASH_MAX]; 65 struct vgrlst vgrlist; 66 67 int nvnodes; 68 69 /* Convert between inode pointers and vnode pointers. */ 70 #ifndef VTOI 71 #define VTOI(vp) ((struct inode *)(vp)->v_data) 72 #endif 73 74 /* 75 * Raw device uvnode ops 76 */ 77 78 int 79 raw_vop_strategy(struct ubuf * bp) 80 { 81 if (bp->b_flags & B_READ) { 82 return pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 83 dbtob(bp->b_blkno)); 84 } else { 85 return pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount, 86 dbtob(bp->b_blkno)); 87 } 88 } 89 90 int 91 raw_vop_bwrite(struct ubuf * bp) 92 { 93 bp->b_flags &= ~(B_READ | B_DELWRI | B_DONE | B_ERROR); 94 raw_vop_strategy(bp); 95 brelse(bp); 96 return 0; 97 } 98 99 int 100 raw_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp) 101 { 102 *daddrp = lbn; 103 return 0; 104 } 105 106 /* Register a fs-specific vget function */ 107 void 108 register_vget(void *fs, struct uvnode *func(void *, ino_t)) 109 { 110 struct vget_reg *vgr; 111 112 vgr = (struct vget_reg *)malloc(sizeof(*vgr)); 113 vgr->vgr_fs = fs; 114 vgr->vgr_func = func; 115 LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list); 116 } 117 118 static struct uvnode * 119 VFS_VGET(void *fs, ino_t ino) 120 { 121 struct vget_reg *vgr; 122 123 LIST_FOREACH(vgr, &vgrlist, vgr_list) { 124 if (vgr->vgr_fs == fs) 125 return vgr->vgr_func(fs, ino); 126 } 127 return NULL; 128 } 129 130 void 131 vnode_destroy(struct uvnode *tossvp) 132 { 133 struct ubuf *bp; 134 135 --nvnodes; 136 LIST_REMOVE(tossvp, v_getvnodes); 137 LIST_REMOVE(tossvp, v_mntvnodes); 138 LIST_FOREACH(bp, &tossvp->v_dirtyblkhd, b_vnbufs) { 139 bremfree(bp); 140 buf_destroy(bp); 141 } 142 LIST_FOREACH(bp, &tossvp->v_cleanblkhd, b_vnbufs) { 143 bremfree(bp); 144 buf_destroy(bp); 145 } 146 free(VTOI(tossvp)->inode_ext.lfs); 147 free(VTOI(tossvp)->i_din.ffs1_din); 148 memset(VTOI(tossvp), 0, sizeof(struct inode)); 149 free(tossvp->v_data); 150 memset(tossvp, 0, sizeof(*tossvp)); 151 free(tossvp); 152 } 153 154 int hits, misses; 155 156 /* 157 * Find a vnode in the cache; if not present, get it from the 158 * filesystem-specific vget routine. 159 */ 160 struct uvnode * 161 vget(void *fs, ino_t ino) 162 { 163 struct uvnode *vp, *tossvp; 164 int hash; 165 166 /* Look in the uvnode cache */ 167 tossvp = NULL; 168 hash = ((unsigned long)fs + ino) & (VNODE_HASH_MAX - 1); 169 LIST_FOREACH(vp, &getvnodelist[hash], v_getvnodes) { 170 if (vp->v_fs != fs) 171 continue; 172 if (VTOI(vp)->i_number == ino) { 173 /* Move to the front of the list */ 174 LIST_REMOVE(vp, v_getvnodes); 175 LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes); 176 ++hits; 177 break; 178 } 179 if (LIST_EMPTY(&vp->v_dirtyblkhd) && 180 vp->v_usecount == 0 && 181 !(vp->v_flag & VDIROP)) 182 tossvp = vp; 183 } 184 /* Don't let vnode list grow arbitrarily */ 185 if (nvnodes > VNODE_CACHE_SIZE && tossvp) { 186 vnode_destroy(tossvp); 187 } 188 if (vp) 189 return vp; 190 191 ++misses; 192 return VFS_VGET(fs, ino); 193 } 194 195 void 196 vfs_init(void) 197 { 198 int i; 199 200 nvnodes = 0; 201 LIST_INIT(&vnodelist); 202 for (i = 0; i < VNODE_HASH_MAX; i++) 203 LIST_INIT(&getvnodelist[i]); 204 LIST_INIT(&vgrlist); 205 } 206