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