xref: /netbsd-src/sbin/fsck_ext2fs/main.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: main.c,v 1.26 2007/07/16 17:06:52 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1997 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *	This product includes software developed by Manuel Bouyer.
46  * 4. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 #ifndef lint
63 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
64 	The Regents of the University of California.  All rights reserved.\n");
65 #endif /* not lint */
66 
67 #ifndef lint
68 #if 0
69 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
70 #else
71 __RCSID("$NetBSD: main.c,v 1.26 2007/07/16 17:06:52 pooka Exp $");
72 #endif
73 #endif /* not lint */
74 
75 #include <sys/param.h>
76 #include <sys/time.h>
77 #include <sys/mount.h>
78 #include <ufs/ufs/ufsmount.h>
79 #include <ufs/ext2fs/ext2fs_dinode.h>
80 #include <ufs/ext2fs/ext2fs.h>
81 #include <fstab.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <ctype.h>
85 #include <stdio.h>
86 #include <time.h>
87 #include <unistd.h>
88 #include <signal.h>
89 
90 #include "fsck.h"
91 #include "extern.h"
92 #include "fsutil.h"
93 
94 int	returntosingle;
95 
96 
97 static int	argtoi(int, const char *, const char *, int);
98 static int	checkfilesys(const char *, char *, long, int);
99 static void	usage(void);
100 
101 int
102 main(int argc, char *argv[])
103 {
104 	int ch;
105 	int ret = 0;
106 
107 	sync();
108 	skipclean = 1;
109 	while ((ch = getopt(argc, argv, "b:dfm:npqy")) != -1) {
110 		switch (ch) {
111 		case 'b':
112 			skipclean = 0;
113 			bflag = argtoi('b', "number", optarg, 10);
114 			printf("Alternate super block location: %d\n", bflag);
115 			break;
116 
117 		case 'd':
118 			debug++;
119 			break;
120 
121 		case 'f':
122 			skipclean = 0;
123 			break;
124 
125 		case 'm':
126 			lfmode = argtoi('m', "mode", optarg, 8);
127 			if (lfmode &~ 07777)
128 				errexit("bad mode to -m: %o\n", lfmode);
129 			printf("** lost+found creation mode %o\n", lfmode);
130 			break;
131 
132 		case 'n':
133 			nflag++;
134 			yflag = 0;
135 			break;
136 
137 		case 'p':
138 			preen++;
139 			break;
140 
141 		case 'P':
142 			/* Progress meter not implemented. */
143 			break;
144 
145 		case 'q':		/* Quiet not implemented */
146 			break;
147 
148 		case 'y':
149 			yflag++;
150 			nflag = 0;
151 			break;
152 
153 		default:
154 			usage();
155 		}
156 	}
157 
158 	argc -= optind;
159 	argv += optind;
160 
161 	if (!argc)
162 		usage();
163 
164 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
165 		(void)signal(SIGINT, catch);
166 	if (preen)
167 		(void)signal(SIGQUIT, catchquit);
168 
169 	while (argc-- > 0)
170 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
171 
172 	if (returntosingle)
173 		ret = 2;
174 
175 	exit(ret);
176 }
177 
178 static int
179 argtoi(int flag, const char *req, const char *str, int base)
180 {
181 	char *cp;
182 	int ret;
183 
184 	ret = (int)strtol(str, &cp, base);
185 	if (cp == str || *cp)
186 		errexit("-%c flag requires a %s\n", flag, req);
187 	return (ret);
188 }
189 
190 /*
191  * Check the specified filesystem.
192  */
193 /* ARGSUSED */
194 static int
195 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
196 {
197 	daddr_t n_bfree;
198 	struct dups *dp;
199 	struct zlncnt *zlnp;
200 	int i;
201 
202 	if (preen && child)
203 		(void)signal(SIGQUIT, voidquit);
204 	setcdevname(filesys, preen);
205 	if (debug && preen)
206 		pwarn("starting\n");
207 	switch (setup(filesys)) {
208 	case 0:
209 		if (preen)
210 			pfatal("CAN'T CHECK FILE SYSTEM.");
211 	case -1:
212 		return (0);
213 	}
214 	/*
215 	 * 1: scan inodes tallying blocks used
216 	 */
217 	if (preen == 0) {
218 		if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
219 			printf("** Last Mounted on %s\n",
220 			    sblock.e2fs.e2fs_fsmnt);
221 		}
222 		if (hotroot())
223 			printf("** Root file system\n");
224 		printf("** Phase 1 - Check Blocks and Sizes\n");
225 	}
226 	pass1();
227 
228 	/*
229 	 * 1b: locate first references to duplicates, if any
230 	 */
231 	if (duplist) {
232 		if (preen)
233 			pfatal("INTERNAL ERROR: dups with -p");
234 		printf("** Phase 1b - Rescan For More DUPS\n");
235 		pass1b();
236 	}
237 
238 	/*
239 	 * 2: traverse directories from root to mark all connected directories
240 	 */
241 	if (preen == 0)
242 		printf("** Phase 2 - Check Pathnames\n");
243 	pass2();
244 
245 	/*
246 	 * 3: scan inodes looking for disconnected directories
247 	 */
248 	if (preen == 0)
249 		printf("** Phase 3 - Check Connectivity\n");
250 	pass3();
251 
252 	/*
253 	 * 4: scan inodes looking for disconnected files; check reference counts
254 	 */
255 	if (preen == 0)
256 		printf("** Phase 4 - Check Reference Counts\n");
257 	pass4();
258 
259 	/*
260 	 * 5: check and repair resource counts in cylinder groups
261 	 */
262 	if (preen == 0)
263 		printf("** Phase 5 - Check Cyl groups\n");
264 	pass5();
265 
266 	/*
267 	 * print out summary statistics
268 	 */
269 	n_bfree = sblock.e2fs.e2fs_fbcount;
270 
271 	pwarn("%lld files, %lld used, %lld free\n",
272 	    (long long)n_files, (long long)n_blks, (long long)n_bfree);
273 	if (debug &&
274 		/* 9 reserved and unused inodes in FS */
275 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
276 		printf("%lld files missing\n", (long long)n_files);
277 	if (debug) {
278 		for (i = 0; i < sblock.e2fs_ncg; i++)
279 			n_blks +=  cgoverhead(i);
280 		n_blks += sblock.e2fs.e2fs_first_dblock;
281 		if (n_blks -= maxfsblock - n_bfree)
282 			printf("%lld blocks missing\n", (long long)n_blks);
283 		if (duplist != NULL) {
284 			printf("The following duplicate blocks remain:");
285 			for (dp = duplist; dp; dp = dp->next)
286 				printf(" %lld,", (long long)dp->dup);
287 			printf("\n");
288 		}
289 		if (zlnhead != NULL) {
290 			printf("The following zero link count inodes remain:");
291 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
292 				printf(" %llu,",
293 				    (unsigned long long)zlnp->zlncnt);
294 			printf("\n");
295 		}
296 	}
297 	zlnhead = (struct zlncnt *)0;
298 	duplist = (struct dups *)0;
299 	muldup = (struct dups *)0;
300 	inocleanup();
301 	if (fsmodified) {
302 		time_t t;
303 		(void)time(&t);
304 		sblock.e2fs.e2fs_wtime = t;
305 		sblock.e2fs.e2fs_lastfsck = t;
306 		sbdirty();
307 	}
308 	ckfini(1);
309 	free(blockmap);
310 	free(statemap);
311 	free((char *)lncntp);
312 	if (!fsmodified)
313 		return (0);
314 	if (!preen)
315 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
316 	if (rerun)
317 		printf("\n***** PLEASE RERUN FSCK *****\n");
318 	if (hotroot()) {
319 		struct statvfs stfs_buf;
320 		/*
321 		 * We modified the root.  Do a mount update on
322 		 * it, unless it is read-write, so we can continue.
323 		 */
324 		if (statvfs("/", &stfs_buf) == 0) {
325 			long flags = stfs_buf.f_flag;
326 			struct ufs_args args;
327 			int ret;
328 
329 			if (flags & MNT_RDONLY) {
330 				args.fspec = 0;
331 				flags |= MNT_UPDATE | MNT_RELOAD;
332 				ret = mount(MOUNT_EXT2FS, "/", flags,
333 				    &args, sizeof args);
334 				if (ret == 0)
335 					return(0);
336 			}
337 		}
338 		if (!preen)
339 			printf("\n***** REBOOT NOW *****\n");
340 		sync();
341 		return (4);
342 	}
343 	return (0);
344 }
345 
346 static void
347 usage(void)
348 {
349 
350 	(void) fprintf(stderr,
351 	    "usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
352 	    getprogname());
353 	exit(1);
354 }
355 
356