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