Home | History | Annotate | Line # | Download | only in fsck_lfs
vnode.c revision 1.13
      1  1.13  dholland /* $NetBSD: vnode.c,v 1.13 2013/06/06 00:54:49 dholland 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.13  dholland #define _SYS_VNODE_H_ /* XXX */
     39  1.13  dholland #define VU_DIROP 0x01000000 /* XXX XXX from sys/vnode.h */
     40  1.13  dholland #define vnode uvnode
     41  1.12  dholland #include <ufs/lfs/ulfs_inode.h>
     42  1.12  dholland #include <ufs/lfs/ulfsmount.h>
     43   1.1  perseant #include <ufs/lfs/lfs.h>
     44   1.1  perseant #undef vnode
     45   1.1  perseant 
     46   1.1  perseant #include <assert.h>
     47   1.1  perseant #include <err.h>
     48   1.1  perseant #include <errno.h>
     49   1.1  perseant #include <stdarg.h>
     50   1.1  perseant #include <stdio.h>
     51   1.1  perseant #include <stdlib.h>
     52   1.1  perseant #include <string.h>
     53   1.1  perseant #include <unistd.h>
     54   1.6  christos #include <util.h>
     55   1.1  perseant 
     56   1.1  perseant #include "bufcache.h"
     57   1.1  perseant #include "vnode.h"
     58  1.10     pooka #include "kernelops.h"
     59   1.1  perseant 
     60   1.1  perseant struct uvnodelst vnodelist;
     61   1.3  perseant struct uvnodelst getvnodelist[VNODE_HASH_MAX];
     62   1.1  perseant struct vgrlst    vgrlist;
     63   1.1  perseant 
     64   1.1  perseant int nvnodes;
     65   1.1  perseant 
     66   1.1  perseant /* Convert between inode pointers and vnode pointers. */
     67   1.1  perseant #ifndef VTOI
     68   1.1  perseant #define	VTOI(vp)	((struct inode *)(vp)->v_data)
     69   1.1  perseant #endif
     70   1.1  perseant 
     71   1.1  perseant /*
     72   1.1  perseant  * Raw device uvnode ops
     73   1.1  perseant  */
     74   1.1  perseant 
     75   1.1  perseant int
     76   1.1  perseant raw_vop_strategy(struct ubuf * bp)
     77   1.1  perseant {
     78   1.1  perseant 	if (bp->b_flags & B_READ) {
     79  1.10     pooka 		return kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
     80  1.11   mlelstv 		    bp->b_blkno * dev_bsize);
     81   1.1  perseant 	} else {
     82  1.10     pooka 		return kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
     83  1.11   mlelstv 		    bp->b_blkno * dev_bsize);
     84   1.1  perseant 	}
     85   1.1  perseant }
     86   1.1  perseant 
     87   1.1  perseant int
     88   1.1  perseant raw_vop_bwrite(struct ubuf * bp)
     89   1.1  perseant {
     90   1.1  perseant 	bp->b_flags &= ~(B_READ | B_DELWRI | B_DONE | B_ERROR);
     91   1.1  perseant 	raw_vop_strategy(bp);
     92   1.7        ad 	brelse(bp, 0);
     93   1.1  perseant 	return 0;
     94   1.1  perseant }
     95   1.1  perseant 
     96   1.1  perseant int
     97   1.1  perseant raw_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
     98   1.1  perseant {
     99   1.1  perseant 	*daddrp = lbn;
    100   1.1  perseant 	return 0;
    101   1.1  perseant }
    102   1.1  perseant 
    103   1.1  perseant /* Register a fs-specific vget function */
    104   1.1  perseant void
    105   1.1  perseant register_vget(void *fs, struct uvnode *func(void *, ino_t))
    106   1.1  perseant {
    107   1.1  perseant 	struct vget_reg *vgr;
    108   1.1  perseant 
    109   1.6  christos 	vgr = emalloc(sizeof(*vgr));
    110   1.1  perseant 	vgr->vgr_fs = fs;
    111   1.1  perseant 	vgr->vgr_func = func;
    112   1.1  perseant 	LIST_INSERT_HEAD(&vgrlist, vgr, vgr_list);
    113   1.1  perseant }
    114   1.1  perseant 
    115   1.1  perseant static struct uvnode *
    116   1.1  perseant VFS_VGET(void *fs, ino_t ino)
    117   1.1  perseant {
    118   1.1  perseant 	struct vget_reg *vgr;
    119   1.1  perseant 
    120   1.1  perseant 	LIST_FOREACH(vgr, &vgrlist, vgr_list) {
    121   1.1  perseant 		if (vgr->vgr_fs == fs)
    122   1.1  perseant 			return vgr->vgr_func(fs, ino);
    123   1.1  perseant 	}
    124   1.1  perseant 	return NULL;
    125   1.1  perseant }
    126   1.1  perseant 
    127   1.1  perseant void
    128   1.1  perseant vnode_destroy(struct uvnode *tossvp)
    129   1.1  perseant {
    130   1.1  perseant 	struct ubuf *bp;
    131   1.1  perseant 
    132   1.1  perseant 	--nvnodes;
    133   1.1  perseant 	LIST_REMOVE(tossvp, v_getvnodes);
    134   1.1  perseant 	LIST_REMOVE(tossvp, v_mntvnodes);
    135   1.5  christos 	while ((bp = LIST_FIRST(&tossvp->v_dirtyblkhd)) != NULL) {
    136   1.5  christos 		LIST_REMOVE(bp, b_vnbufs);
    137   1.1  perseant 		bremfree(bp);
    138   1.1  perseant 		buf_destroy(bp);
    139   1.1  perseant 	}
    140   1.5  christos 	while ((bp = LIST_FIRST(&tossvp->v_cleanblkhd)) != NULL) {
    141   1.5  christos 		LIST_REMOVE(bp, b_vnbufs);
    142   1.1  perseant 		bremfree(bp);
    143   1.1  perseant 		buf_destroy(bp);
    144   1.1  perseant 	}
    145   1.1  perseant 	free(VTOI(tossvp)->inode_ext.lfs);
    146   1.2      fvdl 	free(VTOI(tossvp)->i_din.ffs1_din);
    147   1.1  perseant 	memset(VTOI(tossvp), 0, sizeof(struct inode));
    148   1.1  perseant 	free(tossvp->v_data);
    149   1.1  perseant 	memset(tossvp, 0, sizeof(*tossvp));
    150   1.1  perseant 	free(tossvp);
    151   1.1  perseant }
    152   1.1  perseant 
    153   1.3  perseant int hits, misses;
    154   1.3  perseant 
    155   1.1  perseant /*
    156   1.1  perseant  * Find a vnode in the cache; if not present, get it from the
    157   1.1  perseant  * filesystem-specific vget routine.
    158   1.1  perseant  */
    159   1.1  perseant struct uvnode *
    160   1.1  perseant vget(void *fs, ino_t ino)
    161   1.1  perseant {
    162   1.1  perseant 	struct uvnode *vp, *tossvp;
    163   1.3  perseant 	int hash;
    164   1.1  perseant 
    165   1.1  perseant 	/* Look in the uvnode cache */
    166   1.1  perseant 	tossvp = NULL;
    167   1.3  perseant 	hash = ((unsigned long)fs + ino) & (VNODE_HASH_MAX - 1);
    168   1.3  perseant 	LIST_FOREACH(vp, &getvnodelist[hash], v_getvnodes) {
    169   1.1  perseant 		if (vp->v_fs != fs)
    170   1.1  perseant 			continue;
    171   1.1  perseant 		if (VTOI(vp)->i_number == ino) {
    172   1.3  perseant 			/* Move to the front of the list */
    173   1.1  perseant 			LIST_REMOVE(vp, v_getvnodes);
    174   1.3  perseant 			LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
    175   1.3  perseant 			++hits;
    176   1.1  perseant 			break;
    177   1.1  perseant 		}
    178   1.1  perseant 		if (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    179   1.1  perseant 		    vp->v_usecount == 0 &&
    180   1.8        ad 		    !(vp->v_uflag & VU_DIROP))
    181   1.1  perseant 			tossvp = vp;
    182   1.1  perseant 	}
    183   1.1  perseant 	/* Don't let vnode list grow arbitrarily */
    184   1.1  perseant 	if (nvnodes > VNODE_CACHE_SIZE && tossvp) {
    185   1.1  perseant 		vnode_destroy(tossvp);
    186   1.1  perseant 	}
    187   1.1  perseant 	if (vp)
    188   1.1  perseant 		return vp;
    189   1.1  perseant 
    190   1.3  perseant 	++misses;
    191   1.1  perseant 	return VFS_VGET(fs, ino);
    192   1.1  perseant }
    193   1.1  perseant 
    194   1.1  perseant void
    195   1.1  perseant vfs_init(void)
    196   1.1  perseant {
    197   1.3  perseant 	int i;
    198   1.3  perseant 
    199   1.1  perseant 	nvnodes = 0;
    200   1.1  perseant 	LIST_INIT(&vnodelist);
    201   1.3  perseant 	for (i = 0; i < VNODE_HASH_MAX; i++)
    202   1.3  perseant 		LIST_INIT(&getvnodelist[i]);
    203   1.1  perseant 	LIST_INIT(&vgrlist);
    204   1.1  perseant }
    205