utilities.c revision 23799
1213496Scognet/*
2213496Scognet * Copyright (c) 1980, 1986, 1993
3213496Scognet *	The Regents of the University of California.  All rights reserved.
4213496Scognet *
5213496Scognet * Redistribution and use in source and binary forms, with or without
6213496Scognet * modification, are permitted provided that the following conditions
7213496Scognet * are met:
8213496Scognet * 1. Redistributions of source code must retain the above copyright
9213496Scognet *    notice, this list of conditions and the following disclaimer.
10213496Scognet * 2. Redistributions in binary form must reproduce the above copyright
11213496Scognet *    notice, this list of conditions and the following disclaimer in the
12213496Scognet *    documentation and/or other materials provided with the distribution.
13213496Scognet * 3. All advertising materials mentioning features or use of this software
14213496Scognet *    must display the following acknowledgement:
15213496Scognet *	This product includes software developed by the University of
16213496Scognet *	California, Berkeley and its contributors.
17213496Scognet * 4. Neither the name of the University nor the names of its contributors
18213496Scognet *    may be used to endorse or promote products derived from this software
19213496Scognet *    without specific prior written permission.
20213496Scognet *
21213496Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22213496Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23213496Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24213496Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25213496Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26213496Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27213496Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28213496Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29213496Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30213496Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31213496Scognet * SUCH DAMAGE.
32213496Scognet */
33213496Scognet
34213496Scognet#ifndef lint
35213496Scognetstatic const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
36213496Scognet#endif /* not lint */
37213496Scognet
38213496Scognet#include <sys/param.h>
39213496Scognet#include <sys/time.h>
40213496Scognet
41213496Scognet#include <ufs/ufs/dinode.h>
42213496Scognet#include <ufs/ufs/dir.h>
43234281Smarius#include <ufs/ffs/fs.h>
44213496Scognet
45238369Simp#include <ctype.h>
46213496Scognet#include <err.h>
47238369Simp#include <string.h>
48213496Scognet
49213496Scognet#include "fsck.h"
50213496Scognet
51213496Scognetlong	diskreads, totalreads;	/* Disk cache statistics */
52213496Scognet
53213496Scognetstatic void rwerror __P((char *mesg, ufs_daddr_t blk));
54238369Simp
55213496Scognetint
56213496Scognetftypeok(dp)
57238369Simp	struct dinode *dp;
58213496Scognet{
59213496Scognet	switch (dp->di_mode & IFMT) {
60213496Scognet
61213496Scognet	case IFDIR:
62213496Scognet	case IFREG:
63213496Scognet	case IFBLK:
64238369Simp	case IFCHR:
65213496Scognet	case IFLNK:
66213496Scognet	case IFSOCK:
67213496Scognet	case IFIFO:
68213496Scognet		return (1);
69213496Scognet
70237130Simp	default:
71237130Simp		if (debug)
72237130Simp			printf("bad file type 0%o\n", dp->di_mode);
73238369Simp		return (0);
74238369Simp	}
75213496Scognet}
76213496Scognet
77238369Simpint
78237130Simpreply(question)
79237130Simp	char *question;
80238369Simp{
81237130Simp	int persevere;
82237130Simp	char c;
83238369Simp
84237130Simp	if (preen)
85237130Simp		pfatal("INTERNAL ERROR: GOT TO reply()");
86237130Simp	persevere = !strcmp(question, "CONTINUE");
87237130Simp	printf("\n");
88213496Scognet	if (!persevere && (nflag || fswritefd < 0)) {
89237130Simp		printf("%s? no\n\n", question);
90237130Simp		return (0);
91213496Scognet	}
92213496Scognet	if (yflag || (persevere && nflag)) {
93213496Scognet		printf("%s? yes\n\n", question);
94238369Simp		return (1);
95213496Scognet	}
96237130Simp	do	{
97237130Simp		printf("%s? [yn] ", question);
98237130Simp		(void) fflush(stdout);
99237130Simp		c = getc(stdin);
100237130Simp		while (c != '\n' && getc(stdin) != '\n')
101237130Simp			if (feof(stdin))
102238369Simp				return (0);
103237130Simp	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
104238369Simp	printf("\n");
105213496Scognet	if (c == 'y' || c == 'Y')
106213496Scognet		return (1);
107213496Scognet	return (0);
108238369Simp}
109213496Scognet
110213496Scognet/*
111213496Scognet * Malloc buffers and set up cache.
112213496Scognet */
113213496Scognetvoid
114213496Scognetbufinit()
115213496Scognet{
116213496Scognet	register struct bufarea *bp;
117213496Scognet	long bufcnt, i;
118213496Scognet	char *bufp;
119213496Scognet
120213496Scognet	pbp = pdirbp = (struct bufarea *)0;
121213496Scognet	bufp = malloc((unsigned int)sblock.fs_bsize);
122213496Scognet	if (bufp == 0)
123213496Scognet		errx(EEXIT, "cannot allocate buffer pool");
124213496Scognet	cgblk.b_un.b_buf = bufp;
125213496Scognet	initbarea(&cgblk);
126213496Scognet	bufhead.b_next = bufhead.b_prev = &bufhead;
127213496Scognet	bufcnt = MAXBUFSPACE / sblock.fs_bsize;
128213496Scognet	if (bufcnt < MINBUFS)
129213496Scognet		bufcnt = MINBUFS;
130213496Scognet	for (i = 0; i < bufcnt; i++) {
131213496Scognet		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
132238369Simp		bufp = malloc((unsigned int)sblock.fs_bsize);
133213496Scognet		if (bp == NULL || bufp == NULL) {
134213496Scognet			if (i >= MINBUFS)
135213496Scognet				break;
136238369Simp			errx(EEXIT, "cannot allocate buffer pool");
137213496Scognet		}
138213496Scognet		bp->b_un.b_buf = bufp;
139213496Scognet		bp->b_prev = &bufhead;
140213496Scognet		bp->b_next = bufhead.b_next;
141213496Scognet		bufhead.b_next->b_prev = bp;
142213496Scognet		bufhead.b_next = bp;
143213496Scognet		initbarea(bp);
144213496Scognet	}
145234281Smarius	bufhead.b_size = i;	/* save number of buffers */
146213496Scognet}
147213496Scognet
148213496Scognet/*
149213496Scognet * Manage a cache of directory blocks.
150213496Scognet */
151213496Scognetstruct bufarea *
152213496Scognetgetdatablk(blkno, size)
153213496Scognet	ufs_daddr_t blkno;
154213496Scognet	long size;
155213496Scognet{
156213496Scognet	register struct bufarea *bp;
157213496Scognet
158213496Scognet	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
159213496Scognet		if (bp->b_bno == fsbtodb(&sblock, blkno))
160213496Scognet			goto foundit;
161213496Scognet	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
162213496Scognet		if ((bp->b_flags & B_INUSE) == 0)
163213496Scognet			break;
164213496Scognet	if (bp == &bufhead)
165238369Simp		errx(EEXIT, "deadlocked buffer pool");
166213496Scognet	getblk(bp, blkno, size);
167238369Simp	/* fall through */
168213496Scognetfoundit:
169213496Scognet	totalreads++;
170213496Scognet	bp->b_prev->b_next = bp->b_next;
171213496Scognet	bp->b_next->b_prev = bp->b_prev;
172213496Scognet	bp->b_prev = &bufhead;
173213496Scognet	bp->b_next = bufhead.b_next;
174234281Smarius	bufhead.b_next->b_prev = bp;
175213496Scognet	bufhead.b_next = bp;
176213496Scognet	bp->b_flags |= B_INUSE;
177213496Scognet	return (bp);
178238369Simp}
179213496Scognet
180213496Scognetvoid
181213496Scognetgetblk(bp, blk, size)
182213496Scognet	register struct bufarea *bp;
183238369Simp	ufs_daddr_t blk;
184213496Scognet	long size;
185238369Simp{
186213496Scognet	ufs_daddr_t dblk;
187213496Scognet
188234281Smarius	dblk = fsbtodb(&sblock, blk);
189238369Simp	if (bp->b_bno != dblk) {
190213496Scognet		flush(fswritefd, bp);
191213496Scognet		diskreads++;
192213496Scognet		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
193213496Scognet		bp->b_bno = dblk;
194213496Scognet		bp->b_size = size;
195238369Simp	}
196238369Simp}
197238369Simp
198234281Smariusvoid
199213496Scognetflush(fd, bp)
200213496Scognet	int fd;
201238369Simp	register struct bufarea *bp;
202213496Scognet{
203238369Simp	register int i, j;
204238369Simp
205213496Scognet	if (!bp->b_dirty)
206213496Scognet		return;
207238369Simp	if (bp->b_errs != 0)
208213496Scognet		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
209238369Simp		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
210234281Smarius		    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
224static void
225rwerror(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
237void
238ckfini(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
283int
284bread(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
322void
323bwrite(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 */
359ufs_daddr_t
360allocblk(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 */
390void
391freeblk(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 */
405void
406getpathname(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
461void
462catch(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 */
475void
476catchquit(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 */
488void
489voidquit(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 */
501int
502dofix(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 */
550void
551#if __STDC__
552pfatal(const char *fmt, ...)
553#else
554pfatal(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 */
582void
583#if __STDC__
584pwarn(const char *fmt, ...)
585#else
586pwarn(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 */
606void
607#if __STDC__
608panic(const char *fmt, ...)
609#else
610panic(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