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