xref: /netbsd-src/sbin/fsck_ffs/utilities.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)utilities.c	8.1 (Berkeley) 6/5/93";
37 #else
38 static char rcsid[] = "$NetBSD: utilities.c,v 1.14 1995/04/12 21:24:13 mycroft Exp $";
39 #endif
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include <unistd.h>
52 
53 #include "fsck.h"
54 #include "extern.h"
55 
56 long	diskreads, totalreads;	/* Disk cache statistics */
57 
58 int
59 ftypeok(dp)
60 	struct dinode *dp;
61 {
62 	switch (dp->di_mode & IFMT) {
63 
64 	case IFDIR:
65 	case IFREG:
66 	case IFBLK:
67 	case IFCHR:
68 	case IFLNK:
69 	case IFSOCK:
70 	case IFIFO:
71 		return (1);
72 
73 	default:
74 		if (debug)
75 			printf("bad file type 0%o\n", dp->di_mode);
76 		return (0);
77 	}
78 }
79 
80 int
81 reply(question)
82 	char *question;
83 {
84 	int persevere;
85 	char c;
86 
87 	if (preen)
88 		pfatal("INTERNAL ERROR: GOT TO reply()");
89 	persevere = !strcmp(question, "CONTINUE");
90 	printf("\n");
91 	if (!persevere && (nflag || fswritefd < 0)) {
92 		printf("%s? no\n\n", question);
93 		return (0);
94 	}
95 	if (yflag || (persevere && nflag)) {
96 		printf("%s? yes\n\n", question);
97 		return (1);
98 	}
99 	do	{
100 		printf("%s? [yn] ", question);
101 		(void) fflush(stdout);
102 		c = getc(stdin);
103 		while (c != '\n' && getc(stdin) != '\n')
104 			if (feof(stdin))
105 				return (0);
106 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
107 	printf("\n");
108 	if (c == 'y' || c == 'Y')
109 		return (1);
110 	return (0);
111 }
112 
113 /*
114  * Malloc buffers and set up cache.
115  */
116 void
117 bufinit()
118 {
119 	register struct bufarea *bp;
120 	long bufcnt, i;
121 	char *bufp;
122 
123 	pbp = pdirbp = (struct bufarea *)0;
124 	bufp = malloc((unsigned int)sblock.fs_bsize);
125 	if (bufp == 0)
126 		errexit("cannot allocate buffer pool\n");
127 	cgblk.b_un.b_buf = bufp;
128 	initbarea(&cgblk);
129 	bufhead.b_next = bufhead.b_prev = &bufhead;
130 	bufcnt = MAXBUFSPACE / sblock.fs_bsize;
131 	if (bufcnt < MINBUFS)
132 		bufcnt = MINBUFS;
133 	for (i = 0; i < bufcnt; i++) {
134 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
135 		bufp = malloc((unsigned int)sblock.fs_bsize);
136 		if (bp == NULL || bufp == NULL) {
137 			if (i >= MINBUFS)
138 				break;
139 			errexit("cannot allocate buffer pool\n");
140 		}
141 		bp->b_un.b_buf = bufp;
142 		bp->b_prev = &bufhead;
143 		bp->b_next = bufhead.b_next;
144 		bufhead.b_next->b_prev = bp;
145 		bufhead.b_next = bp;
146 		initbarea(bp);
147 	}
148 	bufhead.b_size = i;	/* save number of buffers */
149 }
150 
151 /*
152  * Manage a cache of directory blocks.
153  */
154 struct bufarea *
155 getdatablk(blkno, size)
156 	daddr_t blkno;
157 	long size;
158 {
159 	register struct bufarea *bp;
160 
161 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
162 		if (bp->b_bno == fsbtodb(&sblock, blkno))
163 			goto foundit;
164 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
165 		if ((bp->b_flags & B_INUSE) == 0)
166 			break;
167 	if (bp == &bufhead)
168 		errexit("deadlocked buffer pool\n");
169 	getblk(bp, blkno, size);
170 	/* fall through */
171 foundit:
172 	totalreads++;
173 	bp->b_prev->b_next = bp->b_next;
174 	bp->b_next->b_prev = bp->b_prev;
175 	bp->b_prev = &bufhead;
176 	bp->b_next = bufhead.b_next;
177 	bufhead.b_next->b_prev = bp;
178 	bufhead.b_next = bp;
179 	bp->b_flags |= B_INUSE;
180 	return (bp);
181 }
182 
183 void
184 getblk(bp, blk, size)
185 	register struct bufarea *bp;
186 	daddr_t blk;
187 	long size;
188 {
189 	daddr_t dblk;
190 
191 	dblk = fsbtodb(&sblock, blk);
192 	if (bp->b_bno != dblk) {
193 		flush(fswritefd, bp);
194 		diskreads++;
195 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
196 		bp->b_bno = dblk;
197 		bp->b_size = size;
198 	}
199 }
200 
201 void
202 flush(fd, bp)
203 	int fd;
204 	register struct bufarea *bp;
205 {
206 	register int i, j;
207 
208 	if (!bp->b_dirty)
209 		return;
210 	if (bp->b_errs != 0)
211 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
212 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
213 		    bp->b_bno);
214 	bp->b_dirty = 0;
215 	bp->b_errs = 0;
216 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
217 	if (bp != &sblk)
218 		return;
219 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
220 		bwrite(fswritefd, (char *)sblock.fs_csp[j],
221 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
222 		    sblock.fs_cssize - i < sblock.fs_bsize ?
223 		    sblock.fs_cssize - i : sblock.fs_bsize);
224 	}
225 }
226 
227 void
228 rwerror(mesg, blk)
229 	char *mesg;
230 	daddr_t blk;
231 {
232 
233 	if (preen == 0)
234 		printf("\n");
235 	pfatal("CANNOT %s: BLK %ld", mesg, blk);
236 	if (reply("CONTINUE") == 0)
237 		errexit("Program terminated\n");
238 }
239 
240 void
241 ckfini(markclean)
242 	int markclean;
243 {
244 	register struct bufarea *bp, *nbp;
245 	int cnt = 0;
246 
247 	if (fswritefd < 0) {
248 		(void)close(fsreadfd);
249 		return;
250 	}
251 	flush(fswritefd, &sblk);
252 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
253 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
254 		sblk.b_bno = SBOFF / dev_bsize;
255 		sbdirty();
256 		flush(fswritefd, &sblk);
257 	}
258 	flush(fswritefd, &cgblk);
259 	free(cgblk.b_un.b_buf);
260 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
261 		cnt++;
262 		flush(fswritefd, bp);
263 		nbp = bp->b_prev;
264 		free(bp->b_un.b_buf);
265 		free((char *)bp);
266 	}
267 	if (bufhead.b_size != cnt)
268 		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
269 	pbp = pdirbp = (struct bufarea *)0;
270 	if (markclean && (sblock.fs_clean & FS_ISCLEAN) == 0) {
271 		/*
272 		 * Mark the file system as clean, and sync the superblock.
273 		 */
274 		if (preen)
275 			pwarn("MARKING FILE SYSTEM CLEAN\n");
276 		else if (!reply("MARK FILE SYSTEM CLEAN"))
277 			markclean = 0;
278 		if (markclean) {
279 			sblock.fs_clean = FS_ISCLEAN;
280 			sbdirty();
281 			flush(fswritefd, &sblk);
282 		}
283 	}
284 	if (debug)
285 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
286 		    totalreads, (int)(diskreads * 100 / totalreads));
287 	(void)close(fsreadfd);
288 	(void)close(fswritefd);
289 }
290 
291 int
292 bread(fd, buf, blk, size)
293 	int fd;
294 	char *buf;
295 	daddr_t blk;
296 	long size;
297 {
298 	char *cp;
299 	int i, errs;
300 	off_t offset;
301 
302 	offset = blk;
303 	offset *= dev_bsize;
304 	if (lseek(fd, offset, 0) < 0)
305 		rwerror("SEEK", blk);
306 	else if (read(fd, buf, (int)size) == size)
307 		return (0);
308 	rwerror("READ", blk);
309 	if (lseek(fd, offset, 0) < 0)
310 		rwerror("SEEK", blk);
311 	errs = 0;
312 	memset(buf, 0, (size_t)size);
313 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
314 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
315 		if (read(fd, cp, (int)secsize) != secsize) {
316 			(void)lseek(fd, offset + i + secsize, 0);
317 			if (secsize != dev_bsize && dev_bsize != 1)
318 				printf(" %ld (%ld),",
319 				    (blk * dev_bsize + i) / secsize,
320 				    blk + i / dev_bsize);
321 			else
322 				printf(" %ld,", blk + i / dev_bsize);
323 			errs++;
324 		}
325 	}
326 	printf("\n");
327 	return (errs);
328 }
329 
330 void
331 bwrite(fd, buf, blk, size)
332 	int fd;
333 	char *buf;
334 	daddr_t blk;
335 	long size;
336 {
337 	int i;
338 	char *cp;
339 	off_t offset;
340 
341 	if (fd < 0)
342 		return;
343 	offset = blk;
344 	offset *= dev_bsize;
345 	if (lseek(fd, offset, 0) < 0)
346 		rwerror("SEEK", blk);
347 	else if (write(fd, buf, (int)size) == size) {
348 		fsmodified = 1;
349 		return;
350 	}
351 	rwerror("WRITE", blk);
352 	if (lseek(fd, offset, 0) < 0)
353 		rwerror("SEEK", blk);
354 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
355 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
356 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
357 			(void)lseek(fd, offset + i + dev_bsize, 0);
358 			printf(" %ld,", blk + i / dev_bsize);
359 		}
360 	printf("\n");
361 	return;
362 }
363 
364 /*
365  * allocate a data block with the specified number of fragments
366  */
367 int
368 allocblk(frags)
369 	long frags;
370 {
371 	register int i, j, k;
372 
373 	if (frags <= 0 || frags > sblock.fs_frag)
374 		return (0);
375 	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
376 		for (j = 0; j <= sblock.fs_frag - frags; j++) {
377 			if (testbmap(i + j))
378 				continue;
379 			for (k = 1; k < frags; k++)
380 				if (testbmap(i + j + k))
381 					break;
382 			if (k < frags) {
383 				j += k;
384 				continue;
385 			}
386 			for (k = 0; k < frags; k++)
387 				setbmap(i + j + k);
388 			n_blks += frags;
389 			return (i + j);
390 		}
391 	}
392 	return (0);
393 }
394 
395 /*
396  * Free a previously allocated block
397  */
398 void
399 freeblk(blkno, frags)
400 	daddr_t blkno;
401 	long frags;
402 {
403 	struct inodesc idesc;
404 
405 	idesc.id_blkno = blkno;
406 	idesc.id_numfrags = frags;
407 	(void)pass4check(&idesc);
408 }
409 
410 /*
411  * Find a pathname
412  */
413 void
414 getpathname(namebuf, curdir, ino)
415 	char *namebuf;
416 	ino_t curdir, ino;
417 {
418 	int len;
419 	register char *cp;
420 	struct inodesc idesc;
421 	static int busy = 0;
422 
423 	if (curdir == ino && ino == ROOTINO) {
424 		(void)strcpy(namebuf, "/");
425 		return;
426 	}
427 	if (busy ||
428 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
429 		(void)strcpy(namebuf, "?");
430 		return;
431 	}
432 	busy = 1;
433 	memset(&idesc, 0, sizeof(struct inodesc));
434 	idesc.id_type = DATA;
435 	idesc.id_fix = IGNORE;
436 	cp = &namebuf[MAXPATHLEN - 1];
437 	*cp = '\0';
438 	if (curdir != ino) {
439 		idesc.id_parent = curdir;
440 		goto namelookup;
441 	}
442 	while (ino != ROOTINO) {
443 		idesc.id_number = ino;
444 		idesc.id_func = findino;
445 		idesc.id_name = "..";
446 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
447 			break;
448 	namelookup:
449 		idesc.id_number = idesc.id_parent;
450 		idesc.id_parent = ino;
451 		idesc.id_func = findname;
452 		idesc.id_name = namebuf;
453 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
454 			break;
455 		len = strlen(namebuf);
456 		cp -= len;
457 		memcpy(cp, namebuf, (size_t)len);
458 		*--cp = '/';
459 		if (cp < &namebuf[MAXNAMLEN])
460 			break;
461 		ino = idesc.id_number;
462 	}
463 	busy = 0;
464 	if (ino != ROOTINO)
465 		*--cp = '?';
466 	memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
467 }
468 
469 void
470 catch()
471 {
472 	if (!doinglevel2)
473 		ckfini(0);
474 	exit(12);
475 }
476 
477 /*
478  * When preening, allow a single quit to signal
479  * a special exit after filesystem checks complete
480  * so that reboot sequence may be interrupted.
481  */
482 void
483 catchquit()
484 {
485 	extern returntosingle;
486 
487 	printf("returning to single-user after filesystem check\n");
488 	returntosingle = 1;
489 	(void)signal(SIGQUIT, SIG_DFL);
490 }
491 
492 /*
493  * Ignore a single quit signal; wait and flush just in case.
494  * Used by child processes in preen.
495  */
496 void
497 voidquit()
498 {
499 
500 	sleep(1);
501 	(void)signal(SIGQUIT, SIG_IGN);
502 	(void)signal(SIGQUIT, SIG_DFL);
503 }
504 
505 /*
506  * determine whether an inode should be fixed.
507  */
508 int
509 dofix(idesc, msg)
510 	register struct inodesc *idesc;
511 	char *msg;
512 {
513 
514 	switch (idesc->id_fix) {
515 
516 	case DONTKNOW:
517 		if (idesc->id_type == DATA)
518 			direrror(idesc->id_number, msg);
519 		else
520 			pwarn(msg);
521 		if (preen) {
522 			printf(" (SALVAGED)\n");
523 			idesc->id_fix = FIX;
524 			return (ALTERED);
525 		}
526 		if (reply("SALVAGE") == 0) {
527 			idesc->id_fix = NOFIX;
528 			return (0);
529 		}
530 		idesc->id_fix = FIX;
531 		return (ALTERED);
532 
533 	case FIX:
534 		return (ALTERED);
535 
536 	case NOFIX:
537 	case IGNORE:
538 		return (0);
539 
540 	default:
541 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
542 	}
543 	/* NOTREACHED */
544 }
545 
546 /* VARARGS1 */
547 errexit(s1, s2, s3, s4)
548 	char *s1;
549 	long s2, s3, s4;
550 {
551 	printf(s1, s2, s3, s4);
552 	exit(8);
553 }
554 
555 /*
556  * An unexpected inconsistency occured.
557  * Die if preening, otherwise just print message and continue.
558  */
559 /* VARARGS1 */
560 pfatal(s, a1, a2, a3)
561 	char *s;
562 	long a1, a2, a3;
563 {
564 
565 	if (preen) {
566 		printf("%s: ", cdevname);
567 		printf(s, a1, a2, a3);
568 		printf("\n");
569 		printf("%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
570 			cdevname);
571 		exit(8);
572 	}
573 	printf(s, a1, a2, a3);
574 }
575 
576 /*
577  * Pwarn just prints a message when not preening,
578  * or a warning (preceded by filename) when preening.
579  */
580 /* VARARGS1 */
581 pwarn(s, a1, a2, a3, a4, a5, a6)
582 	char *s;
583 	long a1, a2, a3, a4, a5, a6;
584 {
585 
586 	if (preen)
587 		printf("%s: ", cdevname);
588 	printf(s, a1, a2, a3, a4, a5, a6);
589 }
590 
591 #ifndef lint
592 /*
593  * Stub for routines from kernel.
594  */
595 panic(s)
596 	char *s;
597 {
598 
599 	pfatal("INTERNAL INCONSISTENCY:");
600 	errexit(s);
601 }
602 #endif
603