xref: /original-bsd/sbin/fsck/utilities.c (revision 3b6250d9)
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.32 (Berkeley) 05/27/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 (busy ||
357 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
358 		(void)strcpy(namebuf, "?");
359 		return;
360 	}
361 	busy = 1;
362 	bzero((char *)&idesc, sizeof(struct inodesc));
363 	idesc.id_type = DATA;
364 	idesc.id_fix = IGNORE;
365 	cp = &namebuf[MAXPATHLEN - 1];
366 	*cp = '\0';
367 	if (curdir != ino) {
368 		idesc.id_parent = curdir;
369 		goto namelookup;
370 	}
371 	while (ino != ROOTINO) {
372 		idesc.id_number = ino;
373 		idesc.id_func = findino;
374 		idesc.id_name = "..";
375 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
376 			break;
377 	namelookup:
378 		idesc.id_number = idesc.id_parent;
379 		idesc.id_parent = ino;
380 		idesc.id_func = findname;
381 		idesc.id_name = namebuf;
382 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
383 			break;
384 		len = strlen(namebuf);
385 		cp -= len;
386 		bcopy(namebuf, cp, (size_t)len);
387 		*--cp = '/';
388 		if (cp < &namebuf[MAXNAMLEN])
389 			break;
390 		ino = idesc.id_number;
391 	}
392 	busy = 0;
393 	if (ino != ROOTINO)
394 		*--cp = '?';
395 	bcopy(cp, namebuf, (size_t)(&namebuf[MAXPATHLEN] - cp));
396 }
397 
398 void
399 catch()
400 {
401 	ckfini();
402 	exit(12);
403 }
404 
405 /*
406  * When preening, allow a single quit to signal
407  * a special exit after filesystem checks complete
408  * so that reboot sequence may be interrupted.
409  */
410 void
411 catchquit()
412 {
413 	extern returntosingle;
414 
415 	printf("returning to single-user after filesystem check\n");
416 	returntosingle = 1;
417 	(void)signal(SIGQUIT, SIG_DFL);
418 }
419 
420 /*
421  * Ignore a single quit signal; wait and flush just in case.
422  * Used by child processes in preen.
423  */
424 void
425 voidquit()
426 {
427 
428 	sleep(1);
429 	(void)signal(SIGQUIT, SIG_IGN);
430 	(void)signal(SIGQUIT, SIG_DFL);
431 }
432 
433 /*
434  * determine whether an inode should be fixed.
435  */
436 dofix(idesc, msg)
437 	register struct inodesc *idesc;
438 	char *msg;
439 {
440 
441 	switch (idesc->id_fix) {
442 
443 	case DONTKNOW:
444 		if (idesc->id_type == DATA)
445 			direrror(idesc->id_number, msg);
446 		else
447 			pwarn(msg);
448 		if (preen) {
449 			printf(" (SALVAGED)\n");
450 			idesc->id_fix = FIX;
451 			return (ALTERED);
452 		}
453 		if (reply("SALVAGE") == 0) {
454 			idesc->id_fix = NOFIX;
455 			return (0);
456 		}
457 		idesc->id_fix = FIX;
458 		return (ALTERED);
459 
460 	case FIX:
461 		return (ALTERED);
462 
463 	case NOFIX:
464 	case IGNORE:
465 		return (0);
466 
467 	default:
468 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
469 	}
470 	/* NOTREACHED */
471 }
472 
473 /* VARARGS1 */
474 errexit(s1, s2, s3, s4)
475 	char *s1;
476 {
477 	printf(s1, s2, s3, s4);
478 	exit(8);
479 }
480 
481 /*
482  * An unexpected inconsistency occured.
483  * Die if preening, otherwise just print message and continue.
484  */
485 /* VARARGS1 */
486 pfatal(s, a1, a2, a3)
487 	char *s;
488 {
489 
490 	if (preen) {
491 		printf("%s: ", devname);
492 		printf(s, a1, a2, a3);
493 		printf("\n");
494 		printf("%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
495 			devname);
496 		exit(8);
497 	}
498 	printf(s, a1, a2, a3);
499 }
500 
501 /*
502  * Pwarn just prints a message when not preening,
503  * or a warning (preceded by filename) when preening.
504  */
505 /* VARARGS1 */
506 pwarn(s, a1, a2, a3, a4, a5, a6)
507 	char *s;
508 {
509 
510 	if (preen)
511 		printf("%s: ", devname);
512 	printf(s, a1, a2, a3, a4, a5, a6);
513 }
514 
515 #ifndef lint
516 /*
517  * Stub for routines from kernel.
518  */
519 panic(s)
520 	char *s;
521 {
522 
523 	pfatal("INTERNAL INCONSISTENCY:");
524 	errexit(s);
525 }
526 #endif
527