main.c revision 1.48 1 /* $NetBSD: main.c,v 1.48 2004/01/05 23:23:33 jmmv 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 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.48 2004/01/05 23:23:33 jmmv Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <fstab.h>
59 #include <string.h>
60 #include <time.h>
61 #include <ctype.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65
66 #include "fsck.h"
67 #include "extern.h"
68 #include "fsutil.h"
69
70 int returntosingle;
71
72 int main __P((int, char *[]));
73
74 static int argtoi __P((int, char *, char *, int));
75 static int checkfilesys __P((const char *, char *, long, int));
76 static void usage __P((void));
77
78 int
79 main(argc, argv)
80 int argc;
81 char *argv[];
82 {
83 struct rlimit r;
84 int ch;
85 int ret = 0;
86
87 if (getrlimit(RLIMIT_DATA, &r) == 0) {
88 r.rlim_cur = r.rlim_max;
89 (void) setrlimit(RLIMIT_DATA, &r);
90 }
91 sync();
92 skipclean = 1;
93 markclean = 1;
94 forceimage = 0;
95 endian = 0;
96 isappleufs = 0;
97 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npqy")) != -1) {
98 switch (ch) {
99 case 'a':
100 isappleufs = 1;
101 break;
102
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 forceimage = 1;
128 break;
129
130 case 'f':
131 skipclean = 0;
132 break;
133
134 case 'm':
135 lfmode = argtoi('m', "mode", optarg, 8);
136 if (lfmode &~ 07777)
137 errx(EEXIT, "bad mode to -m: %o", lfmode);
138 printf("** lost+found creation mode %o\n", lfmode);
139 break;
140
141 case 'n':
142 nflag++;
143 yflag = 0;
144 break;
145
146 case 'p':
147 preen++;
148 break;
149
150 case 'q':
151 quiet++;
152 break;
153
154 case 'y':
155 yflag++;
156 nflag = 0;
157 break;
158
159 default:
160 usage();
161 }
162 }
163
164 argc -= optind;
165 argv += optind;
166
167 if (!argc)
168 usage();
169
170 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
171 (void)signal(SIGINT, catch);
172 if (preen)
173 (void)signal(SIGQUIT, catchquit);
174 signal(SIGINFO, infohandler);
175
176 while (argc-- > 0) {
177 const char *path = blockcheck(*argv);
178
179 if (path == NULL)
180 pfatal("Can't check %s\n", *argv);
181 else
182 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
183 argv++;
184 }
185
186 if (returntosingle)
187 ret = 2;
188
189 exit(ret);
190 }
191
192 static int
193 argtoi(flag, req, str, base)
194 int flag;
195 char *req, *str;
196 int base;
197 {
198 char *cp;
199 int ret;
200
201 ret = (int)strtol(str, &cp, base);
202 if (cp == str || *cp)
203 errx(EEXIT, "-%c flag requires a %s", flag, req);
204 return (ret);
205 }
206
207 /*
208 * Check the specified filesystem.
209 */
210 /* ARGSUSED */
211 static int
212 checkfilesys(filesys, mntpt, auxdata, child)
213 const char *filesys;
214 char *mntpt;
215 long auxdata;
216 int child;
217 {
218 daddr_t n_ffree, n_bfree;
219 struct dups *dp;
220 struct zlncnt *zlnp;
221 int cylno;
222 #ifdef LITE2BORKEN
223 int flags;
224 #endif
225
226 if (preen && child)
227 (void)signal(SIGQUIT, voidquit);
228 setcdevname(filesys, preen);
229 if (debug && preen)
230 pwarn("starting\n");
231 switch (setup(filesys)) {
232 case 0:
233 if (preen)
234 pfatal("CAN'T CHECK FILE SYSTEM.");
235 /* fall through */
236 case -1:
237 return (0);
238 }
239 /*
240 * Cleared if any questions answered no. Used to decide if
241 * the superblock should be marked clean.
242 */
243 resolved = 1;
244 /*
245 * 1: scan inodes tallying blocks used
246 */
247 if (preen == 0) {
248 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
249 if (hotroot())
250 pwarn("** Root file system\n");
251 pwarn("** Phase 1 - Check Blocks and Sizes\n");
252 }
253 pass1();
254
255 /*
256 * 1b: locate first references to duplicates, if any
257 */
258 if (duplist) {
259 if (preen)
260 pfatal("INTERNAL ERROR: dups with -p\n");
261 if (usedsoftdep)
262 pfatal("INTERNAL ERROR: dups with softdep\n");
263 pwarn("** Phase 1b - Rescan For More DUPS\n");
264 pass1b();
265 }
266
267 /*
268 * 2: traverse directories from root to mark all connected directories
269 */
270 if (preen == 0)
271 pwarn("** Phase 2 - Check Pathnames\n");
272 pass2();
273
274 /*
275 * 3: scan inodes looking for disconnected directories
276 */
277 if (preen == 0)
278 pwarn("** Phase 3 - Check Connectivity\n");
279 pass3();
280
281 /*
282 * 4: scan inodes looking for disconnected files; check reference counts
283 */
284 if (preen == 0)
285 pwarn("** Phase 4 - Check Reference Counts\n");
286 pass4();
287
288 /*
289 * 5: check and repair resource counts in cylinder groups
290 */
291 if (preen == 0)
292 pwarn("** Phase 5 - Check Cyl groups\n");
293 pass5();
294
295 /*
296 * print out summary statistics
297 */
298 n_ffree = sblock->fs_cstotal.cs_nffree;
299 n_bfree = sblock->fs_cstotal.cs_nbfree;
300 pwarn("%d files, %lld used, %lld free ",
301 n_files, (long long)n_blks,
302 (long long)(n_ffree + sblock->fs_frag * n_bfree));
303 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
304 (long long)n_ffree, (long long)n_bfree,
305 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
306 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
307 / (daddr_t)sblock->fs_dsize) % 10));
308 if (debug &&
309 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
310 printf("%d files missing\n", n_files);
311 if (debug) {
312 n_blks += sblock->fs_ncg *
313 (cgdmin(sblock, 0) - cgsblock(sblock, 0));
314 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
315 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
316 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
317 printf("%lld blocks missing\n", (long long)n_blks);
318 if (duplist != NULL) {
319 printf("The following duplicate blocks remain:");
320 for (dp = duplist; dp; dp = dp->next)
321 printf(" %lld,", (long long)dp->dup);
322 printf("\n");
323 }
324 if (zlnhead != NULL) {
325 printf("The following zero link count inodes remain:");
326 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
327 printf(" %u,", zlnp->zlncnt);
328 printf("\n");
329 }
330 }
331 zlnhead = (struct zlncnt *)0;
332 duplist = (struct dups *)0;
333 muldup = (struct dups *)0;
334 inocleanup();
335 if (fsmodified) {
336 sblock->fs_time = time(NULL);
337 sbdirty();
338 }
339 if (rerun)
340 markclean = 0;
341 #if LITE2BORKEN
342 if (!hotroot()) {
343 ckfini();
344 } else {
345 struct statfs stfs_buf;
346 /*
347 * Check to see if root is mounted read-write.
348 */
349 if (statfs("/", &stfs_buf) == 0)
350 flags = stfs_buf.f_flags;
351 else
352 flags = 0;
353 if (markclean)
354 markclean = flags & MNT_RDONLY;
355 ckfini();
356 }
357 #else
358 ckfini();
359 #endif
360 for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
361 if (inostathead[cylno].il_stat != NULL)
362 free(inostathead[cylno].il_stat);
363 free(inostathead);
364 inostathead = NULL;
365
366 if (!fsmodified)
367 return (0);
368 if (!preen)
369 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
370 if (rerun)
371 pwarn("\n***** PLEASE RERUN FSCK *****\n");
372 if (hotroot()) {
373 struct statfs stfs_buf;
374 /*
375 * We modified the root. Do a mount update on
376 * it, unless it is read-write, so we can continue.
377 */
378 if (statfs("/", &stfs_buf) == 0) {
379 long flags = stfs_buf.f_flags;
380 struct ufs_args args;
381 int ret;
382
383 if (flags & MNT_RDONLY) {
384 args.fspec = 0;
385 args.export.ex_flags = 0;
386 args.export.ex_root = 0;
387 flags |= MNT_UPDATE | MNT_RELOAD;
388 ret = mount(MOUNT_FFS, "/", flags, &args);
389 if (ret == 0)
390 return(0);
391 }
392 }
393 if (!preen)
394 pwarn("\n***** REBOOT NOW *****\n");
395 sync();
396 return (4);
397 }
398 return (0);
399 }
400
401 static void
402 usage()
403 {
404
405 (void) fprintf(stderr,
406 "usage: %s [-dFfnpqy] [-B be|le] [-b block] [-c level] [-m mode]"
407 " filesystem ...\n",
408 getprogname());
409 exit(1);
410 }
411
412