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