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