Home | History | Annotate | Line # | Download | only in dump
rcache.c revision 1.23.8.1
      1  1.23.8.1       snj /*	$NetBSD: rcache.c,v 1.23.8.1 2016/03/08 10:03:57 snj Exp $	*/
      2       1.1    bouyer 
      3       1.1    bouyer /*-
      4       1.1    bouyer  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5       1.1    bouyer  * All rights reserved.
      6       1.1    bouyer  *
      7       1.1    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8      1.10     lukem  * by Martin J. Laubach <mjl (at) emsi.priv.at> and
      9       1.1    bouyer  *    Manuel Bouyer <Manuel.Bouyer (at) lip6.fr>.
     10       1.1    bouyer  *
     11       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     12       1.1    bouyer  * modification, are permitted provided that the following conditions
     13       1.1    bouyer  * are met:
     14       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     15       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     16       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     18       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     19       1.1    bouyer  *
     20       1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.10     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1    bouyer  */
     32      1.11     lukem 
     33      1.11     lukem #include <sys/cdefs.h>
     34      1.11     lukem #ifndef lint
     35  1.23.8.1       snj __RCSID("$NetBSD: rcache.c,v 1.23.8.1 2016/03/08 10:03:57 snj Exp $");
     36      1.11     lukem #endif /* not lint */
     37      1.11     lukem 
     38       1.1    bouyer #include <sys/types.h>
     39       1.1    bouyer #include <sys/uio.h>
     40       1.1    bouyer #include <sys/mman.h>
     41       1.1    bouyer #include <sys/param.h>
     42       1.1    bouyer #include <sys/sysctl.h>
     43       1.1    bouyer #include <ufs/ufs/dinode.h>
     44       1.1    bouyer 
     45       1.1    bouyer #include <stdio.h>
     46       1.1    bouyer #include <stdlib.h>
     47       1.1    bouyer #include <unistd.h>
     48       1.1    bouyer #include <fcntl.h>
     49       1.1    bouyer #include <errno.h>
     50       1.1    bouyer #include <string.h>
     51       1.1    bouyer 
     52       1.1    bouyer #include "dump.h"
     53       1.1    bouyer 
     54       1.1    bouyer /*-----------------------------------------------------------------------*/
     55       1.1    bouyer #define MAXCACHEBUFS	512	/* max 512 buffers */
     56       1.1    bouyer #define MAXMEMPART	6	/* max 15% of the user mem */
     57       1.1    bouyer 
     58       1.1    bouyer /*-----------------------------------------------------------------------*/
     59      1.13   hannken union cdesc {
     60      1.13   hannken 	volatile size_t cd_count;
     61      1.13   hannken 	struct {
     62      1.13   hannken 		volatile daddr_t blkstart;
     63      1.14     enami 		volatile daddr_t blkend;	/* start + nblksread */
     64      1.13   hannken 		volatile daddr_t blocksRead;
     65      1.13   hannken 		volatile size_t time;
     66       1.1    bouyer #ifdef DIAGNOSTICS
     67      1.13   hannken 		volatile pid_t owner;
     68       1.1    bouyer #endif
     69      1.13   hannken 	} desc;
     70      1.13   hannken #define cd_blkstart	desc.blkstart
     71      1.13   hannken #define cd_blkend	desc.blkend
     72      1.13   hannken #define cd_blocksRead	desc.blocksRead
     73      1.13   hannken #define cd_time		desc.time
     74      1.13   hannken #define cd_owner	desc.owner
     75       1.1    bouyer };
     76       1.1    bouyer 
     77       1.6     lukem static int findlru(void);
     78       1.1    bouyer 
     79       1.1    bouyer static void *shareBuffer = NULL;
     80      1.13   hannken static union cdesc *cheader;
     81      1.13   hannken static union cdesc *cdesc;
     82       1.1    bouyer static char *cdata;
     83       1.1    bouyer static int cachebufs;
     84       1.1    bouyer static int nblksread;
     85       1.1    bouyer 
     86       1.1    bouyer #ifdef STATS
     87       1.1    bouyer static int nreads;
     88       1.1    bouyer static int nphysread;
     89       1.1    bouyer static int64_t readsize;
     90       1.1    bouyer static int64_t physreadsize;
     91       1.1    bouyer #endif
     92       1.1    bouyer 
     93      1.16     enami #define	CSIZE		(nblksread << dev_bshift)	/* cache buf size */
     94      1.16     enami #define	CDATA(desc)	(cdata + ((desc) - cdesc) * CSIZE)
     95       1.1    bouyer 
     96      1.10     lukem void
     97       1.6     lukem initcache(int cachesize, int readblksize)
     98       1.1    bouyer {
     99       1.1    bouyer 	size_t len;
    100      1.14     enami 	size_t sharedSize;
    101       1.1    bouyer 
    102  1.23.8.1       snj 	if (readblksize == -1) { /* use kern.maxphys */
    103  1.23.8.1       snj 		int kern_maxphys;
    104  1.23.8.1       snj 		int mib[2] = { CTL_KERN, KERN_MAXPHYS };
    105  1.23.8.1       snj 
    106  1.23.8.1       snj 		len = sizeof(kern_maxphys);
    107  1.23.8.1       snj 		if (sysctl(mib, 2, &kern_maxphys, &len, NULL, 0) < 0) {
    108  1.23.8.1       snj 			msg("sysctl(kern.maxphys) failed: %s\n",
    109  1.23.8.1       snj 			    strerror(errno));
    110  1.23.8.1       snj 			return;
    111  1.23.8.1       snj 		}
    112  1.23.8.1       snj 		readblksize = kern_maxphys;
    113  1.23.8.1       snj 	}
    114  1.23.8.1       snj 
    115      1.15     enami 	/* Convert read block size in terms of filesystem block size */
    116      1.14     enami 	nblksread = howmany(readblksize, ufsib->ufs_bsize);
    117      1.15     enami 
    118      1.15     enami 	/* Then, convert it in terms of device block size */
    119      1.15     enami 	nblksread <<= ufsib->ufs_bshift - dev_bshift;
    120      1.15     enami 
    121      1.14     enami 	if (cachesize == -1) {	/* Compute from memory available */
    122      1.23       spz 		uint64_t usermem, cachetmp;
    123      1.21    simonb 		int mib[2] = { CTL_HW, HW_USERMEM64 };
    124      1.10     lukem 
    125       1.1    bouyer 		len = sizeof(usermem);
    126       1.1    bouyer 		if (sysctl(mib, 2, &usermem, &len, NULL, 0) < 0) {
    127      1.14     enami 			msg("sysctl(hw.usermem) failed: %s\n",
    128      1.14     enami 			    strerror(errno));
    129       1.1    bouyer 			return;
    130       1.1    bouyer 		}
    131      1.23       spz 		cachetmp = (usermem / MAXMEMPART) / CSIZE;
    132      1.23       spz 		/* for those with TB of RAM */
    133      1.23       spz 		cachebufs = (cachetmp > INT_MAX) ? INT_MAX : cachetmp;
    134       1.1    bouyer 	} else {		/* User specified */
    135       1.1    bouyer 		cachebufs = cachesize;
    136       1.1    bouyer 	}
    137      1.10     lukem 
    138      1.14     enami 	if (cachebufs) {	/* Don't allocate if zero --> no caching */
    139       1.1    bouyer 		if (cachebufs > MAXCACHEBUFS)
    140       1.1    bouyer 			cachebufs = MAXCACHEBUFS;
    141       1.1    bouyer 
    142      1.13   hannken 		sharedSize = sizeof(union cdesc) +
    143      1.13   hannken 	   	    sizeof(union cdesc) * cachebufs +
    144      1.16     enami 	   	    cachebufs * CSIZE;
    145      1.10     lukem #ifdef STATS
    146       1.1    bouyer 		fprintf(stderr, "Using %d buffers (%d bytes)\n", cachebufs,
    147       1.1    bouyer 	   	    sharedSize);
    148       1.1    bouyer #endif
    149       1.1    bouyer 		shareBuffer = mmap(NULL, sharedSize, PROT_READ | PROT_WRITE,
    150       1.1    bouyer 	   	    MAP_ANON | MAP_SHARED, -1, 0);
    151      1.14     enami 		if (shareBuffer == MAP_FAILED) {
    152       1.1    bouyer 			msg("can't mmap shared memory for buffer: %s\n",
    153       1.1    bouyer 			    strerror(errno));
    154       1.1    bouyer 			return;
    155       1.1    bouyer 		}
    156       1.1    bouyer 		cheader = shareBuffer;
    157      1.13   hannken 		cdesc = (union cdesc *) (((char *) shareBuffer) +
    158      1.13   hannken 		    sizeof(union cdesc));
    159      1.13   hannken 		cdata = ((char *) shareBuffer) + sizeof(union cdesc) +
    160      1.13   hannken 	   	    sizeof(union cdesc) * cachebufs;
    161       1.1    bouyer 
    162       1.1    bouyer 		memset(shareBuffer, '\0', sharedSize);
    163       1.1    bouyer 	}
    164       1.1    bouyer }
    165       1.1    bouyer 
    166       1.6     lukem /*
    167       1.6     lukem  * Find the cache buffer descriptor that shows the minimal access time
    168       1.6     lukem  */
    169      1.10     lukem static int
    170       1.6     lukem findlru(void)
    171       1.1    bouyer {
    172       1.8     lukem 	int	i;
    173      1.13   hannken 	size_t	minTime = cdesc[0].cd_time;
    174       1.8     lukem 	int	minIdx = 0;
    175       1.1    bouyer 
    176       1.1    bouyer 	for (i = 0; i < cachebufs; i++) {
    177      1.13   hannken 		if (cdesc[i].cd_time < minTime) {
    178       1.1    bouyer 			minIdx = i;
    179      1.13   hannken 			minTime = cdesc[i].cd_time;
    180       1.1    bouyer 		}
    181       1.1    bouyer 	}
    182       1.1    bouyer 
    183       1.1    bouyer 	return minIdx;
    184       1.1    bouyer }
    185       1.6     lukem 
    186       1.1    bouyer /*
    187       1.1    bouyer  * Read data directly from disk, with smart error handling.
    188       1.1    bouyer  * Try to recover from hard errors by reading in sector sized pieces.
    189       1.1    bouyer  * Error recovery is attempted at most BREADEMAX times before seeking
    190       1.1    bouyer  * consent from the operator to continue.
    191       1.1    bouyer  */
    192       1.1    bouyer 
    193       1.1    bouyer static int breaderrors = 0;
    194       1.1    bouyer #define BREADEMAX 32
    195       1.1    bouyer 
    196      1.10     lukem void
    197       1.6     lukem rawread(daddr_t blkno, char *buf, int size)
    198       1.1    bouyer {
    199       1.1    bouyer 	int cnt, i;
    200      1.14     enami 
    201       1.1    bouyer #ifdef STATS
    202       1.1    bouyer 	nphysread++;
    203       1.1    bouyer 	physreadsize += size;
    204       1.1    bouyer #endif
    205       1.1    bouyer 
    206      1.14     enami loop:
    207      1.18     enami 	if (lseek(diskfd, ((off_t) blkno << dev_bshift), SEEK_SET) == -1) {
    208       1.1    bouyer 		msg("rawread: lseek fails\n");
    209       1.1    bouyer 		goto err;
    210       1.1    bouyer 	}
    211      1.14     enami 	if ((cnt = read(diskfd, buf, size)) == size)
    212       1.1    bouyer 		return;
    213      1.16     enami 	if (blkno + (size >> dev_bshift) > ufsib->ufs_dsize) {
    214       1.7     lukem 		/*
    215       1.7     lukem 		 * Trying to read the final fragment.
    216       1.7     lukem 		 *
    217       1.7     lukem 		 * NB - dump only works in TP_BSIZE blocks, hence
    218       1.7     lukem 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
    219       1.7     lukem 		 * It should be smarter about not actually trying to
    220       1.7     lukem 		 * read more than it can get, but for the time being
    221       1.7     lukem 		 * we punt and scale back the read only when it gets
    222       1.7     lukem 		 * us into trouble. (mkm 9/25/83)
    223       1.7     lukem 		 */
    224       1.7     lukem 		size -= dev_bsize;
    225       1.7     lukem 		goto loop;
    226       1.7     lukem 	}
    227       1.1    bouyer 	if (cnt == -1)
    228      1.12      fvdl 		msg("read error from %s: %s: [block %lld]: count=%d\n",
    229      1.14     enami 		    disk, strerror(errno), (long long)blkno, size);
    230       1.1    bouyer 	else
    231      1.14     enami 		msg("short read error from %s: [block %lld]: "
    232      1.14     enami 		    "count=%d, got=%d\n",
    233      1.14     enami 		    disk, (long long)blkno, size, cnt);
    234       1.1    bouyer err:
    235       1.1    bouyer 	if (++breaderrors > BREADEMAX) {
    236       1.5    briggs 		msg("More than %d block read errors from %s\n",
    237      1.14     enami 		    BREADEMAX, disk);
    238       1.1    bouyer 		broadcast("DUMP IS AILING!\n");
    239       1.1    bouyer 		msg("This is an unrecoverable error.\n");
    240      1.14     enami 		if (!query("Do you want to attempt to continue?")) {
    241       1.1    bouyer 			dumpabort(0);
    242       1.1    bouyer 			/*NOTREACHED*/
    243       1.1    bouyer 		} else
    244       1.1    bouyer 			breaderrors = 0;
    245       1.1    bouyer 	}
    246       1.1    bouyer 	/*
    247       1.1    bouyer 	 * Zero buffer, then try to read each sector of buffer separately.
    248       1.1    bouyer 	 */
    249       1.1    bouyer 	memset(buf, 0, size);
    250       1.1    bouyer 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
    251      1.14     enami 		if (lseek(diskfd, ((off_t)blkno << dev_bshift),
    252      1.18     enami 		    SEEK_SET) == -1) {
    253       1.1    bouyer 			msg("rawread: lseek2 fails: %s!\n",
    254       1.1    bouyer 			    strerror(errno));
    255       1.1    bouyer 			continue;
    256       1.1    bouyer 		}
    257       1.1    bouyer 		if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
    258       1.1    bouyer 			continue;
    259       1.1    bouyer 		if (cnt == -1) {
    260      1.14     enami 			msg("read error from %s: %s: [sector %lld]: "
    261      1.19     enami 			    "count=%ld\n", disk, strerror(errno),
    262      1.19     enami 			    (long long)blkno, dev_bsize);
    263       1.1    bouyer 			continue;
    264       1.1    bouyer 		}
    265      1.14     enami 		msg("short read error from %s: [sector %lld]: "
    266      1.14     enami 		    "count=%ld, got=%d\n",
    267      1.12      fvdl 		    disk, (long long)blkno, dev_bsize, cnt);
    268       1.1    bouyer 	}
    269       1.1    bouyer }
    270       1.1    bouyer 
    271      1.10     lukem void
    272       1.6     lukem bread(daddr_t blkno, char *buf, int size)
    273       1.1    bouyer {
    274      1.16     enami 	int	osize = size, idx;
    275       1.1    bouyer 	daddr_t oblkno = blkno;
    276       1.1    bouyer 	char   *obuf = buf;
    277      1.14     enami 	daddr_t numBlocks = howmany(size, dev_bsize);
    278       1.1    bouyer 
    279       1.1    bouyer #ifdef STATS
    280       1.1    bouyer 	nreads++;
    281       1.1    bouyer 	readsize += size;
    282       1.1    bouyer #endif
    283       1.1    bouyer 
    284       1.1    bouyer 	if (!shareBuffer) {
    285       1.1    bouyer 		rawread(blkno, buf, size);
    286       1.1    bouyer 		return;
    287       1.1    bouyer 	}
    288       1.1    bouyer 
    289       1.1    bouyer 	if (flock(diskfd, LOCK_EX)) {
    290       1.1    bouyer 		msg("flock(LOCK_EX) failed: %s\n",
    291       1.1    bouyer 		    strerror(errno));
    292       1.1    bouyer 		rawread(blkno, buf, size);
    293       1.1    bouyer 		return;
    294       1.1    bouyer 	}
    295       1.1    bouyer 
    296       1.1    bouyer retry:
    297      1.16     enami 	idx = 0;
    298      1.16     enami 	while (size > 0) {
    299       1.8     lukem 		int	i;
    300      1.10     lukem 
    301       1.1    bouyer 		for (i = 0; i < cachebufs; i++) {
    302      1.16     enami 			union cdesc *curr = &cdesc[(i + idx) % cachebufs];
    303       1.1    bouyer 
    304       1.1    bouyer #ifdef DIAGNOSTICS
    305      1.13   hannken 			if (curr->cd_owner) {
    306       1.1    bouyer 				fprintf(stderr, "Owner is set (%d, me=%d), can"
    307      1.13   hannken 				    "not happen.\n", curr->cd_owner, getpid());
    308       1.1    bouyer 			}
    309       1.1    bouyer #endif
    310       1.1    bouyer 
    311      1.13   hannken 			if (curr->cd_blkend == 0)
    312       1.1    bouyer 				continue;
    313       1.1    bouyer 			/*
    314       1.1    bouyer 			 * If we find a bit of the read in the buffers,
    315       1.1    bouyer 			 * now compute how many blocks we can copy,
    316       1.1    bouyer 			 * copy them out, adjust blkno, buf and size,
    317       1.1    bouyer 			 * and restart
    318       1.1    bouyer 			 */
    319      1.13   hannken 			if (curr->cd_blkstart <= blkno &&
    320      1.13   hannken 			    blkno < curr->cd_blkend) {
    321       1.1    bouyer 				/* Number of data blocks to be copied */
    322       1.6     lukem 				int toCopy = MIN(size,
    323      1.16     enami 				    (curr->cd_blkend - blkno) << dev_bshift);
    324       1.1    bouyer #ifdef DIAGNOSTICS
    325      1.16     enami 				if (toCopy <= 0 || toCopy > CSIZE) {
    326       1.1    bouyer 					fprintf(stderr, "toCopy %d !\n",
    327       1.1    bouyer 					    toCopy);
    328       1.1    bouyer 					dumpabort(0);
    329       1.1    bouyer 				}
    330      1.16     enami 				if (CDATA(curr) +
    331      1.16     enami 				    ((blkno - curr->cd_blkstart) <<
    332      1.17     enami 				    dev_bshift) < CDATA(curr) ||
    333      1.16     enami 			   	    CDATA(curr) +
    334      1.16     enami 				    ((blkno - curr->cd_blkstart) <<
    335      1.17     enami 			   	    dev_bshift) > CDATA(curr) + CSIZE) {
    336       1.1    bouyer 					fprintf(stderr, "%p < %p !!!\n",
    337      1.16     enami 				   	   CDATA(curr) + ((blkno -
    338      1.16     enami 					   curr->cd_blkstart) << dev_bshift),
    339      1.16     enami 					   CDATA(curr));
    340      1.16     enami 					fprintf(stderr,
    341      1.16     enami 					    "cdesc[i].cd_blkstart %lld "
    342      1.16     enami 					    "blkno %lld dev_bsize %ld\n",
    343      1.16     enami 				   	    (long long)curr->cd_blkstart,
    344      1.16     enami 					    (long long)blkno,
    345      1.16     enami 					    dev_bsize);
    346       1.1    bouyer 					dumpabort(0);
    347       1.1    bouyer 				}
    348       1.1    bouyer #endif
    349      1.16     enami 				memcpy(buf, CDATA(curr) +
    350      1.17     enami 				    ((blkno - curr->cd_blkstart) <<
    351      1.17     enami 				    dev_bshift),
    352       1.1    bouyer 			   	    toCopy);
    353       1.1    bouyer 
    354       1.1    bouyer 				buf 	+= toCopy;
    355       1.1    bouyer 				size 	-= toCopy;
    356      1.14     enami 				blkno 	+= howmany(toCopy, dev_bsize);
    357      1.14     enami 				numBlocks -= howmany(toCopy, dev_bsize);
    358       1.1    bouyer 
    359      1.13   hannken 				curr->cd_time = cheader->cd_count++;
    360       1.1    bouyer 
    361       1.1    bouyer 				/*
    362       1.1    bouyer 				 * If all data of a cache block have been
    363       1.1    bouyer 				 * read, chances are good no more reads
    364       1.1    bouyer 				 * will occur, so expire the cache immediately
    365       1.1    bouyer 				 */
    366       1.1    bouyer 
    367      1.13   hannken 				curr->cd_blocksRead +=
    368      1.14     enami 				    howmany(toCopy, dev_bsize);
    369      1.13   hannken 				if (curr->cd_blocksRead >= nblksread)
    370      1.13   hannken 					curr->cd_time = 0;
    371       1.1    bouyer 
    372       1.1    bouyer 				goto retry;
    373       1.1    bouyer 			}
    374       1.1    bouyer 		}
    375       1.1    bouyer 
    376       1.1    bouyer 		/* No more to do? */
    377       1.1    bouyer 		if (size == 0)
    378       1.1    bouyer 			break;
    379       1.9     lukem 
    380       1.1    bouyer 		/*
    381       1.1    bouyer 		 * This does actually not happen if fs blocks are not greater
    382       1.1    bouyer 		 * than nblksread.
    383       1.1    bouyer 		 */
    384       1.9     lukem 		if (numBlocks > nblksread || blkno >= ufsib->ufs_dsize) {
    385       1.1    bouyer 			rawread(oblkno, obuf, osize);
    386       1.1    bouyer 			break;
    387       1.1    bouyer 		} else {
    388       1.8     lukem 			ssize_t	rsize;
    389       1.8     lukem 			daddr_t	blockBlkNo;
    390       1.1    bouyer 
    391       1.1    bouyer 			blockBlkNo = (blkno / nblksread) * nblksread;
    392       1.1    bouyer 			idx = findlru();
    393       1.6     lukem 			rsize = MIN(nblksread,
    394      1.16     enami 			    ufsib->ufs_dsize - blockBlkNo) << dev_bshift;
    395       1.3  perseant 
    396       1.1    bouyer #ifdef DIAGNOSTICS
    397      1.13   hannken 			if (cdesc[idx].cd_owner)
    398       1.1    bouyer 				fprintf(stderr, "Owner is set (%d, me=%d), can"
    399      1.13   hannken 				    "not happen(2).\n", cdesc[idx].cd_owner,
    400       1.1    bouyer 				    getpid());
    401      1.13   hannken 			cdesc[idx].cd_owner = getpid();
    402       1.1    bouyer #endif
    403      1.13   hannken 			cdesc[idx].cd_time = cheader->cd_count++;
    404      1.13   hannken 			cdesc[idx].cd_blkstart = blockBlkNo;
    405      1.16     enami 			cdesc[idx].cd_blkend = 0;
    406      1.13   hannken 			cdesc[idx].cd_blocksRead = 0;
    407       1.1    bouyer 
    408      1.14     enami 			if (lseek(diskfd, ((off_t) blockBlkNo << dev_bshift),
    409      1.18     enami 			    SEEK_SET) == -1) {
    410       1.1    bouyer 				msg("readBlocks: lseek fails: %s\n",
    411       1.1    bouyer 				    strerror(errno));
    412       1.1    bouyer 				rsize = -1;
    413       1.1    bouyer 			} else {
    414      1.16     enami 				rsize = read(diskfd,
    415      1.16     enami 				    CDATA(&cdesc[idx]), rsize);
    416       1.1    bouyer 				if (rsize < 0) {
    417       1.1    bouyer 					msg("readBlocks: read fails: %s\n",
    418       1.1    bouyer 					    strerror(errno));
    419       1.1    bouyer 				}
    420       1.1    bouyer 			}
    421       1.1    bouyer 
    422       1.1    bouyer 			/* On errors, panic, punt, try to read without
    423       1.1    bouyer 			 * cache and let raw read routine do the rest.
    424       1.1    bouyer 			 */
    425       1.1    bouyer 
    426       1.1    bouyer 			if (rsize <= 0) {
    427       1.1    bouyer 				rawread(oblkno, obuf, osize);
    428       1.1    bouyer #ifdef DIAGNOSTICS
    429      1.13   hannken 				if (cdesc[idx].cd_owner != getpid())
    430       1.1    bouyer 					fprintf(stderr, "Owner changed from "
    431       1.1    bouyer 					    "%d to %d, can't happen\n",
    432      1.13   hannken 					    getpid(), cdesc[idx].cd_owner);
    433      1.13   hannken 				cdesc[idx].cd_owner = 0;
    434       1.1    bouyer #endif
    435       1.1    bouyer 				break;
    436       1.1    bouyer 			}
    437       1.1    bouyer 
    438       1.1    bouyer 			/* On short read, just note the fact and go on */
    439      1.13   hannken 			cdesc[idx].cd_blkend = blockBlkNo + rsize / dev_bsize;
    440       1.1    bouyer 
    441       1.1    bouyer #ifdef STATS
    442       1.1    bouyer 			nphysread++;
    443       1.1    bouyer 			physreadsize += rsize;
    444       1.1    bouyer #endif
    445       1.1    bouyer #ifdef DIAGNOSTICS
    446      1.13   hannken 			if (cdesc[idx].cd_owner != getpid())
    447       1.1    bouyer 				fprintf(stderr, "Owner changed from "
    448       1.1    bouyer 				    "%d to %d, can't happen\n",
    449      1.13   hannken 				    getpid(), cdesc[idx].cd_owner);
    450      1.13   hannken 			cdesc[idx].cd_owner = 0;
    451       1.1    bouyer #endif
    452       1.1    bouyer 			/*
    453       1.1    bouyer 			 * We swapped some of data in, let the loop fetch
    454       1.1    bouyer 			 * them from cache
    455       1.1    bouyer 			 */
    456       1.1    bouyer 		}
    457       1.1    bouyer 	}
    458      1.10     lukem 
    459       1.1    bouyer 	if (flock(diskfd, LOCK_UN))
    460       1.1    bouyer 		msg("flock(LOCK_UN) failed: %s\n",
    461       1.1    bouyer 		    strerror(errno));
    462       1.1    bouyer }
    463       1.1    bouyer 
    464       1.1    bouyer void
    465       1.6     lukem printcachestats(void)
    466       1.1    bouyer {
    467      1.14     enami 
    468       1.1    bouyer #ifdef STATS
    469       1.1    bouyer 	fprintf(stderr, "Pid %d: %d reads (%u bytes) "
    470       1.1    bouyer 	    "%d physical reads (%u bytes) %d%% hits, %d%% overhead\n",
    471       1.1    bouyer 	    getpid(), nreads, (u_int) readsize, nphysread,
    472       1.1    bouyer 	    (u_int) physreadsize, (nreads - nphysread) * 100 / nreads,
    473       1.1    bouyer 	    (int) (((physreadsize - readsize) * 100) / readsize));
    474       1.1    bouyer #endif
    475       1.1    bouyer }
    476