2015-08-24 20:34:03 +03:00
|
|
|
/* $NetBSD: rcache.c,v 1.25 2015/08/24 17:34:03 bouyer Exp $ */
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2001-12-23 15:29:55 +03:00
|
|
|
* by Martin J. Laubach <mjl@emsi.priv.at> and
|
1999-03-23 17:22:59 +03:00
|
|
|
* Manuel Bouyer <Manuel.Bouyer@lip6.fr>.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2001-12-23 15:29:55 +03:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
1999-03-23 17:22:59 +03:00
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2002-02-20 02:11:28 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2015-08-24 20:34:03 +03:00
|
|
|
__RCSID("$NetBSD: rcache.c,v 1.25 2015/08/24 17:34:03 bouyer Exp $");
|
2002-02-20 02:11:28 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
#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 /* max 512 buffers */
|
|
|
|
#define MAXMEMPART 6 /* max 15% of the user mem */
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
2003-02-04 02:08:37 +03:00
|
|
|
union cdesc {
|
|
|
|
volatile size_t cd_count;
|
|
|
|
struct {
|
|
|
|
volatile daddr_t blkstart;
|
2003-02-04 11:06:42 +03:00
|
|
|
volatile daddr_t blkend; /* start + nblksread */
|
2003-02-04 02:08:37 +03:00
|
|
|
volatile daddr_t blocksRead;
|
|
|
|
volatile size_t time;
|
1999-03-23 17:22:59 +03:00
|
|
|
#ifdef DIAGNOSTICS
|
2003-02-04 02:08:37 +03:00
|
|
|
volatile pid_t owner;
|
1999-03-23 17:22:59 +03:00
|
|
|
#endif
|
2003-02-04 02:08:37 +03:00
|
|
|
} 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
|
1999-03-23 17:22:59 +03:00
|
|
|
};
|
|
|
|
|
2001-05-27 18:17:56 +04:00
|
|
|
static int findlru(void);
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
static void *shareBuffer = NULL;
|
2003-02-04 02:08:37 +03:00
|
|
|
static union cdesc *cheader;
|
|
|
|
static union cdesc *cdesc;
|
1999-03-23 17:22:59 +03:00
|
|
|
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
|
|
|
|
|
2003-02-04 11:24:20 +03:00
|
|
|
#define CSIZE (nblksread << dev_bshift) /* cache buf size */
|
|
|
|
#define CDATA(desc) (cdata + ((desc) - cdesc) * CSIZE)
|
1999-03-23 17:22:59 +03:00
|
|
|
|
2001-12-23 15:29:55 +03:00
|
|
|
void
|
2001-05-27 18:17:56 +04:00
|
|
|
initcache(int cachesize, int readblksize)
|
1999-03-23 17:22:59 +03:00
|
|
|
{
|
|
|
|
size_t len;
|
2003-02-04 11:06:42 +03:00
|
|
|
size_t sharedSize;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
2015-08-24 20:34:03 +03:00
|
|
|
if (readblksize == -1) { /* use kern.maxphys */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-02-04 11:11:50 +03:00
|
|
|
/* Convert read block size in terms of filesystem block size */
|
2003-02-04 11:06:42 +03:00
|
|
|
nblksread = howmany(readblksize, ufsib->ufs_bsize);
|
2003-02-04 11:11:50 +03:00
|
|
|
|
|
|
|
/* Then, convert it in terms of device block size */
|
|
|
|
nblksread <<= ufsib->ufs_bshift - dev_bshift;
|
|
|
|
|
2003-02-04 11:06:42 +03:00
|
|
|
if (cachesize == -1) { /* Compute from memory available */
|
2010-01-27 15:20:25 +03:00
|
|
|
uint64_t usermem, cachetmp;
|
2003-09-13 14:59:50 +04:00
|
|
|
int mib[2] = { CTL_HW, HW_USERMEM64 };
|
2001-12-23 15:29:55 +03:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
len = sizeof(usermem);
|
|
|
|
if (sysctl(mib, 2, &usermem, &len, NULL, 0) < 0) {
|
2003-02-04 11:06:42 +03:00
|
|
|
msg("sysctl(hw.usermem) failed: %s\n",
|
|
|
|
strerror(errno));
|
1999-03-23 17:22:59 +03:00
|
|
|
return;
|
|
|
|
}
|
2010-01-27 15:20:25 +03:00
|
|
|
cachetmp = (usermem / MAXMEMPART) / CSIZE;
|
|
|
|
/* for those with TB of RAM */
|
|
|
|
cachebufs = (cachetmp > INT_MAX) ? INT_MAX : cachetmp;
|
1999-03-23 17:22:59 +03:00
|
|
|
} else { /* User specified */
|
|
|
|
cachebufs = cachesize;
|
|
|
|
}
|
2001-12-23 15:29:55 +03:00
|
|
|
|
2003-02-04 11:06:42 +03:00
|
|
|
if (cachebufs) { /* Don't allocate if zero --> no caching */
|
1999-03-23 17:22:59 +03:00
|
|
|
if (cachebufs > MAXCACHEBUFS)
|
|
|
|
cachebufs = MAXCACHEBUFS;
|
|
|
|
|
2003-02-04 02:08:37 +03:00
|
|
|
sharedSize = sizeof(union cdesc) +
|
|
|
|
sizeof(union cdesc) * cachebufs +
|
2003-02-04 11:24:20 +03:00
|
|
|
cachebufs * CSIZE;
|
2001-12-23 15:29:55 +03:00
|
|
|
#ifdef STATS
|
1999-03-23 17:22:59 +03:00
|
|
|
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);
|
2003-02-04 11:06:42 +03:00
|
|
|
if (shareBuffer == MAP_FAILED) {
|
1999-03-23 17:22:59 +03:00
|
|
|
msg("can't mmap shared memory for buffer: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cheader = shareBuffer;
|
2003-02-04 02:08:37 +03:00
|
|
|
cdesc = (union cdesc *) (((char *) shareBuffer) +
|
|
|
|
sizeof(union cdesc));
|
|
|
|
cdata = ((char *) shareBuffer) + sizeof(union cdesc) +
|
|
|
|
sizeof(union cdesc) * cachebufs;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
memset(shareBuffer, '\0', sharedSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-27 18:17:56 +04:00
|
|
|
/*
|
|
|
|
* Find the cache buffer descriptor that shows the minimal access time
|
|
|
|
*/
|
2001-12-23 15:29:55 +03:00
|
|
|
static int
|
2001-05-27 18:17:56 +04:00
|
|
|
findlru(void)
|
1999-03-23 17:22:59 +03:00
|
|
|
{
|
2001-12-22 11:05:24 +03:00
|
|
|
int i;
|
2003-02-04 02:08:37 +03:00
|
|
|
size_t minTime = cdesc[0].cd_time;
|
2001-12-22 11:05:24 +03:00
|
|
|
int minIdx = 0;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
for (i = 0; i < cachebufs; i++) {
|
2003-02-04 02:08:37 +03:00
|
|
|
if (cdesc[i].cd_time < minTime) {
|
1999-03-23 17:22:59 +03:00
|
|
|
minIdx = i;
|
2003-02-04 02:08:37 +03:00
|
|
|
minTime = cdesc[i].cd_time;
|
1999-03-23 17:22:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return minIdx;
|
|
|
|
}
|
2001-05-27 18:17:56 +04:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
/*
|
|
|
|
* Read data directly from disk, with smart error handling.
|
|
|
|
* Try to recover from hard errors by reading in sector sized pieces.
|
|
|
|
* Error recovery is attempted at most BREADEMAX times before seeking
|
|
|
|
* consent from the operator to continue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int breaderrors = 0;
|
|
|
|
#define BREADEMAX 32
|
|
|
|
|
2001-12-23 15:29:55 +03:00
|
|
|
void
|
2001-05-27 18:17:56 +04:00
|
|
|
rawread(daddr_t blkno, char *buf, int size)
|
1999-03-23 17:22:59 +03:00
|
|
|
{
|
|
|
|
int cnt, i;
|
2003-02-04 11:06:42 +03:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
#ifdef STATS
|
|
|
|
nphysread++;
|
|
|
|
physreadsize += size;
|
|
|
|
#endif
|
|
|
|
|
2003-02-04 11:06:42 +03:00
|
|
|
loop:
|
2003-02-07 02:00:08 +03:00
|
|
|
if (lseek(diskfd, ((off_t) blkno << dev_bshift), SEEK_SET) == -1) {
|
1999-03-23 17:22:59 +03:00
|
|
|
msg("rawread: lseek fails\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2003-02-04 11:06:42 +03:00
|
|
|
if ((cnt = read(diskfd, buf, size)) == size)
|
1999-03-23 17:22:59 +03:00
|
|
|
return;
|
2003-02-04 11:24:20 +03:00
|
|
|
if (blkno + (size >> dev_bshift) > ufsib->ufs_dsize) {
|
2001-12-22 10:45:38 +03:00
|
|
|
/*
|
|
|
|
* Trying to read the final fragment.
|
|
|
|
*
|
|
|
|
* NB - dump only works in TP_BSIZE blocks, hence
|
|
|
|
* rounds `dev_bsize' fragments up to TP_BSIZE pieces.
|
|
|
|
* It should be smarter about not actually trying to
|
|
|
|
* read more than it can get, but for the time being
|
|
|
|
* we punt and scale back the read only when it gets
|
|
|
|
* us into trouble. (mkm 9/25/83)
|
|
|
|
*/
|
|
|
|
size -= dev_bsize;
|
|
|
|
goto loop;
|
|
|
|
}
|
1999-03-23 17:22:59 +03:00
|
|
|
if (cnt == -1)
|
2003-01-25 00:55:02 +03:00
|
|
|
msg("read error from %s: %s: [block %lld]: count=%d\n",
|
2003-02-04 11:06:42 +03:00
|
|
|
disk, strerror(errno), (long long)blkno, size);
|
1999-03-23 17:22:59 +03:00
|
|
|
else
|
2003-02-04 11:06:42 +03:00
|
|
|
msg("short read error from %s: [block %lld]: "
|
|
|
|
"count=%d, got=%d\n",
|
|
|
|
disk, (long long)blkno, size, cnt);
|
1999-03-23 17:22:59 +03:00
|
|
|
err:
|
|
|
|
if (++breaderrors > BREADEMAX) {
|
2000-10-11 08:57:42 +04:00
|
|
|
msg("More than %d block read errors from %s\n",
|
2003-02-04 11:06:42 +03:00
|
|
|
BREADEMAX, disk);
|
1999-03-23 17:22:59 +03:00
|
|
|
broadcast("DUMP IS AILING!\n");
|
|
|
|
msg("This is an unrecoverable error.\n");
|
2003-02-04 11:06:42 +03:00
|
|
|
if (!query("Do you want to attempt to continue?")) {
|
1999-03-23 17:22:59 +03:00
|
|
|
dumpabort(0);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
} else
|
|
|
|
breaderrors = 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Zero buffer, then try to read each sector of buffer separately.
|
|
|
|
*/
|
|
|
|
memset(buf, 0, size);
|
|
|
|
for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
|
2003-02-04 11:06:42 +03:00
|
|
|
if (lseek(diskfd, ((off_t)blkno << dev_bshift),
|
2003-02-07 02:00:08 +03:00
|
|
|
SEEK_SET) == -1) {
|
1999-03-23 17:22:59 +03:00
|
|
|
msg("rawread: lseek2 fails: %s!\n",
|
|
|
|
strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
|
|
|
|
continue;
|
|
|
|
if (cnt == -1) {
|
2003-02-04 11:06:42 +03:00
|
|
|
msg("read error from %s: %s: [sector %lld]: "
|
2003-02-07 02:25:02 +03:00
|
|
|
"count=%ld\n", disk, strerror(errno),
|
|
|
|
(long long)blkno, dev_bsize);
|
1999-03-23 17:22:59 +03:00
|
|
|
continue;
|
|
|
|
}
|
2003-02-04 11:06:42 +03:00
|
|
|
msg("short read error from %s: [sector %lld]: "
|
|
|
|
"count=%ld, got=%d\n",
|
2003-01-25 00:55:02 +03:00
|
|
|
disk, (long long)blkno, dev_bsize, cnt);
|
1999-03-23 17:22:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-23 15:29:55 +03:00
|
|
|
void
|
2001-05-27 18:17:56 +04:00
|
|
|
bread(daddr_t blkno, char *buf, int size)
|
1999-03-23 17:22:59 +03:00
|
|
|
{
|
2003-02-04 11:24:20 +03:00
|
|
|
int osize = size, idx;
|
1999-03-23 17:22:59 +03:00
|
|
|
daddr_t oblkno = blkno;
|
|
|
|
char *obuf = buf;
|
2003-02-04 11:06:42 +03:00
|
|
|
daddr_t numBlocks = howmany(size, dev_bsize);
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
#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:
|
2003-02-04 11:24:20 +03:00
|
|
|
idx = 0;
|
|
|
|
while (size > 0) {
|
2001-12-22 11:05:24 +03:00
|
|
|
int i;
|
2001-12-23 15:29:55 +03:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
for (i = 0; i < cachebufs; i++) {
|
2003-02-04 11:24:20 +03:00
|
|
|
union cdesc *curr = &cdesc[(i + idx) % cachebufs];
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
#ifdef DIAGNOSTICS
|
2003-02-04 02:08:37 +03:00
|
|
|
if (curr->cd_owner) {
|
1999-03-23 17:22:59 +03:00
|
|
|
fprintf(stderr, "Owner is set (%d, me=%d), can"
|
2003-02-04 02:08:37 +03:00
|
|
|
"not happen.\n", curr->cd_owner, getpid());
|
1999-03-23 17:22:59 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-02-04 02:08:37 +03:00
|
|
|
if (curr->cd_blkend == 0)
|
1999-03-23 17:22:59 +03:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* If we find a bit of the read in the buffers,
|
|
|
|
* now compute how many blocks we can copy,
|
|
|
|
* copy them out, adjust blkno, buf and size,
|
|
|
|
* and restart
|
|
|
|
*/
|
2003-02-04 02:08:37 +03:00
|
|
|
if (curr->cd_blkstart <= blkno &&
|
|
|
|
blkno < curr->cd_blkend) {
|
1999-03-23 17:22:59 +03:00
|
|
|
/* Number of data blocks to be copied */
|
2001-05-27 18:17:56 +04:00
|
|
|
int toCopy = MIN(size,
|
2003-02-04 11:24:20 +03:00
|
|
|
(curr->cd_blkend - blkno) << dev_bshift);
|
1999-03-23 17:22:59 +03:00
|
|
|
#ifdef DIAGNOSTICS
|
2003-02-04 11:24:20 +03:00
|
|
|
if (toCopy <= 0 || toCopy > CSIZE) {
|
1999-03-23 17:22:59 +03:00
|
|
|
fprintf(stderr, "toCopy %d !\n",
|
|
|
|
toCopy);
|
|
|
|
dumpabort(0);
|
|
|
|
}
|
2003-02-04 11:24:20 +03:00
|
|
|
if (CDATA(curr) +
|
|
|
|
((blkno - curr->cd_blkstart) <<
|
2003-02-04 11:43:16 +03:00
|
|
|
dev_bshift) < CDATA(curr) ||
|
2003-02-04 11:24:20 +03:00
|
|
|
CDATA(curr) +
|
|
|
|
((blkno - curr->cd_blkstart) <<
|
2003-02-04 11:43:16 +03:00
|
|
|
dev_bshift) > CDATA(curr) + CSIZE) {
|
1999-03-23 17:22:59 +03:00
|
|
|
fprintf(stderr, "%p < %p !!!\n",
|
2003-02-04 11:24:20 +03:00
|
|
|
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);
|
1999-03-23 17:22:59 +03:00
|
|
|
dumpabort(0);
|
|
|
|
}
|
|
|
|
#endif
|
2003-02-04 11:24:20 +03:00
|
|
|
memcpy(buf, CDATA(curr) +
|
2003-02-04 11:43:16 +03:00
|
|
|
((blkno - curr->cd_blkstart) <<
|
|
|
|
dev_bshift),
|
1999-03-23 17:22:59 +03:00
|
|
|
toCopy);
|
|
|
|
|
|
|
|
buf += toCopy;
|
|
|
|
size -= toCopy;
|
2003-02-04 11:06:42 +03:00
|
|
|
blkno += howmany(toCopy, dev_bsize);
|
|
|
|
numBlocks -= howmany(toCopy, dev_bsize);
|
1999-03-23 17:22:59 +03:00
|
|
|
|
2003-02-04 02:08:37 +03:00
|
|
|
curr->cd_time = cheader->cd_count++;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If all data of a cache block have been
|
|
|
|
* read, chances are good no more reads
|
|
|
|
* will occur, so expire the cache immediately
|
|
|
|
*/
|
|
|
|
|
2003-02-04 02:08:37 +03:00
|
|
|
curr->cd_blocksRead +=
|
2003-02-04 11:06:42 +03:00
|
|
|
howmany(toCopy, dev_bsize);
|
2003-02-04 02:08:37 +03:00
|
|
|
if (curr->cd_blocksRead >= nblksread)
|
|
|
|
curr->cd_time = 0;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No more to do? */
|
|
|
|
if (size == 0)
|
|
|
|
break;
|
2001-12-22 11:45:36 +03:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
/*
|
|
|
|
* This does actually not happen if fs blocks are not greater
|
|
|
|
* than nblksread.
|
|
|
|
*/
|
2001-12-22 11:45:36 +03:00
|
|
|
if (numBlocks > nblksread || blkno >= ufsib->ufs_dsize) {
|
1999-03-23 17:22:59 +03:00
|
|
|
rawread(oblkno, obuf, osize);
|
|
|
|
break;
|
|
|
|
} else {
|
2001-12-22 11:05:24 +03:00
|
|
|
ssize_t rsize;
|
|
|
|
daddr_t blockBlkNo;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
blockBlkNo = (blkno / nblksread) * nblksread;
|
|
|
|
idx = findlru();
|
2001-05-27 18:17:56 +04:00
|
|
|
rsize = MIN(nblksread,
|
2003-02-04 11:24:20 +03:00
|
|
|
ufsib->ufs_dsize - blockBlkNo) << dev_bshift;
|
1999-10-01 00:39:58 +04:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
#ifdef DIAGNOSTICS
|
2003-02-04 02:08:37 +03:00
|
|
|
if (cdesc[idx].cd_owner)
|
1999-03-23 17:22:59 +03:00
|
|
|
fprintf(stderr, "Owner is set (%d, me=%d), can"
|
2003-02-04 02:08:37 +03:00
|
|
|
"not happen(2).\n", cdesc[idx].cd_owner,
|
1999-03-23 17:22:59 +03:00
|
|
|
getpid());
|
2003-02-04 02:08:37 +03:00
|
|
|
cdesc[idx].cd_owner = getpid();
|
1999-03-23 17:22:59 +03:00
|
|
|
#endif
|
2003-02-04 02:08:37 +03:00
|
|
|
cdesc[idx].cd_time = cheader->cd_count++;
|
|
|
|
cdesc[idx].cd_blkstart = blockBlkNo;
|
2003-02-04 11:24:20 +03:00
|
|
|
cdesc[idx].cd_blkend = 0;
|
2003-02-04 02:08:37 +03:00
|
|
|
cdesc[idx].cd_blocksRead = 0;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
2003-02-04 11:06:42 +03:00
|
|
|
if (lseek(diskfd, ((off_t) blockBlkNo << dev_bshift),
|
2003-02-07 02:00:08 +03:00
|
|
|
SEEK_SET) == -1) {
|
1999-03-23 17:22:59 +03:00
|
|
|
msg("readBlocks: lseek fails: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
rsize = -1;
|
|
|
|
} else {
|
2003-02-04 11:24:20 +03:00
|
|
|
rsize = read(diskfd,
|
|
|
|
CDATA(&cdesc[idx]), rsize);
|
1999-03-23 17:22:59 +03:00
|
|
|
if (rsize < 0) {
|
|
|
|
msg("readBlocks: read fails: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On errors, panic, punt, try to read without
|
|
|
|
* cache and let raw read routine do the rest.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rsize <= 0) {
|
|
|
|
rawread(oblkno, obuf, osize);
|
|
|
|
#ifdef DIAGNOSTICS
|
2003-02-04 02:08:37 +03:00
|
|
|
if (cdesc[idx].cd_owner != getpid())
|
1999-03-23 17:22:59 +03:00
|
|
|
fprintf(stderr, "Owner changed from "
|
|
|
|
"%d to %d, can't happen\n",
|
2003-02-04 02:08:37 +03:00
|
|
|
getpid(), cdesc[idx].cd_owner);
|
|
|
|
cdesc[idx].cd_owner = 0;
|
1999-03-23 17:22:59 +03:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On short read, just note the fact and go on */
|
2003-02-04 02:08:37 +03:00
|
|
|
cdesc[idx].cd_blkend = blockBlkNo + rsize / dev_bsize;
|
1999-03-23 17:22:59 +03:00
|
|
|
|
|
|
|
#ifdef STATS
|
|
|
|
nphysread++;
|
|
|
|
physreadsize += rsize;
|
|
|
|
#endif
|
|
|
|
#ifdef DIAGNOSTICS
|
2003-02-04 02:08:37 +03:00
|
|
|
if (cdesc[idx].cd_owner != getpid())
|
1999-03-23 17:22:59 +03:00
|
|
|
fprintf(stderr, "Owner changed from "
|
|
|
|
"%d to %d, can't happen\n",
|
2003-02-04 02:08:37 +03:00
|
|
|
getpid(), cdesc[idx].cd_owner);
|
|
|
|
cdesc[idx].cd_owner = 0;
|
1999-03-23 17:22:59 +03:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* We swapped some of data in, let the loop fetch
|
|
|
|
* them from cache
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
2001-12-23 15:29:55 +03:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
if (flock(diskfd, LOCK_UN))
|
|
|
|
msg("flock(LOCK_UN) failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-05-27 18:17:56 +04:00
|
|
|
printcachestats(void)
|
1999-03-23 17:22:59 +03:00
|
|
|
{
|
2003-02-04 11:06:42 +03:00
|
|
|
|
1999-03-23 17:22:59 +03:00
|
|
|
#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
|
|
|
|
}
|