Commit | Line | Data |
---|---|---|
76797561 | 1 | /* |
fe32782c KM |
2 | * Copyright (c) 1980, 1986 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
70ab3c27 | 5 | * %sccs.include.redist.c% |
76797561 DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
fe32782c | 10 | "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\ |
76797561 | 11 | All rights reserved.\n"; |
fe32782c | 12 | #endif /* not lint */ |
76797561 | 13 | |
fa95fc59 | 14 | #ifndef lint |
b4107159 | 15 | static char sccsid[] = "@(#)main.c 5.25 (Berkeley) %G%"; |
fe32782c | 16 | #endif /* not lint */ |
07670f7d | 17 | |
4d308541 | 18 | #include <sys/param.h> |
72e5286b | 19 | #include <ufs/dinode.h> |
4d7f4685 | 20 | #include <ufs/fs.h> |
7718c0e6 | 21 | #include <fstab.h> |
38dde0cd | 22 | #include <string.h> |
c689da7b | 23 | #include <ctype.h> |
304178c6 | 24 | #include <stdio.h> |
f7635e39 | 25 | #include "fsck.h" |
6e884967 | 26 | |
392fe950 | 27 | void catch(), catchquit(), voidquit(); |
3cfe4689 | 28 | int returntosingle; |
7718c0e6 KM |
29 | |
30 | main(argc, argv) | |
31 | int argc; | |
32 | char *argv[]; | |
33 | { | |
304178c6 | 34 | char ch; |
e23b720f | 35 | int ret, maxrun = 0; |
304178c6 KM |
36 | extern int docheck(), checkfilesys(); |
37 | extern char *optarg; | |
38 | extern int optind; | |
7718c0e6 KM |
39 | |
40 | sync(); | |
304178c6 KM |
41 | while ((ch = getopt(argc, argv, "cdpnNyYb:l:m:")) != EOF) { |
42 | switch (ch) { | |
7718c0e6 KM |
43 | case 'p': |
44 | preen++; | |
45 | break; | |
46 | ||
47 | case 'b': | |
304178c6 KM |
48 | if (!isdigit(*optarg)) |
49 | errexit("-b flag requires a number\n"); | |
50 | bflag = atoi(optarg); | |
7718c0e6 KM |
51 | printf("Alternate super block location: %d\n", bflag); |
52 | break; | |
53 | ||
d3e80ec3 KM |
54 | case 'c': |
55 | cvtflag++; | |
56 | break; | |
57 | ||
7718c0e6 KM |
58 | case 'd': |
59 | debug++; | |
60 | break; | |
61 | ||
5eeaeb24 | 62 | case 'l': |
304178c6 | 63 | if (!isdigit(*optarg)) |
5eeaeb24 | 64 | errexit("-l flag requires a number\n"); |
304178c6 | 65 | maxrun = atoi(optarg); |
5eeaeb24 MK |
66 | break; |
67 | ||
c689da7b | 68 | case 'm': |
304178c6 | 69 | if (!isdigit(*optarg)) |
c689da7b | 70 | errexit("-m flag requires a mode\n"); |
304178c6 | 71 | sscanf(optarg, "%o", &lfmode); |
c689da7b KM |
72 | if (lfmode &~ 07777) |
73 | errexit("bad mode to -m: %o\n", lfmode); | |
c689da7b KM |
74 | printf("** lost+found creation mode %o\n", lfmode); |
75 | break; | |
76 | ||
569ec282 | 77 | case 'n': |
7718c0e6 KM |
78 | case 'N': |
79 | nflag++; | |
80 | yflag = 0; | |
81 | break; | |
82 | ||
569ec282 | 83 | case 'y': |
7718c0e6 KM |
84 | case 'Y': |
85 | yflag++; | |
86 | nflag = 0; | |
87 | break; | |
88 | ||
89 | default: | |
304178c6 | 90 | errexit("%c option?\n", ch); |
7718c0e6 KM |
91 | } |
92 | } | |
304178c6 KM |
93 | argc -= optind; |
94 | argv += optind; | |
7718c0e6 KM |
95 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
96 | (void)signal(SIGINT, catch); | |
3cfe4689 MK |
97 | if (preen) |
98 | (void)signal(SIGQUIT, catchquit); | |
7718c0e6 | 99 | if (argc) { |
304178c6 | 100 | while (argc-- > 0) |
b4107159 | 101 | checkfilesys(*argv++, (char *)0, 0); |
7718c0e6 KM |
102 | exit(0); |
103 | } | |
e23b720f | 104 | ret = checkfstab(preen, maxrun, docheck, checkfilesys); |
3cfe4689 MK |
105 | if (returntosingle) |
106 | exit(2); | |
e23b720f | 107 | exit(ret); |
7718c0e6 KM |
108 | } |
109 | ||
e23b720f KM |
110 | /* |
111 | * Determine whether a filesystem should be checked. | |
112 | */ | |
113 | docheck(fsp) | |
114 | register struct fstab *fsp; | |
5eeaeb24 MK |
115 | { |
116 | ||
e23b720f KM |
117 | if (strcmp(fsp->fs_vfstype, "ufs") || |
118 | (strcmp(fsp->fs_type, FSTAB_RW) && | |
119 | strcmp(fsp->fs_type, FSTAB_RO)) || | |
120 | fsp->fs_passno == 0) | |
121 | return (0); | |
122 | return (1); | |
5eeaeb24 MK |
123 | } |
124 | ||
e23b720f KM |
125 | /* |
126 | * Check the specified filesystem. | |
127 | */ | |
128 | /* ARGSUSED */ | |
129 | checkfilesys(filesys, mntpt, auxdata) | |
130 | char *filesys, *mntpt; | |
131 | long auxdata; | |
6e884967 | 132 | { |
993a756c | 133 | daddr_t n_ffree, n_bfree; |
d4233021 | 134 | struct dups *dp; |
1a02fd3a | 135 | struct zlncnt *zlnp; |
6e884967 | 136 | |
e23b720f | 137 | (void)signal(SIGQUIT, voidquit); |
d2c95d76 | 138 | devname = filesys; |
5eeaeb24 MK |
139 | if (debug && preen) |
140 | pwarn("starting\n"); | |
d2c95d76 | 141 | if (setup(filesys) == 0) { |
6e884967 | 142 | if (preen) |
d2c95d76 | 143 | pfatal("CAN'T CHECK FILE SYSTEM."); |
e23b720f | 144 | return (0); |
6e884967 | 145 | } |
993a756c KM |
146 | /* |
147 | * 1: scan inodes tallying blocks used | |
148 | */ | |
690f77ba KM |
149 | if (preen == 0) { |
150 | printf("** Last Mounted on %s\n", sblock.fs_fsmnt); | |
6e884967 KM |
151 | if (hotroot) |
152 | printf("** Root file system\n"); | |
153 | printf("** Phase 1 - Check Blocks and Sizes\n"); | |
154 | } | |
d2c95d76 BJ |
155 | pass1(); |
156 | ||
993a756c KM |
157 | /* |
158 | * 1b: locate first references to duplicates, if any | |
159 | */ | |
62e6c152 | 160 | if (duplist) { |
d2c95d76 BJ |
161 | if (preen) |
162 | pfatal("INTERNAL ERROR: dups with -p"); | |
163 | printf("** Phase 1b - Rescan For More DUPS\n"); | |
164 | pass1b(); | |
165 | } | |
166 | ||
993a756c KM |
167 | /* |
168 | * 2: traverse directories from root to mark all connected directories | |
169 | */ | |
d2c95d76 BJ |
170 | if (preen == 0) |
171 | printf("** Phase 2 - Check Pathnames\n"); | |
172 | pass2(); | |
173 | ||
993a756c KM |
174 | /* |
175 | * 3: scan inodes looking for disconnected directories | |
176 | */ | |
d2c95d76 BJ |
177 | if (preen == 0) |
178 | printf("** Phase 3 - Check Connectivity\n"); | |
179 | pass3(); | |
180 | ||
993a756c KM |
181 | /* |
182 | * 4: scan inodes looking for disconnected files; check reference counts | |
183 | */ | |
d2c95d76 BJ |
184 | if (preen == 0) |
185 | printf("** Phase 4 - Check Reference Counts\n"); | |
186 | pass4(); | |
187 | ||
993a756c KM |
188 | /* |
189 | * 5: check and repair resource counts in cylinder groups | |
190 | */ | |
d2c95d76 BJ |
191 | if (preen == 0) |
192 | printf("** Phase 5 - Check Cyl groups\n"); | |
193 | pass5(); | |
194 | ||
993a756c KM |
195 | /* |
196 | * print out summary statistics | |
197 | */ | |
198 | n_ffree = sblock.fs_cstotal.cs_nffree; | |
199 | n_bfree = sblock.fs_cstotal.cs_nbfree; | |
8755a38b KM |
200 | pwarn("%d files, %d used, %d free ", |
201 | n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree); | |
202 | printf("(%d frags, %d blocks, %.1f%% fragmentation)\n", | |
203 | n_ffree, n_bfree, (float)(n_ffree * 100) / sblock.fs_dsize); | |
569ec282 KM |
204 | if (debug && |
205 | (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree)) | |
993a756c KM |
206 | printf("%d files missing\n", n_files); |
207 | if (debug) { | |
208 | n_blks += sblock.fs_ncg * | |
209 | (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); | |
210 | n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); | |
211 | n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize); | |
569ec282 | 212 | if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree)) |
993a756c | 213 | printf("%d blocks missing\n", n_blks); |
d4233021 KM |
214 | if (duplist != NULL) { |
215 | printf("The following duplicate blocks remain:"); | |
216 | for (dp = duplist; dp; dp = dp->next) | |
217 | printf(" %d,", dp->dup); | |
218 | printf("\n"); | |
219 | } | |
1a02fd3a KM |
220 | if (zlnhead != NULL) { |
221 | printf("The following zero link count inodes remain:"); | |
222 | for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) | |
223 | printf(" %d,", zlnp->zlncnt); | |
224 | printf("\n"); | |
225 | } | |
d2c95d76 | 226 | } |
1a02fd3a KM |
227 | zlnhead = (struct zlncnt *)0; |
228 | duplist = (struct dups *)0; | |
57117c0f | 229 | inocleanup(); |
569ec282 | 230 | if (fsmodified) { |
1a724405 | 231 | (void)time(&sblock.fs_time); |
d2c95d76 BJ |
232 | sbdirty(); |
233 | } | |
234 | ckfini(); | |
235 | free(blockmap); | |
d2c95d76 | 236 | free(statemap); |
1a724405 | 237 | free((char *)lncntp); |
569ec282 | 238 | if (!fsmodified) |
e23b720f | 239 | return (0); |
addfa1aa BJ |
240 | if (!preen) { |
241 | printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); | |
242 | if (hotroot) | |
243 | printf("\n***** REBOOT UNIX *****\n"); | |
244 | } | |
245 | if (hotroot) { | |
246 | sync(); | |
e23b720f | 247 | return (4); |
7718c0e6 | 248 | } |
7718c0e6 KM |
249 | return (0); |
250 | } |