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