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