xref: /netbsd-src/sbin/fsck_ffs/main.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*
2  * Copyright (c) 1980, 1986 The Regents of the University of California.
3  * 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 char copyright[] =
36 "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)main.c	5.27 (Berkeley) 8/7/90";*/
42 static char rcsid[] = "$Id: main.c,v 1.12 1994/04/25 18:28:29 cgd Exp $";
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <ufs/dinode.h>
48 #include <ufs/fs.h>
49 #include <fstab.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <ctype.h>
53 #include <stdio.h>
54 #include "fsck.h"
55 
56 void	catch(), catchquit(), voidquit();
57 int	returntosingle;
58 
59 main(argc, argv)
60 	int	argc;
61 	char	*argv[];
62 {
63 	int ch;
64 	int ret, maxrun = 0;
65 	extern int docheck(), checkfilesys();
66 	extern char *optarg;
67 	extern int optind;
68 
69 	sync();
70 	while ((ch = getopt(argc, argv, "cdpnNyYb:l:m:")) != EOF) {
71 		switch (ch) {
72 		case 'p':
73 			preen++;
74 			break;
75 
76 		case 'b':
77 			bflag = argtoi('b', "number", optarg, 10);
78 			printf("Alternate super block location: %d\n", bflag);
79 			break;
80 
81 		case 'c':
82 			cvtflag++;
83 			break;
84 
85 		case 'd':
86 			debug++;
87 			break;
88 
89 		case 'l':
90 			maxrun = argtoi('l', "number", optarg, 10);
91 			break;
92 
93 		case 'm':
94 			lfmode = argtoi('m', "mode", optarg, 8);
95 			if (lfmode &~ 07777)
96 				errexit("bad mode to -m: %o\n", lfmode);
97 			printf("** lost+found creation mode %o\n", lfmode);
98 			break;
99 
100 		case 'n':
101 		case 'N':
102 			nflag++;
103 			yflag = 0;
104 			break;
105 
106 		case 'y':
107 		case 'Y':
108 			yflag++;
109 			nflag = 0;
110 			break;
111 
112 		default:
113 			errexit("%c option?\n", ch);
114 		}
115 	}
116 	argc -= optind;
117 	argv += optind;
118 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
119 		(void)signal(SIGINT, catch);
120 	if (preen)
121 		(void)signal(SIGQUIT, catchquit);
122 	if (argc) {
123 		while (argc-- > 0)
124 			(void)checkfilesys(*argv++, (char *)0, 0L, 0);
125 		exit(0);
126 	}
127 	ret = checkfstab(preen, maxrun, docheck, checkfilesys);
128 	if (returntosingle)
129 		exit(2);
130 	exit(ret);
131 }
132 
133 argtoi(flag, req, str, base)
134 	int flag;
135 	char *req, *str;
136 	int base;
137 {
138 	char *cp;
139 	int ret;
140 
141 	ret = (int)strtol(str, &cp, base);
142 	if (cp == str || *cp)
143 		errexit("-%c flag requires a %s\n", flag, req);
144 	return (ret);
145 }
146 
147 /*
148  * Determine whether a filesystem should be checked.
149  */
150 docheck(fsp)
151 	register struct fstab *fsp;
152 {
153 
154 	if (strcmp(fsp->fs_vfstype, "ufs") ||
155 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
156 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
157 	    fsp->fs_passno == 0)
158 		return (0);
159 	return (1);
160 }
161 
162 /*
163  * Check the specified filesystem.
164  */
165 /* ARGSUSED */
166 checkfilesys(filesys, mntpt, auxdata, child)
167 	char *filesys, *mntpt;
168 	long auxdata;
169 {
170 	daddr_t n_ffree, n_bfree;
171 	struct dups *dp;
172 	struct zlncnt *zlnp;
173 	int clean;
174 
175 	if (preen && child)
176 		(void)signal(SIGQUIT, voidquit);
177 	devname = filesys;
178 	if (debug && preen)
179 		pwarn("starting\n");
180 	if (setup(filesys) == 0) {
181 		if (preen)
182 			pfatal("CAN'T CHECK FILE SYSTEM.");
183 		return (0);
184 	}
185 
186 	/*
187 	 * 0: check whether file system is already clean
188 	 */
189 	clean = preen && (sblock.fs_state == FSOKAY) && sblock.fs_clean;
190 
191 #ifdef notyet
192 	if (clean) {
193 #else
194 	if (0) {
195 #endif
196 		printf("** filesystem clean -- skipping checks\n");
197 	} else {
198 		/*
199 		 * 1: scan inodes tallying blocks used
200 		 */
201 		if (preen == 0) {
202 			printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
203 			if (hotroot)
204 				printf("** Root file system\n");
205 			printf("** Phase 1 - Check Blocks and Sizes\n");
206 		}
207 		pass1();
208 
209 		/*
210 		 * 1b: locate first references to duplicates, if any
211 		 */
212 		if (duplist) {
213 			if (preen)
214 				pfatal("INTERNAL ERROR: dups with -p");
215 			printf("** Phase 1b - Rescan For More DUPS\n");
216 			pass1b();
217 		}
218 
219 		/*
220 		 * 2: traverse directories from root to mark all connected
221 		 * directories
222 		 */
223 		if (preen == 0)
224 			printf("** Phase 2 - Check Pathnames\n");
225 		pass2();
226 
227 		/*
228 		 * 3: scan inodes looking for disconnected directories
229 		 */
230 		if (preen == 0)
231 			printf("** Phase 3 - Check Connectivity\n");
232 		pass3();
233 
234 		/*
235 		 * 4: scan inodes looking for disconnected files; check
236 		 * reference counts
237 		 */
238 		if (preen == 0)
239 			printf("** Phase 4 - Check Reference Counts\n");
240 		pass4();
241 
242 		/*
243 		 * 5: check and repair resource counts in cylinder groups
244 		 */
245 		if (preen == 0)
246 			printf("** Phase 5 - Check Cyl groups\n");
247 		pass5();
248 	}
249 
250 	/*
251 	 * print out summary statistics
252 	 */
253 	n_ffree = sblock.fs_cstotal.cs_nffree;
254 	n_bfree = sblock.fs_cstotal.cs_nbfree;
255 	pwarn("%ld files, %ld used, %ld free ",
256 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
257 	printf("(%ld frags, %ld blocks, %.1f%% fragmentation)\n",
258 	    n_ffree, n_bfree, (float)(n_ffree * 100) / sblock.fs_dsize);
259 	if (debug &&
260 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
261 		printf("%ld files missing\n", n_files);
262 	if (debug) {
263 		n_blks += sblock.fs_ncg *
264 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
265 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
266 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
267 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
268 			printf("%ld blocks missing\n", n_blks);
269 		if (duplist != NULL) {
270 			printf("The following duplicate blocks remain:");
271 			for (dp = duplist; dp; dp = dp->next)
272 				printf(" %ld,", dp->dup);
273 			printf("\n");
274 		}
275 		if (zlnhead != NULL) {
276 			printf("The following zero link count inodes remain:");
277 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
278 				printf(" %lu,", zlnp->zlncnt);
279 			printf("\n");
280 		}
281 	}
282 	zlnhead = (struct zlncnt *)0;
283 	duplist = (struct dups *)0;
284 	inocleanup();
285 #ifdef notyet
286 	if (!clean && !nflag && fswritefd != -1) {
287 		sblock.fs_state = FSOKAY;
288 		sblock.fs_clean = FS_CLEANFREQ;
289 		fsmodified = 1;
290 	}
291 #endif
292 	if (fsmodified) {
293 		(void)time(&sblock.fs_time);
294 		sbdirty();
295 	}
296 	ckfini();
297 	free(blockmap);
298 	free(statemap);
299 	free((char *)lncntp);
300 	if (!fsmodified)
301 		return (0);
302 	if (!preen) {
303 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
304 		if (hotroot)
305 			printf("\n***** REBOOT NETBSD *****\n");
306 	}
307 	if (hotroot) {
308 		sync();
309 		return (4);
310 	}
311 	return (0);
312 }
313