#include <sys/param.h>
#include <sys/stdint.h>
#include <stand.h>
#include <string.h>
#include <strings.h>
#include "bootstrap.h"
#ifdef BCACHE_DEBUG
# define DPRINTF(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
#else
# define DPRINTF(fmt, args...) ((void)0)
#endif
struct bcachectl
{
daddr_t bc_blkno;
int bc_count;
};
struct bcache {
struct bcachectl *bcache_ctl;
caddr_t bcache_data;
size_t bcache_nblks;
size_t ra;
daddr_t bcache_nextblkno;
size_t ralen;
};
static u_int bcache_total_nblks;
static u_int bcache_blksize;
static u_int bcache_numdev;
static u_int bcache_units;
static u_int bcache_unit_nblks;
static u_int bcache_hits;
static u_int bcache_misses;
static u_int bcache_ops;
static u_int bcache_bypasses;
static u_int bcache_bcount;
static u_int bcache_rablks;
#define BHASH(bc, blkno) ((blkno) & ((bc)->bcache_nblks - 1))
#define BCACHE_LOOKUP(bc, blkno) \
((bc)->bcache_ctl[BHASH((bc), (blkno))].bc_blkno != (blkno))
#define BCACHE_READAHEAD 512
#define BCACHE_MINREADAHEAD 32
#define BCACHE_MAXIOWRA 512
static void bcache_invalidate(struct bcache *bc, daddr_t blkno);
static void bcache_insert(struct bcache *bc, daddr_t blkno);
static void bcache_free_instance(struct bcache *bc);
void
bcache_init(size_t nblks, size_t bsize)
{
bcache_total_nblks = nblks;
bcache_blksize = bsize;
}
void
bcache_add_dev(int devices)
{
bcache_numdev += devices;
}
void *
bcache_allocate(void)
{
u_int i;
struct bcache *bc = malloc(sizeof (struct bcache));
int disks = bcache_numdev;
if (disks == 0)
disks = 1;
if (bc == NULL) {
errno = ENOMEM;
return (bc);
}
i = fls(disks) - 1;
if (disks > (1 << i))
i++;
bc->bcache_nblks = bcache_total_nblks >> i;
bcache_unit_nblks = bc->bcache_nblks;
bc->bcache_data = malloc(bc->bcache_nblks * bcache_blksize);
if (bc->bcache_data == NULL) {
bc->bcache_nblks = 32;
bc->bcache_data = malloc(bc->bcache_nblks * bcache_blksize +
sizeof(uint32_t));
}
bc->bcache_ctl = malloc(bc->bcache_nblks * sizeof(struct bcachectl));
if ((bc->bcache_data == NULL) || (bc->bcache_ctl == NULL)) {
bcache_free_instance(bc);
errno = ENOMEM;
return (NULL);
}
for (i = 0; i < bc->bcache_nblks; i++) {
bc->bcache_ctl[i].bc_count = -1;
bc->bcache_ctl[i].bc_blkno = -1;
}
bcache_units++;
bc->ra = BCACHE_READAHEAD;
bc->bcache_nextblkno = -1;
return (bc);
}
void
bcache_free(void *cache)
{
struct bcache *bc = cache;
if (bc == NULL)
return;
bcache_free_instance(bc);
bcache_units--;
}
static int
write_strategy(void *devdata, int rw, daddr_t blk, size_t size,
char *buf, size_t *rsize)
{
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
struct bcache *bc = dd->dv_cache;
daddr_t i, nblk;
nblk = size / bcache_blksize;
for (i = 0; i < nblk; i++) {
bcache_invalidate(bc, blk + i);
}
return (dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize));
}
static int
read_strategy(void *devdata, int rw, daddr_t blk, size_t size,
char *buf, size_t *rsize)
{
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
struct bcache *bc = dd->dv_cache;
size_t i, nblk, p_size, r_size, complete, ra;
int result;
daddr_t p_blk;
caddr_t p_buf;
if (bc == NULL) {
errno = ENODEV;
return (-1);
}
if (rsize != NULL)
*rsize = 0;
nblk = size / bcache_blksize;
if (nblk == 0 && size != 0)
nblk++;
result = 0;
complete = 1;
for (i = 0; i < nblk; i++) {
if (BCACHE_LOOKUP(bc, (daddr_t)(blk + i))) {
bcache_misses += (nblk - i);
complete = 0;
break;
} else {
bcache_hits++;
}
}
if (complete || (i == bc->ralen && bc->ralen > 0)) {
if (bc->ra < BCACHE_READAHEAD)
bc->ra <<= 1;
} else {
if (nblk - i > BCACHE_MINREADAHEAD && bc->ralen > 0 &&
bc->ra > BCACHE_MINREADAHEAD)
bc->ra >>= 1;
}
if (blk == bc->bcache_nextblkno) {
if (nblk > bc->ralen)
bc->ralen = 0;
else
bc->ralen -= nblk;
}
if (complete) {
bcopy(bc->bcache_data + (bcache_blksize * BHASH(bc, blk)), buf, size);
goto done;
}
p_blk = blk + i;
p_buf = bc->bcache_data + (bcache_blksize * BHASH(bc, p_blk));
r_size = bc->bcache_nblks - BHASH(bc, p_blk);
p_size = MIN(r_size, nblk - i);
if ((rw & F_NORA) == F_NORA)
ra = 0;
else
ra = bc->bcache_nblks - BHASH(bc, p_blk + p_size);
if ((bc->bcache_nextblkno != blk) && ra != 0) {
ra = 0;
}
if (ra != 0 && ra != bc->bcache_nblks) {
ra = MIN(bc->ra, ra - 1);
ra = rounddown(ra, 16);
if (ra + p_size > BCACHE_MAXIOWRA)
ra = BCACHE_MAXIOWRA - p_size;
bc->ralen = ra;
p_size += ra;
} else {
bc->ralen = 0;
}
for (i = 0; i < p_size; i++) {
bcache_invalidate(bc, p_blk + i);
}
r_size = 0;
rw &= F_MASK;
result = dd->dv_strategy(dd->dv_devdata, rw, p_blk,
p_size * bcache_blksize, p_buf, &r_size);
r_size /= bcache_blksize;
for (i = 0; i < r_size; i++)
bcache_insert(bc, p_blk + i);
if (r_size != 0) {
if (r_size < p_size)
bcache_rablks += (p_size - r_size);
else
bcache_rablks += ra;
}
for (i = 0; i < nblk; i++) {
if (BCACHE_LOOKUP(bc, (daddr_t)(blk + i)))
break;
}
if (size > i * bcache_blksize)
size = i * bcache_blksize;
if (size != 0) {
bcopy(bc->bcache_data + (bcache_blksize * BHASH(bc, blk)), buf, size);
result = 0;
}
done:
if (result == 0) {
if (rsize != NULL)
*rsize = size;
bc->bcache_nextblkno = blk + (size / DEV_BSIZE);
}
return(result);
}
int
bcache_strategy(void *devdata, int rw, daddr_t blk, size_t size,
char *buf, size_t *rsize)
{
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
struct bcache *bc = dd->dv_cache;
u_int bcache_nblks = 0;
int nblk, cblk, ret;
size_t csize, isize, total;
bcache_ops++;
if (bc != NULL)
bcache_nblks = bc->bcache_nblks;
if (bc == NULL ||
((size * 2 / bcache_blksize) > bcache_nblks)) {
DPRINTF("bypass %zu from %jd", size / bcache_blksize, blk);
bcache_bypasses++;
rw &= F_MASK;
return (dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize));
}
switch (rw & F_MASK) {
case F_READ:
nblk = size / bcache_blksize;
if (size != 0 && nblk == 0)
nblk++;
ret = 0;
total = 0;
while(size) {
cblk = bcache_nblks - BHASH(bc, blk);
cblk = MIN(cblk, nblk);
if (size <= bcache_blksize)
csize = size;
else
csize = cblk * bcache_blksize;
ret = read_strategy(devdata, rw, blk, csize, buf+total, &isize);
if (ret != 0 || isize == 0) {
if (total != 0)
ret = 0;
break;
}
blk += isize / bcache_blksize;
total += isize;
size -= isize;
nblk = size / bcache_blksize;
}
if (rsize)
*rsize = total;
return (ret);
case F_WRITE:
return write_strategy(devdata, F_WRITE, blk, size, buf, rsize);
}
return -1;
}
static void
bcache_free_instance(struct bcache *bc)
{
if (bc != NULL) {
free(bc->bcache_ctl);
free(bc->bcache_data);
free(bc);
}
}
static void
bcache_insert(struct bcache *bc, daddr_t blkno)
{
u_int cand;
cand = BHASH(bc, blkno);
DPRINTF("insert blk %jd -> %u # %d", blkno, cand, bcache_bcount);
bc->bcache_ctl[cand].bc_blkno = blkno;
bc->bcache_ctl[cand].bc_count = bcache_bcount++;
}
static void
bcache_invalidate(struct bcache *bc, daddr_t blkno)
{
u_int i;
i = BHASH(bc, blkno);
if (bc->bcache_ctl[i].bc_blkno == blkno) {
bc->bcache_ctl[i].bc_count = -1;
bc->bcache_ctl[i].bc_blkno = -1;
DPRINTF("invalidate blk %ju", blkno);
}
}
#ifndef BOOT2
COMMAND_SET(bcachestat, "bcachestat", "get disk block cache stats", command_bcache);
static int
command_bcache(int argc, char *argv[] __unused)
{
if (argc != 1) {
command_errmsg = "wrong number of arguments";
return(CMD_ERROR);
}
printf("\ncache blocks: %u\n", bcache_total_nblks);
printf("cache blocksz: %u\n", bcache_blksize);
printf("cache readahead: %u\n", bcache_rablks);
printf("unit cache blocks: %u\n", bcache_unit_nblks);
printf("cached units: %u\n", bcache_units);
printf("%u ops %d bypasses %u hits %u misses\n", bcache_ops,
bcache_bypasses, bcache_hits, bcache_misses);
return(CMD_OK);
}
#endif