main.c revision 1.21 1 /* $NetBSD: main.c,v 1.21 2005/06/26 23:01:39 christos 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
64 The Regents of the University of California. All rights reserved.\n");
65 #endif /* not lint */
66
67 #ifndef lint
68 #if 0
69 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/23/94";
70 #else
71 __RCSID("$NetBSD: main.c,v 1.21 2005/06/26 23:01:39 christos Exp $");
72 #endif
73 #endif /* not lint */
74
75 #include <sys/param.h>
76 #include <sys/time.h>
77 #include <sys/mount.h>
78 #include <ufs/ufs/ufsmount.h>
79 #include <ufs/ext2fs/ext2fs_dinode.h>
80 #include <ufs/ext2fs/ext2fs.h>
81 #include <fstab.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <ctype.h>
85 #include <stdio.h>
86 #include <time.h>
87 #include <unistd.h>
88
89 #include "fsck.h"
90 #include "extern.h"
91 #include "fsutil.h"
92
93 int returntosingle;
94
95
96 static int argtoi(int, const char *, const char *, int);
97 static int checkfilesys(const char *, char *, long, int);
98 static void usage(void);
99
100 int
101 main(int argc, char *argv[])
102 {
103 int ch;
104 int ret = 0;
105
106 sync();
107 skipclean = 1;
108 while ((ch = getopt(argc, argv, "b:dfm:npqy")) != -1) {
109 switch (ch) {
110 case 'b':
111 skipclean = 0;
112 bflag = argtoi('b', "number", optarg, 10);
113 printf("Alternate super block location: %d\n", bflag);
114 break;
115
116 case 'd':
117 debug++;
118 break;
119
120 case 'f':
121 skipclean = 0;
122 break;
123
124 case 'm':
125 lfmode = argtoi('m', "mode", optarg, 8);
126 if (lfmode &~ 07777)
127 errexit("bad mode to -m: %o\n", lfmode);
128 printf("** lost+found creation mode %o\n", lfmode);
129 break;
130
131 case 'n':
132 nflag++;
133 yflag = 0;
134 break;
135
136 case 'p':
137 preen++;
138 break;
139
140 case 'P':
141 /* Progress meter not implemented. */
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(int flag, const char *req, const char *str, 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(const char *filesys, char *mntpt, long auxdata, int child)
195 {
196 daddr_t n_bfree;
197 struct dups *dp;
198 struct zlncnt *zlnp;
199 int i;
200
201 if (preen && child)
202 (void)signal(SIGQUIT, voidquit);
203 setcdevname(filesys, preen);
204 if (debug && preen)
205 pwarn("starting\n");
206 switch (setup(filesys)) {
207 case 0:
208 if (preen)
209 pfatal("CAN'T CHECK FILE SYSTEM.");
210 case -1:
211 return (0);
212 }
213 /*
214 * 1: scan inodes tallying blocks used
215 */
216 if (preen == 0) {
217 if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
218 printf("** Last Mounted on %s\n",
219 sblock.e2fs.e2fs_fsmnt);
220 }
221 if (hotroot())
222 printf("** Root file system\n");
223 printf("** Phase 1 - Check Blocks and Sizes\n");
224 }
225 pass1();
226
227 /*
228 * 1b: locate first references to duplicates, if any
229 */
230 if (duplist) {
231 if (preen)
232 pfatal("INTERNAL ERROR: dups with -p");
233 printf("** Phase 1b - Rescan For More DUPS\n");
234 pass1b();
235 }
236
237 /*
238 * 2: traverse directories from root to mark all connected directories
239 */
240 if (preen == 0)
241 printf("** Phase 2 - Check Pathnames\n");
242 pass2();
243
244 /*
245 * 3: scan inodes looking for disconnected directories
246 */
247 if (preen == 0)
248 printf("** Phase 3 - Check Connectivity\n");
249 pass3();
250
251 /*
252 * 4: scan inodes looking for disconnected files; check reference counts
253 */
254 if (preen == 0)
255 printf("** Phase 4 - Check Reference Counts\n");
256 pass4();
257
258 /*
259 * 5: check and repair resource counts in cylinder groups
260 */
261 if (preen == 0)
262 printf("** Phase 5 - Check Cyl groups\n");
263 pass5();
264
265 /*
266 * print out summary statistics
267 */
268 n_bfree = sblock.e2fs.e2fs_fbcount;
269
270 pwarn("%lld files, %lld used, %lld free\n",
271 (long long)n_files, (long long)n_blks, (long long)n_bfree);
272 if (debug &&
273 /* 9 reserved and unused inodes in FS */
274 (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
275 printf("%lld files missing\n", (long long)n_files);
276 if (debug) {
277 for (i = 0; i < sblock.e2fs_ncg; i++)
278 n_blks += cgoverhead(i);
279 n_blks += sblock.e2fs.e2fs_first_dblock;
280 if (n_blks -= maxfsblock - n_bfree)
281 printf("%lld blocks missing\n", (long long)n_blks);
282 if (duplist != NULL) {
283 printf("The following duplicate blocks remain:");
284 for (dp = duplist; dp; dp = dp->next)
285 printf(" %lld,", (long long)dp->dup);
286 printf("\n");
287 }
288 if (zlnhead != NULL) {
289 printf("The following zero link count inodes remain:");
290 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
291 printf(" %u,", zlnp->zlncnt);
292 printf("\n");
293 }
294 }
295 zlnhead = (struct zlncnt *)0;
296 duplist = (struct dups *)0;
297 muldup = (struct dups *)0;
298 inocleanup();
299 if (fsmodified) {
300 time_t t;
301 (void)time(&t);
302 sblock.e2fs.e2fs_wtime = t;
303 sblock.e2fs.e2fs_lastfsck = t;
304 sbdirty();
305 }
306 ckfini(1);
307 free(blockmap);
308 free(statemap);
309 free((char *)lncntp);
310 if (!fsmodified)
311 return (0);
312 if (!preen)
313 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
314 if (rerun)
315 printf("\n***** PLEASE RERUN FSCK *****\n");
316 if (hotroot()) {
317 struct statvfs stfs_buf;
318 /*
319 * We modified the root. Do a mount update on
320 * it, unless it is read-write, so we can continue.
321 */
322 if (statvfs("/", &stfs_buf) == 0) {
323 long flags = stfs_buf.f_flag;
324 struct ufs_args args;
325 int ret;
326
327 if (flags & MNT_RDONLY) {
328 args.fspec = 0;
329 args.export.ex_flags = 0;
330 args.export.ex_root = 0;
331 flags |= MNT_UPDATE | MNT_RELOAD;
332 ret = mount(MOUNT_EXT2FS, "/", flags, &args);
333 if (ret == 0)
334 return(0);
335 }
336 }
337 if (!preen)
338 printf("\n***** REBOOT NOW *****\n");
339 sync();
340 return (4);
341 }
342 return (0);
343 }
344
345 static void
346 usage(void)
347 {
348
349 (void) fprintf(stderr,
350 "usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
351 getprogname());
352 exit(1);
353 }
354
355