main.c revision 1.29 1 /* $NetBSD: main.c,v 1.29 1997/11/02 01:42:44 mjacob 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.29 1997/11/02 01:42:44 mjacob 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/ffs/fs.h>
57
58 #include <ctype.h>
59 #include <err.h>
60 #include <fstab.h>
61 #include <string.h>
62 #include <ctype.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <unistd.h>
66
67 #include "fsck.h"
68 #include "extern.h"
69 #include "fsutil.h"
70
71 int returntosingle;
72
73 int main __P((int, char *[]));
74
75 static int argtoi __P((int, char *, char *, int));
76 static int checkfilesys __P((char *, char *, long, int));
77 static void usage __P((void));
78
79
80 int
81 main(argc, argv)
82 int argc;
83 char *argv[];
84 {
85 struct rlimit r;
86 int ch;
87 int ret = 0;
88
89 if (getrlimit(RLIMIT_DATA, &r) == 0) {
90 r.rlim_cur = r.rlim_max;
91 (void) setrlimit(RLIMIT_DATA, &r);
92 }
93 sync();
94 skipclean = 1;
95 while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
96 switch (ch) {
97 case 'b':
98 skipclean = 0;
99 bflag = argtoi('b', "number", optarg, 10);
100 printf("Alternate super block location: %d\n", bflag);
101 break;
102
103 case 'c':
104 skipclean = 0;
105 cvtlevel = argtoi('c', "conversion level", optarg, 10);
106 break;
107
108 case 'd':
109 debug++;
110 break;
111
112 case 'f':
113 skipclean = 0;
114 break;
115
116 case 'm':
117 lfmode = argtoi('m', "mode", optarg, 8);
118 if (lfmode &~ 07777)
119 errx(EEXIT, "bad mode to -m: %o", lfmode);
120 printf("** lost+found creation mode %o\n", lfmode);
121 break;
122
123 case 'n':
124 nflag++;
125 yflag = 0;
126 break;
127
128 case 'p':
129 preen++;
130 break;
131
132 case 'y':
133 yflag++;
134 nflag = 0;
135 break;
136
137 default:
138 usage();
139 }
140 }
141
142 argc -= optind;
143 argv += optind;
144
145 if (!argc)
146 usage();
147
148 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
149 (void)signal(SIGINT, catch);
150 if (preen)
151 (void)signal(SIGQUIT, catchquit);
152
153 while (argc-- > 0)
154 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
155
156 if (returntosingle)
157 ret = 2;
158
159 exit(ret);
160 }
161
162 static int
163 argtoi(flag, req, str, base)
164 int flag;
165 char *req, *str;
166 int base;
167 {
168 char *cp;
169 int ret;
170
171 ret = (int)strtol(str, &cp, base);
172 if (cp == str || *cp)
173 errx(EEXIT, "-%c flag requires a %s", flag, req);
174 return (ret);
175 }
176
177 /*
178 * Check the specified filesystem.
179 */
180 /* ARGSUSED */
181 static int
182 checkfilesys(filesys, mntpt, auxdata, child)
183 char *filesys, *mntpt;
184 long auxdata;
185 int child;
186 {
187 ufs_daddr_t n_ffree, n_bfree;
188 struct dups *dp;
189 struct zlncnt *zlnp;
190 #ifdef LITE2BORKEN
191 int cylno, flags;
192 #else
193 int cylno;
194 #endif
195
196 if (preen && child)
197 (void)signal(SIGQUIT, voidquit);
198 setcdevname(filesys, preen);
199 if (debug && preen)
200 pwarn("starting\n");
201 switch (setup(filesys)) {
202 case 0:
203 if (preen)
204 pfatal("CAN'T CHECK FILE SYSTEM.");
205 /* fall through */
206 case -1:
207 return (0);
208 }
209 /*
210 * 1: scan inodes tallying blocks used
211 */
212 if (preen == 0) {
213 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
214 if (hotroot())
215 printf("** Root file system\n");
216 printf("** Phase 1 - Check Blocks and Sizes\n");
217 }
218 pass1();
219
220 /*
221 * 1b: locate first references to duplicates, if any
222 */
223 if (duplist) {
224 if (preen)
225 pfatal("INTERNAL ERROR: dups with -p");
226 printf("** Phase 1b - Rescan For More DUPS\n");
227 pass1b();
228 }
229
230 /*
231 * 2: traverse directories from root to mark all connected directories
232 */
233 if (preen == 0)
234 printf("** Phase 2 - Check Pathnames\n");
235 pass2();
236
237 /*
238 * 3: scan inodes looking for disconnected directories
239 */
240 if (preen == 0)
241 printf("** Phase 3 - Check Connectivity\n");
242 pass3();
243
244 /*
245 * 4: scan inodes looking for disconnected files; check reference counts
246 */
247 if (preen == 0)
248 printf("** Phase 4 - Check Reference Counts\n");
249 pass4();
250
251 /*
252 * 5: check and repair resource counts in cylinder groups
253 */
254 if (preen == 0)
255 printf("** Phase 5 - Check Cyl groups\n");
256 pass5();
257
258 /*
259 * print out summary statistics
260 */
261 n_ffree = sblock.fs_cstotal.cs_nffree;
262 n_bfree = sblock.fs_cstotal.cs_nbfree;
263 pwarn("%d files, %d used, %d free ",
264 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
265 printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n",
266 n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
267 ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
268 if (debug &&
269 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
270 printf("%d files missing\n", n_files);
271 if (debug) {
272 n_blks += sblock.fs_ncg *
273 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
274 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
275 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
276 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
277 printf("%d blocks missing\n", n_blks);
278 if (duplist != NULL) {
279 printf("The following duplicate blocks remain:");
280 for (dp = duplist; dp; dp = dp->next)
281 printf(" %d,", dp->dup);
282 printf("\n");
283 }
284 if (zlnhead != NULL) {
285 printf("The following zero link count inodes remain:");
286 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
287 printf(" %u,", zlnp->zlncnt);
288 printf("\n");
289 }
290 }
291 zlnhead = (struct zlncnt *)0;
292 duplist = (struct dups *)0;
293 muldup = (struct dups *)0;
294 inocleanup();
295 if (fsmodified) {
296 (void)time(&sblock.fs_time);
297 sbdirty();
298 }
299 if (cvtlevel && sblk.b_dirty) {
300 /*
301 * Write out the duplicate super blocks
302 */
303 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
304 bwrite(fswritefd, (char *)&sblock,
305 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
306 }
307 #if LITE2BORKEN
308 if (!hotroot()) {
309 ckfini(1);
310 } else {
311 struct statfs stfs_buf;
312 /*
313 * Check to see if root is mounted read-write.
314 */
315 if (statfs("/", &stfs_buf) == 0)
316 flags = stfs_buf.f_flags;
317 else
318 flags = 0;
319 ckfini(flags & MNT_RDONLY);
320 }
321 #else
322 ckfini(1);
323 #endif
324 free(blockmap);
325 free(statemap);
326 free((char *)lncntp);
327 if (!fsmodified)
328 return (0);
329 if (!preen)
330 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
331 if (rerun)
332 printf("\n***** PLEASE RERUN FSCK *****\n");
333 if (hotroot()) {
334 struct statfs stfs_buf;
335 /*
336 * We modified the root. Do a mount update on
337 * it, unless it is read-write, so we can continue.
338 */
339 if (statfs("/", &stfs_buf) == 0) {
340 long flags = stfs_buf.f_flags;
341 struct ufs_args args;
342 int ret;
343
344 if (flags & MNT_RDONLY) {
345 args.fspec = 0;
346 args.export.ex_flags = 0;
347 args.export.ex_root = 0;
348 flags |= MNT_UPDATE | MNT_RELOAD;
349 ret = mount(MOUNT_FFS, "/", flags, &args);
350 if (ret == 0)
351 return(0);
352 }
353 }
354 if (!preen)
355 printf("\n***** REBOOT NOW *****\n");
356 sync();
357 return (4);
358 }
359 return (0);
360 }
361
362 static void
363 usage()
364 {
365 extern char *__progname;
366
367 (void) fprintf(stderr,
368 "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
369 __progname);
370 exit(1);
371 }
372
373