xref: /netbsd-src/sbin/fsck_ext2fs/main.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: main.c,v 1.1 1997/06/11 11:21:50 bouyer 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 #ifndef lint
38 static char copyright[] =
39 "@(#) 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 static char rcsid[] = "$NetBSD: main.c,v 1.1 1997/06/11 11:21:50 bouyer 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 int	docheck __P((struct fstab *));
74 static  void usage __P((void));
75 
76 
77 int
78 main(argc, argv)
79 	int	argc;
80 	char	*argv[];
81 {
82 	int ch;
83 	int ret = 0;
84 	extern char *optarg;
85 	extern int optind;
86 
87 	sync();
88 	skipclean = 1;
89 	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != EOF) {
90 		switch (ch) {
91 		case 'b':
92 			skipclean = 0;
93 			bflag = argtoi('b', "number", optarg, 10);
94 			printf("Alternate super block location: %d\n", bflag);
95 			break;
96 
97 		case 'd':
98 			debug++;
99 			break;
100 
101 		case 'f':
102 			skipclean = 0;
103 			break;
104 
105 		case 'm':
106 			lfmode = argtoi('m', "mode", optarg, 8);
107 			if (lfmode &~ 07777)
108 				errexit("bad mode to -m: %o\n", lfmode);
109 			printf("** lost+found creation mode %o\n", lfmode);
110 			break;
111 
112 		case 'n':
113 			nflag++;
114 			yflag = 0;
115 			break;
116 
117 		case 'p':
118 			preen++;
119 			break;
120 
121 		case 'y':
122 			yflag++;
123 			nflag = 0;
124 			break;
125 
126 		default:
127 			usage();
128 		}
129 	}
130 
131 	argc -= optind;
132 	argv += optind;
133 
134 	if (!argc)
135 		usage();
136 
137 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
138 		(void)signal(SIGINT, catch);
139 	if (preen)
140 		(void)signal(SIGQUIT, catchquit);
141 
142 	while (argc-- > 0)
143 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
144 
145 	if (returntosingle)
146 		ret = 2;
147 
148 	exit(ret);
149 }
150 
151 static int
152 argtoi(flag, req, str, base)
153 	int flag;
154 	char *req, *str;
155 	int base;
156 {
157 	char *cp;
158 	int ret;
159 
160 	ret = (int)strtol(str, &cp, base);
161 	if (cp == str || *cp)
162 		errexit("-%c flag requires a %s\n", flag, req);
163 	return (ret);
164 }
165 
166 /*
167  * Determine whether a filesystem should be checked.
168  */
169 static int
170 docheck(fsp)
171 	register struct fstab *fsp;
172 {
173 
174 	if ( strcmp(fsp->fs_vfstype, "ext2fs") ||
175 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
176 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
177 	    fsp->fs_passno == 0)
178 		return (0);
179 	return (1);
180 }
181 
182 /*
183  * Check the specified filesystem.
184  */
185 /* ARGSUSED */
186 static int
187 checkfilesys(filesys, mntpt, auxdata, child)
188 	char *filesys, *mntpt;
189 	long auxdata;
190 	int child;
191 {
192 	daddr_t n_bfree;
193 	struct dups *dp;
194 	struct zlncnt *zlnp;
195 	int cylno;
196 
197 	if (preen && child)
198 		(void)signal(SIGQUIT, voidquit);
199 	setcdevname(filesys, preen);
200 	if (debug && preen)
201 		pwarn("starting\n");
202 	switch (setup(filesys)) {
203 	case 0:
204 		if (preen)
205 			pfatal("CAN'T CHECK FILE SYSTEM.");
206 	case -1:
207 		return (0);
208 	}
209 	/*
210 	 * 1: scan inodes tallying blocks used
211 	 */
212 	if (preen == 0) {
213 		if (hotroot())
214 			printf("** Root file system\n");
215 		printf("** Phase 1 - Check Blocks and Sizes\n");
216 	}
217 	pass1();
218 
219 	/*
220 	 * 1b: locate first references to duplicates, if any
221 	 */
222 	if (duplist) {
223 		if (preen)
224 			pfatal("INTERNAL ERROR: dups with -p");
225 		printf("** Phase 1b - Rescan For More DUPS\n");
226 		pass1b();
227 	}
228 
229 	/*
230 	 * 2: traverse directories from root to mark all connected directories
231 	 */
232 	if (preen == 0)
233 		printf("** Phase 2 - Check Pathnames\n");
234 	pass2();
235 
236 	/*
237 	 * 3: scan inodes looking for disconnected directories
238 	 */
239 	if (preen == 0)
240 		printf("** Phase 3 - Check Connectivity\n");
241 	pass3();
242 
243 	/*
244 	 * 4: scan inodes looking for disconnected files; check reference counts
245 	 */
246 	if (preen == 0)
247 		printf("** Phase 4 - Check Reference Counts\n");
248 	pass4();
249 
250 	/*
251 	 * 5: check and repair resource counts in cylinder groups
252 	 */
253 	if (preen == 0)
254 		printf("** Phase 5 - Check Cyl groups\n");
255 	pass5();
256 
257 	/*
258 	 * print out summary statistics
259 	 */
260 	n_bfree = sblock.e2fs.e2fs_fbcount;
261 
262 	pwarn("%d files, %d used, %d free\n",
263 	    n_files, n_blks, n_bfree);
264 	if (debug &&
265 		/* 9 reserved and unused inodes in FS */
266 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
267 		printf("%d files missing\n", n_files);
268 	if (debug) {
269 		n_blks += sblock.e2fs_ncg * cgoverhead;
270 		n_blks += sblock.e2fs.e2fs_first_dblock;
271 		if (n_blks -= maxfsblock - n_bfree)
272 			printf("%d blocks missing\n", n_blks);
273 		if (duplist != NULL) {
274 			printf("The following duplicate blocks remain:");
275 			for (dp = duplist; dp; dp = dp->next)
276 				printf(" %d,", dp->dup);
277 			printf("\n");
278 		}
279 		if (zlnhead != NULL) {
280 			printf("The following zero link count inodes remain:");
281 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
282 				printf(" %u,", zlnp->zlncnt);
283 			printf("\n");
284 		}
285 	}
286 	zlnhead = (struct zlncnt *)0;
287 	duplist = (struct dups *)0;
288 	muldup = (struct dups *)0;
289 	inocleanup();
290 	if (fsmodified) {
291 		time_t t;
292 		(void)time(&t);
293 		sblock.e2fs.e2fs_wtime = t;
294 		sblock.e2fs.e2fs_lastfsck = t;
295 		sbdirty();
296 	}
297 	ckfini(1);
298 	free(blockmap);
299 	free(statemap);
300 	free((char *)lncntp);
301 	if (!fsmodified)
302 		return (0);
303 	if (!preen)
304 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
305 	if (rerun)
306 		printf("\n***** PLEASE RERUN FSCK *****\n");
307 	if (hotroot()) {
308 		struct statfs stfs_buf;
309 		/*
310 		 * We modified the root.  Do a mount update on
311 		 * it, unless it is read-write, so we can continue.
312 		 */
313 		if (statfs("/", &stfs_buf) == 0) {
314 			long flags = stfs_buf.f_flags;
315 			struct ufs_args args;
316 			int ret;
317 
318 			if (flags & MNT_RDONLY) {
319 				args.fspec = 0;
320 				args.export.ex_flags = 0;
321 				args.export.ex_root = 0;
322 				flags |= MNT_UPDATE | MNT_RELOAD;
323 				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
324 				if (ret == 0)
325 					return(0);
326 			}
327 		}
328 		if (!preen)
329 			printf("\n***** REBOOT NOW *****\n");
330 		sync();
331 		return (4);
332 	}
333 	return (0);
334 }
335 
336 static void
337 usage()
338 {
339 	extern char *__progname;
340 
341 	(void) fprintf(stderr,
342 	    "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
343 	    __progname);
344 	exit(1);
345 }
346 
347