main.c revision 1.59 1 1.59 christos /* $NetBSD: main.c,v 1.59 2005/08/19 02:07:19 christos Exp $ */
2 1.15 cgd
3 1.1 cgd /*
4 1.13 mycroft * Copyright (c) 1980, 1986, 1993
5 1.13 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.46 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.24 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.24 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
35 1.24 lukem The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.15 cgd #if 0
40 1.25 lukem static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
41 1.15 cgd #else
42 1.59 christos __RCSID("$NetBSD: main.c,v 1.59 2005/08/19 02:07:19 christos Exp $");
43 1.15 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd #include <sys/param.h>
47 1.12 cgd #include <sys/time.h>
48 1.13 mycroft #include <sys/mount.h>
49 1.29 mjacob #include <sys/resource.h>
50 1.25 lukem
51 1.13 mycroft #include <ufs/ufs/dinode.h>
52 1.30 fvdl #include <ufs/ufs/ufsmount.h>
53 1.13 mycroft #include <ufs/ffs/fs.h>
54 1.31 bouyer #include <ufs/ffs/ffs_extern.h>
55 1.25 lukem
56 1.25 lukem #include <ctype.h>
57 1.25 lukem #include <err.h>
58 1.1 cgd #include <fstab.h>
59 1.1 cgd #include <string.h>
60 1.32 kleink #include <time.h>
61 1.1 cgd #include <ctype.h>
62 1.1 cgd #include <stdio.h>
63 1.26 lukem #include <stdlib.h>
64 1.14 cgd #include <unistd.h>
65 1.14 cgd
66 1.1 cgd #include "fsck.h"
67 1.14 cgd #include "extern.h"
68 1.21 christos #include "fsutil.h"
69 1.1 cgd
70 1.1 cgd int returntosingle;
71 1.53 christos int progress = 0;
72 1.23 christos
73 1.58 christos static int argtoi(int, const char *, const char *, int);
74 1.57 xtraeme static int checkfilesys(const char *, char *, long, int);
75 1.57 xtraeme static void usage(void);
76 1.23 christos
77 1.14 cgd int
78 1.56 xtraeme main(int argc, char *argv[])
79 1.1 cgd {
80 1.29 mjacob struct rlimit r;
81 1.1 cgd int ch;
82 1.20 christos int ret = 0;
83 1.1 cgd
84 1.29 mjacob if (getrlimit(RLIMIT_DATA, &r) == 0) {
85 1.29 mjacob r.rlim_cur = r.rlim_max;
86 1.29 mjacob (void) setrlimit(RLIMIT_DATA, &r);
87 1.29 mjacob }
88 1.1 cgd sync();
89 1.16 mycroft skipclean = 1;
90 1.31 bouyer markclean = 1;
91 1.40 lukem forceimage = 0;
92 1.31 bouyer endian = 0;
93 1.42 dbj isappleufs = 0;
94 1.53 christos while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqy")) != -1) {
95 1.1 cgd switch (ch) {
96 1.42 dbj case 'a':
97 1.42 dbj isappleufs = 1;
98 1.42 dbj break;
99 1.42 dbj
100 1.31 bouyer case 'B':
101 1.31 bouyer if (strcmp(optarg, "be") == 0)
102 1.31 bouyer endian = BIG_ENDIAN;
103 1.31 bouyer else if (strcmp(optarg, "le") == 0)
104 1.31 bouyer endian = LITTLE_ENDIAN;
105 1.31 bouyer else usage();
106 1.31 bouyer break;
107 1.31 bouyer
108 1.1 cgd case 'b':
109 1.16 mycroft skipclean = 0;
110 1.1 cgd bflag = argtoi('b', "number", optarg, 10);
111 1.1 cgd printf("Alternate super block location: %d\n", bflag);
112 1.1 cgd break;
113 1.1 cgd
114 1.1 cgd case 'c':
115 1.16 mycroft skipclean = 0;
116 1.13 mycroft cvtlevel = argtoi('c', "conversion level", optarg, 10);
117 1.49 dbj if (cvtlevel > 4) {
118 1.49 dbj cvtlevel = 4;
119 1.49 dbj warnx("Using maximum conversion level of %d\n",cvtlevel);
120 1.49 dbj }
121 1.1 cgd break;
122 1.13 mycroft
123 1.1 cgd case 'd':
124 1.1 cgd debug++;
125 1.17 cgd break;
126 1.17 cgd
127 1.40 lukem case 'F':
128 1.40 lukem forceimage = 1;
129 1.40 lukem break;
130 1.40 lukem
131 1.17 cgd case 'f':
132 1.17 cgd skipclean = 0;
133 1.1 cgd break;
134 1.1 cgd
135 1.1 cgd case 'm':
136 1.1 cgd lfmode = argtoi('m', "mode", optarg, 8);
137 1.1 cgd if (lfmode &~ 07777)
138 1.25 lukem errx(EEXIT, "bad mode to -m: %o", lfmode);
139 1.1 cgd printf("** lost+found creation mode %o\n", lfmode);
140 1.1 cgd break;
141 1.1 cgd
142 1.1 cgd case 'n':
143 1.1 cgd nflag++;
144 1.1 cgd yflag = 0;
145 1.1 cgd break;
146 1.1 cgd
147 1.23 christos case 'p':
148 1.23 christos preen++;
149 1.23 christos break;
150 1.23 christos
151 1.53 christos case 'P':
152 1.53 christos progress = 1;
153 1.53 christos break;
154 1.53 christos
155 1.47 dsl case 'q':
156 1.47 dsl quiet++;
157 1.47 dsl break;
158 1.47 dsl
159 1.1 cgd case 'y':
160 1.1 cgd yflag++;
161 1.1 cgd nflag = 0;
162 1.1 cgd break;
163 1.1 cgd
164 1.1 cgd default:
165 1.23 christos usage();
166 1.1 cgd }
167 1.1 cgd }
168 1.23 christos
169 1.1 cgd argc -= optind;
170 1.1 cgd argv += optind;
171 1.23 christos
172 1.23 christos if (!argc)
173 1.23 christos usage();
174 1.23 christos
175 1.53 christos if (debug)
176 1.53 christos progress = 0;
177 1.53 christos
178 1.1 cgd if (signal(SIGINT, SIG_IGN) != SIG_IGN)
179 1.1 cgd (void)signal(SIGINT, catch);
180 1.1 cgd if (preen)
181 1.1 cgd (void)signal(SIGQUIT, catchquit);
182 1.54 christos #ifdef PROGRESS
183 1.53 christos if (progress) {
184 1.53 christos progress_ttywidth(0);
185 1.53 christos (void)signal(SIGWINCH, progress_ttywidth);
186 1.53 christos }
187 1.54 christos #endif /* ! PROGRESS */
188 1.41 lukem signal(SIGINFO, infohandler);
189 1.20 christos
190 1.45 fvdl while (argc-- > 0) {
191 1.45 fvdl const char *path = blockcheck(*argv);
192 1.45 fvdl
193 1.45 fvdl if (path == NULL)
194 1.45 fvdl pfatal("Can't check %s\n", *argv);
195 1.45 fvdl else
196 1.45 fvdl (void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
197 1.45 fvdl argv++;
198 1.45 fvdl }
199 1.20 christos
200 1.1 cgd if (returntosingle)
201 1.20 christos ret = 2;
202 1.20 christos
203 1.1 cgd exit(ret);
204 1.1 cgd }
205 1.1 cgd
206 1.23 christos static int
207 1.58 christos argtoi(int flag, const char *req, const char *str, int base)
208 1.1 cgd {
209 1.1 cgd char *cp;
210 1.1 cgd int ret;
211 1.1 cgd
212 1.1 cgd ret = (int)strtol(str, &cp, base);
213 1.1 cgd if (cp == str || *cp)
214 1.25 lukem errx(EEXIT, "-%c flag requires a %s", flag, req);
215 1.1 cgd return (ret);
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd /*
219 1.1 cgd * Check the specified filesystem.
220 1.1 cgd */
221 1.1 cgd /* ARGSUSED */
222 1.23 christos static int
223 1.56 xtraeme checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
224 1.1 cgd {
225 1.43 fvdl daddr_t n_ffree, n_bfree;
226 1.1 cgd struct dups *dp;
227 1.1 cgd struct zlncnt *zlnp;
228 1.45 fvdl int cylno;
229 1.27 lukem #ifdef LITE2BORKEN
230 1.36 thorpej int flags;
231 1.27 lukem #endif
232 1.54 christos #ifdef PROGRESS
233 1.53 christos off_t progress_total = 0;
234 1.54 christos #endif /* PROGRESS */
235 1.1 cgd
236 1.1 cgd if (preen && child)
237 1.1 cgd (void)signal(SIGQUIT, voidquit);
238 1.20 christos setcdevname(filesys, preen);
239 1.1 cgd if (debug && preen)
240 1.1 cgd pwarn("starting\n");
241 1.16 mycroft switch (setup(filesys)) {
242 1.16 mycroft case 0:
243 1.1 cgd if (preen)
244 1.1 cgd pfatal("CAN'T CHECK FILE SYSTEM.");
245 1.25 lukem /* fall through */
246 1.16 mycroft case -1:
247 1.1 cgd return (0);
248 1.1 cgd }
249 1.13 mycroft /*
250 1.34 fvdl * Cleared if any questions answered no. Used to decide if
251 1.34 fvdl * the superblock should be marked clean.
252 1.34 fvdl */
253 1.34 fvdl resolved = 1;
254 1.53 christos
255 1.54 christos #ifdef PROGRESS
256 1.53 christos /*
257 1.53 christos * Pass 1, Pass 4, and Pass 5 all iterate over cylinder
258 1.53 christos * groups. Account for those now. We'll never need to
259 1.53 christos * add in Pass 1b, since that pass is never executed when
260 1.53 christos * preening.
261 1.53 christos *
262 1.53 christos * Pass 2 and Pass 3 iterate over directory inodes, but we
263 1.53 christos * don't know how many of those exist until after Pass 1.
264 1.53 christos * We'll add those in after Pass 1 has completed.
265 1.53 christos */
266 1.53 christos if (preen)
267 1.53 christos progress_total += sblock->fs_ncg * 3;
268 1.54 christos #endif /* PROGRESS */
269 1.53 christos
270 1.34 fvdl /*
271 1.13 mycroft * 1: scan inodes tallying blocks used
272 1.13 mycroft */
273 1.13 mycroft if (preen == 0) {
274 1.47 dsl pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
275 1.20 christos if (hotroot())
276 1.47 dsl pwarn("** Root file system\n");
277 1.47 dsl pwarn("** Phase 1 - Check Blocks and Sizes\n");
278 1.13 mycroft }
279 1.13 mycroft pass1();
280 1.1 cgd
281 1.54 christos #ifdef PROGRESS
282 1.53 christos /* Account for number of directory inodes (used twice). */
283 1.53 christos if (preen)
284 1.53 christos progress_total += inplast * 2;
285 1.53 christos progress_switch(progress);
286 1.53 christos progress_init(progress_total);
287 1.54 christos #endif /* PROGRESS */
288 1.53 christos
289 1.53 christos
290 1.1 cgd /*
291 1.13 mycroft * 1b: locate first references to duplicates, if any
292 1.1 cgd */
293 1.13 mycroft if (duplist) {
294 1.35 mycroft if (preen)
295 1.35 mycroft pfatal("INTERNAL ERROR: dups with -p\n");
296 1.35 mycroft if (usedsoftdep)
297 1.35 mycroft pfatal("INTERNAL ERROR: dups with softdep\n");
298 1.47 dsl pwarn("** Phase 1b - Rescan For More DUPS\n");
299 1.13 mycroft pass1b();
300 1.13 mycroft }
301 1.9 cgd
302 1.13 mycroft /*
303 1.13 mycroft * 2: traverse directories from root to mark all connected directories
304 1.13 mycroft */
305 1.13 mycroft if (preen == 0)
306 1.47 dsl pwarn("** Phase 2 - Check Pathnames\n");
307 1.13 mycroft pass2();
308 1.1 cgd
309 1.13 mycroft /*
310 1.13 mycroft * 3: scan inodes looking for disconnected directories
311 1.13 mycroft */
312 1.13 mycroft if (preen == 0)
313 1.47 dsl pwarn("** Phase 3 - Check Connectivity\n");
314 1.13 mycroft pass3();
315 1.1 cgd
316 1.13 mycroft /*
317 1.13 mycroft * 4: scan inodes looking for disconnected files; check reference counts
318 1.13 mycroft */
319 1.13 mycroft if (preen == 0)
320 1.47 dsl pwarn("** Phase 4 - Check Reference Counts\n");
321 1.13 mycroft pass4();
322 1.5 mycroft
323 1.13 mycroft /*
324 1.13 mycroft * 5: check and repair resource counts in cylinder groups
325 1.13 mycroft */
326 1.13 mycroft if (preen == 0)
327 1.47 dsl pwarn("** Phase 5 - Check Cyl groups\n");
328 1.13 mycroft pass5();
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * print out summary statistics
332 1.1 cgd */
333 1.31 bouyer n_ffree = sblock->fs_cstotal.cs_nffree;
334 1.31 bouyer n_bfree = sblock->fs_cstotal.cs_nbfree;
335 1.59 christos pwarn("%llu files, %lld used, %lld free ",
336 1.59 christos (unsigned long long)n_files, (long long)n_blks,
337 1.44 mrg (long long)(n_ffree + sblock->fs_frag * n_bfree));
338 1.43 fvdl printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
339 1.43 fvdl (long long)n_ffree, (long long)n_bfree,
340 1.44 mrg (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
341 1.44 mrg (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
342 1.44 mrg / (daddr_t)sblock->fs_dsize) % 10));
343 1.1 cgd if (debug &&
344 1.31 bouyer (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
345 1.59 christos printf("%llu files missing\n", (unsigned long long)n_files);
346 1.1 cgd if (debug) {
347 1.31 bouyer n_blks += sblock->fs_ncg *
348 1.31 bouyer (cgdmin(sblock, 0) - cgsblock(sblock, 0));
349 1.31 bouyer n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
350 1.31 bouyer n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
351 1.31 bouyer if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
352 1.43 fvdl printf("%lld blocks missing\n", (long long)n_blks);
353 1.1 cgd if (duplist != NULL) {
354 1.1 cgd printf("The following duplicate blocks remain:");
355 1.1 cgd for (dp = duplist; dp; dp = dp->next)
356 1.43 fvdl printf(" %lld,", (long long)dp->dup);
357 1.1 cgd printf("\n");
358 1.1 cgd }
359 1.1 cgd if (zlnhead != NULL) {
360 1.1 cgd printf("The following zero link count inodes remain:");
361 1.1 cgd for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
362 1.59 christos printf(" %llu,",
363 1.59 christos (unsigned long long)zlnp->zlncnt);
364 1.1 cgd printf("\n");
365 1.1 cgd }
366 1.1 cgd }
367 1.1 cgd zlnhead = (struct zlncnt *)0;
368 1.1 cgd duplist = (struct dups *)0;
369 1.13 mycroft muldup = (struct dups *)0;
370 1.1 cgd inocleanup();
371 1.1 cgd if (fsmodified) {
372 1.45 fvdl sblock->fs_time = time(NULL);
373 1.1 cgd sbdirty();
374 1.13 mycroft }
375 1.34 fvdl if (rerun)
376 1.34 fvdl markclean = 0;
377 1.27 lukem #if LITE2BORKEN
378 1.25 lukem if (!hotroot()) {
379 1.31 bouyer ckfini();
380 1.25 lukem } else {
381 1.50 christos struct statvfs stfs_buf;
382 1.25 lukem /*
383 1.25 lukem * Check to see if root is mounted read-write.
384 1.25 lukem */
385 1.50 christos if (statvfs("/", &stfs_buf) == 0)
386 1.50 christos flags = stfs_buf.f_flag;
387 1.25 lukem else
388 1.25 lukem flags = 0;
389 1.31 bouyer if (markclean)
390 1.31 bouyer markclean = flags & MNT_RDONLY;
391 1.31 bouyer ckfini();
392 1.25 lukem }
393 1.27 lukem #else
394 1.31 bouyer ckfini();
395 1.27 lukem #endif
396 1.45 fvdl for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
397 1.45 fvdl if (inostathead[cylno].il_stat != NULL)
398 1.45 fvdl free(inostathead[cylno].il_stat);
399 1.45 fvdl free(inostathead);
400 1.45 fvdl inostathead = NULL;
401 1.45 fvdl
402 1.52 dbj if (!resolved || rerun) {
403 1.52 dbj pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
404 1.52 dbj returntosingle = 1;
405 1.52 dbj }
406 1.1 cgd if (!fsmodified)
407 1.1 cgd return (0);
408 1.13 mycroft if (!preen)
409 1.47 dsl pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
410 1.22 thorpej if (rerun)
411 1.47 dsl pwarn("\n***** PLEASE RERUN FSCK *****\n");
412 1.20 christos if (hotroot()) {
413 1.50 christos struct statvfs stfs_buf;
414 1.13 mycroft /*
415 1.13 mycroft * We modified the root. Do a mount update on
416 1.13 mycroft * it, unless it is read-write, so we can continue.
417 1.13 mycroft */
418 1.50 christos if (statvfs("/", &stfs_buf) == 0) {
419 1.50 christos long flags = stfs_buf.f_flag;
420 1.13 mycroft struct ufs_args args;
421 1.13 mycroft int ret;
422 1.13 mycroft
423 1.13 mycroft if (flags & MNT_RDONLY) {
424 1.13 mycroft args.fspec = 0;
425 1.13 mycroft args.export.ex_flags = 0;
426 1.13 mycroft args.export.ex_root = 0;
427 1.13 mycroft flags |= MNT_UPDATE | MNT_RELOAD;
428 1.19 jtc ret = mount(MOUNT_FFS, "/", flags, &args);
429 1.13 mycroft if (ret == 0)
430 1.13 mycroft return(0);
431 1.13 mycroft }
432 1.13 mycroft }
433 1.13 mycroft if (!preen)
434 1.47 dsl pwarn("\n***** REBOOT NOW *****\n");
435 1.1 cgd sync();
436 1.1 cgd return (4);
437 1.1 cgd }
438 1.1 cgd return (0);
439 1.1 cgd }
440 1.23 christos
441 1.23 christos static void
442 1.56 xtraeme usage(void)
443 1.23 christos {
444 1.38 cgd
445 1.23 christos (void) fprintf(stderr,
446 1.55 wiz "usage: %s [-adFfnPpqy] [-B be|le] [-b block] [-c level] [-m mode]"
447 1.40 lukem " filesystem ...\n",
448 1.38 cgd getprogname());
449 1.23 christos exit(1);
450 1.23 christos }
451 1.23 christos
452