xref: /original-bsd/sbin/fsck/utilities.c (revision ca98dac2)
1 /*
2  * Copyright (c) 1980, 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)utilities.c	5.33 (Berkeley) 07/02/92";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <ufs/ufs/dinode.h>
15 #include <ufs/ufs/dir.h>
16 #include <ufs/ffs/fs.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include "fsck.h"
22 
23 long	diskreads, totalreads;	/* Disk cache statistics */
24 long	lseek();
25 
26 ftypeok(dp)
27 	struct dinode *dp;
28 {
29 	switch (dp->di_mode & IFMT) {
30 
31 	case IFDIR:
32 	case IFREG:
33 	case IFBLK:
34 	case IFCHR:
35 	case IFLNK:
36 	case IFSOCK:
37 	case IFIFO:
38 		return (1);
39 
40 	default:
41 		if (debug)
42 			printf("bad file type 0%o\n", dp->di_mode);
43 		return (0);
44 	}
45 }
46 
47 reply(question)
48 	char *question;
49 {
50 	int persevere;
51 	char c;
52 
53 	if (preen)
54 		pfatal("INTERNAL ERROR: GOT TO reply()");
55 	persevere = !strcmp(question, "CONTINUE");
56 	printf("\n");
57 	if (!persevere && (nflag || fswritefd < 0)) {
58 		printf("%s? no\n\n", question);
59 		return (0);
60 	}
61 	if (yflag || (persevere && nflag)) {
62 		printf("%s? yes\n\n", question);
63 		return (1);
64 	}
65 	do	{
66 		printf("%s? [yn] ", question);
67 		(void) fflush(stdout);
68 		c = getc(stdin);
69 		while (c != '\n' && getc(stdin) != '\n')
70 			if (feof(stdin))
71 				return (0);
72 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
73 	printf("\n");
74 	if (c == 'y' || c == 'Y')
75 		return (1);
76 	return (0);
77 }
78 
79 /*
80  * Malloc buffers and set up cache.
81  */
82 bufinit()
83 {
84 	register struct bufarea *bp;
85 	long bufcnt, i;
86 	char *bufp;
87 
88 	pbp = pdirbp = (struct bufarea *)0;
89 	bufp = malloc((unsigned int)sblock.fs_bsize);
90 	if (bufp == 0)
91 		errexit("cannot allocate buffer pool\n");
92 	cgblk.b_un.b_buf = bufp;
93 	initbarea(&cgblk);
94 	bufhead.b_next = bufhead.b_prev = &bufhead;
95 	bufcnt = MAXBUFSPACE / sblock.fs_bsize;
96 	if (bufcnt < MINBUFS)
97 		bufcnt = MINBUFS;
98 	for (i = 0; i < bufcnt; i++) {
99 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
100 		bufp = malloc((unsigned int)sblock.fs_bsize);
101 		if (bp == NULL || bufp == NULL) {
102 			if (i >= MINBUFS)
103 				break;
104 			errexit("cannot allocate buffer pool\n");
105 		}
106 		bp->b_un.b_buf = bufp;
107 		bp->b_prev = &bufhead;
108 		bp->b_next = bufhead.b_next;
109 		bufhead.b_next->b_prev = bp;
110 		bufhead.b_next = bp;
111 		initbarea(bp);
112 	}
113 	bufhead.b_size = i;	/* save number of buffers */
114 }
115 
116 /*
117  * Manage a cache of directory blocks.
118  */
119 struct bufarea *
120 getdatablk(blkno, size)
121 	daddr_t blkno;
122 	long size;
123 {
124 	register struct bufarea *bp;
125 
126 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
127 		if (bp->b_bno == fsbtodb(&sblock, blkno))
128 			goto foundit;
129 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
130 		if ((bp->b_flags & B_INUSE) == 0)
131 			break;
132 	if (bp == &bufhead)
133 		errexit("deadlocked buffer pool\n");
134 	getblk(bp, blkno, size);
135 	/* fall through */
136 foundit:
137 	totalreads++;
138 	bp->b_prev->b_next = bp->b_next;
139 	bp->b_next->b_prev = bp->b_prev;
140 	bp->b_prev = &bufhead;
141 	bp->b_next = bufhead.b_next;
142 	bufhead.b_next->b_prev = bp;
143 	bufhead.b_next = bp;
144 	bp->b_flags |= B_INUSE;
145 	return (bp);
146 }
147 
148 void
149 getblk(bp, blk, size)
150 	register struct bufarea *bp;
151 	daddr_t blk;
152 	long size;
153 {
154 	daddr_t dblk;
155 
156 	dblk = fsbtodb(&sblock, blk);
157 	if (bp->b_bno != dblk) {
158 		flush(fswritefd, bp);
159 		diskreads++;
160 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
161 		bp->b_bno = dblk;
162 		bp->b_size = size;
163 	}
164 }
165 
166 flush(fd, bp)
167 	int fd;
168 	register struct bufarea *bp;
169 {
170 	register int i, j;
171 
172 	if (!bp->b_dirty)
173 		return;
174 	if (bp->b_errs != 0)
175 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
176 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
177 		    bp->b_bno);
178 	bp->b_dirty = 0;
179 	bp->b_errs = 0;
180 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
181 	if (bp != &sblk)
182 		return;
183 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
184 		bwrite(fswritefd, (char *)sblock.fs_csp[j],
185 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
186 		    sblock.fs_cssize - i < sblock.fs_bsize ?
187 		    sblock.fs_cssize - i : sblock.fs_bsize);
188 	}
189 }
190 
191 rwerror(mesg, blk)
192 	char *mesg;
193 	daddr_t blk;
194 {
195 
196 	if (preen == 0)
197 		printf("\n");
198 	pfatal("CANNOT %s: BLK %ld", mesg, blk);
199 	if (reply("CONTINUE") == 0)
200 		errexit("Program terminated\n");
201 }
202 
203 ckfini()
204 {
205 	register struct bufarea *bp, *nbp;
206 	int cnt = 0;
207 
208 	flush(fswritefd, &sblk);
209 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
210 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
211 		sblk.b_bno = SBOFF / dev_bsize;
212 		sbdirty();
213 		flush(fswritefd, &sblk);
214 	}
215 	flush(fswritefd, &cgblk);
216 	free(cgblk.b_un.b_buf);
217 	for (bp = bufhead.b_prev; bp != &bufhead; bp = nbp) {
218 		cnt++;
219 		flush(fswritefd, bp);
220 		nbp = bp->b_prev;
221 		free(bp->b_un.b_buf);
222 		free((char *)bp);
223 	}
224 	if (bufhead.b_size != cnt)
225 		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
226 	pbp = pdirbp = (struct bufarea *)0;
227 	if (debug)
228 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
229 		    totalreads, (int)(diskreads * 100 / totalreads));
230 	(void)close(fsreadfd);
231 	(void)close(fswritefd);
232 }
233 
234 bread(fd, buf, blk, size)
235 	int fd;
236 	char *buf;
237 	daddr_t blk;
238 	long size;
239 {
240 	char *cp;
241 	int i, errs;
242 
243 	if (lseek(fd, blk * dev_bsize, 0) < 0)
244 		rwerror("SEEK", blk);
245 	else if (read(fd, buf, (int)size) == size)
246 		return (0);
247 	rwerror("READ", blk);
248 	if (lseek(fd, blk * dev_bsize, 0) < 0)
249 		rwerror("SEEK", blk);
250 	errs = 0;
251 	bzero(buf, (size_t)size);
252 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
253 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
254 		if (read(fd, cp, (int)secsize) != secsize) {
255 			(void)lseek(fd, blk * dev_bsize + i + secsize, 0);
256 			if (secsize != dev_bsize && dev_bsize != 1)
257 				printf(" %ld (%ld),",
258 				    (blk * dev_bsize + i) / secsize,
259 				    blk + i / dev_bsize);
260 			else
261 				printf(" %ld,", blk + i / dev_bsize);
262 			errs++;
263 		}
264 	}
265 	printf("\n");
266 	return (errs);
267 }
268 
269 bwrite(fd, buf, blk, size)
270 	int fd;
271 	char *buf;
272 	daddr_t blk;
273 	long size;
274 {
275 	int i;
276 	char *cp;
277 
278 	if (fd < 0)
279 		return;
280 	if (lseek(fd, blk * dev_bsize, 0) < 0)
281 		rwerror("SEEK", blk);
282 	else if (write(fd, buf, (int)size) == size) {
283 		fsmodified = 1;
284 		return;
285 	}
286 	rwerror("WRITE", blk);
287 	if (lseek(fd, blk * dev_bsize, 0) < 0)
288 		rwerror("SEEK", blk);
289 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
290 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
291 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
292 			(void)lseek(fd, blk * dev_bsize + i + dev_bsize, 0);
293 			printf(" %ld,", blk + i / dev_bsize);
294 		}
295 	printf("\n");
296 	return;
297 }
298 
299 /*
300  * allocate a data block with the specified number of fragments
301  */
302 allocblk(frags)
303 	long frags;
304 {
305 	register int i, j, k;
306 
307 	if (frags <= 0 || frags > sblock.fs_frag)
308 		return (0);
309 	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
310 		for (j = 0; j <= sblock.fs_frag - frags; j++) {
311 			if (testbmap(i + j))
312 				continue;
313 			for (k = 1; k < frags; k++)
314 				if (testbmap(i + j + k))
315 					break;
316 			if (k < frags) {
317 				j += k;
318 				continue;
319 			}
320 			for (k = 0; k < frags; k++)
321 				setbmap(i + j + k);
322 			n_blks += frags;
323 			return (i + j);
324 		}
325 	}
326 	return (0);
327 }
328 
329 /*
330  * Free a previously allocated block
331  */
332 freeblk(blkno, frags)
333 	daddr_t blkno;
334 	long frags;
335 {
336 	struct inodesc idesc;
337 
338 	idesc.id_blkno = blkno;
339 	idesc.id_numfrags = frags;
340 	(void)pass4check(&idesc);
341 }
342 
343 /*
344  * Find a pathname
345  */
346 getpathname(namebuf, curdir, ino)
347 	char *namebuf;
348 	ino_t curdir, ino;
349 {
350 	int len;
351 	register char *cp;
352 	struct inodesc idesc;
353 	static int busy = 0;
354 	extern int findname();
355 
356 	if (curdir == ino && ino == ROOTINO) {
357 		(void)strcpy(namebuf, "/");
358 		return;
359 	}
360 	if (busy ||
361 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
362 		(void)strcpy(namebuf, "?");
363 		return;
364 	}
365 	busy = 1;
366 	bzero((char *)&idesc, sizeof(struct inodesc));
367 	idesc.id_type = DATA;
368 	idesc.id_fix = IGNORE;
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 		bcopy(namebuf, cp, (size_t)len);
391 		*--cp = '/';
392 		if (cp < &namebuf[MAXNAMLEN])
393 			break;
394 		ino = idesc.id_number;
395 	}
396 	busy = 0;
397 	if (ino != ROOTINO)
398 		*--cp = '?';
399 	bcopy(cp, namebuf, (size_t)(&namebuf[MAXPATHLEN] - cp));
400 }
401 
402 void
403 catch()
404 {
405 	if (!doinglevel2)
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 	case IGNORE:
470 		return (0);
471 
472 	default:
473 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
474 	}
475 	/* NOTREACHED */
476 }
477 
478 /* VARARGS1 */
479 errexit(s1, s2, s3, s4)
480 	char *s1;
481 {
482 	printf(s1, s2, s3, s4);
483 	exit(8);
484 }
485 
486 /*
487  * An unexpected inconsistency occured.
488  * Die if preening, otherwise just print message and continue.
489  */
490 /* VARARGS1 */
491 pfatal(s, a1, a2, a3)
492 	char *s;
493 {
494 
495 	if (preen) {
496 		printf("%s: ", devname);
497 		printf(s, a1, a2, a3);
498 		printf("\n");
499 		printf("%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
500 			devname);
501 		exit(8);
502 	}
503 	printf(s, a1, a2, a3);
504 }
505 
506 /*
507  * Pwarn just prints a message when not preening,
508  * or a warning (preceded by filename) when preening.
509  */
510 /* VARARGS1 */
511 pwarn(s, a1, a2, a3, a4, a5, a6)
512 	char *s;
513 {
514 
515 	if (preen)
516 		printf("%s: ", devname);
517 	printf(s, a1, a2, a3, a4, a5, a6);
518 }
519 
520 #ifndef lint
521 /*
522  * Stub for routines from kernel.
523  */
524 panic(s)
525 	char *s;
526 {
527 
528 	pfatal("INTERNAL INCONSISTENCY:");
529 	errexit(s);
530 }
531 #endif
532