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