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