xref: /netbsd-src/sbin/fsck_ffs/main.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: main.c,v 1.23 1996/10/22 16:35:04 christos 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.23 1996/10/22 16:35:04 christos 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 #include "fsutil.h"
65 
66 int	returntosingle;
67 
68 int	main __P((int, char *[]));
69 
70 static int	argtoi __P((int, char *, char *, int));
71 static int	checkfilesys __P((char *, char *, long, int));
72 static int	docheck __P((struct fstab *));
73 static  void usage __P((void));
74 
75 
76 int
77 main(argc, argv)
78 	int	argc;
79 	char	*argv[];
80 {
81 	int ch;
82 	int ret = 0;
83 	extern char *optarg;
84 	extern int optind;
85 
86 	sync();
87 	skipclean = 1;
88 	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != EOF) {
89 		switch (ch) {
90 		case 'b':
91 			skipclean = 0;
92 			bflag = argtoi('b', "number", optarg, 10);
93 			printf("Alternate super block location: %d\n", bflag);
94 			break;
95 
96 		case 'c':
97 			skipclean = 0;
98 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
99 			break;
100 
101 		case 'd':
102 			debug++;
103 			break;
104 
105 		case 'f':
106 			skipclean = 0;
107 			break;
108 
109 		case 'm':
110 			lfmode = argtoi('m', "mode", optarg, 8);
111 			if (lfmode &~ 07777)
112 				errexit("bad mode to -m: %o\n", lfmode);
113 			printf("** lost+found creation mode %o\n", lfmode);
114 			break;
115 
116 		case 'n':
117 			nflag++;
118 			yflag = 0;
119 			break;
120 
121 		case 'p':
122 			preen++;
123 			break;
124 
125 		case 'y':
126 			yflag++;
127 			nflag = 0;
128 			break;
129 
130 		default:
131 			usage();
132 		}
133 	}
134 
135 	argc -= optind;
136 	argv += optind;
137 
138 	if (!argc)
139 		usage();
140 
141 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
142 		(void)signal(SIGINT, catch);
143 	if (preen)
144 		(void)signal(SIGQUIT, catchquit);
145 
146 	while (argc-- > 0)
147 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
148 
149 	if (returntosingle)
150 		ret = 2;
151 
152 	exit(ret);
153 }
154 
155 static int
156 argtoi(flag, req, str, base)
157 	int flag;
158 	char *req, *str;
159 	int base;
160 {
161 	char *cp;
162 	int ret;
163 
164 	ret = (int)strtol(str, &cp, base);
165 	if (cp == str || *cp)
166 		errexit("-%c flag requires a %s\n", flag, req);
167 	return (ret);
168 }
169 
170 /*
171  * Determine whether a filesystem should be checked.
172  */
173 static int
174 docheck(fsp)
175 	register struct fstab *fsp;
176 {
177 
178 	if ((strcmp(fsp->fs_vfstype, "ufs") &&
179 	     strcmp(fsp->fs_vfstype, "ffs")) ||
180 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
181 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
182 	    fsp->fs_passno == 0)
183 		return (0);
184 	return (1);
185 }
186 
187 /*
188  * Check the specified filesystem.
189  */
190 /* ARGSUSED */
191 static int
192 checkfilesys(filesys, mntpt, auxdata, child)
193 	char *filesys, *mntpt;
194 	long auxdata;
195 	int child;
196 {
197 	daddr_t n_ffree, n_bfree;
198 	struct dups *dp;
199 	struct zlncnt *zlnp;
200 	int cylno;
201 
202 	if (preen && child)
203 		(void)signal(SIGQUIT, voidquit);
204 	setcdevname(filesys, preen);
205 	if (debug && preen)
206 		pwarn("starting\n");
207 	switch (setup(filesys)) {
208 	case 0:
209 		if (preen)
210 			pfatal("CAN'T CHECK FILE SYSTEM.");
211 	case -1:
212 		return (0);
213 	}
214 	/*
215 	 * 1: scan inodes tallying blocks used
216 	 */
217 	if (preen == 0) {
218 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
219 		if (hotroot())
220 			printf("** Root file system\n");
221 		printf("** Phase 1 - Check Blocks and Sizes\n");
222 	}
223 	pass1();
224 
225 	/*
226 	 * 1b: locate first references to duplicates, if any
227 	 */
228 	if (duplist) {
229 		if (preen)
230 			pfatal("INTERNAL ERROR: dups with -p");
231 		printf("** Phase 1b - Rescan For More DUPS\n");
232 		pass1b();
233 	}
234 
235 	/*
236 	 * 2: traverse directories from root to mark all connected directories
237 	 */
238 	if (preen == 0)
239 		printf("** Phase 2 - Check Pathnames\n");
240 	pass2();
241 
242 	/*
243 	 * 3: scan inodes looking for disconnected directories
244 	 */
245 	if (preen == 0)
246 		printf("** Phase 3 - Check Connectivity\n");
247 	pass3();
248 
249 	/*
250 	 * 4: scan inodes looking for disconnected files; check reference counts
251 	 */
252 	if (preen == 0)
253 		printf("** Phase 4 - Check Reference Counts\n");
254 	pass4();
255 
256 	/*
257 	 * 5: check and repair resource counts in cylinder groups
258 	 */
259 	if (preen == 0)
260 		printf("** Phase 5 - Check Cyl groups\n");
261 	pass5();
262 
263 	/*
264 	 * print out summary statistics
265 	 */
266 	n_ffree = sblock.fs_cstotal.cs_nffree;
267 	n_bfree = sblock.fs_cstotal.cs_nbfree;
268 	pwarn("%d files, %d used, %d free ",
269 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
270 	printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n",
271 	    n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
272 	    ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
273 	if (debug &&
274 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
275 		printf("%d files missing\n", n_files);
276 	if (debug) {
277 		n_blks += sblock.fs_ncg *
278 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
279 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
280 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
281 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
282 			printf("%d blocks missing\n", n_blks);
283 		if (duplist != NULL) {
284 			printf("The following duplicate blocks remain:");
285 			for (dp = duplist; dp; dp = dp->next)
286 				printf(" %d,", dp->dup);
287 			printf("\n");
288 		}
289 		if (zlnhead != NULL) {
290 			printf("The following zero link count inodes remain:");
291 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
292 				printf(" %u,", zlnp->zlncnt);
293 			printf("\n");
294 		}
295 	}
296 	zlnhead = (struct zlncnt *)0;
297 	duplist = (struct dups *)0;
298 	muldup = (struct dups *)0;
299 	inocleanup();
300 	if (fsmodified) {
301 		(void)time(&sblock.fs_time);
302 		sbdirty();
303 	}
304 	if (cvtlevel && sblk.b_dirty) {
305 		/*
306 		 * Write out the duplicate super blocks
307 		 */
308 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
309 			bwrite(fswritefd, (char *)&sblock,
310 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
311 	}
312 	ckfini(1);
313 	free(blockmap);
314 	free(statemap);
315 	free((char *)lncntp);
316 	if (!fsmodified)
317 		return (0);
318 	if (!preen)
319 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
320 	if (rerun)
321 		printf("\n***** PLEASE RERUN FSCK *****\n");
322 	if (hotroot()) {
323 		struct statfs stfs_buf;
324 		/*
325 		 * We modified the root.  Do a mount update on
326 		 * it, unless it is read-write, so we can continue.
327 		 */
328 		if (statfs("/", &stfs_buf) == 0) {
329 			long flags = stfs_buf.f_flags;
330 			struct ufs_args args;
331 			int ret;
332 
333 			if (flags & MNT_RDONLY) {
334 				args.fspec = 0;
335 				args.export.ex_flags = 0;
336 				args.export.ex_root = 0;
337 				flags |= MNT_UPDATE | MNT_RELOAD;
338 				ret = mount(MOUNT_FFS, "/", flags, &args);
339 				if (ret == 0)
340 					return(0);
341 			}
342 		}
343 		if (!preen)
344 			printf("\n***** REBOOT NOW *****\n");
345 		sync();
346 		return (4);
347 	}
348 	return (0);
349 }
350 
351 static void
352 usage()
353 {
354 	extern char *__progname;
355 
356 	(void) fprintf(stderr,
357 	    "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
358 	    __progname);
359 	exit(1);
360 }
361 
362