2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1986, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.6 (Berkeley) 5/14/95
35 * $FreeBSD: src/sbin/fsck/main.c,v 1.21.2.1 2001/01/23 23:11:07 iedowse Exp $
36 * $DragonFly: src/sbin/fsck/main.c,v 1.5 2003/11/01 17:15:58 drhodus Exp $
39 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <sys/resource.h>
45 #include <vfs/ufs/dinode.h>
46 #include <vfs/ufs/ufsmount.h>
47 #include <vfs/ufs/fs.h>
56 static int argtoi(int flag, char *req, char *str, int base);
57 static int docheck(struct fstab *fsp);
58 static int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
60 static struct statfs *getmntpt(const char *);
61 int main(int argc, char *argv[]);
64 main(int argc, char **argv)
71 while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) {
78 bflag = argtoi('b', "number", optarg, 10);
79 printf("Alternate super block location: %d\n", bflag);
83 cvtlevel = argtoi('c', "conversion level", optarg, 10);
95 maxrun = argtoi('l', "number", optarg, 10);
99 lfmode = argtoi('m', "mode", optarg, 8);
101 errx(EEXIT, "bad mode to -m: %o", lfmode);
102 printf("** lost+found creation mode %o\n", lfmode);
118 errx(EEXIT, "%c option?", ch);
123 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
124 (void)signal(SIGINT, catch);
126 (void)signal(SIGQUIT, catchquit);
127 signal(SIGINFO, infohandler);
129 * Push up our allowed memory limit so we can cope
130 * with huge filesystems.
132 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
133 rlimit.rlim_cur = rlimit.rlim_max;
134 (void)setrlimit(RLIMIT_DATA, &rlimit);
138 char *path = blockcheck(*argv);
141 pfatal("Can't check %s\n", *argv);
143 (void)checkfilesys(path, 0, 0L, 0);
148 ret = checkfstab(preen, maxrun, docheck, checkfilesys);
155 argtoi(int flag, char *req, char *str, int base)
160 ret = (int)strtol(str, &cp, base);
161 if (cp == str || *cp)
162 errx(EEXIT, "-%c flag requires a %s", flag, req);
167 * Determine whether a filesystem should be checked.
170 docheck(register struct fstab *fsp)
173 if (strcmp(fsp->fs_vfstype, "ufs") ||
174 (strcmp(fsp->fs_type, FSTAB_RW) &&
175 strcmp(fsp->fs_type, FSTAB_RO)) ||
182 * Check the specified filesystem.
186 checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
188 ufs_daddr_t n_ffree, n_bfree;
190 struct statfs *mntbuf;
195 (void)signal(SIGQUIT, voidquit);
199 switch (setup(filesys)) {
202 pfatal("CAN'T CHECK FILE SYSTEM.");
205 pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
206 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
207 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
208 sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
209 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
215 * Get the mount point information of the filesystem, if
218 mntbuf = getmntpt(filesys);
221 * Cleared if any questions answered no. Used to decide if
222 * the superblock should be marked clean.
226 * 1: scan inodes tallying blocks used
229 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
230 if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
231 printf("** Root file system\n");
232 printf("** Phase 1 - Check Blocks and Sizes\n");
237 * 1b: locate first references to duplicates, if any
240 if (preen || usedsoftdep)
241 pfatal("INTERNAL ERROR: dups with -p");
242 printf("** Phase 1b - Rescan For More DUPS\n");
247 * 2: traverse directories from root to mark all connected directories
250 printf("** Phase 2 - Check Pathnames\n");
254 * 3: scan inodes looking for disconnected directories
257 printf("** Phase 3 - Check Connectivity\n");
261 * 4: scan inodes looking for disconnected files; check reference counts
264 printf("** Phase 4 - Check Reference Counts\n");
268 * 5: check and repair resource counts in cylinder groups
271 printf("** Phase 5 - Check Cyl groups\n");
275 * print out summary statistics
277 n_ffree = sblock.fs_cstotal.cs_nffree;
278 n_bfree = sblock.fs_cstotal.cs_nbfree;
279 pwarn("%ld files, %ld used, %ld free ",
280 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
281 printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
282 n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
284 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
285 printf("%d files missing\n", n_files);
287 n_blks += sblock.fs_ncg *
288 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
289 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
290 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
291 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
292 printf("%d blocks missing\n", n_blks);
293 if (duplist != NULL) {
294 printf("The following duplicate blocks remain:");
295 for (dp = duplist; dp; dp = dp->next)
296 printf(" %d,", dp->dup);
299 if (zlnhead != NULL) {
300 printf("The following zero link count inodes remain:");
301 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
302 printf(" %u,", zlnp->zlncnt);
306 zlnhead = (struct zlncnt *)0;
307 duplist = (struct dups *)0;
308 muldup = (struct dups *)0;
311 sblock.fs_time = time(NULL);
314 if (cvtlevel && sblk.b_dirty) {
316 * Write out the duplicate super blocks
318 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
319 bwrite(fswritefd, (char *)&sblock,
320 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
326 * Check to see if the filesystem is mounted read-write.
328 if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
332 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
333 if (inostathead[cylno].il_stat != NULL)
334 free((char *)inostathead[cylno].il_stat);
335 free((char *)inostathead);
337 if (fsmodified && !preen)
338 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
340 printf("\n***** PLEASE RERUN FSCK *****\n");
341 if (mntbuf != NULL) {
342 struct ufs_args args;
345 * We modified a mounted filesystem. Do a mount update on
346 * it unless it is read-write, so we can continue using it
347 * as safely as possible.
349 if (mntbuf->f_flags & MNT_RDONLY) {
351 args.export.ex_flags = 0;
352 args.export.ex_root = 0;
353 ret = mount("ufs", mntbuf->f_mntonname,
354 mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
357 pwarn("mount reload of '%s' failed: %s\n\n",
358 mntbuf->f_mntonname, strerror(errno));
363 printf("\n***** REBOOT NOW *****\n");
371 * Get the directory that the device is mounted on.
373 static struct statfs *
374 getmntpt(const char *name)
376 struct stat devstat, mntdevstat;
377 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
379 struct statfs *mntbuf;
382 if (stat(name, &devstat) != 0 ||
383 !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
385 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
386 for (i = 0; i < mntsize; i++) {
387 if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0)
389 devname = mntbuf[i].f_mntfromname;
390 if (*devname != '/') {
391 strcpy(device, _PATH_DEV);
392 strcat(device, devname);
395 if (stat(devname, &mntdevstat) == 0 &&
396 mntdevstat.st_rdev == devstat.st_rdev)