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