main.c revision 1.16 1 /* $NetBSD: main.c,v 1.16 1995/04/12 21:24:10 mycroft 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.16 1995/04/12 21:24:10 mycroft 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, "dpnNyYb: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 'l':
105 maxrun = argtoi('l', "number", optarg, 10);
106 break;
107
108 case 'm':
109 lfmode = argtoi('m', "mode", optarg, 8);
110 if (lfmode &~ 07777)
111 errexit("bad mode to -m: %o\n", lfmode);
112 printf("** lost+found creation mode %o\n", lfmode);
113 break;
114
115 case 'n':
116 case 'N':
117 nflag++;
118 yflag = 0;
119 break;
120
121 case 'y':
122 case 'Y':
123 yflag++;
124 nflag = 0;
125 break;
126
127 default:
128 errexit("%c option?\n", ch);
129 }
130 }
131 argc -= optind;
132 argv += optind;
133 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
134 (void)signal(SIGINT, catch);
135 if (preen)
136 (void)signal(SIGQUIT, catchquit);
137 if (argc) {
138 while (argc-- > 0)
139 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
140 exit(0);
141 }
142 ret = checkfstab(preen, maxrun, docheck, checkfilesys);
143 if (returntosingle)
144 exit(2);
145 exit(ret);
146 }
147
148 int
149 argtoi(flag, req, str, base)
150 int flag;
151 char *req, *str;
152 int base;
153 {
154 char *cp;
155 int ret;
156
157 ret = (int)strtol(str, &cp, base);
158 if (cp == str || *cp)
159 errexit("-%c flag requires a %s\n", flag, req);
160 return (ret);
161 }
162
163 /*
164 * Determine whether a filesystem should be checked.
165 */
166 int
167 docheck(fsp)
168 register struct fstab *fsp;
169 {
170
171 if (strcmp(fsp->fs_vfstype, "ufs") ||
172 (strcmp(fsp->fs_type, FSTAB_RW) &&
173 strcmp(fsp->fs_type, FSTAB_RO)) ||
174 fsp->fs_passno == 0)
175 return (0);
176 return (1);
177 }
178
179 /*
180 * Check the specified filesystem.
181 */
182 /* ARGSUSED */
183 int
184 checkfilesys(filesys, mntpt, auxdata, child)
185 char *filesys, *mntpt;
186 long auxdata;
187 int child;
188 {
189 daddr_t n_ffree, n_bfree;
190 struct dups *dp;
191 struct zlncnt *zlnp;
192 int cylno;
193
194 if (preen && child)
195 (void)signal(SIGQUIT, voidquit);
196 cdevname = filesys;
197 if (debug && preen)
198 pwarn("starting\n");
199 switch (setup(filesys)) {
200 case 0:
201 if (preen)
202 pfatal("CAN'T CHECK FILE SYSTEM.");
203 case -1:
204 return (0);
205 }
206 /*
207 * 1: scan inodes tallying blocks used
208 */
209 if (preen == 0) {
210 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
211 if (hotroot)
212 printf("** Root file system\n");
213 printf("** Phase 1 - Check Blocks and Sizes\n");
214 }
215 pass1();
216
217 /*
218 * 1b: locate first references to duplicates, if any
219 */
220 if (duplist) {
221 if (preen)
222 pfatal("INTERNAL ERROR: dups with -p");
223 printf("** Phase 1b - Rescan For More DUPS\n");
224 pass1b();
225 }
226
227 /*
228 * 2: traverse directories from root to mark all connected directories
229 */
230 if (preen == 0)
231 printf("** Phase 2 - Check Pathnames\n");
232 pass2();
233
234 /*
235 * 3: scan inodes looking for disconnected directories
236 */
237 if (preen == 0)
238 printf("** Phase 3 - Check Connectivity\n");
239 pass3();
240
241 /*
242 * 4: scan inodes looking for disconnected files; check reference counts
243 */
244 if (preen == 0)
245 printf("** Phase 4 - Check Reference Counts\n");
246 pass4();
247
248 /*
249 * 5: check and repair resource counts in cylinder groups
250 */
251 if (preen == 0)
252 printf("** Phase 5 - Check Cyl groups\n");
253 pass5();
254
255 /*
256 * print out summary statistics
257 */
258 n_ffree = sblock.fs_cstotal.cs_nffree;
259 n_bfree = sblock.fs_cstotal.cs_nbfree;
260 pwarn("%ld files, %ld used, %ld free ",
261 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
262 printf("(%ld frags, %ld blocks, %d.%d%% fragmentation)\n",
263 n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
264 ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
265 if (debug &&
266 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
267 printf("%ld files missing\n", n_files);
268 if (debug) {
269 n_blks += sblock.fs_ncg *
270 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
271 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
272 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
273 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
274 printf("%ld blocks missing\n", n_blks);
275 if (duplist != NULL) {
276 printf("The following duplicate blocks remain:");
277 for (dp = duplist; dp; dp = dp->next)
278 printf(" %ld,", dp->dup);
279 printf("\n");
280 }
281 if (zlnhead != NULL) {
282 printf("The following zero link count inodes remain:");
283 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
284 printf(" %lu,", zlnp->zlncnt);
285 printf("\n");
286 }
287 }
288 zlnhead = (struct zlncnt *)0;
289 duplist = (struct dups *)0;
290 muldup = (struct dups *)0;
291 inocleanup();
292 if (fsmodified) {
293 (void)time(&sblock.fs_time);
294 sbdirty();
295 }
296 if (cvtlevel && sblk.b_dirty) {
297 /*
298 * Write out the duplicate super blocks
299 */
300 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
301 bwrite(fswritefd, (char *)&sblock,
302 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
303 }
304 ckfini(1);
305 free(blockmap);
306 free(statemap);
307 free((char *)lncntp);
308 if (!fsmodified)
309 return (0);
310 if (!preen)
311 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
312 if (hotroot) {
313 struct statfs stfs_buf;
314 /*
315 * We modified the root. Do a mount update on
316 * it, unless it is read-write, so we can continue.
317 */
318 if (statfs("/", &stfs_buf) == 0) {
319 long flags = stfs_buf.f_flags;
320 struct ufs_args args;
321 int ret;
322
323 if (flags & MNT_RDONLY) {
324 args.fspec = 0;
325 args.export.ex_flags = 0;
326 args.export.ex_root = 0;
327 flags |= MNT_UPDATE | MNT_RELOAD;
328 ret = mount(MOUNT_UFS, "/", flags, &args);
329 if (ret == 0)
330 return(0);
331 }
332 }
333 if (!preen)
334 printf("\n***** REBOOT NOW *****\n");
335 sync();
336 return (4);
337 }
338 return (0);
339 }
340