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