1 /* $NetBSD: vnode.c,v 1.5 2006/03/20 01:20:55 christos 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 if (vgr == NULL) 114 err(1, NULL); 115 vgr->vgr_fs = fs; 116 vgr->vgr_func = func; 117 LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list); 118 } 119 120 static struct uvnode * 121 VFS_VGET(void *fs, ino_t ino) 122 { 123 struct vget_reg *vgr; 124 125 LIST_FOREACH(vgr, &vgrlist, vgr_list) { 126 if (vgr->vgr_fs == fs) 127 return vgr->vgr_func(fs, ino); 128 } 129 return NULL; 130 } 131 132 void 133 vnode_destroy(struct uvnode *tossvp) 134 { 135 struct ubuf *bp; 136 137 --nvnodes; 138 LIST_REMOVE(tossvp, v_getvnodes); 139 LIST_REMOVE(tossvp, v_mntvnodes); 140 while ((bp = LIST_FIRST(&tossvp->v_dirtyblkhd)) != NULL) { 141 LIST_REMOVE(bp, b_vnbufs); 142 bremfree(bp); 143 buf_destroy(bp); 144 } 145 while ((bp = LIST_FIRST(&tossvp->v_cleanblkhd)) != NULL) { 146 LIST_REMOVE(bp, b_vnbufs); 147 bremfree(bp); 148 buf_destroy(bp); 149 } 150 free(VTOI(tossvp)->inode_ext.lfs); 151 free(VTOI(tossvp)->i_din.ffs1_din); 152 memset(VTOI(tossvp), 0, sizeof(struct inode)); 153 free(tossvp->v_data); 154 memset(tossvp, 0, sizeof(*tossvp)); 155 free(tossvp); 156 } 157 158 int hits, misses; 159 160 /* 161 * Find a vnode in the cache; if not present, get it from the 162 * filesystem-specific vget routine. 163 */ 164 struct uvnode * 165 vget(void *fs, ino_t ino) 166 { 167 struct uvnode *vp, *tossvp; 168 int hash; 169 170 /* Look in the uvnode cache */ 171 tossvp = NULL; 172 hash = ((unsigned long)fs + ino) & (VNODE_HASH_MAX - 1); 173 LIST_FOREACH(vp, &getvnodelist[hash], v_getvnodes) { 174 if (vp->v_fs != fs) 175 continue; 176 if (VTOI(vp)->i_number == ino) { 177 /* Move to the front of the list */ 178 LIST_REMOVE(vp, v_getvnodes); 179 LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes); 180 ++hits; 181 break; 182 } 183 if (LIST_EMPTY(&vp->v_dirtyblkhd) && 184 vp->v_usecount == 0 && 185 !(vp->v_flag & VDIROP)) 186 tossvp = vp; 187 } 188 /* Don't let vnode list grow arbitrarily */ 189 if (nvnodes > VNODE_CACHE_SIZE && tossvp) { 190 vnode_destroy(tossvp); 191 } 192 if (vp) 193 return vp; 194 195 ++misses; 196 return VFS_VGET(fs, ino); 197 } 198 199 void 200 vfs_init(void) 201 { 202 int i; 203 204 nvnodes = 0; 205 LIST_INIT(&vnodelist); 206 for (i = 0; i < VNODE_HASH_MAX; i++) 207 LIST_INIT(&getvnodelist[i]); 208 LIST_INIT(&vgrlist); 209 } 210