main.c revision 1.56 1 /* $NetBSD: main.c,v 1.56 2005/01/19 17:33:58 xtraeme 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.56 2005/01/19 17:33:58 xtraeme 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 int progress = 0;
72
73 static int argtoi (int, char *, char *, int);
74 static int checkfilesys (const char *, char *, long, int);
75 static void usage (void);
76
77 int
78 main(int argc, char *argv[])
79 {
80 struct rlimit r;
81 int ch;
82 int ret = 0;
83
84 if (getrlimit(RLIMIT_DATA, &r) == 0) {
85 r.rlim_cur = r.rlim_max;
86 (void) setrlimit(RLIMIT_DATA, &r);
87 }
88 sync();
89 skipclean = 1;
90 markclean = 1;
91 forceimage = 0;
92 endian = 0;
93 isappleufs = 0;
94 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqy")) != -1) {
95 switch (ch) {
96 case 'a':
97 isappleufs = 1;
98 break;
99
100 case 'B':
101 if (strcmp(optarg, "be") == 0)
102 endian = BIG_ENDIAN;
103 else if (strcmp(optarg, "le") == 0)
104 endian = LITTLE_ENDIAN;
105 else usage();
106 break;
107
108 case 'b':
109 skipclean = 0;
110 bflag = argtoi('b', "number", optarg, 10);
111 printf("Alternate super block location: %d\n", bflag);
112 break;
113
114 case 'c':
115 skipclean = 0;
116 cvtlevel = argtoi('c', "conversion level", optarg, 10);
117 if (cvtlevel > 4) {
118 cvtlevel = 4;
119 warnx("Using maximum conversion level of %d\n",cvtlevel);
120 }
121 break;
122
123 case 'd':
124 debug++;
125 break;
126
127 case 'F':
128 forceimage = 1;
129 break;
130
131 case 'f':
132 skipclean = 0;
133 break;
134
135 case 'm':
136 lfmode = argtoi('m', "mode", optarg, 8);
137 if (lfmode &~ 07777)
138 errx(EEXIT, "bad mode to -m: %o", lfmode);
139 printf("** lost+found creation mode %o\n", lfmode);
140 break;
141
142 case 'n':
143 nflag++;
144 yflag = 0;
145 break;
146
147 case 'p':
148 preen++;
149 break;
150
151 case 'P':
152 progress = 1;
153 break;
154
155 case 'q':
156 quiet++;
157 break;
158
159 case 'y':
160 yflag++;
161 nflag = 0;
162 break;
163
164 default:
165 usage();
166 }
167 }
168
169 argc -= optind;
170 argv += optind;
171
172 if (!argc)
173 usage();
174
175 if (debug)
176 progress = 0;
177
178 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
179 (void)signal(SIGINT, catch);
180 if (preen)
181 (void)signal(SIGQUIT, catchquit);
182 #ifdef PROGRESS
183 if (progress) {
184 progress_ttywidth(0);
185 (void)signal(SIGWINCH, progress_ttywidth);
186 }
187 #endif /* ! PROGRESS */
188 signal(SIGINFO, infohandler);
189
190 while (argc-- > 0) {
191 const char *path = blockcheck(*argv);
192
193 if (path == NULL)
194 pfatal("Can't check %s\n", *argv);
195 else
196 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
197 argv++;
198 }
199
200 if (returntosingle)
201 ret = 2;
202
203 exit(ret);
204 }
205
206 static int
207 argtoi(int flag, char *req, char *str, int base)
208 {
209 char *cp;
210 int ret;
211
212 ret = (int)strtol(str, &cp, base);
213 if (cp == str || *cp)
214 errx(EEXIT, "-%c flag requires a %s", flag, req);
215 return (ret);
216 }
217
218 /*
219 * Check the specified filesystem.
220 */
221 /* ARGSUSED */
222 static int
223 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
224 {
225 daddr_t n_ffree, n_bfree;
226 struct dups *dp;
227 struct zlncnt *zlnp;
228 int cylno;
229 #ifdef LITE2BORKEN
230 int flags;
231 #endif
232 #ifdef PROGRESS
233 off_t progress_total = 0;
234 #endif /* PROGRESS */
235
236 if (preen && child)
237 (void)signal(SIGQUIT, voidquit);
238 setcdevname(filesys, preen);
239 if (debug && preen)
240 pwarn("starting\n");
241 switch (setup(filesys)) {
242 case 0:
243 if (preen)
244 pfatal("CAN'T CHECK FILE SYSTEM.");
245 /* fall through */
246 case -1:
247 return (0);
248 }
249 /*
250 * Cleared if any questions answered no. Used to decide if
251 * the superblock should be marked clean.
252 */
253 resolved = 1;
254
255 #ifdef PROGRESS
256 /*
257 * Pass 1, Pass 4, and Pass 5 all iterate over cylinder
258 * groups. Account for those now. We'll never need to
259 * add in Pass 1b, since that pass is never executed when
260 * preening.
261 *
262 * Pass 2 and Pass 3 iterate over directory inodes, but we
263 * don't know how many of those exist until after Pass 1.
264 * We'll add those in after Pass 1 has completed.
265 */
266 if (preen)
267 progress_total += sblock->fs_ncg * 3;
268 #endif /* PROGRESS */
269
270 /*
271 * 1: scan inodes tallying blocks used
272 */
273 if (preen == 0) {
274 pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
275 if (hotroot())
276 pwarn("** Root file system\n");
277 pwarn("** Phase 1 - Check Blocks and Sizes\n");
278 }
279 pass1();
280
281 #ifdef PROGRESS
282 /* Account for number of directory inodes (used twice). */
283 if (preen)
284 progress_total += inplast * 2;
285 progress_switch(progress);
286 progress_init(progress_total);
287 #endif /* PROGRESS */
288
289
290 /*
291 * 1b: locate first references to duplicates, if any
292 */
293 if (duplist) {
294 if (preen)
295 pfatal("INTERNAL ERROR: dups with -p\n");
296 if (usedsoftdep)
297 pfatal("INTERNAL ERROR: dups with softdep\n");
298 pwarn("** Phase 1b - Rescan For More DUPS\n");
299 pass1b();
300 }
301
302 /*
303 * 2: traverse directories from root to mark all connected directories
304 */
305 if (preen == 0)
306 pwarn("** Phase 2 - Check Pathnames\n");
307 pass2();
308
309 /*
310 * 3: scan inodes looking for disconnected directories
311 */
312 if (preen == 0)
313 pwarn("** Phase 3 - Check Connectivity\n");
314 pass3();
315
316 /*
317 * 4: scan inodes looking for disconnected files; check reference counts
318 */
319 if (preen == 0)
320 pwarn("** Phase 4 - Check Reference Counts\n");
321 pass4();
322
323 /*
324 * 5: check and repair resource counts in cylinder groups
325 */
326 if (preen == 0)
327 pwarn("** Phase 5 - Check Cyl groups\n");
328 pass5();
329
330 /*
331 * print out summary statistics
332 */
333 n_ffree = sblock->fs_cstotal.cs_nffree;
334 n_bfree = sblock->fs_cstotal.cs_nbfree;
335 pwarn("%d files, %lld used, %lld free ",
336 n_files, (long long)n_blks,
337 (long long)(n_ffree + sblock->fs_frag * n_bfree));
338 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
339 (long long)n_ffree, (long long)n_bfree,
340 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
341 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
342 / (daddr_t)sblock->fs_dsize) % 10));
343 if (debug &&
344 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
345 printf("%d files missing\n", n_files);
346 if (debug) {
347 n_blks += sblock->fs_ncg *
348 (cgdmin(sblock, 0) - cgsblock(sblock, 0));
349 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
350 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
351 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
352 printf("%lld blocks missing\n", (long long)n_blks);
353 if (duplist != NULL) {
354 printf("The following duplicate blocks remain:");
355 for (dp = duplist; dp; dp = dp->next)
356 printf(" %lld,", (long long)dp->dup);
357 printf("\n");
358 }
359 if (zlnhead != NULL) {
360 printf("The following zero link count inodes remain:");
361 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
362 printf(" %u,", zlnp->zlncnt);
363 printf("\n");
364 }
365 }
366 zlnhead = (struct zlncnt *)0;
367 duplist = (struct dups *)0;
368 muldup = (struct dups *)0;
369 inocleanup();
370 if (fsmodified) {
371 sblock->fs_time = time(NULL);
372 sbdirty();
373 }
374 if (rerun)
375 markclean = 0;
376 #if LITE2BORKEN
377 if (!hotroot()) {
378 ckfini();
379 } else {
380 struct statvfs stfs_buf;
381 /*
382 * Check to see if root is mounted read-write.
383 */
384 if (statvfs("/", &stfs_buf) == 0)
385 flags = stfs_buf.f_flag;
386 else
387 flags = 0;
388 if (markclean)
389 markclean = flags & MNT_RDONLY;
390 ckfini();
391 }
392 #else
393 ckfini();
394 #endif
395 for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
396 if (inostathead[cylno].il_stat != NULL)
397 free(inostathead[cylno].il_stat);
398 free(inostathead);
399 inostathead = NULL;
400
401 if (!resolved || rerun) {
402 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
403 returntosingle = 1;
404 }
405 if (!fsmodified)
406 return (0);
407 if (!preen)
408 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
409 if (rerun)
410 pwarn("\n***** PLEASE RERUN FSCK *****\n");
411 if (hotroot()) {
412 struct statvfs stfs_buf;
413 /*
414 * We modified the root. Do a mount update on
415 * it, unless it is read-write, so we can continue.
416 */
417 if (statvfs("/", &stfs_buf) == 0) {
418 long flags = stfs_buf.f_flag;
419 struct ufs_args args;
420 int ret;
421
422 if (flags & MNT_RDONLY) {
423 args.fspec = 0;
424 args.export.ex_flags = 0;
425 args.export.ex_root = 0;
426 flags |= MNT_UPDATE | MNT_RELOAD;
427 ret = mount(MOUNT_FFS, "/", flags, &args);
428 if (ret == 0)
429 return(0);
430 }
431 }
432 if (!preen)
433 pwarn("\n***** REBOOT NOW *****\n");
434 sync();
435 return (4);
436 }
437 return (0);
438 }
439
440 static void
441 usage(void)
442 {
443
444 (void) fprintf(stderr,
445 "usage: %s [-adFfnPpqy] [-B be|le] [-b block] [-c level] [-m mode]"
446 " filesystem ...\n",
447 getprogname());
448 exit(1);
449 }
450
451