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