Home | History | Annotate | Line # | Download | only in fsck_lfs
bufcache.c revision 1.1
      1 /* $NetBSD: bufcache.c,v 1.1 2003/03/28 08:09:52 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/queue.h>
     43 #include <sys/mount.h>
     44 
     45 #include <assert.h>
     46 #include <err.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 
     52 #include "bufcache.h"
     53 #include "vnode.h"
     54 
     55 /*
     56  * Definitions for the buffer free lists.
     57  */
     58 #define	BQUEUES		3	/* number of free buffer queues */
     59 
     60 #define	BQ_LOCKED	0	/* super-blocks &c */
     61 #define	BQ_LRU		1	/* lru, useful buffers */
     62 #define	BQ_AGE		2	/* rubbish */
     63 
     64 TAILQ_HEAD(bqueues, ubuf) bufqueues[BQUEUES];
     65 
     66 #define HASH_MAX 101
     67 
     68 struct bufhash_struct bufhash[HASH_MAX];
     69 
     70 int maxbufs = BUF_CACHE_SIZE;
     71 int nbufs = 0;
     72 int cachehits = 0;
     73 int cachemisses = 0;
     74 int hashmax = 0;
     75 off_t locked_queue_bytes = 0;
     76 
     77 /* Simple buffer hash function */
     78 static int
     79 vl_hash(struct uvnode * vp, daddr_t lbn)
     80 {
     81 	return (int)((unsigned long) vp + lbn) % HASH_MAX;
     82 }
     83 
     84 /* Initialize buffer cache */
     85 void
     86 bufinit(void)
     87 {
     88 	int i;
     89 
     90 	for (i = 0; i < BQUEUES; i++) {
     91 		TAILQ_INIT(&bufqueues[i]);
     92 	}
     93 	for (i = 0; i < HASH_MAX; i++)
     94 		LIST_INIT(&bufhash[HASH_MAX]);
     95 }
     96 
     97 /* Print statistics of buffer cache usage */
     98 void
     99 bufstats(void)
    100 {
    101 	printf("buffer cache: %d hits %d misses (%2.2f%%); hash depth %d\n",
    102 	    cachehits, cachemisses,
    103 	    (cachehits * 100.0) / (cachehits + cachemisses),
    104 	    hashmax);
    105 }
    106 
    107 /*
    108  * Remove a buffer from the cache.
    109  * Caller must remove the buffer from its free list.
    110  */
    111 void
    112 buf_destroy(struct ubuf * bp)
    113 {
    114 	bp->b_flags |= B_NEEDCOMMIT;
    115 	LIST_REMOVE(bp, b_vnbufs);
    116 	LIST_REMOVE(bp, b_hash);
    117 	free(bp->b_data);
    118 	free(bp);
    119 	--nbufs;
    120 }
    121 
    122 /* Remove a buffer from its free list. */
    123 void
    124 bremfree(struct ubuf * bp)
    125 {
    126 	struct bqueues *dp = NULL;
    127 
    128 	/*
    129 	 * We only calculate the head of the freelist when removing
    130 	 * the last element of the list as that is the only time that
    131 	 * it is needed (e.g. to reset the tail pointer).
    132 	 *
    133 	 * NB: This makes an assumption about how tailq's are implemented.
    134 	 */
    135 	if (bp->b_flags & B_LOCKED)
    136 		locked_queue_bytes -= bp->b_bcount;
    137 	if (TAILQ_NEXT(bp, b_freelist) == NULL) {
    138 		for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
    139 			if (dp->tqh_last == &bp->b_freelist.tqe_next)
    140 				break;
    141 		if (dp == &bufqueues[BQUEUES])
    142 			errx(1, "bremfree: lost tail");
    143 	}
    144 	++bp->b_vp->v_usecount;
    145 	TAILQ_REMOVE(dp, bp, b_freelist);
    146 }
    147 
    148 /* Return a buffer if it is in the cache, otherwise return NULL. */
    149 struct ubuf *
    150 incore(struct uvnode * vp, int lbn)
    151 {
    152 	struct ubuf *bp;
    153 	int hash, depth;
    154 
    155 	hash = vl_hash(vp, lbn);
    156 	/* XXX use a real hash instead. */
    157 	depth = 0;
    158 	LIST_FOREACH(bp, &bufhash[hash], b_hash) {
    159 		if (++depth > hashmax)
    160 			hashmax = depth;
    161 		if (bp->b_vp == vp && bp->b_lblkno == lbn) {
    162 			return bp;
    163 		}
    164 	}
    165 	return NULL;
    166 }
    167 
    168 /*
    169  * Return a buffer of the given size, lbn and uvnode.
    170  * If none is in core, make a new one.
    171  */
    172 struct ubuf *
    173 getblk(struct uvnode * vp, daddr_t lbn, int size)
    174 {
    175 	struct ubuf *bp;
    176 
    177 	/*
    178 	 * First check the buffer cache lists.
    179 	 */
    180 	if ((bp = incore(vp, lbn)) != NULL) {
    181 		assert(!(bp->b_flags & B_NEEDCOMMIT));
    182 		assert(!(bp->b_flags & B_BUSY));
    183 		assert(bp->b_bcount == size);
    184 		bp->b_flags |= B_BUSY;
    185 		bremfree(bp);
    186 		return bp;
    187 	}
    188 	/*
    189 	 * Not on the list.
    190 	 * Get a new block of the appropriate size and use that.
    191 	 * If not enough space, free blocks from the AGE and LRU lists
    192 	 * to make room.
    193 	 */
    194 	while (nbufs >= maxbufs) {
    195 		bp = TAILQ_FIRST(&bufqueues[BQ_AGE]);
    196 		if (bp)
    197 			TAILQ_REMOVE(&bufqueues[BQ_AGE], bp, b_freelist);
    198 		if (bp == NULL) {
    199 			bp = TAILQ_FIRST(&bufqueues[BQ_LRU]);
    200 			if (bp)
    201 				TAILQ_REMOVE(&bufqueues[BQ_AGE], bp,
    202 				    b_freelist);
    203 		}
    204 		if (bp) {
    205 			if (bp->b_flags & B_DELWRI)
    206 				VOP_STRATEGY(bp);
    207 			buf_destroy(bp);
    208 		}
    209 #ifdef DEBUG
    210 		else {
    211 			warnx("no free buffers, allocating more than %d",
    212 			    maxbufs);
    213 		}
    214 #endif
    215 	}
    216 	++nbufs;
    217 	bp = (struct ubuf *) malloc(sizeof(*bp));
    218 	memset(bp, 0, sizeof(*bp));
    219 	bp->b_data = malloc(size);
    220 	memset(bp->b_data, 0, size);
    221 
    222 	bp->b_vp = vp;
    223 	bp->b_blkno = bp->b_lblkno = lbn;
    224 	bp->b_bcount = size;
    225 	LIST_INSERT_HEAD(&bufhash[vl_hash(vp, lbn)], bp, b_hash);
    226 	LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
    227 	bp->b_flags = B_BUSY;
    228 
    229 	return bp;
    230 }
    231 
    232 /* Write a buffer to disk according to its strategy routine. */
    233 void
    234 bwrite(struct ubuf * bp)
    235 {
    236 	bp->b_flags &= ~(B_READ | B_DONE | B_DELWRI | B_LOCKED);
    237 	VOP_STRATEGY(bp);
    238 	bp->b_flags |= B_DONE;
    239 	reassignbuf(bp, bp->b_vp);
    240 	brelse(bp);
    241 }
    242 
    243 /* Put a buffer back on its free list, clear B_BUSY. */
    244 void
    245 brelse(struct ubuf * bp)
    246 {
    247 	int age;
    248 
    249 	assert(!(bp->b_flags & B_NEEDCOMMIT));
    250 	assert(bp->b_flags & B_BUSY);
    251 
    252 	age = bp->b_flags & B_AGE;
    253 	bp->b_flags &= ~(B_BUSY | B_AGE);
    254 	if (bp->b_flags & B_INVAL) {
    255 		buf_destroy(bp);
    256 		return;
    257 	}
    258 	if (bp->b_flags & B_LOCKED) {
    259 		locked_queue_bytes += bp->b_bcount;
    260 		TAILQ_INSERT_TAIL(&bufqueues[BQ_LOCKED], bp, b_freelist);
    261 	} else if (age) {
    262 		TAILQ_INSERT_TAIL(&bufqueues[BQ_AGE], bp, b_freelist);
    263 	} else {
    264 		TAILQ_INSERT_TAIL(&bufqueues[BQ_LRU], bp, b_freelist);
    265 	}
    266 	--bp->b_vp->v_usecount;
    267 }
    268 
    269 /* Read the given block from disk, return it B_BUSY. */
    270 int
    271 bread(struct uvnode * vp, daddr_t lbn, int size, struct ucred * unused,
    272     struct ubuf ** bpp)
    273 {
    274 	struct ubuf *bp;
    275 	daddr_t daddr;
    276 	int error, count;
    277 
    278 	bp = getblk(vp, lbn, size);
    279 	*bpp = bp;
    280 	if (bp->b_flags & (B_DELWRI | B_DONE)) {
    281 		++cachehits;
    282 		return 0;
    283 	}
    284 	++cachemisses;
    285 
    286 	/*
    287 	 * Not found.  Need to find that block's location on disk,
    288 	 * and load it in.
    289 	 */
    290 	daddr = -1;
    291 	error = VOP_BMAP(vp, lbn, &daddr);
    292 	bp->b_blkno = daddr;
    293 	if (daddr >= 0) {
    294 		count = pread(vp->v_fd, bp->b_data, bp->b_bcount,
    295 				dbtob((off_t) daddr));
    296 		if (count == bp->b_bcount) {
    297 			bp->b_flags |= B_DONE;
    298 			return 0;
    299 		}
    300 		return -1;
    301 	}
    302 	memset(bp->b_data, 0, bp->b_bcount);
    303 	return 0;
    304 }
    305 
    306 /* Move a buffer between dirty and clean block lists. */
    307 void
    308 reassignbuf(struct ubuf * bp, struct uvnode * vp)
    309 {
    310 	LIST_REMOVE(bp, b_vnbufs);
    311 	if (bp->b_flags & B_DELWRI) {
    312 		LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs);
    313 	} else {
    314 		LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
    315 	}
    316 }
    317