main.c revision 1.14 1 /* $NetBSD: main.c,v 1.14 2003/10/20 12:04:38 dsl 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.14 2003/10/20 12:04:38 dsl 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:npqy")) != -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 'q': /* Quiet not implemented */
145 break;
146
147 case 'y':
148 yflag++;
149 nflag = 0;
150 break;
151
152 default:
153 usage();
154 }
155 }
156
157 argc -= optind;
158 argv += optind;
159
160 if (!argc)
161 usage();
162
163 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
164 (void)signal(SIGINT, catch);
165 if (preen)
166 (void)signal(SIGQUIT, catchquit);
167
168 while (argc-- > 0)
169 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
170
171 if (returntosingle)
172 ret = 2;
173
174 exit(ret);
175 }
176
177 static int
178 argtoi(flag, req, str, base)
179 int flag;
180 char *req, *str;
181 int base;
182 {
183 char *cp;
184 int ret;
185
186 ret = (int)strtol(str, &cp, base);
187 if (cp == str || *cp)
188 errexit("-%c flag requires a %s\n", flag, req);
189 return (ret);
190 }
191
192 /*
193 * Check the specified filesystem.
194 */
195 /* ARGSUSED */
196 static int
197 checkfilesys(filesys, mntpt, auxdata, child)
198 const char *filesys;
199 char *mntpt;
200 long auxdata;
201 int child;
202 {
203 daddr_t n_bfree;
204 struct dups *dp;
205 struct zlncnt *zlnp;
206 int i;
207
208 if (preen && child)
209 (void)signal(SIGQUIT, voidquit);
210 setcdevname(filesys, preen);
211 if (debug && preen)
212 pwarn("starting\n");
213 switch (setup(filesys)) {
214 case 0:
215 if (preen)
216 pfatal("CAN'T CHECK FILE SYSTEM.");
217 case -1:
218 return (0);
219 }
220 /*
221 * 1: scan inodes tallying blocks used
222 */
223 if (preen == 0) {
224 if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
225 printf("** Last Mounted on %s\n",
226 sblock.e2fs.e2fs_fsmnt);
227 }
228 if (hotroot())
229 printf("** Root file system\n");
230 printf("** Phase 1 - Check Blocks and Sizes\n");
231 }
232 pass1();
233
234 /*
235 * 1b: locate first references to duplicates, if any
236 */
237 if (duplist) {
238 if (preen)
239 pfatal("INTERNAL ERROR: dups with -p");
240 printf("** Phase 1b - Rescan For More DUPS\n");
241 pass1b();
242 }
243
244 /*
245 * 2: traverse directories from root to mark all connected directories
246 */
247 if (preen == 0)
248 printf("** Phase 2 - Check Pathnames\n");
249 pass2();
250
251 /*
252 * 3: scan inodes looking for disconnected directories
253 */
254 if (preen == 0)
255 printf("** Phase 3 - Check Connectivity\n");
256 pass3();
257
258 /*
259 * 4: scan inodes looking for disconnected files; check reference counts
260 */
261 if (preen == 0)
262 printf("** Phase 4 - Check Reference Counts\n");
263 pass4();
264
265 /*
266 * 5: check and repair resource counts in cylinder groups
267 */
268 if (preen == 0)
269 printf("** Phase 5 - Check Cyl groups\n");
270 pass5();
271
272 /*
273 * print out summary statistics
274 */
275 n_bfree = sblock.e2fs.e2fs_fbcount;
276
277 pwarn("%lld files, %lld used, %lld free\n",
278 (long long)n_files, (long long)n_blks, (long long)n_bfree);
279 if (debug &&
280 /* 9 reserved and unused inodes in FS */
281 (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
282 printf("%lld files missing\n", (long long)n_files);
283 if (debug) {
284 for (i = 0; i < sblock.e2fs_ncg; i++)
285 n_blks += cgoverhead(i);
286 n_blks += sblock.e2fs.e2fs_first_dblock;
287 if (n_blks -= maxfsblock - n_bfree)
288 printf("%lld blocks missing\n", (long long)n_blks);
289 if (duplist != NULL) {
290 printf("The following duplicate blocks remain:");
291 for (dp = duplist; dp; dp = dp->next)
292 printf(" %lld,", (long long)dp->dup);
293 printf("\n");
294 }
295 if (zlnhead != NULL) {
296 printf("The following zero link count inodes remain:");
297 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
298 printf(" %u,", zlnp->zlncnt);
299 printf("\n");
300 }
301 }
302 zlnhead = (struct zlncnt *)0;
303 duplist = (struct dups *)0;
304 muldup = (struct dups *)0;
305 inocleanup();
306 if (fsmodified) {
307 time_t t;
308 (void)time(&t);
309 sblock.e2fs.e2fs_wtime = t;
310 sblock.e2fs.e2fs_lastfsck = t;
311 sbdirty();
312 }
313 ckfini(1);
314 free(blockmap);
315 free(statemap);
316 free((char *)lncntp);
317 if (!fsmodified)
318 return (0);
319 if (!preen)
320 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
321 if (rerun)
322 printf("\n***** PLEASE RERUN FSCK *****\n");
323 if (hotroot()) {
324 struct statfs stfs_buf;
325 /*
326 * We modified the root. Do a mount update on
327 * it, unless it is read-write, so we can continue.
328 */
329 if (statfs("/", &stfs_buf) == 0) {
330 long flags = stfs_buf.f_flags;
331 struct ufs_args args;
332 int ret;
333
334 if (flags & MNT_RDONLY) {
335 args.fspec = 0;
336 args.export.ex_flags = 0;
337 args.export.ex_root = 0;
338 flags |= MNT_UPDATE | MNT_RELOAD;
339 ret = mount(MOUNT_EXT2FS, "/", flags, &args);
340 if (ret == 0)
341 return(0);
342 }
343 }
344 if (!preen)
345 printf("\n***** REBOOT NOW *****\n");
346 sync();
347 return (4);
348 }
349 return (0);
350 }
351
352 static void
353 usage()
354 {
355
356 (void) fprintf(stderr,
357 "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
358 getprogname());
359 exit(1);
360 }
361
362