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