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