Home | History | Annotate | Line # | Download | only in common
bcache.c revision 1.2.10.3
      1  1.2.10.3  yamt /*	$NetBSD: bcache.c,v 1.2.10.3 2007/09/03 14:26:58 yamt Exp $	*/
      2  1.2.10.2  yamt 
      3  1.2.10.2  yamt /*-
      4  1.2.10.2  yamt  * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
      5  1.2.10.2  yamt  * All rights reserved.
      6  1.2.10.2  yamt  *
      7  1.2.10.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.2.10.2  yamt  * modification, are permitted provided that the following conditions
      9  1.2.10.2  yamt  * are met:
     10  1.2.10.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.2.10.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.2.10.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.10.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.10.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.2.10.2  yamt  *
     16  1.2.10.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.2.10.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.2.10.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.2.10.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.2.10.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.2.10.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.2.10.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.2.10.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.2.10.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.2.10.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.2.10.2  yamt  * SUCH DAMAGE.
     27  1.2.10.2  yamt  */
     28  1.2.10.2  yamt 
     29  1.2.10.2  yamt #include <sys/cdefs.h>
     30  1.2.10.2  yamt /* __FBSDID("$FreeBSD: src/sys/boot/common/bcache.c,v 1.12 2003/08/25 23:30:41 obrien Exp $"); */
     31  1.2.10.2  yamt 
     32  1.2.10.2  yamt /*
     33  1.2.10.2  yamt  * Simple LRU block cache
     34  1.2.10.2  yamt  */
     35  1.2.10.2  yamt 
     36  1.2.10.2  yamt #include <sys/stdint.h>
     37  1.2.10.2  yamt 
     38  1.2.10.2  yamt #include <lib/libsa/stand.h>
     39  1.2.10.2  yamt 
     40  1.2.10.2  yamt #include "bitstring.h"  /* XXX: Brought this in from FreeBSD:sys/sys/. Do we have an equivalent in NetBSD ? */
     41  1.2.10.2  yamt #include "bootstrap.h"
     42  1.2.10.2  yamt 
     43  1.2.10.2  yamt /* #define BCACHE_DEBUG */
     44  1.2.10.2  yamt 
     45  1.2.10.2  yamt #ifdef BCACHE_DEBUG
     46  1.2.10.2  yamt #define BCACHE_TIMEOUT	10
     47  1.2.10.2  yamt # define DEBUG(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
     48  1.2.10.2  yamt #else
     49  1.2.10.2  yamt #define BCACHE_TIMEOUT	2
     50  1.2.10.2  yamt # define DEBUG(fmt, args...)
     51  1.2.10.2  yamt #endif
     52  1.2.10.2  yamt 
     53  1.2.10.2  yamt 
     54  1.2.10.2  yamt struct bcachectl
     55  1.2.10.2  yamt {
     56  1.2.10.2  yamt     daddr_t	bc_blkno;
     57  1.2.10.2  yamt     time_t	bc_stamp;
     58  1.2.10.2  yamt     int		bc_count;
     59  1.2.10.2  yamt };
     60  1.2.10.2  yamt 
     61  1.2.10.2  yamt static struct bcachectl	*bcache_ctl;
     62  1.2.10.3  yamt static void *		bcache_data;
     63  1.2.10.2  yamt static bitstr_t		*bcache_miss;
     64  1.2.10.2  yamt static u_int		bcache_nblks;
     65  1.2.10.2  yamt static u_int		bcache_blksize;
     66  1.2.10.2  yamt static u_int		bcache_hits, bcache_misses, bcache_ops, bcache_bypasses;
     67  1.2.10.2  yamt static u_int		bcache_flushes;
     68  1.2.10.2  yamt static u_int		bcache_bcount;
     69  1.2.10.2  yamt 
     70  1.2.10.2  yamt static void	bcache_invalidate(daddr_t blkno);
     71  1.2.10.3  yamt static void	bcache_insert(void *buf, daddr_t blkno);
     72  1.2.10.3  yamt static int	bcache_lookup(void *buf, daddr_t blkno);
     73  1.2.10.2  yamt 
     74  1.2.10.2  yamt /*
     75  1.2.10.2  yamt  * Initialise the cache for (nblks) of (bsize).
     76  1.2.10.2  yamt  */
     77  1.2.10.2  yamt int
     78  1.2.10.2  yamt bcache_init(u_int nblks, size_t bsize)
     79  1.2.10.2  yamt {
     80  1.2.10.2  yamt     /* discard any old contents */
     81  1.2.10.2  yamt     if (bcache_data != NULL) {
     82  1.2.10.2  yamt 	free(bcache_data);
     83  1.2.10.2  yamt 	bcache_data = NULL;
     84  1.2.10.2  yamt 	free(bcache_ctl);
     85  1.2.10.2  yamt     }
     86  1.2.10.2  yamt 
     87  1.2.10.2  yamt     /* Allocate control structures */
     88  1.2.10.2  yamt     bcache_nblks = nblks;
     89  1.2.10.2  yamt     bcache_blksize = bsize;
     90  1.2.10.2  yamt     bcache_data = alloc(bcache_nblks * bcache_blksize);
     91  1.2.10.2  yamt     bcache_ctl = (struct bcachectl *)alloc(bcache_nblks * sizeof(struct bcachectl));
     92  1.2.10.2  yamt     bcache_miss = bit_alloc((bcache_nblks + 1) / 2);
     93  1.2.10.2  yamt     if ((bcache_data == NULL) || (bcache_ctl == NULL) || (bcache_miss == NULL)) {
     94  1.2.10.2  yamt 	if (bcache_miss)
     95  1.2.10.2  yamt 	    free(bcache_miss);
     96  1.2.10.2  yamt 	if (bcache_ctl)
     97  1.2.10.2  yamt 	    free(bcache_ctl);
     98  1.2.10.2  yamt 	if (bcache_data)
     99  1.2.10.2  yamt 	    free(bcache_data);
    100  1.2.10.2  yamt 	bcache_data = NULL;
    101  1.2.10.2  yamt 	return(ENOMEM);
    102  1.2.10.2  yamt     }
    103  1.2.10.2  yamt 
    104  1.2.10.2  yamt     return(0);
    105  1.2.10.2  yamt }
    106  1.2.10.2  yamt 
    107  1.2.10.2  yamt /*
    108  1.2.10.2  yamt  * Flush the cache
    109  1.2.10.2  yamt  */
    110  1.2.10.2  yamt void
    111  1.2.10.2  yamt bcache_flush(void)
    112  1.2.10.2  yamt {
    113  1.2.10.2  yamt     u_int	i;
    114  1.2.10.2  yamt 
    115  1.2.10.2  yamt     bcache_flushes++;
    116  1.2.10.2  yamt 
    117  1.2.10.2  yamt     /* Flush the cache */
    118  1.2.10.2  yamt     for (i = 0; i < bcache_nblks; i++) {
    119  1.2.10.2  yamt 	bcache_ctl[i].bc_count = -1;
    120  1.2.10.2  yamt 	bcache_ctl[i].bc_blkno = -1;
    121  1.2.10.2  yamt     }
    122  1.2.10.2  yamt }
    123  1.2.10.2  yamt 
    124  1.2.10.2  yamt /*
    125  1.2.10.2  yamt  * Handle a write request; write directly to the disk, and populate the
    126  1.2.10.2  yamt  * cache with the new values.
    127  1.2.10.2  yamt  */
    128  1.2.10.2  yamt static int
    129  1.2.10.2  yamt write_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
    130  1.2.10.2  yamt 		char *buf, size_t *rsize)
    131  1.2.10.2  yamt {
    132  1.2.10.2  yamt     struct bcache_devdata	*dd = (struct bcache_devdata *)devdata;
    133  1.2.10.2  yamt     daddr_t			i, nblk;
    134  1.2.10.2  yamt     int				err;
    135  1.2.10.2  yamt 
    136  1.2.10.2  yamt     nblk = size / bcache_blksize;
    137  1.2.10.2  yamt 
    138  1.2.10.2  yamt     /* Invalidate the blocks being written */
    139  1.2.10.2  yamt     for (i = 0; i < nblk; i++) {
    140  1.2.10.2  yamt 	bcache_invalidate(blk + i);
    141  1.2.10.2  yamt     }
    142  1.2.10.2  yamt 
    143  1.2.10.2  yamt     /* Write the blocks */
    144  1.2.10.2  yamt     err = dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize);
    145  1.2.10.2  yamt 
    146  1.2.10.2  yamt     /* Populate the block cache with the new data */
    147  1.2.10.2  yamt     if (err == 0) {
    148  1.2.10.2  yamt     	for (i = 0; i < nblk; i++) {
    149  1.2.10.2  yamt 	    bcache_insert(buf + (i * bcache_blksize),blk + i);
    150  1.2.10.2  yamt     	}
    151  1.2.10.2  yamt     }
    152  1.2.10.2  yamt 
    153  1.2.10.2  yamt     return err;
    154  1.2.10.2  yamt }
    155  1.2.10.2  yamt 
    156  1.2.10.2  yamt /*
    157  1.2.10.2  yamt  * Handle a read request; fill in parts of the request that can
    158  1.2.10.2  yamt  * be satisfied by the cache, use the supplied strategy routine to do
    159  1.2.10.2  yamt  * device I/O and then use the I/O results to populate the cache.
    160  1.2.10.2  yamt  */
    161  1.2.10.2  yamt static int
    162  1.2.10.2  yamt read_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
    163  1.2.10.2  yamt 		char *buf, size_t *rsize)
    164  1.2.10.2  yamt {
    165  1.2.10.2  yamt     struct bcache_devdata	*dd = (struct bcache_devdata *)devdata;
    166  1.2.10.2  yamt     int				p_size, result;
    167  1.2.10.2  yamt     daddr_t			p_blk, i, j, nblk;
    168  1.2.10.3  yamt     void *			p_buf;
    169  1.2.10.2  yamt 
    170  1.2.10.2  yamt     nblk = size / bcache_blksize;
    171  1.2.10.2  yamt     result = 0;
    172  1.2.10.2  yamt 
    173  1.2.10.2  yamt     /* Satisfy any cache hits up front */
    174  1.2.10.2  yamt     for (i = 0; i < nblk; i++) {
    175  1.2.10.2  yamt 	if (bcache_lookup(buf + (bcache_blksize * i), blk + i)) {
    176  1.2.10.2  yamt 	    bit_set(bcache_miss, i);	/* cache miss */
    177  1.2.10.2  yamt 	    bcache_misses++;
    178  1.2.10.2  yamt 	} else {
    179  1.2.10.2  yamt 	    bit_clear(bcache_miss, i);	/* cache hit */
    180  1.2.10.2  yamt 	    bcache_hits++;
    181  1.2.10.2  yamt 	}
    182  1.2.10.2  yamt     }
    183  1.2.10.2  yamt 
    184  1.2.10.2  yamt     /* Go back and fill in any misses  XXX optimise */
    185  1.2.10.2  yamt     p_blk = -1;
    186  1.2.10.2  yamt     p_buf = NULL;
    187  1.2.10.2  yamt     p_size = 0;
    188  1.2.10.2  yamt     for (i = 0; i < nblk; i++) {
    189  1.2.10.2  yamt 	if (bit_test(bcache_miss, i)) {
    190  1.2.10.2  yamt 	    /* miss, add to pending transfer */
    191  1.2.10.2  yamt 	    if (p_blk == -1) {
    192  1.2.10.2  yamt 		p_blk = blk + i;
    193  1.2.10.2  yamt 		p_buf = buf + (bcache_blksize * i);
    194  1.2.10.2  yamt 		p_size = 1;
    195  1.2.10.2  yamt 	    } else {
    196  1.2.10.2  yamt 		p_size++;
    197  1.2.10.2  yamt 	    }
    198  1.2.10.2  yamt 	} else if (p_blk != -1) {
    199  1.2.10.2  yamt 	    /* hit, complete pending transfer */
    200  1.2.10.2  yamt 	    result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
    201  1.2.10.2  yamt 	    if (result != 0)
    202  1.2.10.2  yamt 		goto done;
    203  1.2.10.2  yamt 	    for (j = 0; j < p_size; j++)
    204  1.2.10.2  yamt 		bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
    205  1.2.10.2  yamt 	    p_blk = -1;
    206  1.2.10.2  yamt 	}
    207  1.2.10.2  yamt     }
    208  1.2.10.2  yamt     if (p_blk != -1) {
    209  1.2.10.2  yamt 	/* pending transfer left */
    210  1.2.10.2  yamt 	result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
    211  1.2.10.2  yamt 	if (result != 0)
    212  1.2.10.2  yamt 	    goto done;
    213  1.2.10.2  yamt 	for (j = 0; j < p_size; j++)
    214  1.2.10.2  yamt 	    bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
    215  1.2.10.2  yamt     }
    216  1.2.10.2  yamt 
    217  1.2.10.2  yamt  done:
    218  1.2.10.2  yamt     if ((result == 0) && (rsize != NULL))
    219  1.2.10.2  yamt 	*rsize = size;
    220  1.2.10.2  yamt     return(result);
    221  1.2.10.2  yamt }
    222  1.2.10.2  yamt 
    223  1.2.10.2  yamt /*
    224  1.2.10.2  yamt  * Requests larger than 1/2 the cache size will be bypassed and go
    225  1.2.10.2  yamt  * directly to the disk.  XXX tune this.
    226  1.2.10.2  yamt  */
    227  1.2.10.2  yamt int
    228  1.2.10.2  yamt bcache_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
    229  1.2.10.2  yamt 		char *buf, size_t *rsize)
    230  1.2.10.2  yamt {
    231  1.2.10.2  yamt     static int			bcache_unit = -1;
    232  1.2.10.2  yamt     struct bcache_devdata	*dd = (struct bcache_devdata *)devdata;
    233  1.2.10.2  yamt 
    234  1.2.10.2  yamt     bcache_ops++;
    235  1.2.10.2  yamt 
    236  1.2.10.2  yamt     if(bcache_unit != unit) {
    237  1.2.10.2  yamt 	bcache_flush();
    238  1.2.10.2  yamt 	bcache_unit = unit;
    239  1.2.10.2  yamt     }
    240  1.2.10.2  yamt 
    241  1.2.10.2  yamt     /* bypass large requests, or when the cache is inactive */
    242  1.2.10.2  yamt     if ((bcache_data == NULL) || ((size * 2 / bcache_blksize) > bcache_nblks)) {
    243  1.2.10.2  yamt 	DEBUG("bypass %d from %d", size / bcache_blksize, blk);
    244  1.2.10.2  yamt 	bcache_bypasses++;
    245  1.2.10.2  yamt 	return(dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize));
    246  1.2.10.2  yamt     }
    247  1.2.10.2  yamt 
    248  1.2.10.2  yamt     switch (rw) {
    249  1.2.10.2  yamt     case F_READ:
    250  1.2.10.2  yamt 	return read_strategy(devdata, unit, rw, blk, size, buf, rsize);
    251  1.2.10.2  yamt     case F_WRITE:
    252  1.2.10.2  yamt 	return write_strategy(devdata, unit, rw, blk, size, buf, rsize);
    253  1.2.10.2  yamt     }
    254  1.2.10.2  yamt     return -1;
    255  1.2.10.2  yamt }
    256  1.2.10.2  yamt 
    257  1.2.10.2  yamt 
    258  1.2.10.2  yamt /*
    259  1.2.10.2  yamt  * Insert a block into the cache.  Retire the oldest block to do so, if required.
    260  1.2.10.2  yamt  *
    261  1.2.10.2  yamt  * XXX the LRU algorithm will fail after 2^31 blocks have been transferred.
    262  1.2.10.2  yamt  */
    263  1.2.10.2  yamt static void
    264  1.2.10.3  yamt bcache_insert(void *buf, daddr_t blkno)
    265  1.2.10.2  yamt {
    266  1.2.10.2  yamt     time_t	now;
    267  1.2.10.2  yamt     int		cand, ocount;
    268  1.2.10.2  yamt     u_int	i;
    269  1.2.10.2  yamt 
    270  1.2.10.2  yamt     time(&now);
    271  1.2.10.2  yamt     cand = 0;				/* assume the first block */
    272  1.2.10.2  yamt     ocount = bcache_ctl[0].bc_count;
    273  1.2.10.2  yamt 
    274  1.2.10.2  yamt     /* find the oldest block */
    275  1.2.10.2  yamt     for (i = 1; i < bcache_nblks; i++) {
    276  1.2.10.2  yamt 	if (bcache_ctl[i].bc_blkno == blkno) {
    277  1.2.10.2  yamt 	    /* reuse old entry */
    278  1.2.10.2  yamt 	    cand = i;
    279  1.2.10.2  yamt 	    break;
    280  1.2.10.2  yamt 	}
    281  1.2.10.2  yamt 	if (bcache_ctl[i].bc_count < ocount) {
    282  1.2.10.2  yamt 	    ocount = bcache_ctl[i].bc_count;
    283  1.2.10.2  yamt 	    cand = i;
    284  1.2.10.2  yamt 	}
    285  1.2.10.2  yamt     }
    286  1.2.10.2  yamt 
    287  1.2.10.2  yamt     DEBUG("insert blk %d -> %d @ %d # %d", blkno, cand, now, bcache_bcount);
    288  1.2.10.2  yamt     bcopy(buf, bcache_data + (bcache_blksize * cand), bcache_blksize);
    289  1.2.10.2  yamt     bcache_ctl[cand].bc_blkno = blkno;
    290  1.2.10.2  yamt     bcache_ctl[cand].bc_stamp = now;
    291  1.2.10.2  yamt     bcache_ctl[cand].bc_count = bcache_bcount++;
    292  1.2.10.2  yamt }
    293  1.2.10.2  yamt 
    294  1.2.10.2  yamt /*
    295  1.2.10.2  yamt  * Look for a block in the cache.  Blocks more than BCACHE_TIMEOUT seconds old
    296  1.2.10.2  yamt  * may be stale (removable media) and thus are discarded.  Copy the block out
    297  1.2.10.2  yamt  * if successful and return zero, or return nonzero on failure.
    298  1.2.10.2  yamt  */
    299  1.2.10.2  yamt static int
    300  1.2.10.3  yamt bcache_lookup(void *buf, daddr_t blkno)
    301  1.2.10.2  yamt {
    302  1.2.10.2  yamt     time_t	now;
    303  1.2.10.2  yamt     u_int	i;
    304  1.2.10.2  yamt 
    305  1.2.10.2  yamt     time(&now);
    306  1.2.10.2  yamt 
    307  1.2.10.2  yamt     for (i = 0; i < bcache_nblks; i++)
    308  1.2.10.2  yamt 	/* cache hit? */
    309  1.2.10.2  yamt 	if ((bcache_ctl[i].bc_blkno == blkno) && ((bcache_ctl[i].bc_stamp + BCACHE_TIMEOUT) >= now)) {
    310  1.2.10.2  yamt 	    bcopy(bcache_data + (bcache_blksize * i), buf, bcache_blksize);
    311  1.2.10.2  yamt 	    DEBUG("hit blk %d <- %d (now %d then %d)", blkno, i, now, bcache_ctl[i].bc_stamp);
    312  1.2.10.2  yamt 	    return(0);
    313  1.2.10.2  yamt 	}
    314  1.2.10.2  yamt     return(ENOENT);
    315  1.2.10.2  yamt }
    316  1.2.10.2  yamt 
    317  1.2.10.2  yamt /*
    318  1.2.10.2  yamt  * Invalidate a block from the cache.
    319  1.2.10.2  yamt  */
    320  1.2.10.2  yamt static void
    321  1.2.10.2  yamt bcache_invalidate(daddr_t blkno)
    322  1.2.10.2  yamt {
    323  1.2.10.2  yamt     u_int	i;
    324  1.2.10.2  yamt 
    325  1.2.10.2  yamt     for (i = 0; i < bcache_nblks; i++) {
    326  1.2.10.2  yamt 	if (bcache_ctl[i].bc_blkno == blkno) {
    327  1.2.10.2  yamt 	    bcache_ctl[i].bc_count = -1;
    328  1.2.10.2  yamt 	    bcache_ctl[i].bc_blkno = -1;
    329  1.2.10.2  yamt 	    DEBUG("invalidate blk %d", blkno);
    330  1.2.10.2  yamt 	    break;
    331  1.2.10.2  yamt 	}
    332  1.2.10.2  yamt     }
    333  1.2.10.2  yamt }
    334  1.2.10.2  yamt 
    335  1.2.10.2  yamt int
    336  1.2.10.2  yamt command_bcache(int argc, char *argv[])
    337  1.2.10.2  yamt {
    338  1.2.10.2  yamt     u_int	i;
    339  1.2.10.2  yamt 
    340  1.2.10.2  yamt     for (i = 0; i < bcache_nblks; i++) {
    341  1.2.10.2  yamt 	printf("%lx %x %x|", (uintmax_t)bcache_ctl[i].bc_blkno, (unsigned int)bcache_ctl[i].bc_stamp & 0xffff, bcache_ctl[i].bc_count & 0xffff);
    342  1.2.10.2  yamt 	if (((i + 1) % 4) == 0)
    343  1.2.10.2  yamt 	    printf("\n");
    344  1.2.10.2  yamt     }
    345  1.2.10.2  yamt     printf("\n%d ops  %d bypasses  %d hits  %d misses  %d flushes\n", bcache_ops, bcache_bypasses, bcache_hits, bcache_misses, bcache_flushes);
    346  1.2.10.2  yamt     return(CMD_OK);
    347  1.2.10.2  yamt }
    348  1.2.10.2  yamt 
    349