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