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