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