main.c revision 1.31 1 /* $NetBSD: main.c,v 1.31 1998/03/18 17:01:24 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. 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.31 1998/03/18 17:01:24 bouyer 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 <ctype.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68
69 #include "fsck.h"
70 #include "extern.h"
71 #include "fsutil.h"
72
73 int returntosingle;
74
75 int main __P((int, char *[]));
76
77 static int argtoi __P((int, char *, char *, int));
78 static int checkfilesys __P((char *, char *, long, int));
79 static void usage __P((void));
80
81
82 int
83 main(argc, argv)
84 int argc;
85 char *argv[];
86 {
87 struct rlimit r;
88 int ch;
89 int ret = 0;
90
91 if (getrlimit(RLIMIT_DATA, &r) == 0) {
92 r.rlim_cur = r.rlim_max;
93 (void) setrlimit(RLIMIT_DATA, &r);
94 }
95 sync();
96 skipclean = 1;
97 markclean = 1;
98 endian = 0;
99 while ((ch = getopt(argc, argv, "B:b:c:dfm:npy")) != -1) {
100 switch (ch) {
101 case 'B':
102 if (strcmp(optarg, "be") == 0)
103 endian = BIG_ENDIAN;
104 else if (strcmp(optarg, "le") == 0)
105 endian = LITTLE_ENDIAN;
106 else usage();
107 break;
108
109 case 'b':
110 skipclean = 0;
111 bflag = argtoi('b', "number", optarg, 10);
112 printf("Alternate super block location: %d\n", bflag);
113 break;
114
115 case 'c':
116 skipclean = 0;
117 cvtlevel = argtoi('c', "conversion level", optarg, 10);
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 errx(EEXIT, "bad mode to -m: %o", 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 errx(EEXIT, "-%c flag requires a %s", 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 char *filesys, *mntpt;
196 long auxdata;
197 int child;
198 {
199 ufs_daddr_t n_ffree, n_bfree;
200 struct dups *dp;
201 struct zlncnt *zlnp;
202 #ifdef LITE2BORKEN
203 int cylno, flags;
204 #else
205 int cylno;
206 #endif
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 /* fall through */
218 case -1:
219 return (0);
220 }
221 /*
222 * 1: scan inodes tallying blocks used
223 */
224 if (preen == 0) {
225 printf("** Last Mounted on %s\n", sblock->fs_fsmnt);
226 if (hotroot())
227 printf("** Root file system\n");
228 printf("** Phase 1 - Check Blocks and Sizes\n");
229 }
230 pass1();
231
232 /*
233 * 1b: locate first references to duplicates, if any
234 */
235 if (duplist) {
236 if (preen)
237 pfatal("INTERNAL ERROR: dups with -p");
238 printf("** Phase 1b - Rescan For More DUPS\n");
239 pass1b();
240 }
241
242 /*
243 * 2: traverse directories from root to mark all connected directories
244 */
245 if (preen == 0)
246 printf("** Phase 2 - Check Pathnames\n");
247 pass2();
248
249 /*
250 * 3: scan inodes looking for disconnected directories
251 */
252 if (preen == 0)
253 printf("** Phase 3 - Check Connectivity\n");
254 pass3();
255
256 /*
257 * 4: scan inodes looking for disconnected files; check reference counts
258 */
259 if (preen == 0)
260 printf("** Phase 4 - Check Reference Counts\n");
261 pass4();
262
263 /*
264 * 5: check and repair resource counts in cylinder groups
265 */
266 if (preen == 0)
267 printf("** Phase 5 - Check Cyl groups\n");
268 pass5();
269
270 /*
271 * print out summary statistics
272 */
273 n_ffree = sblock->fs_cstotal.cs_nffree;
274 n_bfree = sblock->fs_cstotal.cs_nbfree;
275 pwarn("%d files, %d used, %d free ",
276 n_files, n_blks, n_ffree + sblock->fs_frag * n_bfree);
277 printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n",
278 n_ffree, n_bfree, (n_ffree * 100) / sblock->fs_dsize,
279 ((n_ffree * 1000 + sblock->fs_dsize / 2) / sblock->fs_dsize) % 10);
280 if (debug &&
281 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
282 printf("%d files missing\n", n_files);
283 if (debug) {
284 n_blks += sblock->fs_ncg *
285 (cgdmin(sblock, 0) - cgsblock(sblock, 0));
286 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
287 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
288 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
289 printf("%d blocks missing\n", n_blks);
290 if (duplist != NULL) {
291 printf("The following duplicate blocks remain:");
292 for (dp = duplist; dp; dp = dp->next)
293 printf(" %d,", dp->dup);
294 printf("\n");
295 }
296 if (zlnhead != NULL) {
297 printf("The following zero link count inodes remain:");
298 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
299 printf(" %u,", zlnp->zlncnt);
300 printf("\n");
301 }
302 }
303 zlnhead = (struct zlncnt *)0;
304 duplist = (struct dups *)0;
305 muldup = (struct dups *)0;
306 inocleanup();
307 if (fsmodified) {
308 (void)time(&sblock->fs_time);
309 sbdirty();
310 }
311 if ((cvtlevel && sblk.b_dirty) || doswap) {
312 /*
313 * Write out the duplicate super blocks
314 */
315 for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
316 bwrite(fswritefd, sblk.b_un.b_buf,
317 fsbtodb(sblock, cgsblock(sblock, cylno)), SBSIZE);
318 }
319 #if LITE2BORKEN
320 if (!hotroot()) {
321 ckfini();
322 } else {
323 struct statfs stfs_buf;
324 /*
325 * Check to see if root is mounted read-write.
326 */
327 if (statfs("/", &stfs_buf) == 0)
328 flags = stfs_buf.f_flags;
329 else
330 flags = 0;
331 if (markclean)
332 markclean = flags & MNT_RDONLY;
333 ckfini();
334 }
335 #else
336 ckfini();
337 #endif
338 free(blockmap);
339 free(statemap);
340 free((char *)lncntp);
341 if (!fsmodified)
342 return (0);
343 if (!preen)
344 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
345 if (rerun)
346 printf("\n***** PLEASE RERUN FSCK *****\n");
347 if (hotroot()) {
348 struct statfs stfs_buf;
349 /*
350 * We modified the root. Do a mount update on
351 * it, unless it is read-write, so we can continue.
352 */
353 if (statfs("/", &stfs_buf) == 0) {
354 long flags = stfs_buf.f_flags;
355 struct ufs_args args;
356 int ret;
357
358 if (flags & MNT_RDONLY) {
359 args.fspec = 0;
360 args.export.ex_flags = 0;
361 args.export.ex_root = 0;
362 flags |= MNT_UPDATE | MNT_RELOAD;
363 ret = mount(MOUNT_FFS, "/", flags, &args);
364 if (ret == 0)
365 return(0);
366 }
367 }
368 if (!preen)
369 printf("\n***** REBOOT NOW *****\n");
370 sync();
371 return (4);
372 }
373 return (0);
374 }
375
376 static void
377 usage()
378 {
379 extern char *__progname;
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