#include <sys/cdefs.h>
#include <sys/stdint.h>
#include <lib/libsa/stand.h>
#include "bitstring.h"
#include "bootstrap.h"
#ifdef BCACHE_DEBUG
#define BCACHE_TIMEOUT 10
# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
#else
#define BCACHE_TIMEOUT 2
# define DEBUG(fmt, args...)
#endif
struct bcachectl
{
daddr_t bc_blkno;
time_t bc_stamp;
int bc_count;
};
static struct bcachectl *bcache_ctl;
static void * bcache_data;
static bitstr_t *bcache_miss;
static u_int bcache_nblks;
static u_int bcache_blksize;
static u_int bcache_hits, bcache_misses, bcache_ops, bcache_bypasses;
static u_int bcache_flushes;
static u_int bcache_bcount;
static void bcache_invalidate(daddr_t blkno);
static void bcache_insert(void *buf, daddr_t blkno);
static int bcache_lookup(void *buf, daddr_t blkno);
int
bcache_init(u_int nblks, size_t bsize)
{
if (bcache_data != NULL) {
free(bcache_data);
bcache_data = NULL;
free(bcache_ctl);
}
bcache_nblks = nblks;
bcache_blksize = bsize;
bcache_data = alloc(bcache_nblks * bcache_blksize);
bcache_ctl = (struct bcachectl *)alloc(bcache_nblks * sizeof(struct bcachectl));
bcache_miss = bit_alloc((bcache_nblks + 1) / 2);
if ((bcache_data == NULL) || (bcache_ctl == NULL) || (bcache_miss == NULL)) {
if (bcache_miss)
free(bcache_miss);
if (bcache_ctl)
free(bcache_ctl);
if (bcache_data)
free(bcache_data);
bcache_data = NULL;
return(ENOMEM);
}
return(0);
}
void
bcache_flush(void)
{
u_int i;
bcache_flushes++;
for (i = 0; i < bcache_nblks; i++) {
bcache_ctl[i].bc_count = -1;
bcache_ctl[i].bc_blkno = -1;
}
}
static int
write_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
char *buf, size_t *rsize)
{
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
daddr_t i, nblk;
int err;
nblk = size / bcache_blksize;
for (i = 0; i < nblk; i++) {
bcache_invalidate(blk + i);
}
err = dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize);
if (err == 0) {
for (i = 0; i < nblk; i++) {
bcache_insert(buf + (i * bcache_blksize),blk + i);
}
}
return err;
}
static int
read_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
char *buf, size_t *rsize)
{
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
int p_size, result;
daddr_t p_blk, i, j, nblk;
void * p_buf;
nblk = size / bcache_blksize;
result = 0;
for (i = 0; i < nblk; i++) {
if (bcache_lookup(buf + (bcache_blksize * i), blk + i)) {
bit_set(bcache_miss, i);
bcache_misses++;
} else {
bit_clear(bcache_miss, i);
bcache_hits++;
}
}
p_blk = -1;
p_buf = NULL;
p_size = 0;
for (i = 0; i < nblk; i++) {
if (bit_test(bcache_miss, i)) {
if (p_blk == -1) {
p_blk = blk + i;
p_buf = buf + (bcache_blksize * i);
p_size = 1;
} else {
p_size++;
}
} else if (p_blk != -1) {
result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
if (result != 0)
goto done;
for (j = 0; j < p_size; j++)
bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
p_blk = -1;
}
}
if (p_blk != -1) {
result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
if (result != 0)
goto done;
for (j = 0; j < p_size; j++)
bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
}
done:
if ((result == 0) && (rsize != NULL))
*rsize = size;
return(result);
}
int
bcache_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
char *buf, size_t *rsize)
{
static int bcache_unit = -1;
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
bcache_ops++;
if(bcache_unit != unit) {
bcache_flush();
bcache_unit = unit;
}
if ((bcache_data == NULL) || ((size * 2 / bcache_blksize) > bcache_nblks)) {
DEBUG("bypass %d from %d", size / bcache_blksize, blk);
bcache_bypasses++;
return(dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize));
}
switch (rw) {
case F_READ:
return read_strategy(devdata, unit, rw, blk, size, buf, rsize);
case F_WRITE:
return write_strategy(devdata, unit, rw, blk, size, buf, rsize);
}
return -1;
}
static void
bcache_insert(void *buf, daddr_t blkno)
{
time_t now;
int cand, ocount;
u_int i;
time(&now);
cand = 0;
ocount = bcache_ctl[0].bc_count;
for (i = 1; i < bcache_nblks; i++) {
if (bcache_ctl[i].bc_blkno == blkno) {
cand = i;
break;
}
if (bcache_ctl[i].bc_count < ocount) {
ocount = bcache_ctl[i].bc_count;
cand = i;
}
}
DEBUG("insert blk %d -> %d @ %d # %d", blkno, cand, now, bcache_bcount);
memcpy(bcache_data + (bcache_blksize * cand), buf, bcache_blksize);
bcache_ctl[cand].bc_blkno = blkno;
bcache_ctl[cand].bc_stamp = now;
bcache_ctl[cand].bc_count = bcache_bcount++;
}
static int
bcache_lookup(void *buf, daddr_t blkno)
{
time_t now;
u_int i;
time(&now);
for (i = 0; i < bcache_nblks; i++)
if ((bcache_ctl[i].bc_blkno == blkno) && ((bcache_ctl[i].bc_stamp + BCACHE_TIMEOUT) >= now)) {
memcpy(buf, bcache_data + (bcache_blksize * i), bcache_blksize);
DEBUG("hit blk %d <- %d (now %d then %d)", blkno, i, now, bcache_ctl[i].bc_stamp);
return(0);
}
return(ENOENT);
}
static void
bcache_invalidate(daddr_t blkno)
{
u_int i;
for (i = 0; i < bcache_nblks; i++) {
if (bcache_ctl[i].bc_blkno == blkno) {
bcache_ctl[i].bc_count = -1;
bcache_ctl[i].bc_blkno = -1;
DEBUG("invalidate blk %d", blkno);
break;
}
}
}
int
command_bcache(int argc, char *argv[])
{
u_int i;
for (i = 0; i < bcache_nblks; i++) {
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);
if (((i + 1) % 4) == 0)
printf("\n");
}
printf("\n%d ops %d bypasses %d hits %d misses %d flushes\n", bcache_ops, bcache_bypasses, bcache_hits, bcache_misses, bcache_flushes);
return(CMD_OK);
}