main.c revision 1.22 1 /* $NetBSD: main.c,v 1.22 1996/10/11 20:15:48 thorpej 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.22 1996/10/11 20:15:48 thorpej 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 #include "fsutil.h"
65
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 int main __P((int, char *[]));
71
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
76 {
77 int ch;
78 int ret = 0;
79 extern char *optarg;
80 extern int optind;
81
82 sync();
83 skipclean = 1;
84 while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != EOF) {
85 switch (ch) {
86 case 'p':
87 preen++;
88 break;
89
90 case 'b':
91 skipclean = 0;
92 bflag = argtoi('b', "number", optarg, 10);
93 printf("Alternate super block location: %d\n", bflag);
94 break;
95
96 case 'c':
97 skipclean = 0;
98 cvtlevel = argtoi('c', "conversion level", optarg, 10);
99 break;
100
101 case 'd':
102 debug++;
103 break;
104
105 case 'f':
106 skipclean = 0;
107 break;
108
109 case 'm':
110 lfmode = argtoi('m', "mode", optarg, 8);
111 if (lfmode &~ 07777)
112 errexit("bad mode to -m: %o\n", lfmode);
113 printf("** lost+found creation mode %o\n", lfmode);
114 break;
115
116 case 'n':
117 case 'N':
118 nflag++;
119 yflag = 0;
120 break;
121
122 case 'y':
123 case 'Y':
124 yflag++;
125 nflag = 0;
126 break;
127
128 default:
129 errexit("%c option?\n", ch);
130 }
131 }
132 argc -= optind;
133 argv += optind;
134 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
135 (void)signal(SIGINT, catch);
136 if (preen)
137 (void)signal(SIGQUIT, catchquit);
138
139 if (argc)
140 while (argc-- > 0)
141 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
142
143 if (returntosingle)
144 ret = 2;
145
146 exit(ret);
147 }
148
149 int
150 argtoi(flag, req, str, base)
151 int flag;
152 char *req, *str;
153 int base;
154 {
155 char *cp;
156 int ret;
157
158 ret = (int)strtol(str, &cp, base);
159 if (cp == str || *cp)
160 errexit("-%c flag requires a %s\n", flag, req);
161 return (ret);
162 }
163
164 /*
165 * Determine whether a filesystem should be checked.
166 */
167 int
168 docheck(fsp)
169 register struct fstab *fsp;
170 {
171
172 if ((strcmp(fsp->fs_vfstype, "ufs") &&
173 strcmp(fsp->fs_vfstype, "ffs")) ||
174 (strcmp(fsp->fs_type, FSTAB_RW) &&
175 strcmp(fsp->fs_type, FSTAB_RO)) ||
176 fsp->fs_passno == 0)
177 return (0);
178 return (1);
179 }
180
181 /*
182 * Check the specified filesystem.
183 */
184 /* ARGSUSED */
185 int
186 checkfilesys(filesys, mntpt, auxdata, child)
187 char *filesys, *mntpt;
188 long auxdata;
189 int child;
190 {
191 daddr_t n_ffree, n_bfree;
192 struct dups *dp;
193 struct zlncnt *zlnp;
194 int cylno;
195
196 if (preen && child)
197 (void)signal(SIGQUIT, voidquit);
198 setcdevname(filesys, preen);
199 if (debug && preen)
200 pwarn("starting\n");
201 switch (setup(filesys)) {
202 case 0:
203 if (preen)
204 pfatal("CAN'T CHECK FILE SYSTEM.");
205 case -1:
206 return (0);
207 }
208 /*
209 * 1: scan inodes tallying blocks used
210 */
211 if (preen == 0) {
212 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
213 if (hotroot())
214 printf("** Root file system\n");
215 printf("** Phase 1 - Check Blocks and Sizes\n");
216 }
217 pass1();
218
219 /*
220 * 1b: locate first references to duplicates, if any
221 */
222 if (duplist) {
223 if (preen)
224 pfatal("INTERNAL ERROR: dups with -p");
225 printf("** Phase 1b - Rescan For More DUPS\n");
226 pass1b();
227 }
228
229 /*
230 * 2: traverse directories from root to mark all connected directories
231 */
232 if (preen == 0)
233 printf("** Phase 2 - Check Pathnames\n");
234 pass2();
235
236 /*
237 * 3: scan inodes looking for disconnected directories
238 */
239 if (preen == 0)
240 printf("** Phase 3 - Check Connectivity\n");
241 pass3();
242
243 /*
244 * 4: scan inodes looking for disconnected files; check reference counts
245 */
246 if (preen == 0)
247 printf("** Phase 4 - Check Reference Counts\n");
248 pass4();
249
250 /*
251 * 5: check and repair resource counts in cylinder groups
252 */
253 if (preen == 0)
254 printf("** Phase 5 - Check Cyl groups\n");
255 pass5();
256
257 /*
258 * print out summary statistics
259 */
260 n_ffree = sblock.fs_cstotal.cs_nffree;
261 n_bfree = sblock.fs_cstotal.cs_nbfree;
262 pwarn("%d files, %d used, %d free ",
263 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
264 printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n",
265 n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
266 ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
267 if (debug &&
268 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
269 printf("%d files missing\n", n_files);
270 if (debug) {
271 n_blks += sblock.fs_ncg *
272 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
273 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
274 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
275 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
276 printf("%d blocks missing\n", n_blks);
277 if (duplist != NULL) {
278 printf("The following duplicate blocks remain:");
279 for (dp = duplist; dp; dp = dp->next)
280 printf(" %d,", dp->dup);
281 printf("\n");
282 }
283 if (zlnhead != NULL) {
284 printf("The following zero link count inodes remain:");
285 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
286 printf(" %u,", zlnp->zlncnt);
287 printf("\n");
288 }
289 }
290 zlnhead = (struct zlncnt *)0;
291 duplist = (struct dups *)0;
292 muldup = (struct dups *)0;
293 inocleanup();
294 if (fsmodified) {
295 (void)time(&sblock.fs_time);
296 sbdirty();
297 }
298 if (cvtlevel && sblk.b_dirty) {
299 /*
300 * Write out the duplicate super blocks
301 */
302 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
303 bwrite(fswritefd, (char *)&sblock,
304 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
305 }
306 ckfini(1);
307 free(blockmap);
308 free(statemap);
309 free((char *)lncntp);
310 if (!fsmodified)
311 return (0);
312 if (!preen)
313 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
314 if (rerun)
315 printf("\n***** PLEASE RERUN FSCK *****\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_FFS, "/", 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