utilities.c revision 7585
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
35static const char sccsid[] = "@(#)utilities.c	8.1 (Berkeley) 6/5/93";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/time.h>
40#include <ufs/ufs/dinode.h>
41#include <ufs/ufs/dir.h>
42#include <ufs/ffs/fs.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <stdarg.h>
47#include <string.h>
48#include <ctype.h>
49#include "fsck.h"
50
51long	diskreads, totalreads;	/* Disk cache statistics */
52
53static void	rwerror __P((char *mesg, daddr_t blk));
54
55int
56ftypeok(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
77int
78reply(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 */
113void
114bufinit()
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		errexit("cannot allocate buffer pool\n");
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			errexit("cannot allocate buffer pool\n");
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 */
151struct bufarea *
152getdatablk(blkno, size)
153	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		errexit("deadlocked buffer pool\n");
166	getblk(bp, blkno, size);
167	/* fall through */
168foundit:
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
180void
181getblk(bp, blk, size)
182	register struct bufarea *bp;
183	daddr_t blk;
184	long size;
185{
186	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
198void
199flush(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
224void
225rwerror(mesg, blk)
226	char *mesg;
227	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		errexit("Program terminated\n");
235}
236
237void
238ckfini()
239{
240	register struct bufarea *bp, *nbp;
241	int cnt = 0;
242
243	if (fswritefd < 0) {
244		(void)close(fsreadfd);
245		return;
246	}
247	flush(fswritefd, &sblk);
248	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
249	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
250		sblk.b_bno = SBOFF / dev_bsize;
251		sbdirty();
252		flush(fswritefd, &sblk);
253	}
254	flush(fswritefd, &cgblk);
255	free(cgblk.b_un.b_buf);
256	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
257		cnt++;
258		flush(fswritefd, bp);
259		nbp = bp->b_prev;
260		free(bp->b_un.b_buf);
261		free((char *)bp);
262	}
263	if (bufhead.b_size != cnt)
264		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
265	pbp = pdirbp = (struct bufarea *)0;
266	if (debug)
267		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
268		    totalreads, (int)(diskreads * 100 / totalreads));
269	(void)close(fsreadfd);
270	(void)close(fswritefd);
271}
272
273int
274bread(fd, buf, blk, size)
275	int fd;
276	char *buf;
277	daddr_t blk;
278	long size;
279{
280	char *cp;
281	int i, errs;
282	off_t offset;
283
284	offset = blk;
285	offset *= dev_bsize;
286	if (lseek(fd, offset, 0) < 0)
287		rwerror("SEEK", blk);
288	else if (read(fd, buf, (int)size) == size)
289		return (0);
290	rwerror("READ", blk);
291	if (lseek(fd, offset, 0) < 0)
292		rwerror("SEEK", blk);
293	errs = 0;
294	bzero(buf, (size_t)size);
295	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
296	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
297		if (read(fd, cp, (int)secsize) != secsize) {
298			(void)lseek(fd, offset + i + secsize, 0);
299			if (secsize != dev_bsize && dev_bsize != 1)
300				printf(" %ld (%ld),",
301				    (blk * dev_bsize + i) / secsize,
302				    blk + i / dev_bsize);
303			else
304				printf(" %ld,", blk + i / dev_bsize);
305			errs++;
306		}
307	}
308	printf("\n");
309	return (errs);
310}
311
312void
313bwrite(fd, buf, blk, size)
314	int fd;
315	char *buf;
316	daddr_t blk;
317	long size;
318{
319	int i;
320	char *cp;
321	off_t offset;
322
323	if (fd < 0)
324		return;
325	offset = blk;
326	offset *= dev_bsize;
327	if (lseek(fd, offset, 0) < 0)
328		rwerror("SEEK", blk);
329	else if (write(fd, buf, (int)size) == size) {
330		fsmodified = 1;
331		return;
332	}
333	rwerror("WRITE", blk);
334	if (lseek(fd, offset, 0) < 0)
335		rwerror("SEEK", blk);
336	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
337	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
338		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
339			(void)lseek(fd, offset + i + dev_bsize, 0);
340			printf(" %ld,", blk + i / dev_bsize);
341		}
342	printf("\n");
343	return;
344}
345
346/*
347 * allocate a data block with the specified number of fragments
348 */
349int
350allocblk(frags)
351	long frags;
352{
353	register int i, j, k;
354
355	if (frags <= 0 || frags > sblock.fs_frag)
356		return (0);
357	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
358		for (j = 0; j <= sblock.fs_frag - frags; j++) {
359			if (testbmap(i + j))
360				continue;
361			for (k = 1; k < frags; k++)
362				if (testbmap(i + j + k))
363					break;
364			if (k < frags) {
365				j += k;
366				continue;
367			}
368			for (k = 0; k < frags; k++)
369				setbmap(i + j + k);
370			n_blks += frags;
371			return (i + j);
372		}
373	}
374	return (0);
375}
376
377/*
378 * Free a previously allocated block
379 */
380void
381freeblk(blkno, frags)
382	daddr_t blkno;
383	long frags;
384{
385	struct inodesc idesc;
386
387	idesc.id_blkno = blkno;
388	idesc.id_numfrags = frags;
389	(void)pass4check(&idesc);
390}
391
392/*
393 * Find a pathname
394 */
395void
396getpathname(namebuf, curdir, ino)
397	char *namebuf;
398	ino_t curdir, ino;
399{
400	int len;
401	register char *cp;
402	struct inodesc idesc;
403	static int busy = 0;
404
405	if (curdir == ino && ino == ROOTINO) {
406		(void)strcpy(namebuf, "/");
407		return;
408	}
409	if (busy ||
410	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
411		(void)strcpy(namebuf, "?");
412		return;
413	}
414	busy = 1;
415	bzero((char *)&idesc, sizeof(struct inodesc));
416	idesc.id_type = DATA;
417	idesc.id_fix = IGNORE;
418	cp = &namebuf[MAXPATHLEN - 1];
419	*cp = '\0';
420	if (curdir != ino) {
421		idesc.id_parent = curdir;
422		goto namelookup;
423	}
424	while (ino != ROOTINO) {
425		idesc.id_number = ino;
426		idesc.id_func = findino;
427		idesc.id_name = "..";
428		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
429			break;
430	namelookup:
431		idesc.id_number = idesc.id_parent;
432		idesc.id_parent = ino;
433		idesc.id_func = findname;
434		idesc.id_name = namebuf;
435		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
436			break;
437		len = strlen(namebuf);
438		cp -= len;
439		bcopy(namebuf, cp, (size_t)len);
440		*--cp = '/';
441		if (cp < &namebuf[MAXNAMLEN])
442			break;
443		ino = idesc.id_number;
444	}
445	busy = 0;
446	if (ino != ROOTINO)
447		*--cp = '?';
448	bcopy(cp, namebuf, (size_t)(&namebuf[MAXPATHLEN] - cp));
449}
450
451void
452catch(x)
453	int x;
454{
455	if (!doinglevel2)
456		ckfini();
457	exit(12);
458}
459
460/*
461 * When preening, allow a single quit to signal
462 * a special exit after filesystem checks complete
463 * so that reboot sequence may be interrupted.
464 */
465void
466catchquit(x)
467	int x;
468{
469	printf("returning to single-user after filesystem check\n");
470	returntosingle = 1;
471	(void)signal(SIGQUIT, SIG_DFL);
472}
473
474/*
475 * Ignore a single quit signal; wait and flush just in case.
476 * Used by child processes in preen.
477 */
478void
479voidquit(x)
480	int x;
481{
482
483	sleep(1);
484	(void)signal(SIGQUIT, SIG_IGN);
485	(void)signal(SIGQUIT, SIG_DFL);
486}
487
488/*
489 * determine whether an inode should be fixed.
490 */
491int
492dofix(idesc, msg)
493	register struct inodesc *idesc;
494	char *msg;
495{
496
497	switch (idesc->id_fix) {
498
499	case DONTKNOW:
500		if (idesc->id_type == DATA)
501			direrror(idesc->id_number, msg);
502		else
503			pwarn(msg);
504		if (preen) {
505			printf(" (SALVAGED)\n");
506			idesc->id_fix = FIX;
507			return (ALTERED);
508		}
509		if (reply("SALVAGE") == 0) {
510			idesc->id_fix = NOFIX;
511			return (0);
512		}
513		idesc->id_fix = FIX;
514		return (ALTERED);
515
516	case FIX:
517		return (ALTERED);
518
519	case NOFIX:
520	case IGNORE:
521		return (0);
522
523	default:
524		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
525		return (0);
526	}
527	/* NOTREACHED */
528}
529
530/* VARARGS1 */
531__dead void
532errexit(const char *s1, ...)
533{
534	va_list ap;
535	va_start(ap,s1);
536	vfprintf(stdout, s1, ap);
537	va_end(ap);
538	exit(8);
539}
540
541/*
542 * An unexpected inconsistency occured.
543 * Die if preening, otherwise just print message and continue.
544 */
545/* VARARGS1 */
546void
547pfatal(const char *s, ...)
548{
549
550	va_list ap;
551	va_start(ap,s);
552	if (preen) {
553		printf("%s: ", cdevname);
554		vfprintf(stdout, s, ap);
555		printf("\n");
556		printf("%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
557			cdevname);
558		exit(8);
559	}
560	vfprintf(stdout, s, ap);
561	va_end(ap);
562}
563
564/*
565 * Pwarn just prints a message when not preening,
566 * or a warning (preceded by filename) when preening.
567 */
568/* VARARGS1 */
569void
570pwarn(const char *s, ...)
571{
572	va_list ap;
573	va_start(ap,s);
574	if (preen)
575		printf("%s: ", cdevname);
576	vfprintf(stdout, s, ap);
577	va_end(ap);
578}
579
580#ifndef lint
581/*
582 * Stub for routines from kernel.
583 */
584__dead void
585#ifdef __STDC__
586panic(const char *fmt, ...)
587#else
588panic(fmt, va_alist)
589	char *fmt;
590#endif
591{
592
593	pfatal("INTERNAL INCONSISTENCY:");
594	errexit(fmt);
595}
596#endif
597