#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: rcache.c,v 1.25 2015/08/24 17:34:03 bouyer Exp $");
#endif
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "dump.h"
#define MAXCACHEBUFS 512
#define MAXMEMPART 6
union cdesc {
volatile size_t cd_count;
struct {
volatile daddr_t blkstart;
volatile daddr_t blkend;
volatile daddr_t blocksRead;
volatile size_t time;
#ifdef DIAGNOSTICS
volatile pid_t owner;
#endif
} desc;
#define cd_blkstart desc.blkstart
#define cd_blkend desc.blkend
#define cd_blocksRead desc.blocksRead
#define cd_time desc.time
#define cd_owner desc.owner
};
static int findlru(void);
static void *shareBuffer = NULL;
static union cdesc *cheader;
static union cdesc *cdesc;
static char *cdata;
static int cachebufs;
static int nblksread;
#ifdef STATS
static int nreads;
static int nphysread;
static int64_t readsize;
static int64_t physreadsize;
#endif
#define CSIZE (nblksread << dev_bshift)
#define CDATA(desc) (cdata + ((desc) - cdesc) * CSIZE)
void
initcache(int cachesize, int readblksize)
{
size_t len;
size_t sharedSize;
if (readblksize == -1) {
int kern_maxphys;
int mib[2] = { CTL_KERN, KERN_MAXPHYS };
len = sizeof(kern_maxphys);
if (sysctl(mib, 2, &kern_maxphys, &len, NULL, 0) < 0) {
msg("sysctl(kern.maxphys) failed: %s\n",
strerror(errno));
return;
}
readblksize = kern_maxphys;
}
nblksread = howmany(readblksize, ufsib->ufs_bsize);
nblksread <<= ufsib->ufs_bshift - dev_bshift;
if (cachesize == -1) {
uint64_t usermem, cachetmp;
int mib[2] = { CTL_HW, HW_USERMEM64 };
len = sizeof(usermem);
if (sysctl(mib, 2, &usermem, &len, NULL, 0) < 0) {
msg("sysctl(hw.usermem) failed: %s\n",
strerror(errno));
return;
}
cachetmp = (usermem / MAXMEMPART) / CSIZE;
cachebufs = (cachetmp > INT_MAX) ? INT_MAX : cachetmp;
} else {
cachebufs = cachesize;
}
if (cachebufs) {
if (cachebufs > MAXCACHEBUFS)
cachebufs = MAXCACHEBUFS;
sharedSize = sizeof(union cdesc) +
sizeof(union cdesc) * cachebufs +
cachebufs * CSIZE;
#ifdef STATS
fprintf(stderr, "Using %d buffers (%d bytes)\n", cachebufs,
sharedSize);
#endif
shareBuffer = mmap(NULL, sharedSize, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED, -1, 0);
if (shareBuffer == MAP_FAILED) {
msg("can't mmap shared memory for buffer: %s\n",
strerror(errno));
return;
}
cheader = shareBuffer;
cdesc = (union cdesc *) (((char *) shareBuffer) +
sizeof(union cdesc));
cdata = ((char *) shareBuffer) + sizeof(union cdesc) +
sizeof(union cdesc) * cachebufs;
memset(shareBuffer, '\0', sharedSize);
}
}
static int
findlru(void)
{
int i;
size_t minTime = cdesc[0].cd_time;
int minIdx = 0;
for (i = 0; i < cachebufs; i++) {
if (cdesc[i].cd_time < minTime) {
minIdx = i;
minTime = cdesc[i].cd_time;
}
}
return minIdx;
}
static int breaderrors = 0;
#define BREADEMAX 32
void
rawread(daddr_t blkno, char *buf, int size)
{
int cnt, i;
#ifdef STATS
nphysread++;
physreadsize += size;
#endif
loop:
if (lseek(diskfd, ((off_t) blkno << dev_bshift), SEEK_SET) == -1) {
msg("rawread: lseek fails\n");
goto err;
}
if ((cnt = read(diskfd, buf, size)) == size)
return;
if (blkno + (size >> dev_bshift) > ufsib->ufs_dsize) {
size -= dev_bsize;
goto loop;
}
if (cnt == -1)
msg("read error from %s: %s: [block %lld]: count=%d\n",
disk, strerror(errno), (long long)blkno, size);
else
msg("short read error from %s: [block %lld]: "
"count=%d, got=%d\n",
disk, (long long)blkno, size, cnt);
err:
if (++breaderrors > BREADEMAX) {
msg("More than %d block read errors from %s\n",
BREADEMAX, disk);
broadcast("DUMP IS AILING!\n");
msg("This is an unrecoverable error.\n");
if (!query("Do you want to attempt to continue?")) {
dumpabort(0);
} else
breaderrors = 0;
}
memset(buf, 0, size);
for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
if (lseek(diskfd, ((off_t)blkno << dev_bshift),
SEEK_SET) == -1) {
msg("rawread: lseek2 fails: %s!\n",
strerror(errno));
continue;
}
if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
continue;
if (cnt == -1) {
msg("read error from %s: %s: [sector %lld]: "
"count=%ld\n", disk, strerror(errno),
(long long)blkno, dev_bsize);
continue;
}
msg("short read error from %s: [sector %lld]: "
"count=%ld, got=%d\n",
disk, (long long)blkno, dev_bsize, cnt);
}
}
void
bread(daddr_t blkno, char *buf, int size)
{
int osize = size, idx;
daddr_t oblkno = blkno;
char *obuf = buf;
daddr_t numBlocks = howmany(size, dev_bsize);
#ifdef STATS
nreads++;
readsize += size;
#endif
if (!shareBuffer) {
rawread(blkno, buf, size);
return;
}
if (flock(diskfd, LOCK_EX)) {
msg("flock(LOCK_EX) failed: %s\n",
strerror(errno));
rawread(blkno, buf, size);
return;
}
retry:
idx = 0;
while (size > 0) {
int i;
for (i = 0; i < cachebufs; i++) {
union cdesc *curr = &cdesc[(i + idx) % cachebufs];
#ifdef DIAGNOSTICS
if (curr->cd_owner) {
fprintf(stderr, "Owner is set (%d, me=%d), can"
"not happen.\n", curr->cd_owner, getpid());
}
#endif
if (curr->cd_blkend == 0)
continue;
if (curr->cd_blkstart <= blkno &&
blkno < curr->cd_blkend) {
int toCopy = MIN(size,
(curr->cd_blkend - blkno) << dev_bshift);
#ifdef DIAGNOSTICS
if (toCopy <= 0 || toCopy > CSIZE) {
fprintf(stderr, "toCopy %d !\n",
toCopy);
dumpabort(0);
}
if (CDATA(curr) +
((blkno - curr->cd_blkstart) <<
dev_bshift) < CDATA(curr) ||
CDATA(curr) +
((blkno - curr->cd_blkstart) <<
dev_bshift) > CDATA(curr) + CSIZE) {
fprintf(stderr, "%p < %p !!!\n",
CDATA(curr) + ((blkno -
curr->cd_blkstart) << dev_bshift),
CDATA(curr));
fprintf(stderr,
"cdesc[i].cd_blkstart %lld "
"blkno %lld dev_bsize %ld\n",
(long long)curr->cd_blkstart,
(long long)blkno,
dev_bsize);
dumpabort(0);
}
#endif
memcpy(buf, CDATA(curr) +
((blkno - curr->cd_blkstart) <<
dev_bshift),
toCopy);
buf += toCopy;
size -= toCopy;
blkno += howmany(toCopy, dev_bsize);
numBlocks -= howmany(toCopy, dev_bsize);
curr->cd_time = cheader->cd_count++;
curr->cd_blocksRead +=
howmany(toCopy, dev_bsize);
if (curr->cd_blocksRead >= nblksread)
curr->cd_time = 0;
goto retry;
}
}
if (size == 0)
break;
if (numBlocks > nblksread || blkno >= ufsib->ufs_dsize) {
rawread(oblkno, obuf, osize);
break;
} else {
ssize_t rsize;
daddr_t blockBlkNo;
blockBlkNo = (blkno / nblksread) * nblksread;
idx = findlru();
rsize = MIN(nblksread,
ufsib->ufs_dsize - blockBlkNo) << dev_bshift;
#ifdef DIAGNOSTICS
if (cdesc[idx].cd_owner)
fprintf(stderr, "Owner is set (%d, me=%d), can"
"not happen(2).\n", cdesc[idx].cd_owner,
getpid());
cdesc[idx].cd_owner = getpid();
#endif
cdesc[idx].cd_time = cheader->cd_count++;
cdesc[idx].cd_blkstart = blockBlkNo;
cdesc[idx].cd_blkend = 0;
cdesc[idx].cd_blocksRead = 0;
if (lseek(diskfd, ((off_t) blockBlkNo << dev_bshift),
SEEK_SET) == -1) {
msg("readBlocks: lseek fails: %s\n",
strerror(errno));
rsize = -1;
} else {
rsize = read(diskfd,
CDATA(&cdesc[idx]), rsize);
if (rsize < 0) {
msg("readBlocks: read fails: %s\n",
strerror(errno));
}
}
if (rsize <= 0) {
rawread(oblkno, obuf, osize);
#ifdef DIAGNOSTICS
if (cdesc[idx].cd_owner != getpid())
fprintf(stderr, "Owner changed from "
"%d to %d, can't happen\n",
getpid(), cdesc[idx].cd_owner);
cdesc[idx].cd_owner = 0;
#endif
break;
}
cdesc[idx].cd_blkend = blockBlkNo + rsize / dev_bsize;
#ifdef STATS
nphysread++;
physreadsize += rsize;
#endif
#ifdef DIAGNOSTICS
if (cdesc[idx].cd_owner != getpid())
fprintf(stderr, "Owner changed from "
"%d to %d, can't happen\n",
getpid(), cdesc[idx].cd_owner);
cdesc[idx].cd_owner = 0;
#endif
}
}
if (flock(diskfd, LOCK_UN))
msg("flock(LOCK_UN) failed: %s\n",
strerror(errno));
}
void
printcachestats(void)
{
#ifdef STATS
fprintf(stderr, "Pid %d: %d reads (%u bytes) "
"%d physical reads (%u bytes) %d%% hits, %d%% overhead\n",
getpid(), nreads, (u_int) readsize, nphysread,
(u_int) physreadsize, (nreads - nphysread) * 100 / nreads,
(int) (((physreadsize - readsize) * 100) / readsize));
#endif
}