1 /* $NetBSD: main.c,v 1.72 2008/10/09 18:38:24 wiz Exp $ */
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
42 __RCSID("$NetBSD: main.c,v 1.72 2008/10/09 18:38:24 wiz Exp $");
46 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
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>
70 #include "exitvalues.h"
74 int returntosingle
= 0;
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 static char *get_snap_device(char *);
82 main(int argc
, char *argv
[])
86 int ret
= FSCK_EXIT_OK
;
87 char *snap_backup
= NULL
;
88 int snap_internal
= 0;
90 if (getrlimit(RLIMIT_DATA
, &r
) == 0) {
91 r
.rlim_cur
= r
.rlim_max
;
92 (void) setrlimit(RLIMIT_DATA
, &r
);
100 while ((ch
= getopt(argc
, argv
, "aB:b:c:dFfm:npPqUyx:X")) != -1) {
107 if (strcmp(optarg
, "be") == 0)
109 else if (strcmp(optarg
, "le") == 0)
110 endian
= LITTLE_ENDIAN
;
116 bflag
= argtoi('b', "number", optarg
, 10);
117 printf("Alternate super block location: %d\n", bflag
);
122 cvtlevel
= argtoi('c', "conversion level", optarg
, 10);
125 warnx("Using maximum conversion level of %d\n",
143 lfmode
= argtoi('m', "mode", optarg
, 8);
145 errx(FSCK_EXIT_USAGE
, "bad mode to -m: %o",
147 printf("** lost+found creation mode %o\n", lfmode
);
177 snap_backup
= optarg
;
188 if (snap_backup
|| snap_internal
) {
189 if (!nflag
|| yflag
) {
190 warnx("Cannot use -x or -X without -n\n");
206 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
207 (void)signal(SIGINT
, catch);
209 (void)signal(SIGQUIT
, catchquit
);
212 progress_ttywidth(0);
213 (void)signal(SIGWINCH
, progress_ttywidth
);
215 #endif /* ! PROGRESS */
216 signal(SIGINFO
, infohandler
);
220 char *path
= strdup(blockcheck(*argv
));
223 pfatal("Can't check %s\n", *argv
);
225 if (snap_backup
|| snap_internal
) {
230 mpt
= get_snap_device(*argv
);
233 snapfd
= snap_open(mpt
, snap_backup
, NULL
, &snap_dev
);
235 warn("can't take snapshot of %s", mpt
);
239 nret
= checkfilesys(blockcheck(snap_dev
), path
, 0);
245 nret
= checkfilesys(path
, path
, 0);
254 return returntosingle
? FSCK_EXIT_UNRESOLVED
: ret
;
258 argtoi(int flag
, const char *req
, const char *str
, int base
)
263 ret
= (int)strtol(str
, &cp
, base
);
264 if (cp
== str
|| *cp
)
265 errx(FSCK_EXIT_USAGE
, "-%c flag requires a %s",
271 * Check the specified filesystem.
275 checkfilesys(const char *filesys
, const char *origfs
, int child
)
277 daddr_t n_ffree
, n_bfree
;
286 * In prune mode, how far does the progress bar travel during
287 * each pass? (In non-prune mode, each pass has a separate
288 * progress bar that travels from 0 to 100%.)
290 * The numbers below are percentages, intended to correspond
291 * roughly to the cumulative time up to the end of each pass.
292 * They don't have to be accurate. In reality, on a large
293 * file system, Pass 1 and Pass 2 together are likely to use
294 * significantly more than the 95% reflected below, so users
295 * will get a pleasant surprise when the last 5% of the progress
296 * bar runs more quickly than they had expected.
298 static int progress_limits
[] = {0, 20, 95, 96, 97, 100};
299 #endif /* PROGRESS */
302 (void)signal(SIGQUIT
, voidquit
);
303 setcdevname(filesys
, preen
);
306 switch (setup(filesys
, origfs
)) {
309 pfatal("CAN'T CHECK FILE SYSTEM.");
315 * Cleared if any questions answered no. Used to decide if
316 * the superblock should be marked clean.
321 progress_switch(progress
);
323 #endif /* PROGRESS */
326 * 1: scan inodes tallying blocks used
329 pwarn("** Last Mounted on %s\n", sblock
->fs_fsmnt
);
331 pwarn("** Root file system\n");
332 pwarn("** Phase 1 - Check Blocks and Sizes\n");
336 progress_setrange(0, progress_limits
[1]);
337 #endif /* PROGRESS */
341 * 1b: locate first references to duplicates, if any
345 pfatal("INTERNAL ERROR: dups with -p\n");
347 pfatal("INTERNAL ERROR: dups with softdep\n");
348 pwarn("** Phase 1b - Rescan For More DUPS\n");
353 * 2: traverse directories from root to mark all connected directories
356 pwarn("** Phase 2 - Check Pathnames\n");
359 progress_sethighlim(progress_limits
[2]);
360 #endif /* PROGRESS */
364 * 3: scan inodes looking for disconnected directories
367 pwarn("** Phase 3 - Check Connectivity\n");
370 progress_sethighlim(progress_limits
[3]);
371 #endif /* PROGRESS */
375 * 4: scan inodes looking for disconnected files; check reference counts
378 pwarn("** Phase 4 - Check Reference Counts\n");
381 progress_sethighlim(progress_limits
[4]);
382 #endif /* PROGRESS */
386 * 5: check and repair resource counts in cylinder groups
389 pwarn("** Phase 5 - Check Cyl groups\n");
392 progress_sethighlim(progress_limits
[5]);
393 #endif /* PROGRESS */
397 * print out summary statistics
399 n_ffree
= sblock
->fs_cstotal
.cs_nffree
;
400 n_bfree
= sblock
->fs_cstotal
.cs_nbfree
;
401 pwarn("%llu files, %lld used, %lld free ",
402 (unsigned long long)n_files
, (long long)n_blks
,
403 (long long)(n_ffree
+ sblock
->fs_frag
* n_bfree
));
404 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
405 (long long)n_ffree
, (long long)n_bfree
,
406 (long long)(n_ffree
* 100 / (daddr_t
)sblock
->fs_dsize
),
407 (long long)(((n_ffree
* 1000 + (daddr_t
)sblock
->fs_dsize
/ 2)
408 / (daddr_t
)sblock
->fs_dsize
) % 10));
410 (n_files
-= maxino
- ROOTINO
- sblock
->fs_cstotal
.cs_nifree
))
411 printf("%llu files missing\n", (unsigned long long)n_files
);
413 n_blks
+= sblock
->fs_ncg
*
414 (cgdmin(sblock
, 0) - cgsblock(sblock
, 0));
415 n_blks
+= cgsblock(sblock
, 0) - cgbase(sblock
, 0);
416 n_blks
+= howmany(sblock
->fs_cssize
, sblock
->fs_fsize
);
417 if (n_blks
-= maxfsblock
- (n_ffree
+ sblock
->fs_frag
* n_bfree
))
418 printf("%lld blocks missing\n", (long long)n_blks
);
419 if (duplist
!= NULL
) {
420 printf("The following duplicate blocks remain:");
421 for (dp
= duplist
; dp
; dp
= dp
->next
)
422 printf(" %lld,", (long long)dp
->dup
);
425 if (zlnhead
!= NULL
) {
426 printf("The following zero link count inodes remain:");
427 for (zlnp
= zlnhead
; zlnp
; zlnp
= zlnp
->next
)
429 (unsigned long long)zlnp
->zlncnt
);
433 zlnhead
= (struct zlncnt
*)0;
434 duplist
= (struct dups
*)0;
435 muldup
= (struct dups
*)0;
438 sblock
->fs_time
= time(NULL
);
447 struct statvfs stfs_buf
;
449 * Check to see if root is mounted read-write.
451 if (statvfs("/", &stfs_buf
) == 0)
452 flags
= stfs_buf
.f_flag
;
456 markclean
= flags
& MNT_RDONLY
;
462 for (cylno
= 0; cylno
< sblock
->fs_ncg
; cylno
++)
463 if (inostathead
[cylno
].il_stat
!= NULL
)
464 free(inostathead
[cylno
].il_stat
);
468 if (!resolved
|| rerun
) {
469 pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
475 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
477 pwarn("\n***** PLEASE RERUN FSCK *****\n");
479 struct statvfs stfs_buf
;
481 * We modified the root. Do a mount update on
482 * it, unless it is read-write, so we can continue.
484 if (statvfs("/", &stfs_buf
) == 0) {
485 long flags
= stfs_buf
.f_flag
;
486 struct ufs_args args
;
488 if (flags
& MNT_RDONLY
) {
490 flags
|= MNT_UPDATE
| MNT_RELOAD
;
491 if (mount(MOUNT_FFS
, "/", flags
,
492 &args
, sizeof args
) == 0)
497 pwarn("\n***** REBOOT NOW *****\n");
499 return FSCK_EXIT_ROOT_CHANGED
;
508 (void) fprintf(stderr
,
509 "usage: %s [-adFfPpqUX] [-B byteorder] [-b block] [-c level] "
511 "\t[-x snap-backup] [-y | -n] filesystem ...\n",
513 exit(FSCK_EXIT_USAGE
);
517 char *get_snap_device(char *file
)
519 char *mountpoint
= NULL
;
520 struct statvfs
*mntbuf
, *fs
, fsbuf
;
523 /* find the mount point */
524 if (lstat(file
, &sb
) == -1) {
525 warn("can't stat %s", file
);
528 if (S_ISCHR(sb
.st_mode
) || S_ISBLK(sb
.st_mode
)) {
530 if ((mntbufc
= getmntinfo(&mntbuf
, MNT_NOWAIT
)) == 0)
531 pfatal("can't get mount list: %s\n", strerror(errno
));
532 for (fs
= mntbuf
, i
= 0;
533 i
< mntbufc
; i
++, fs
++) {
534 if (strcmp(fs
->f_fstypename
, "ufs") != 0 &&
535 strcmp(fs
->f_fstypename
, "ffs") != 0)
537 if (fs
->f_flag
& ST_RDONLY
) {
538 warnx("Cannot use -x or -X "
539 "on read-only filesystem");
543 if (strcmp(fs
->f_mntfromname
, unrawname(file
)) == 0) {
544 mountpoint
= strdup(fs
->f_mntonname
);
549 warnx("Cannot use -x or -X on unmounted device");
553 if (S_ISDIR(sb
.st_mode
)) {
554 if (statvfs(file
, &fsbuf
) == -1)
555 pfatal("can't statvfs %s: %s\n", file
, strerror(errno
));
556 if (strcmp(fsbuf
.f_mntonname
, file
))
557 pfatal("%s is not a mount point\n", file
);
558 if (fsbuf
.f_flag
& ST_RDONLY
) {
559 warnx("Cannot use -x or -X "
560 "on read-only filesystem");
563 mountpoint
= strdup(file
);
566 pfatal("%s is not a mount point\n", file
);