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