main.c revision 1.67 1 1.67 christos /* $NetBSD: main.c,v 1.67 2008/02/23 21:41:48 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.67 christos __RCSID("$NetBSD: main.c,v 1.67 2008/02/23 21:41:48 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 <stdio.h>
62 1.26 lukem #include <stdlib.h>
63 1.14 cgd #include <unistd.h>
64 1.64 drochner #include <signal.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.67 christos #include "exitvalues.h"
70 1.1 cgd
71 1.53 christos int progress = 0;
72 1.67 christos int returntosingle = 0;
73 1.23 christos
74 1.58 christos static int argtoi(int, const char *, const char *, int);
75 1.57 xtraeme static int checkfilesys(const char *, char *, long, int);
76 1.57 xtraeme static void usage(void);
77 1.23 christos
78 1.14 cgd int
79 1.56 xtraeme main(int argc, char *argv[])
80 1.1 cgd {
81 1.29 mjacob struct rlimit r;
82 1.1 cgd int ch;
83 1.67 christos int ret = FSCK_EXIT_OK;
84 1.1 cgd
85 1.29 mjacob if (getrlimit(RLIMIT_DATA, &r) == 0) {
86 1.29 mjacob r.rlim_cur = r.rlim_max;
87 1.29 mjacob (void) setrlimit(RLIMIT_DATA, &r);
88 1.29 mjacob }
89 1.1 cgd sync();
90 1.16 mycroft skipclean = 1;
91 1.31 bouyer markclean = 1;
92 1.40 lukem forceimage = 0;
93 1.31 bouyer endian = 0;
94 1.42 dbj isappleufs = 0;
95 1.53 christos while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqy")) != -1) {
96 1.1 cgd switch (ch) {
97 1.42 dbj case 'a':
98 1.42 dbj isappleufs = 1;
99 1.42 dbj break;
100 1.42 dbj
101 1.31 bouyer case 'B':
102 1.31 bouyer if (strcmp(optarg, "be") == 0)
103 1.31 bouyer endian = BIG_ENDIAN;
104 1.31 bouyer else if (strcmp(optarg, "le") == 0)
105 1.31 bouyer endian = LITTLE_ENDIAN;
106 1.31 bouyer else usage();
107 1.31 bouyer break;
108 1.31 bouyer
109 1.1 cgd case 'b':
110 1.16 mycroft skipclean = 0;
111 1.1 cgd bflag = argtoi('b', "number", optarg, 10);
112 1.1 cgd printf("Alternate super block location: %d\n", bflag);
113 1.1 cgd break;
114 1.1 cgd
115 1.1 cgd case 'c':
116 1.16 mycroft skipclean = 0;
117 1.13 mycroft cvtlevel = argtoi('c', "conversion level", optarg, 10);
118 1.49 dbj if (cvtlevel > 4) {
119 1.49 dbj cvtlevel = 4;
120 1.67 christos warnx("Using maximum conversion level of %d\n",
121 1.67 christos cvtlevel);
122 1.49 dbj }
123 1.1 cgd break;
124 1.13 mycroft
125 1.1 cgd case 'd':
126 1.1 cgd debug++;
127 1.17 cgd break;
128 1.17 cgd
129 1.40 lukem case 'F':
130 1.40 lukem forceimage = 1;
131 1.40 lukem break;
132 1.40 lukem
133 1.17 cgd case 'f':
134 1.17 cgd skipclean = 0;
135 1.1 cgd break;
136 1.1 cgd
137 1.1 cgd case 'm':
138 1.1 cgd lfmode = argtoi('m', "mode", optarg, 8);
139 1.1 cgd if (lfmode &~ 07777)
140 1.67 christos errx(FSCK_EXIT_USAGE, "bad mode to -m: %o",
141 1.67 christos 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.53 christos case 'P':
155 1.53 christos progress = 1;
156 1.53 christos break;
157 1.53 christos
158 1.47 dsl case 'q':
159 1.47 dsl quiet++;
160 1.47 dsl break;
161 1.47 dsl
162 1.1 cgd case 'y':
163 1.1 cgd yflag++;
164 1.1 cgd nflag = 0;
165 1.1 cgd break;
166 1.1 cgd
167 1.1 cgd default:
168 1.23 christos usage();
169 1.1 cgd }
170 1.1 cgd }
171 1.23 christos
172 1.1 cgd argc -= optind;
173 1.1 cgd argv += optind;
174 1.23 christos
175 1.23 christos if (!argc)
176 1.23 christos usage();
177 1.23 christos
178 1.53 christos if (debug)
179 1.53 christos progress = 0;
180 1.53 christos
181 1.1 cgd if (signal(SIGINT, SIG_IGN) != SIG_IGN)
182 1.1 cgd (void)signal(SIGINT, catch);
183 1.1 cgd if (preen)
184 1.1 cgd (void)signal(SIGQUIT, catchquit);
185 1.54 christos #ifdef PROGRESS
186 1.53 christos if (progress) {
187 1.53 christos progress_ttywidth(0);
188 1.53 christos (void)signal(SIGWINCH, progress_ttywidth);
189 1.53 christos }
190 1.54 christos #endif /* ! PROGRESS */
191 1.41 lukem signal(SIGINFO, infohandler);
192 1.20 christos
193 1.45 fvdl while (argc-- > 0) {
194 1.45 fvdl const char *path = blockcheck(*argv);
195 1.45 fvdl
196 1.45 fvdl if (path == NULL)
197 1.45 fvdl pfatal("Can't check %s\n", *argv);
198 1.67 christos else {
199 1.67 christos int nret = checkfilesys(blockcheck(*argv), 0, 0L, 0);
200 1.67 christos if (ret < nret)
201 1.67 christos ret = nret;
202 1.67 christos }
203 1.45 fvdl argv++;
204 1.45 fvdl }
205 1.20 christos
206 1.67 christos return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
207 1.1 cgd }
208 1.1 cgd
209 1.23 christos static int
210 1.58 christos argtoi(int flag, const char *req, const char *str, int base)
211 1.1 cgd {
212 1.1 cgd char *cp;
213 1.1 cgd int ret;
214 1.1 cgd
215 1.1 cgd ret = (int)strtol(str, &cp, base);
216 1.1 cgd if (cp == str || *cp)
217 1.67 christos errx(FSCK_EXIT_USAGE, "-%c flag requires a %s",
218 1.67 christos flag, req);
219 1.1 cgd return (ret);
220 1.1 cgd }
221 1.1 cgd
222 1.1 cgd /*
223 1.1 cgd * Check the specified filesystem.
224 1.1 cgd */
225 1.1 cgd /* ARGSUSED */
226 1.23 christos static int
227 1.56 xtraeme checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
228 1.1 cgd {
229 1.43 fvdl daddr_t n_ffree, n_bfree;
230 1.1 cgd struct dups *dp;
231 1.1 cgd struct zlncnt *zlnp;
232 1.45 fvdl int cylno;
233 1.27 lukem #ifdef LITE2BORKEN
234 1.36 thorpej int flags;
235 1.27 lukem #endif
236 1.54 christos #ifdef PROGRESS
237 1.61 apb /*
238 1.61 apb * In prune mode, how far does the progress bar travel during
239 1.61 apb * each pass? (In non-prune mode, each pass has a separate
240 1.61 apb * progress bar that travels from 0 to 100%.)
241 1.61 apb *
242 1.61 apb * The numbers below are percentages, intended to correspond
243 1.61 apb * roughly to the cumulative time up to the end of each pass.
244 1.61 apb * They don't have to be accurate. In reality, on a large
245 1.61 apb * file system, Pass 1 and Pass 2 together are likely to use
246 1.61 apb * significantly more than the 95% reflected below, so users
247 1.61 apb * will get a pleasant surprise when the last 5% of the progress
248 1.61 apb * bar runs more quickly than they had expected.
249 1.61 apb */
250 1.61 apb static int progress_limits[] = {0, 20, 95, 96, 97, 100};
251 1.54 christos #endif /* PROGRESS */
252 1.1 cgd
253 1.1 cgd if (preen && child)
254 1.1 cgd (void)signal(SIGQUIT, voidquit);
255 1.20 christos setcdevname(filesys, preen);
256 1.1 cgd if (debug && preen)
257 1.1 cgd pwarn("starting\n");
258 1.16 mycroft switch (setup(filesys)) {
259 1.16 mycroft case 0:
260 1.1 cgd if (preen)
261 1.1 cgd pfatal("CAN'T CHECK FILE SYSTEM.");
262 1.25 lukem /* fall through */
263 1.16 mycroft case -1:
264 1.67 christos return FSCK_EXIT_OK;
265 1.1 cgd }
266 1.13 mycroft /*
267 1.34 fvdl * Cleared if any questions answered no. Used to decide if
268 1.34 fvdl * the superblock should be marked clean.
269 1.34 fvdl */
270 1.34 fvdl resolved = 1;
271 1.53 christos
272 1.54 christos #ifdef PROGRESS
273 1.61 apb progress_switch(progress);
274 1.61 apb progress_init();
275 1.54 christos #endif /* PROGRESS */
276 1.53 christos
277 1.34 fvdl /*
278 1.13 mycroft * 1: scan inodes tallying blocks used
279 1.13 mycroft */
280 1.13 mycroft if (preen == 0) {
281 1.47 dsl pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
282 1.20 christos if (hotroot())
283 1.47 dsl pwarn("** Root file system\n");
284 1.47 dsl pwarn("** Phase 1 - Check Blocks and Sizes\n");
285 1.13 mycroft }
286 1.54 christos #ifdef PROGRESS
287 1.53 christos if (preen)
288 1.62 apb progress_setrange(0, progress_limits[1]);
289 1.54 christos #endif /* PROGRESS */
290 1.61 apb pass1();
291 1.53 christos
292 1.1 cgd /*
293 1.13 mycroft * 1b: locate first references to duplicates, if any
294 1.1 cgd */
295 1.13 mycroft if (duplist) {
296 1.35 mycroft if (preen)
297 1.35 mycroft pfatal("INTERNAL ERROR: dups with -p\n");
298 1.35 mycroft if (usedsoftdep)
299 1.35 mycroft pfatal("INTERNAL ERROR: dups with softdep\n");
300 1.47 dsl pwarn("** Phase 1b - Rescan For More DUPS\n");
301 1.13 mycroft pass1b();
302 1.13 mycroft }
303 1.9 cgd
304 1.13 mycroft /*
305 1.13 mycroft * 2: traverse directories from root to mark all connected directories
306 1.13 mycroft */
307 1.13 mycroft if (preen == 0)
308 1.47 dsl pwarn("** Phase 2 - Check Pathnames\n");
309 1.61 apb #ifdef PROGRESS
310 1.61 apb if (preen)
311 1.61 apb progress_sethighlim(progress_limits[2]);
312 1.61 apb #endif /* PROGRESS */
313 1.13 mycroft pass2();
314 1.1 cgd
315 1.13 mycroft /*
316 1.13 mycroft * 3: scan inodes looking for disconnected directories
317 1.13 mycroft */
318 1.13 mycroft if (preen == 0)
319 1.47 dsl pwarn("** Phase 3 - Check Connectivity\n");
320 1.61 apb #ifdef PROGRESS
321 1.61 apb if (preen)
322 1.61 apb progress_sethighlim(progress_limits[3]);
323 1.61 apb #endif /* PROGRESS */
324 1.13 mycroft pass3();
325 1.1 cgd
326 1.13 mycroft /*
327 1.13 mycroft * 4: scan inodes looking for disconnected files; check reference counts
328 1.13 mycroft */
329 1.13 mycroft if (preen == 0)
330 1.47 dsl pwarn("** Phase 4 - Check Reference Counts\n");
331 1.61 apb #ifdef PROGRESS
332 1.61 apb if (preen)
333 1.61 apb progress_sethighlim(progress_limits[4]);
334 1.61 apb #endif /* PROGRESS */
335 1.13 mycroft pass4();
336 1.5 mycroft
337 1.13 mycroft /*
338 1.13 mycroft * 5: check and repair resource counts in cylinder groups
339 1.13 mycroft */
340 1.13 mycroft if (preen == 0)
341 1.47 dsl pwarn("** Phase 5 - Check Cyl groups\n");
342 1.61 apb #ifdef PROGRESS
343 1.61 apb if (preen)
344 1.61 apb progress_sethighlim(progress_limits[5]);
345 1.61 apb #endif /* PROGRESS */
346 1.13 mycroft pass5();
347 1.1 cgd
348 1.1 cgd /*
349 1.1 cgd * print out summary statistics
350 1.1 cgd */
351 1.31 bouyer n_ffree = sblock->fs_cstotal.cs_nffree;
352 1.31 bouyer n_bfree = sblock->fs_cstotal.cs_nbfree;
353 1.59 christos pwarn("%llu files, %lld used, %lld free ",
354 1.59 christos (unsigned long long)n_files, (long long)n_blks,
355 1.44 mrg (long long)(n_ffree + sblock->fs_frag * n_bfree));
356 1.43 fvdl printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
357 1.43 fvdl (long long)n_ffree, (long long)n_bfree,
358 1.44 mrg (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
359 1.44 mrg (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
360 1.44 mrg / (daddr_t)sblock->fs_dsize) % 10));
361 1.1 cgd if (debug &&
362 1.31 bouyer (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
363 1.59 christos printf("%llu files missing\n", (unsigned long long)n_files);
364 1.1 cgd if (debug) {
365 1.31 bouyer n_blks += sblock->fs_ncg *
366 1.31 bouyer (cgdmin(sblock, 0) - cgsblock(sblock, 0));
367 1.31 bouyer n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
368 1.31 bouyer n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
369 1.31 bouyer if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
370 1.43 fvdl printf("%lld blocks missing\n", (long long)n_blks);
371 1.1 cgd if (duplist != NULL) {
372 1.1 cgd printf("The following duplicate blocks remain:");
373 1.1 cgd for (dp = duplist; dp; dp = dp->next)
374 1.43 fvdl printf(" %lld,", (long long)dp->dup);
375 1.1 cgd printf("\n");
376 1.1 cgd }
377 1.1 cgd if (zlnhead != NULL) {
378 1.1 cgd printf("The following zero link count inodes remain:");
379 1.1 cgd for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
380 1.59 christos printf(" %llu,",
381 1.59 christos (unsigned long long)zlnp->zlncnt);
382 1.1 cgd printf("\n");
383 1.1 cgd }
384 1.1 cgd }
385 1.1 cgd zlnhead = (struct zlncnt *)0;
386 1.1 cgd duplist = (struct dups *)0;
387 1.13 mycroft muldup = (struct dups *)0;
388 1.1 cgd inocleanup();
389 1.1 cgd if (fsmodified) {
390 1.45 fvdl sblock->fs_time = time(NULL);
391 1.1 cgd sbdirty();
392 1.13 mycroft }
393 1.34 fvdl if (rerun)
394 1.34 fvdl markclean = 0;
395 1.27 lukem #if LITE2BORKEN
396 1.25 lukem if (!hotroot()) {
397 1.31 bouyer ckfini();
398 1.25 lukem } else {
399 1.50 christos struct statvfs stfs_buf;
400 1.25 lukem /*
401 1.25 lukem * Check to see if root is mounted read-write.
402 1.25 lukem */
403 1.50 christos if (statvfs("/", &stfs_buf) == 0)
404 1.50 christos flags = stfs_buf.f_flag;
405 1.25 lukem else
406 1.25 lukem flags = 0;
407 1.31 bouyer if (markclean)
408 1.31 bouyer markclean = flags & MNT_RDONLY;
409 1.31 bouyer ckfini();
410 1.25 lukem }
411 1.27 lukem #else
412 1.31 bouyer ckfini();
413 1.27 lukem #endif
414 1.45 fvdl for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
415 1.45 fvdl if (inostathead[cylno].il_stat != NULL)
416 1.45 fvdl free(inostathead[cylno].il_stat);
417 1.45 fvdl free(inostathead);
418 1.45 fvdl inostathead = NULL;
419 1.45 fvdl
420 1.52 dbj if (!resolved || rerun) {
421 1.52 dbj pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
422 1.52 dbj returntosingle = 1;
423 1.52 dbj }
424 1.1 cgd if (!fsmodified)
425 1.67 christos return FSCK_EXIT_OK;
426 1.13 mycroft if (!preen)
427 1.47 dsl pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
428 1.22 thorpej if (rerun)
429 1.47 dsl pwarn("\n***** PLEASE RERUN FSCK *****\n");
430 1.20 christos if (hotroot()) {
431 1.50 christos struct statvfs stfs_buf;
432 1.13 mycroft /*
433 1.13 mycroft * We modified the root. Do a mount update on
434 1.13 mycroft * it, unless it is read-write, so we can continue.
435 1.13 mycroft */
436 1.50 christos if (statvfs("/", &stfs_buf) == 0) {
437 1.50 christos long flags = stfs_buf.f_flag;
438 1.13 mycroft struct ufs_args args;
439 1.13 mycroft
440 1.13 mycroft if (flags & MNT_RDONLY) {
441 1.13 mycroft args.fspec = 0;
442 1.13 mycroft flags |= MNT_UPDATE | MNT_RELOAD;
443 1.67 christos if (mount(MOUNT_FFS, "/", flags,
444 1.67 christos &args, sizeof args) == 0)
445 1.67 christos return FSCK_EXIT_OK;
446 1.13 mycroft }
447 1.13 mycroft }
448 1.13 mycroft if (!preen)
449 1.47 dsl pwarn("\n***** REBOOT NOW *****\n");
450 1.1 cgd sync();
451 1.67 christos return FSCK_EXIT_ROOT_CHANGED;
452 1.1 cgd }
453 1.67 christos return FSCK_EXIT_OK;
454 1.1 cgd }
455 1.23 christos
456 1.23 christos static void
457 1.56 xtraeme usage(void)
458 1.23 christos {
459 1.38 cgd
460 1.23 christos (void) fprintf(stderr,
461 1.55 wiz "usage: %s [-adFfnPpqy] [-B be|le] [-b block] [-c level] [-m mode]"
462 1.40 lukem " filesystem ...\n",
463 1.38 cgd getprogname());
464 1.67 christos exit(FSCK_EXIT_USAGE);
465 1.23 christos }
466 1.23 christos
467