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