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