main.c revision 1.46 1 /* $NetBSD: main.c,v 1.46 2003/08/07 10:04:20 agc 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.46 2003/08/07 10:04:20 agc Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <fstab.h>
59 #include <string.h>
60 #include <time.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((const char *, char *, long, int));
76 static void usage __P((void));
77
78 int
79 main(argc, argv)
80 int argc;
81 char *argv[];
82 {
83 struct rlimit r;
84 int ch;
85 int ret = 0;
86
87 if (getrlimit(RLIMIT_DATA, &r) == 0) {
88 r.rlim_cur = r.rlim_max;
89 (void) setrlimit(RLIMIT_DATA, &r);
90 }
91 sync();
92 skipclean = 1;
93 markclean = 1;
94 forceimage = 0;
95 endian = 0;
96 isappleufs = 0;
97 while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npy")) != -1) {
98 switch (ch) {
99 case 'a':
100 isappleufs = 1;
101 break;
102
103 case 'B':
104 if (strcmp(optarg, "be") == 0)
105 endian = BIG_ENDIAN;
106 else if (strcmp(optarg, "le") == 0)
107 endian = LITTLE_ENDIAN;
108 else usage();
109 break;
110
111 case 'b':
112 skipclean = 0;
113 bflag = argtoi('b', "number", optarg, 10);
114 printf("Alternate super block location: %d\n", bflag);
115 break;
116
117 case 'c':
118 skipclean = 0;
119 cvtlevel = argtoi('c', "conversion level", optarg, 10);
120 break;
121
122 case 'd':
123 debug++;
124 break;
125
126 case 'F':
127 forceimage = 1;
128 break;
129
130 case 'f':
131 skipclean = 0;
132 break;
133
134 case 'm':
135 lfmode = argtoi('m', "mode", optarg, 8);
136 if (lfmode &~ 07777)
137 errx(EEXIT, "bad mode to -m: %o", lfmode);
138 printf("** lost+found creation mode %o\n", lfmode);
139 break;
140
141 case 'n':
142 nflag++;
143 yflag = 0;
144 break;
145
146 case 'p':
147 preen++;
148 break;
149
150 case 'y':
151 yflag++;
152 nflag = 0;
153 break;
154
155 default:
156 usage();
157 }
158 }
159
160 argc -= optind;
161 argv += optind;
162
163 if (!argc)
164 usage();
165
166 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
167 (void)signal(SIGINT, catch);
168 if (preen)
169 (void)signal(SIGQUIT, catchquit);
170 signal(SIGINFO, infohandler);
171
172 while (argc-- > 0) {
173 const char *path = blockcheck(*argv);
174
175 if (path == NULL)
176 pfatal("Can't check %s\n", *argv);
177 else
178 (void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
179 argv++;
180 }
181
182 if (returntosingle)
183 ret = 2;
184
185 exit(ret);
186 }
187
188 static int
189 argtoi(flag, req, str, base)
190 int flag;
191 char *req, *str;
192 int base;
193 {
194 char *cp;
195 int ret;
196
197 ret = (int)strtol(str, &cp, base);
198 if (cp == str || *cp)
199 errx(EEXIT, "-%c flag requires a %s", flag, req);
200 return (ret);
201 }
202
203 /*
204 * Check the specified filesystem.
205 */
206 /* ARGSUSED */
207 static int
208 checkfilesys(filesys, mntpt, auxdata, child)
209 const char *filesys;
210 char *mntpt;
211 long auxdata;
212 int child;
213 {
214 daddr_t n_ffree, n_bfree;
215 struct dups *dp;
216 struct zlncnt *zlnp;
217 int cylno;
218 #ifdef LITE2BORKEN
219 int flags;
220 #endif
221
222 if (preen && child)
223 (void)signal(SIGQUIT, voidquit);
224 setcdevname(filesys, preen);
225 if (debug && preen)
226 pwarn("starting\n");
227 switch (setup(filesys)) {
228 case 0:
229 if (preen)
230 pfatal("CAN'T CHECK FILE SYSTEM.");
231 /* fall through */
232 case -1:
233 return (0);
234 }
235 /*
236 * Cleared if any questions answered no. Used to decide if
237 * the superblock should be marked clean.
238 */
239 resolved = 1;
240 /*
241 * 1: scan inodes tallying blocks used
242 */
243 if (preen == 0) {
244 printf("** Last Mounted on %s\n", sblock->fs_fsmnt);
245 if (hotroot())
246 printf("** Root file system\n");
247 printf("** Phase 1 - Check Blocks and Sizes\n");
248 }
249 pass1();
250
251 /*
252 * 1b: locate first references to duplicates, if any
253 */
254 if (duplist) {
255 if (preen)
256 pfatal("INTERNAL ERROR: dups with -p\n");
257 if (usedsoftdep)
258 pfatal("INTERNAL ERROR: dups with softdep\n");
259 printf("** Phase 1b - Rescan For More DUPS\n");
260 pass1b();
261 }
262
263 /*
264 * 2: traverse directories from root to mark all connected directories
265 */
266 if (preen == 0)
267 printf("** Phase 2 - Check Pathnames\n");
268 pass2();
269
270 /*
271 * 3: scan inodes looking for disconnected directories
272 */
273 if (preen == 0)
274 printf("** Phase 3 - Check Connectivity\n");
275 pass3();
276
277 /*
278 * 4: scan inodes looking for disconnected files; check reference counts
279 */
280 if (preen == 0)
281 printf("** Phase 4 - Check Reference Counts\n");
282 pass4();
283
284 /*
285 * 5: check and repair resource counts in cylinder groups
286 */
287 if (preen == 0)
288 printf("** Phase 5 - Check Cyl groups\n");
289 pass5();
290
291 /*
292 * print out summary statistics
293 */
294 n_ffree = sblock->fs_cstotal.cs_nffree;
295 n_bfree = sblock->fs_cstotal.cs_nbfree;
296 pwarn("%d files, %lld used, %lld free ",
297 n_files, (long long)n_blks,
298 (long long)(n_ffree + sblock->fs_frag * n_bfree));
299 printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
300 (long long)n_ffree, (long long)n_bfree,
301 (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
302 (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
303 / (daddr_t)sblock->fs_dsize) % 10));
304 if (debug &&
305 (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
306 printf("%d files missing\n", n_files);
307 if (debug) {
308 n_blks += sblock->fs_ncg *
309 (cgdmin(sblock, 0) - cgsblock(sblock, 0));
310 n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
311 n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
312 if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
313 printf("%lld blocks missing\n", (long long)n_blks);
314 if (duplist != NULL) {
315 printf("The following duplicate blocks remain:");
316 for (dp = duplist; dp; dp = dp->next)
317 printf(" %lld,", (long long)dp->dup);
318 printf("\n");
319 }
320 if (zlnhead != NULL) {
321 printf("The following zero link count inodes remain:");
322 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
323 printf(" %u,", zlnp->zlncnt);
324 printf("\n");
325 }
326 }
327 zlnhead = (struct zlncnt *)0;
328 duplist = (struct dups *)0;
329 muldup = (struct dups *)0;
330 inocleanup();
331 if (fsmodified) {
332 sblock->fs_time = time(NULL);
333 sbdirty();
334 }
335 if (rerun)
336 markclean = 0;
337 #if LITE2BORKEN
338 if (!hotroot()) {
339 ckfini();
340 } else {
341 struct statfs stfs_buf;
342 /*
343 * Check to see if root is mounted read-write.
344 */
345 if (statfs("/", &stfs_buf) == 0)
346 flags = stfs_buf.f_flags;
347 else
348 flags = 0;
349 if (markclean)
350 markclean = flags & MNT_RDONLY;
351 ckfini();
352 }
353 #else
354 ckfini();
355 #endif
356 for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
357 if (inostathead[cylno].il_stat != NULL)
358 free(inostathead[cylno].il_stat);
359 free(inostathead);
360 inostathead = NULL;
361
362 if (!fsmodified)
363 return (0);
364 if (!preen)
365 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
366 if (rerun)
367 printf("\n***** PLEASE RERUN FSCK *****\n");
368 if (hotroot()) {
369 struct statfs stfs_buf;
370 /*
371 * We modified the root. Do a mount update on
372 * it, unless it is read-write, so we can continue.
373 */
374 if (statfs("/", &stfs_buf) == 0) {
375 long flags = stfs_buf.f_flags;
376 struct ufs_args args;
377 int ret;
378
379 if (flags & MNT_RDONLY) {
380 args.fspec = 0;
381 args.export.ex_flags = 0;
382 args.export.ex_root = 0;
383 flags |= MNT_UPDATE | MNT_RELOAD;
384 ret = mount(MOUNT_FFS, "/", flags, &args);
385 if (ret == 0)
386 return(0);
387 }
388 }
389 if (!preen)
390 printf("\n***** REBOOT NOW *****\n");
391 sync();
392 return (4);
393 }
394 return (0);
395 }
396
397 static void
398 usage()
399 {
400
401 (void) fprintf(stderr,
402 "Usage: %s [-dFfnpy] [-B be|le] [-b block] [-c level] [-m mode]"
403 " filesystem ...\n",
404 getprogname());
405 exit(1);
406 }
407
408