main.c revision 1.24 1 /* $NetBSD: main.c,v 1.24 1997/09/14 14:36:32 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: main.c,v 1.24 1997/09/14 14:36:32 lukem 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
68 int main __P((int, char *[]));
69
70 static int argtoi __P((int, char *, char *, int));
71 static int checkfilesys __P((char *, char *, long, int));
72 static void usage __P((void));
73
74
75 int
76 main(argc, argv)
77 int argc;
78 char *argv[];
79 {
80 int ch;
81 int ret = 0;
82 extern char *optarg;
83 extern int optind;
84
85 sync();
86 skipclean = 1;
87 while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
88 switch (ch) {
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 '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 nflag++;
117 yflag = 0;
118 break;
119
120 case 'p':
121 preen++;
122 break;
123
124 case 'y':
125 yflag++;
126 nflag = 0;
127 break;
128
129 default:
130 usage();
131 }
132 }
133
134 argc -= optind;
135 argv += optind;
136
137 if (!argc)
138 usage();
139
140 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
141 (void)signal(SIGINT, catch);
142 if (preen)
143 (void)signal(SIGQUIT, catchquit);
144
145 while (argc-- > 0)
146 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
147
148 if (returntosingle)
149 ret = 2;
150
151 exit(ret);
152 }
153
154 static int
155 argtoi(flag, req, str, base)
156 int flag;
157 char *req, *str;
158 int base;
159 {
160 char *cp;
161 int ret;
162
163 ret = (int)strtol(str, &cp, base);
164 if (cp == str || *cp)
165 errexit("-%c flag requires a %s\n", flag, req);
166 return (ret);
167 }
168
169 /*
170 * Check the specified filesystem.
171 */
172 /* ARGSUSED */
173 static int
174 checkfilesys(filesys, mntpt, auxdata, child)
175 char *filesys, *mntpt;
176 long auxdata;
177 int child;
178 {
179 daddr_t n_ffree, n_bfree;
180 struct dups *dp;
181 struct zlncnt *zlnp;
182 int cylno;
183
184 if (preen && child)
185 (void)signal(SIGQUIT, voidquit);
186 setcdevname(filesys, preen);
187 if (debug && preen)
188 pwarn("starting\n");
189 switch (setup(filesys)) {
190 case 0:
191 if (preen)
192 pfatal("CAN'T CHECK FILE SYSTEM.");
193 case -1:
194 return (0);
195 }
196 /*
197 * 1: scan inodes tallying blocks used
198 */
199 if (preen == 0) {
200 printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
201 if (hotroot())
202 printf("** Root file system\n");
203 printf("** Phase 1 - Check Blocks and Sizes\n");
204 }
205 pass1();
206
207 /*
208 * 1b: locate first references to duplicates, if any
209 */
210 if (duplist) {
211 if (preen)
212 pfatal("INTERNAL ERROR: dups with -p");
213 printf("** Phase 1b - Rescan For More DUPS\n");
214 pass1b();
215 }
216
217 /*
218 * 2: traverse directories from root to mark all connected directories
219 */
220 if (preen == 0)
221 printf("** Phase 2 - Check Pathnames\n");
222 pass2();
223
224 /*
225 * 3: scan inodes looking for disconnected directories
226 */
227 if (preen == 0)
228 printf("** Phase 3 - Check Connectivity\n");
229 pass3();
230
231 /*
232 * 4: scan inodes looking for disconnected files; check reference counts
233 */
234 if (preen == 0)
235 printf("** Phase 4 - Check Reference Counts\n");
236 pass4();
237
238 /*
239 * 5: check and repair resource counts in cylinder groups
240 */
241 if (preen == 0)
242 printf("** Phase 5 - Check Cyl groups\n");
243 pass5();
244
245 /*
246 * print out summary statistics
247 */
248 n_ffree = sblock.fs_cstotal.cs_nffree;
249 n_bfree = sblock.fs_cstotal.cs_nbfree;
250 pwarn("%d files, %d used, %d free ",
251 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
252 printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n",
253 n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
254 ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
255 if (debug &&
256 (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
257 printf("%d files missing\n", n_files);
258 if (debug) {
259 n_blks += sblock.fs_ncg *
260 (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
261 n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
262 n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
263 if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
264 printf("%d blocks missing\n", n_blks);
265 if (duplist != NULL) {
266 printf("The following duplicate blocks remain:");
267 for (dp = duplist; dp; dp = dp->next)
268 printf(" %d,", dp->dup);
269 printf("\n");
270 }
271 if (zlnhead != NULL) {
272 printf("The following zero link count inodes remain:");
273 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
274 printf(" %u,", zlnp->zlncnt);
275 printf("\n");
276 }
277 }
278 zlnhead = (struct zlncnt *)0;
279 duplist = (struct dups *)0;
280 muldup = (struct dups *)0;
281 inocleanup();
282 if (fsmodified) {
283 (void)time(&sblock.fs_time);
284 sbdirty();
285 }
286 if (cvtlevel && sblk.b_dirty) {
287 /*
288 * Write out the duplicate super blocks
289 */
290 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
291 bwrite(fswritefd, (char *)&sblock,
292 fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
293 }
294 ckfini(1);
295 free(blockmap);
296 free(statemap);
297 free((char *)lncntp);
298 if (!fsmodified)
299 return (0);
300 if (!preen)
301 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
302 if (rerun)
303 printf("\n***** PLEASE RERUN FSCK *****\n");
304 if (hotroot()) {
305 struct statfs stfs_buf;
306 /*
307 * We modified the root. Do a mount update on
308 * it, unless it is read-write, so we can continue.
309 */
310 if (statfs("/", &stfs_buf) == 0) {
311 long flags = stfs_buf.f_flags;
312 struct ufs_args args;
313 int ret;
314
315 if (flags & MNT_RDONLY) {
316 args.fspec = 0;
317 args.export.ex_flags = 0;
318 args.export.ex_root = 0;
319 flags |= MNT_UPDATE | MNT_RELOAD;
320 ret = mount(MOUNT_FFS, "/", flags, &args);
321 if (ret == 0)
322 return(0);
323 }
324 }
325 if (!preen)
326 printf("\n***** REBOOT NOW *****\n");
327 sync();
328 return (4);
329 }
330 return (0);
331 }
332
333 static void
334 usage()
335 {
336 extern char *__progname;
337
338 (void) fprintf(stderr,
339 "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
340 __progname);
341 exit(1);
342 }
343
344