Home | History | Annotate | Line # | Download | only in fsck_lfs
vnode.c revision 1.1
      1 /* $NetBSD: vnode.c,v 1.1 2003/03/28 08:09:55 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 (at) 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;
     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 	memset(VTOI(tossvp), 0, sizeof(struct inode));
    148 	free(tossvp->v_data);
    149 	memset(tossvp, 0, sizeof(*tossvp));
    150 	free(tossvp);
    151 }
    152 
    153 /*
    154  * Find a vnode in the cache; if not present, get it from the
    155  * filesystem-specific vget routine.
    156  */
    157 struct uvnode *
    158 vget(void *fs, ino_t ino)
    159 {
    160 	struct uvnode *vp, *tossvp;
    161 
    162 	/* Look in the uvnode cache */
    163 	tossvp = NULL;
    164 	LIST_FOREACH(vp, &getvnodelist, v_getvnodes) {
    165 		if (vp->v_fs != fs)
    166 			continue;
    167 		if (VTOI(vp)->i_number == ino) {
    168 			LIST_REMOVE(vp, v_getvnodes);
    169 			LIST_INSERT_HEAD(&getvnodelist, vp, v_getvnodes);
    170 			break;
    171 		}
    172 		if (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    173 		    vp->v_usecount == 0 &&
    174 		    !(vp->v_flag & VDIROP))
    175 			tossvp = vp;
    176 	}
    177 	/* Don't let vnode list grow arbitrarily */
    178 	if (nvnodes > VNODE_CACHE_SIZE && tossvp) {
    179 		vnode_destroy(tossvp);
    180 	}
    181 	if (vp)
    182 		return vp;
    183 
    184 	return VFS_VGET(fs, ino);
    185 }
    186 
    187 void
    188 vfs_init(void)
    189 {
    190 	nvnodes = 0;
    191 	LIST_INIT(&vnodelist);
    192 	LIST_INIT(&getvnodelist);
    193 	LIST_INIT(&vgrlist);
    194 }
    195