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