main.c revision 1.32 1 /* $NetBSD: main.c,v 1.32 2008/10/12 20:49:43 wiz 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 /*
33 * Copyright (c) 1997 Manuel Bouyer.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Manuel Bouyer.
46 * 4. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
64 The Regents of the University of California. All rights reserved.");
65 #endif /* not lint */
66
67 #ifndef lint
68 #if 0
69 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/23/94";
70 #else
71 __RCSID("$NetBSD: main.c,v 1.32 2008/10/12 20:49:43 wiz Exp $");
72 #endif
73 #endif /* not lint */
74
75 #include <sys/param.h>
76 #include <sys/time.h>
77 #include <sys/mount.h>
78 #include <ufs/ufs/ufsmount.h>
79 #include <ufs/ext2fs/ext2fs_dinode.h>
80 #include <ufs/ext2fs/ext2fs.h>
81 #include <fstab.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <ctype.h>
85 #include <stdio.h>
86 #include <time.h>
87 #include <unistd.h>
88 #include <signal.h>
89
90 #include "fsck.h"
91 #include "extern.h"
92 #include "fsutil.h"
93 #include "exitvalues.h"
94
95 int returntosingle = 0;
96
97
98 static int argtoi(int, const char *, const char *, int);
99 static int checkfilesys(const char *, char *, long, int);
100 static void usage(void) __dead;
101
102 int
103 main(int argc, char *argv[])
104 {
105 int ch;
106 int ret = FSCK_EXIT_OK;
107
108 sync();
109 skipclean = 1;
110 while ((ch = getopt(argc, argv, "b:dfm:npqUy")) != -1) {
111 switch (ch) {
112 case 'b':
113 skipclean = 0;
114 bflag = argtoi('b', "number", optarg, 10);
115 printf("Alternate super block location: %d\n", bflag);
116 break;
117
118 case 'd':
119 debug++;
120 break;
121
122 case 'f':
123 skipclean = 0;
124 break;
125
126 case 'm':
127 lfmode = argtoi('m', "mode", optarg, 8);
128 if (lfmode &~ 07777)
129 errexit("bad mode to -m: %o", lfmode);
130 printf("** lost+found creation mode %o\n", lfmode);
131 break;
132
133 case 'n':
134 nflag++;
135 yflag = 0;
136 break;
137
138 case 'p':
139 preen++;
140 break;
141
142 case 'P':
143 /* Progress meter not implemented. */
144 break;
145
146 case 'q': /* Quiet not implemented */
147 break;
148
149 #ifndef SMALL
150 case 'U':
151 Uflag++;
152 break;
153 #endif
154
155 case 'y':
156 yflag++;
157 nflag = 0;
158 break;
159
160 default:
161 usage();
162 }
163 }
164
165 argc -= optind;
166 argv += optind;
167
168 if (!argc)
169 usage();
170
171 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
172 (void)signal(SIGINT, catch);
173 if (preen)
174 (void)signal(SIGQUIT, catchquit);
175
176 while (argc-- > 0) {
177 int nret = checkfilesys(blockcheck(*argv++), 0, 0L, 0);
178 if (ret < nret)
179 ret = nret;
180 }
181
182 return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
183 }
184
185 static int
186 argtoi(int flag, const char *req, const char *str, int base)
187 {
188 char *cp;
189 int ret;
190
191 ret = (int)strtol(str, &cp, base);
192 if (cp == str || *cp)
193 errexit("-%c flag requires a %s", flag, req);
194 return (ret);
195 }
196
197 /*
198 * Check the specified filesystem.
199 */
200 /* ARGSUSED */
201 static int
202 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
203 {
204 daddr_t n_bfree;
205 struct dups *dp;
206 struct zlncnt *zlnp;
207 int i;
208
209 if (preen && child)
210 (void)signal(SIGQUIT, voidquit);
211 setcdevname(filesys, preen);
212 if (debug && preen)
213 pwarn("starting\n");
214 switch (setup(filesys)) {
215 case 0:
216 if (preen)
217 pfatal("CAN'T CHECK FILE SYSTEM.");
218 case -1:
219 return FSCK_EXIT_OK;
220 }
221 /*
222 * 1: scan inodes tallying blocks used
223 */
224 if (preen == 0) {
225 if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
226 printf("** Last Mounted on %s\n",
227 sblock.e2fs.e2fs_fsmnt);
228 }
229 if (hotroot())
230 printf("** Root file system\n");
231 printf("** Phase 1 - Check Blocks and Sizes\n");
232 }
233 pass1();
234
235 /*
236 * 1b: locate first references to duplicates, if any
237 */
238 if (duplist) {
239 if (preen)
240 pfatal("INTERNAL ERROR: dups with -p");
241 printf("** Phase 1b - Rescan For More DUPS\n");
242 pass1b();
243 }
244
245 /*
246 * 2: traverse directories from root to mark all connected directories
247 */
248 if (preen == 0)
249 printf("** Phase 2 - Check Pathnames\n");
250 pass2();
251
252 /*
253 * 3: scan inodes looking for disconnected directories
254 */
255 if (preen == 0)
256 printf("** Phase 3 - Check Connectivity\n");
257 pass3();
258
259 /*
260 * 4: scan inodes looking for disconnected files; check reference counts
261 */
262 if (preen == 0)
263 printf("** Phase 4 - Check Reference Counts\n");
264 pass4();
265
266 /*
267 * 5: check and repair resource counts in cylinder groups
268 */
269 if (preen == 0)
270 printf("** Phase 5 - Check Cyl groups\n");
271 pass5();
272
273 /*
274 * print out summary statistics
275 */
276 n_bfree = sblock.e2fs.e2fs_fbcount;
277
278 pwarn("%lld files, %lld used, %lld free\n",
279 (long long)n_files, (long long)n_blks, (long long)n_bfree);
280 if (debug &&
281 /* 9 reserved and unused inodes in FS */
282 (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
283 printf("%lld files missing\n", (long long)n_files);
284 if (debug) {
285 for (i = 0; i < sblock.e2fs_ncg; i++)
286 n_blks += cgoverhead(i);
287 n_blks += sblock.e2fs.e2fs_first_dblock;
288 if (n_blks -= maxfsblock - n_bfree)
289 printf("%lld blocks missing\n", (long long)n_blks);
290 if (duplist != NULL) {
291 printf("The following duplicate blocks remain:");
292 for (dp = duplist; dp; dp = dp->next)
293 printf(" %lld,", (long long)dp->dup);
294 printf("\n");
295 }
296 if (zlnhead != NULL) {
297 printf("The following zero link count inodes remain:");
298 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
299 printf(" %llu,",
300 (unsigned long long)zlnp->zlncnt);
301 printf("\n");
302 }
303 }
304 zlnhead = (struct zlncnt *)0;
305 duplist = (struct dups *)0;
306 muldup = (struct dups *)0;
307 inocleanup();
308 if (fsmodified) {
309 time_t t;
310 (void)time(&t);
311 sblock.e2fs.e2fs_wtime = t;
312 sblock.e2fs.e2fs_lastfsck = t;
313 sbdirty();
314 }
315 ckfini(1);
316 free(blockmap);
317 free(statemap);
318 free((char *)lncntp);
319 if (!fsmodified)
320 return FSCK_EXIT_OK;
321 if (!preen)
322 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
323 if (rerun)
324 printf("\n***** PLEASE RERUN FSCK *****\n");
325 if (hotroot()) {
326 struct statvfs stfs_buf;
327 /*
328 * We modified the root. Do a mount update on
329 * it, unless it is read-write, so we can continue.
330 */
331 if (statvfs("/", &stfs_buf) == 0) {
332 long flags = stfs_buf.f_flag;
333 struct ufs_args args;
334
335 if (flags & MNT_RDONLY) {
336 args.fspec = 0;
337 flags |= MNT_UPDATE | MNT_RELOAD;
338 if (mount(MOUNT_EXT2FS, "/", flags,
339 &args, sizeof args) == 0)
340 return FSCK_EXIT_OK;
341 }
342 }
343 if (!preen)
344 printf("\n***** REBOOT NOW *****\n");
345 sync();
346 return FSCK_EXIT_ROOT_CHANGED;
347 }
348 return FSCK_EXIT_OK;
349 }
350
351 static void
352 usage(void)
353 {
354
355 (void) fprintf(stderr,
356 "usage: %s [-dfnpUy] [-b block] [-c level] [-m mode] filesystem ...\n",
357 getprogname());
358 exit(FSCK_EXIT_USAGE);
359 }
360
361