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