main.c revision 1.1.1.3 1 /*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/mount.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/ufsmount.h>
50 #include <ufs/ffs/fs.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <fstab.h>
55 #include <string.h>
56
57 #include "fsck.h"
58
59 int returntosingle;
60
61 static int argtoi __P((int flag, char *req, char *str, int base));
62 static int docheck __P((struct fstab *fsp));
63 static int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
64 int child));
65 void main __P((int argc, char *argv[]));
66
67 void
68 main(argc, argv)
69 int argc;
70 char *argv[];
71 {
72 int ch;
73 int ret, maxrun = 0;
74 extern char *optarg;
75 extern int optind;
76
77 sync();
78 while ((ch = getopt(argc, argv, "dpnNyYb:c:l:m:")) != EOF) {
79 switch (ch) {
80 case 'p':
81 preen++;
82 break;
83
84 case 'b':
85 bflag = argtoi('b', "number", optarg, 10);
86 printf("Alternate super block location: %d\n", bflag);
87 break;
88
89 case 'c':
90 cvtlevel = argtoi('c', "conversion level", optarg, 10);
91 break;
92
93 case 'd':
94 debug++;
95 break;
96
97 case 'l':
98 maxrun = argtoi('l', "number", optarg, 10);
99 break;
100
101 case 'm':
102 lfmode = argtoi('m', "mode", optarg, 8);
103 if (lfmode &~ 07777)
104 errx(EEXIT, "bad mode to -m: %o", lfmode);
105 printf("** lost+found creation mode %o\n", lfmode);
106 break;
107
108 case 'n':
109 case 'N':
110 nflag++;
111 yflag = 0;
112 break;
113
114 case 'y':
115 case 'Y':
116 yflag++;
117 nflag = 0;
118 break;
119
120 default:
121 errx(EEXIT, "%c option?", ch);
122 }
123 }
124 argc -= optind;
125 argv += optind;
126 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
127 (void)signal(SIGINT, catch);
128 if (preen)
129 (void)signal(SIGQUIT, catchquit);
130 if (argc) {
131 while (argc-- > 0)
132 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
133 exit(0);
134 }
135 ret = checkfstab(preen, maxrun, docheck, checkfilesys);
136 if (returntosingle)
137 exit(2);
138 exit(ret);
139 }
140
141 static int
142 argtoi(flag, req, str, base)
143 int flag;
144 char *req, *str;
145 int base;
146 {
147 char *cp;
148 int ret;
149
150 ret = (int)strtol(str, &cp, base);
151 if (cp == str || *cp)
152 errx(EEXIT, "-%c flag requires a %s", flag, req);
153 return (ret);
154 }
155
156 /*
157 * Determine whether a filesystem should be checked.
158 */
159 static int
160 docheck(fsp)
161 register struct fstab *fsp;
162 {
163
164 if (strcmp(fsp->fs_vfstype, "ufs") ||
165 (strcmp(fsp->fs_type, FSTAB_RW) &&
166 strcmp(fsp->fs_type, FSTAB_RO)) ||
167 fsp->fs_passno == 0)
168 return (0);
169 return (1);
170 }
171
172 /*
173 * Check the specified filesystem.
174 */
175 /* ARGSUSED */
176 static int
177 checkfilesys(filesys, mntpt, auxdata, child)
178 char *filesys, *mntpt;
179 long auxdata;
180 int child;
181 {
182 ufs_daddr_t n_ffree, n_bfree;
183 struct dups *dp;
184 struct zlncnt *zlnp;
185 int cylno, flags;
186
187 if (preen && child)
188 (void)signal(SIGQUIT, voidquit);
189 cdevname = filesys;
190 if (debug && preen)
191 pwarn("starting\n");
192 switch (setup(filesys)) {
193 case 0:
194 if (preen)
195 pfatal("CAN'T CHECK FILE SYSTEM.");
196 /* fall through */
197 case -1:
198 return (0);
199 }
200 /*
201 * 1: scan inodes tallying blocks used
202 */
203 if (preen == 0) {
204 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
205 if (hotroot)
206 printf("** Root file system\n");
207 printf("** Phase 1 - Check Blocks and Sizes\n");
208 }
209 pass1();
210
211 /*
212 * 1b: locate first references to duplicates, if any
213 */
214 if (duplist) {
215 if (preen)
216 pfatal("INTERNAL ERROR: dups with -p");
217 printf("** Phase 1b - Rescan For More DUPS\n");
218 pass1b();
219 }
220
221 /*
222 * 2: traverse directories from root to mark all connected directories
223 */
224 if (preen == 0)
225 printf("** Phase 2 - Check Pathnames\n");
226 pass2();
227
228 /*
229 * 3: scan inodes looking for disconnected directories
230 */
231 if (preen == 0)
232 printf("** Phase 3 - Check Connectivity\n");
233 pass3();
234
235 /*
236 * 4: scan inodes looking for disconnected files; check reference counts
237 */
238 if (preen == 0)
239 printf("** Phase 4 - Check Reference Counts\n");
240 pass4();
241
242 /*
243 * 5: check and repair resource counts in cylinder groups
244 */
245 if (preen == 0)
246 printf("** Phase 5 - Check Cyl groups\n");
247 pass5();
248
249 /*
250 * print out summary statistics
251 */
252 n_ffree = sblock.fs_cstotal.cs_nffree;
253 n_bfree = sblock.fs_cstotal.cs_nbfree;
254 pwarn("%ld files, %ld used, %ld free ",
255 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
256 printf("(%ld frags, %ld blocks, %d.%d%% fragmentation)\n",
257 n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
258 ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
259 if (debug &&
260 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
261 printf("%ld files missing\n", n_files);
262 if (debug) {
263 n_blks += sblock.fs_ncg *
264 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
265 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
266 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
267 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
268 printf("%ld blocks missing\n", n_blks);
269 if (duplist != NULL) {
270 printf("The following duplicate blocks remain:");
271 for (dp = duplist; dp; dp = dp->next)
272 printf(" %ld,", dp->dup);
273 printf("\n");
274 }
275 if (zlnhead != NULL) {
276 printf("The following zero link count inodes remain:");
277 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
278 printf(" %lu,", zlnp->zlncnt);
279 printf("\n");
280 }
281 }
282 zlnhead = (struct zlncnt *)0;
283 duplist = (struct dups *)0;
284 muldup = (struct dups *)0;
285 inocleanup();
286 if (fsmodified) {
287 (void)time(&sblock.fs_time);
288 sbdirty();
289 }
290 if (cvtlevel && sblk.b_dirty) {
291 /*
292 * Write out the duplicate super blocks
293 */
294 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
295 bwrite(fswritefd, (char *)&sblock,
296 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
297 }
298 if (!hotroot) {
299 ckfini(1);
300 } else {
301 struct statfs stfs_buf;
302 /*
303 * Check to see if root is mounted read-write.
304 */
305 if (statfs("/", &stfs_buf) == 0)
306 flags = stfs_buf.f_flags;
307 else
308 flags = 0;
309 ckfini(flags & MNT_RDONLY);
310 }
311 free(blockmap);
312 free(statemap);
313 free((char *)lncntp);
314 if (!fsmodified)
315 return (0);
316 if (!preen)
317 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
318 if (hotroot) {
319 struct ufs_args args;
320 int ret;
321 /*
322 * We modified the root. Do a mount update on
323 * it, unless it is read-write, so we can continue.
324 */
325 if (flags & MNT_RDONLY) {
326 args.fspec = 0;
327 args.export.ex_flags = 0;
328 args.export.ex_root = 0;
329 flags |= MNT_UPDATE | MNT_RELOAD;
330 ret = mount("ufs", "/", flags, &args);
331 if (ret == 0)
332 return (0);
333 }
334 if (!preen)
335 printf("\n***** REBOOT NOW *****\n");
336 sync();
337 return (4);
338 }
339 return (0);
340 }
341