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