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