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