main.c revision 1.15 1 /* $NetBSD: main.c,v 1.15 1995/03/18 14:55:48 cgd 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.15 1995/03/18 14:55:48 cgd 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 while ((ch = getopt(argc, argv, "dpnNyYb:c:l:m:")) != EOF) {
83 switch (ch) {
84 case 'p':
85 preen++;
86 break;
87
88 case 'b':
89 bflag = argtoi('b', "number", optarg, 10);
90 printf("Alternate super block location: %d\n", bflag);
91 break;
92
93 case 'c':
94 cvtlevel = argtoi('c', "conversion level", optarg, 10);
95 break;
96
97 case 'd':
98 debug++;
99 break;
100
101 case 'l':
102 maxrun = argtoi('l', "number", optarg, 10);
103 break;
104
105 case 'm':
106 lfmode = argtoi('m', "mode", optarg, 8);
107 if (lfmode &~ 07777)
108 errexit("bad mode to -m: %o\n", lfmode);
109 printf("** lost+found creation mode %o\n", lfmode);
110 break;
111
112 case 'n':
113 case 'N':
114 nflag++;
115 yflag = 0;
116 break;
117
118 case 'y':
119 case 'Y':
120 yflag++;
121 nflag = 0;
122 break;
123
124 default:
125 errexit("%c option?\n", ch);
126 }
127 }
128 argc -= optind;
129 argv += optind;
130 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
131 (void)signal(SIGINT, catch);
132 if (preen)
133 (void)signal(SIGQUIT, catchquit);
134 if (argc) {
135 while (argc-- > 0)
136 (void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
137 exit(0);
138 }
139 ret = checkfstab(preen, maxrun, docheck, checkfilesys);
140 if (returntosingle)
141 exit(2);
142 exit(ret);
143 }
144
145 int
146 argtoi(flag, req, str, base)
147 int flag;
148 char *req, *str;
149 int base;
150 {
151 char *cp;
152 int ret;
153
154 ret = (int)strtol(str, &cp, base);
155 if (cp == str || *cp)
156 errexit("-%c flag requires a %s\n", flag, req);
157 return (ret);
158 }
159
160 /*
161 * Determine whether a filesystem should be checked.
162 */
163 int
164 docheck(fsp)
165 register struct fstab *fsp;
166 {
167
168 if (strcmp(fsp->fs_vfstype, "ufs") ||
169 (strcmp(fsp->fs_type, FSTAB_RW) &&
170 strcmp(fsp->fs_type, FSTAB_RO)) ||
171 fsp->fs_passno == 0)
172 return (0);
173 return (1);
174 }
175
176 /*
177 * Check the specified filesystem.
178 */
179 /* ARGSUSED */
180 int
181 checkfilesys(filesys, mntpt, auxdata, child)
182 char *filesys, *mntpt;
183 long auxdata;
184 int child;
185 {
186 daddr_t n_ffree, n_bfree;
187 struct dups *dp;
188 struct zlncnt *zlnp;
189 int cylno;
190
191 if (preen && child)
192 (void)signal(SIGQUIT, voidquit);
193 cdevname = filesys;
194 if (debug && preen)
195 pwarn("starting\n");
196 if (setup(filesys) == 0) {
197 if (preen)
198 pfatal("CAN'T CHECK FILE SYSTEM.");
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("%ld files, %ld used, %ld free ",
256 n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
257 printf("(%ld frags, %ld 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("%ld 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("%ld 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(" %ld,", 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(" %lu,", 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 ckfini();
300 free(blockmap);
301 free(statemap);
302 free((char *)lncntp);
303 if (!fsmodified)
304 return (0);
305 if (!preen)
306 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
307 if (hotroot) {
308 struct statfs stfs_buf;
309 /*
310 * We modified the root. Do a mount update on
311 * it, unless it is read-write, so we can continue.
312 */
313 if (statfs("/", &stfs_buf) == 0) {
314 long flags = stfs_buf.f_flags;
315 struct ufs_args args;
316 int ret;
317
318 if (flags & MNT_RDONLY) {
319 args.fspec = 0;
320 args.export.ex_flags = 0;
321 args.export.ex_root = 0;
322 flags |= MNT_UPDATE | MNT_RELOAD;
323 ret = mount(MOUNT_UFS, "/", flags, &args);
324 if (ret == 0)
325 return(0);
326 }
327 }
328 if (!preen)
329 printf("\n***** REBOOT NOW *****\n");
330 sync();
331 return (4);
332 }
333 return (0);
334 }
335