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