xref: /freebsd/sbin/fsck_ffs/utilities.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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 static const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <sys/time.h>
40 
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
43 #include <ufs/ffs/fs.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <string.h>
48 
49 #include "fsck.h"
50 
51 long	diskreads, totalreads;	/* Disk cache statistics */
52 
53 static void rwerror __P((char *mesg, ufs_daddr_t blk));
54 
55 int
56 ftypeok(dp)
57 	struct dinode *dp;
58 {
59 	switch (dp->di_mode & IFMT) {
60 
61 	case IFDIR:
62 	case IFREG:
63 	case IFBLK:
64 	case IFCHR:
65 	case IFLNK:
66 	case IFSOCK:
67 	case IFIFO:
68 		return (1);
69 
70 	default:
71 		if (debug)
72 			printf("bad file type 0%o\n", dp->di_mode);
73 		return (0);
74 	}
75 }
76 
77 int
78 reply(question)
79 	char *question;
80 {
81 	int persevere;
82 	char c;
83 
84 	if (preen)
85 		pfatal("INTERNAL ERROR: GOT TO reply()");
86 	persevere = !strcmp(question, "CONTINUE");
87 	printf("\n");
88 	if (!persevere && (nflag || fswritefd < 0)) {
89 		printf("%s? no\n\n", question);
90 		return (0);
91 	}
92 	if (yflag || (persevere && nflag)) {
93 		printf("%s? yes\n\n", question);
94 		return (1);
95 	}
96 	do	{
97 		printf("%s? [yn] ", question);
98 		(void) fflush(stdout);
99 		c = getc(stdin);
100 		while (c != '\n' && getc(stdin) != '\n')
101 			if (feof(stdin))
102 				return (0);
103 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
104 	printf("\n");
105 	if (c == 'y' || c == 'Y')
106 		return (1);
107 	return (0);
108 }
109 
110 /*
111  * Malloc buffers and set up cache.
112  */
113 void
114 bufinit()
115 {
116 	register struct bufarea *bp;
117 	long bufcnt, i;
118 	char *bufp;
119 
120 	pbp = pdirbp = (struct bufarea *)0;
121 	bufp = malloc((unsigned int)sblock.fs_bsize);
122 	if (bufp == 0)
123 		errx(EEXIT, "cannot allocate buffer pool");
124 	cgblk.b_un.b_buf = bufp;
125 	initbarea(&cgblk);
126 	bufhead.b_next = bufhead.b_prev = &bufhead;
127 	bufcnt = MAXBUFSPACE / sblock.fs_bsize;
128 	if (bufcnt < MINBUFS)
129 		bufcnt = MINBUFS;
130 	for (i = 0; i < bufcnt; i++) {
131 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
132 		bufp = malloc((unsigned int)sblock.fs_bsize);
133 		if (bp == NULL || bufp == NULL) {
134 			if (i >= MINBUFS)
135 				break;
136 			errx(EEXIT, "cannot allocate buffer pool");
137 		}
138 		bp->b_un.b_buf = bufp;
139 		bp->b_prev = &bufhead;
140 		bp->b_next = bufhead.b_next;
141 		bufhead.b_next->b_prev = bp;
142 		bufhead.b_next = bp;
143 		initbarea(bp);
144 	}
145 	bufhead.b_size = i;	/* save number of buffers */
146 }
147 
148 /*
149  * Manage a cache of directory blocks.
150  */
151 struct bufarea *
152 getdatablk(blkno, size)
153 	ufs_daddr_t blkno;
154 	long size;
155 {
156 	register struct bufarea *bp;
157 
158 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
159 		if (bp->b_bno == fsbtodb(&sblock, blkno))
160 			goto foundit;
161 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
162 		if ((bp->b_flags & B_INUSE) == 0)
163 			break;
164 	if (bp == &bufhead)
165 		errx(EEXIT, "deadlocked buffer pool");
166 	getblk(bp, blkno, size);
167 	/* fall through */
168 foundit:
169 	totalreads++;
170 	bp->b_prev->b_next = bp->b_next;
171 	bp->b_next->b_prev = bp->b_prev;
172 	bp->b_prev = &bufhead;
173 	bp->b_next = bufhead.b_next;
174 	bufhead.b_next->b_prev = bp;
175 	bufhead.b_next = bp;
176 	bp->b_flags |= B_INUSE;
177 	return (bp);
178 }
179 
180 void
181 getblk(bp, blk, size)
182 	register struct bufarea *bp;
183 	ufs_daddr_t blk;
184 	long size;
185 {
186 	ufs_daddr_t dblk;
187 
188 	dblk = fsbtodb(&sblock, blk);
189 	if (bp->b_bno != dblk) {
190 		flush(fswritefd, bp);
191 		diskreads++;
192 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
193 		bp->b_bno = dblk;
194 		bp->b_size = size;
195 	}
196 }
197 
198 void
199 flush(fd, bp)
200 	int fd;
201 	register struct bufarea *bp;
202 {
203 	register int i, j;
204 
205 	if (!bp->b_dirty)
206 		return;
207 	if (bp->b_errs != 0)
208 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
209 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
210 		    bp->b_bno);
211 	bp->b_dirty = 0;
212 	bp->b_errs = 0;
213 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
214 	if (bp != &sblk)
215 		return;
216 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
217 		bwrite(fswritefd, (char *)sblock.fs_csp[j],
218 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
219 		    sblock.fs_cssize - i < sblock.fs_bsize ?
220 		    sblock.fs_cssize - i : sblock.fs_bsize);
221 	}
222 }
223 
224 static void
225 rwerror(mesg, blk)
226 	char *mesg;
227 	ufs_daddr_t blk;
228 {
229 
230 	if (preen == 0)
231 		printf("\n");
232 	pfatal("CANNOT %s: BLK %ld", mesg, blk);
233 	if (reply("CONTINUE") == 0)
234 		exit(EEXIT);
235 }
236 
237 void
238 ckfini(markclean)
239 	int markclean;
240 {
241 	register struct bufarea *bp, *nbp;
242 	int ofsmodified, cnt = 0;
243 
244 	if (fswritefd < 0) {
245 		(void)close(fsreadfd);
246 		return;
247 	}
248 	flush(fswritefd, &sblk);
249 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
250 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
251 		sblk.b_bno = SBOFF / dev_bsize;
252 		sbdirty();
253 		flush(fswritefd, &sblk);
254 	}
255 	flush(fswritefd, &cgblk);
256 	free(cgblk.b_un.b_buf);
257 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
258 		cnt++;
259 		flush(fswritefd, bp);
260 		nbp = bp->b_prev;
261 		free(bp->b_un.b_buf);
262 		free((char *)bp);
263 	}
264 	if (bufhead.b_size != cnt)
265 		errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
266 	pbp = pdirbp = (struct bufarea *)0;
267 	if (markclean && sblock.fs_clean == 0) {
268 		sblock.fs_clean = 1;
269 		sbdirty();
270 		ofsmodified = fsmodified;
271 		flush(fswritefd, &sblk);
272 		fsmodified = ofsmodified;
273 		if (!preen)
274 			printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
275 	}
276 	if (debug)
277 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
278 		    totalreads, (int)(diskreads * 100 / totalreads));
279 	(void)close(fsreadfd);
280 	(void)close(fswritefd);
281 }
282 
283 int
284 bread(fd, buf, blk, size)
285 	int fd;
286 	char *buf;
287 	ufs_daddr_t blk;
288 	long size;
289 {
290 	char *cp;
291 	int i, errs;
292 	off_t offset;
293 
294 	offset = blk;
295 	offset *= dev_bsize;
296 	if (lseek(fd, offset, 0) < 0)
297 		rwerror("SEEK", blk);
298 	else if (read(fd, buf, (int)size) == size)
299 		return (0);
300 	rwerror("READ", blk);
301 	if (lseek(fd, offset, 0) < 0)
302 		rwerror("SEEK", blk);
303 	errs = 0;
304 	memset(buf, 0, (size_t)size);
305 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
306 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
307 		if (read(fd, cp, (int)secsize) != secsize) {
308 			(void)lseek(fd, offset + i + secsize, 0);
309 			if (secsize != dev_bsize && dev_bsize != 1)
310 				printf(" %ld (%ld),",
311 				    (blk * dev_bsize + i) / secsize,
312 				    blk + i / dev_bsize);
313 			else
314 				printf(" %ld,", blk + i / dev_bsize);
315 			errs++;
316 		}
317 	}
318 	printf("\n");
319 	return (errs);
320 }
321 
322 void
323 bwrite(fd, buf, blk, size)
324 	int fd;
325 	char *buf;
326 	ufs_daddr_t blk;
327 	long size;
328 {
329 	int i;
330 	char *cp;
331 	off_t offset;
332 
333 	if (fd < 0)
334 		return;
335 	offset = blk;
336 	offset *= dev_bsize;
337 	if (lseek(fd, offset, 0) < 0)
338 		rwerror("SEEK", blk);
339 	else if (write(fd, buf, (int)size) == size) {
340 		fsmodified = 1;
341 		return;
342 	}
343 	rwerror("WRITE", blk);
344 	if (lseek(fd, offset, 0) < 0)
345 		rwerror("SEEK", blk);
346 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
347 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
348 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
349 			(void)lseek(fd, offset + i + dev_bsize, 0);
350 			printf(" %ld,", blk + i / dev_bsize);
351 		}
352 	printf("\n");
353 	return;
354 }
355 
356 /*
357  * allocate a data block with the specified number of fragments
358  */
359 ufs_daddr_t
360 allocblk(frags)
361 	long frags;
362 {
363 	register int i, j, k;
364 
365 	if (frags <= 0 || frags > sblock.fs_frag)
366 		return (0);
367 	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
368 		for (j = 0; j <= sblock.fs_frag - frags; j++) {
369 			if (testbmap(i + j))
370 				continue;
371 			for (k = 1; k < frags; k++)
372 				if (testbmap(i + j + k))
373 					break;
374 			if (k < frags) {
375 				j += k;
376 				continue;
377 			}
378 			for (k = 0; k < frags; k++)
379 				setbmap(i + j + k);
380 			n_blks += frags;
381 			return (i + j);
382 		}
383 	}
384 	return (0);
385 }
386 
387 /*
388  * Free a previously allocated block
389  */
390 void
391 freeblk(blkno, frags)
392 	ufs_daddr_t blkno;
393 	long frags;
394 {
395 	struct inodesc idesc;
396 
397 	idesc.id_blkno = blkno;
398 	idesc.id_numfrags = frags;
399 	(void)pass4check(&idesc);
400 }
401 
402 /*
403  * Find a pathname
404  */
405 void
406 getpathname(namebuf, curdir, ino)
407 	char *namebuf;
408 	ino_t curdir, ino;
409 {
410 	int len;
411 	register char *cp;
412 	struct inodesc idesc;
413 	static int busy = 0;
414 
415 	if (curdir == ino && ino == ROOTINO) {
416 		(void)strcpy(namebuf, "/");
417 		return;
418 	}
419 	if (busy ||
420 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
421 		(void)strcpy(namebuf, "?");
422 		return;
423 	}
424 	busy = 1;
425 	memset(&idesc, 0, sizeof(struct inodesc));
426 	idesc.id_type = DATA;
427 	idesc.id_fix = IGNORE;
428 	cp = &namebuf[MAXPATHLEN - 1];
429 	*cp = '\0';
430 	if (curdir != ino) {
431 		idesc.id_parent = curdir;
432 		goto namelookup;
433 	}
434 	while (ino != ROOTINO) {
435 		idesc.id_number = ino;
436 		idesc.id_func = findino;
437 		idesc.id_name = "..";
438 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
439 			break;
440 	namelookup:
441 		idesc.id_number = idesc.id_parent;
442 		idesc.id_parent = ino;
443 		idesc.id_func = findname;
444 		idesc.id_name = namebuf;
445 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
446 			break;
447 		len = strlen(namebuf);
448 		cp -= len;
449 		memmove(cp, namebuf, (size_t)len);
450 		*--cp = '/';
451 		if (cp < &namebuf[MAXNAMLEN])
452 			break;
453 		ino = idesc.id_number;
454 	}
455 	busy = 0;
456 	if (ino != ROOTINO)
457 		*--cp = '?';
458 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
459 }
460 
461 void
462 catch(sig)
463 	int sig;
464 {
465 	if (!doinglevel2)
466 		ckfini(0);
467 	exit(12);
468 }
469 
470 /*
471  * When preening, allow a single quit to signal
472  * a special exit after filesystem checks complete
473  * so that reboot sequence may be interrupted.
474  */
475 void
476 catchquit(sig)
477 	int sig;
478 {
479 	printf("returning to single-user after filesystem check\n");
480 	returntosingle = 1;
481 	(void)signal(SIGQUIT, SIG_DFL);
482 }
483 
484 /*
485  * Ignore a single quit signal; wait and flush just in case.
486  * Used by child processes in preen.
487  */
488 void
489 voidquit(sig)
490 	int sig;
491 {
492 
493 	sleep(1);
494 	(void)signal(SIGQUIT, SIG_IGN);
495 	(void)signal(SIGQUIT, SIG_DFL);
496 }
497 
498 /*
499  * determine whether an inode should be fixed.
500  */
501 int
502 dofix(idesc, msg)
503 	register struct inodesc *idesc;
504 	char *msg;
505 {
506 
507 	switch (idesc->id_fix) {
508 
509 	case DONTKNOW:
510 		if (idesc->id_type == DATA)
511 			direrror(idesc->id_number, msg);
512 		else
513 			pwarn(msg);
514 		if (preen) {
515 			printf(" (SALVAGED)\n");
516 			idesc->id_fix = FIX;
517 			return (ALTERED);
518 		}
519 		if (reply("SALVAGE") == 0) {
520 			idesc->id_fix = NOFIX;
521 			return (0);
522 		}
523 		idesc->id_fix = FIX;
524 		return (ALTERED);
525 
526 	case FIX:
527 		return (ALTERED);
528 
529 	case NOFIX:
530 	case IGNORE:
531 		return (0);
532 
533 	default:
534 		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
535 	}
536 	/* NOTREACHED */
537 	return (0);
538 }
539 
540 #if __STDC__
541 #include <stdarg.h>
542 #else
543 #include <varargs.h>
544 #endif
545 
546 /*
547  * An unexpected inconsistency occured.
548  * Die if preening, otherwise just print message and continue.
549  */
550 void
551 #if __STDC__
552 pfatal(const char *fmt, ...)
553 #else
554 pfatal(fmt, va_alist)
555 	char *fmt;
556 	va_dcl
557 #endif
558 {
559 	va_list ap;
560 #if __STDC__
561 	va_start(ap, fmt);
562 #else
563 	va_start(ap);
564 #endif
565 	if (!preen) {
566 		(void)vfprintf(stderr, fmt, ap);
567 		va_end(ap);
568 		return;
569 	}
570 	(void)fprintf(stderr, "%s: ", cdevname);
571 	(void)vfprintf(stderr, fmt, ap);
572 	(void)fprintf(stderr,
573 	    "\n%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
574 	    cdevname);
575 	exit(EEXIT);
576 }
577 
578 /*
579  * Pwarn just prints a message when not preening,
580  * or a warning (preceded by filename) when preening.
581  */
582 void
583 #if __STDC__
584 pwarn(const char *fmt, ...)
585 #else
586 pwarn(fmt, va_alist)
587 	char *fmt;
588 	va_dcl
589 #endif
590 {
591 	va_list ap;
592 #if __STDC__
593 	va_start(ap, fmt);
594 #else
595 	va_start(ap);
596 #endif
597 	if (preen)
598 		(void)fprintf(stderr, "%s: ", cdevname);
599 	(void)vfprintf(stderr, fmt, ap);
600 	va_end(ap);
601 }
602 
603 /*
604  * Stub for routines from kernel.
605  */
606 void
607 #if __STDC__
608 panic(const char *fmt, ...)
609 #else
610 panic(fmt, va_alist)
611 	char *fmt;
612 	va_dcl
613 #endif
614 {
615 	va_list ap;
616 #if __STDC__
617 	va_start(ap, fmt);
618 #else
619 	va_start(ap);
620 #endif
621 	pfatal("INTERNAL INCONSISTENCY:");
622 	(void)vfprintf(stderr, fmt, ap);
623 	va_end(ap);
624 	exit(EEXIT);
625 }
626