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