xref: /freebsd/sbin/fsck_ffs/main.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. 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 const 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 static const char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/mount.h>
47 
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/ufsmount.h>
50 #include <ufs/ffs/fs.h>
51 
52 #include <ctype.h>
53 #include <err.h>
54 #include <fstab.h>
55 #include <string.h>
56 
57 #include "fsck.h"
58 
59 int	returntosingle;
60 
61 static int argtoi __P((int flag, char *req, char *str, int base));
62 static int docheck __P((struct fstab *fsp));
63 static int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
64 		int child));
65 int main __P((int argc, char *argv[]));
66 
67 int
68 main(argc, argv)
69 	int	argc;
70 	char	*argv[];
71 {
72 	int ch;
73 	int ret, maxrun = 0;
74 
75 	sync();
76 	while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) {
77 		switch (ch) {
78 		case 'p':
79 			preen++;
80 			break;
81 
82 		case 'b':
83 			bflag = argtoi('b', "number", optarg, 10);
84 			printf("Alternate super block location: %d\n", bflag);
85 			break;
86 
87 		case 'c':
88 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
89 			break;
90 
91 		case 'd':
92 			debug++;
93 			break;
94 
95 		case 'f':
96 			fflag++;
97 			break;
98 
99 		case 'l':
100 			maxrun = argtoi('l', "number", optarg, 10);
101 			break;
102 
103 		case 'm':
104 			lfmode = argtoi('m', "mode", optarg, 8);
105 			if (lfmode &~ 07777)
106 				errx(EEXIT, "bad mode to -m: %o", lfmode);
107 			printf("** lost+found creation mode %o\n", lfmode);
108 			break;
109 
110 		case 'n':
111 		case 'N':
112 			nflag++;
113 			yflag = 0;
114 			break;
115 
116 		case 'y':
117 		case 'Y':
118 			yflag++;
119 			nflag = 0;
120 			break;
121 
122 		default:
123 			errx(EEXIT, "%c option?", ch);
124 		}
125 	}
126 	argc -= optind;
127 	argv += optind;
128 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
129 		(void)signal(SIGINT, catch);
130 	if (preen)
131 		(void)signal(SIGQUIT, catchquit);
132 	if (argc) {
133 		while (argc-- > 0)
134 			(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
135 		exit(0);
136 	}
137 	ret = checkfstab(preen, maxrun, docheck, checkfilesys);
138 	if (returntosingle)
139 		exit(2);
140 	exit(ret);
141 }
142 
143 static int
144 argtoi(flag, req, str, base)
145 	int flag;
146 	char *req, *str;
147 	int base;
148 {
149 	char *cp;
150 	int ret;
151 
152 	ret = (int)strtol(str, &cp, base);
153 	if (cp == str || *cp)
154 		errx(EEXIT, "-%c flag requires a %s", flag, req);
155 	return (ret);
156 }
157 
158 /*
159  * Determine whether a filesystem should be checked.
160  */
161 static int
162 docheck(fsp)
163 	register struct fstab *fsp;
164 {
165 
166 	if (strcmp(fsp->fs_vfstype, "ufs") ||
167 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
168 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
169 	    fsp->fs_passno == 0)
170 		return (0);
171 	return (1);
172 }
173 
174 /*
175  * Check the specified filesystem.
176  */
177 /* ARGSUSED */
178 static int
179 checkfilesys(filesys, mntpt, auxdata, child)
180 	char *filesys, *mntpt;
181 	long auxdata;
182 	int child;
183 {
184 	ufs_daddr_t n_ffree, n_bfree;
185 	struct dups *dp;
186 	struct zlncnt *zlnp;
187 	int cylno, flags;
188 
189 	if (preen && child)
190 		(void)signal(SIGQUIT, voidquit);
191 	cdevname = filesys;
192 	if (debug && preen)
193 		pwarn("starting\n");
194 	switch (setup(filesys)) {
195 	case 0:
196 		if (preen)
197 			pfatal("CAN'T CHECK FILE SYSTEM.");
198 		/* fall through */
199 	case -1:
200 		pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
201 			sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
202 		printf("(%ld frags, %ld blocks, %.1f%% fragmentation)\n",
203 			sblock.fs_cstotal.cs_nffree,
204 			sblock.fs_cstotal.cs_nbfree,
205 			(float)(sblock.fs_cstotal.cs_nffree * 100) /
206 			sblock.fs_dsize);
207 		return(0);
208 	}
209 
210 	/*
211 	 * 1: scan inodes tallying blocks used
212 	 */
213 	if (preen == 0) {
214 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
215 		if (hotroot)
216 			printf("** Root file system\n");
217 		printf("** Phase 1 - Check Blocks and Sizes\n");
218 	}
219 	pass1();
220 
221 	/*
222 	 * 1b: locate first references to duplicates, if any
223 	 */
224 	if (duplist) {
225 		if (preen)
226 			pfatal("INTERNAL ERROR: dups with -p");
227 		printf("** Phase 1b - Rescan For More DUPS\n");
228 		pass1b();
229 	}
230 
231 	/*
232 	 * 2: traverse directories from root to mark all connected directories
233 	 */
234 	if (preen == 0)
235 		printf("** Phase 2 - Check Pathnames\n");
236 	pass2();
237 
238 	/*
239 	 * 3: scan inodes looking for disconnected directories
240 	 */
241 	if (preen == 0)
242 		printf("** Phase 3 - Check Connectivity\n");
243 	pass3();
244 
245 	/*
246 	 * 4: scan inodes looking for disconnected files; check reference counts
247 	 */
248 	if (preen == 0)
249 		printf("** Phase 4 - Check Reference Counts\n");
250 	pass4();
251 
252 	/*
253 	 * 5: check and repair resource counts in cylinder groups
254 	 */
255 	if (preen == 0)
256 		printf("** Phase 5 - Check Cyl groups\n");
257 	pass5();
258 
259 	/*
260 	 * print out summary statistics
261 	 */
262 	n_ffree = sblock.fs_cstotal.cs_nffree;
263 	n_bfree = sblock.fs_cstotal.cs_nbfree;
264 	pwarn("%ld files, %ld used, %ld free ",
265 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
266 	printf("(%ld frags, %ld blocks, %ld.%ld%% fragmentation)\n",
267 	    n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
268 	    ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
269 	if (debug &&
270 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
271 		printf("%ld files missing\n", n_files);
272 	if (debug) {
273 		n_blks += sblock.fs_ncg *
274 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
275 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
276 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
277 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
278 			printf("%ld blocks missing\n", n_blks);
279 		if (duplist != NULL) {
280 			printf("The following duplicate blocks remain:");
281 			for (dp = duplist; dp; dp = dp->next)
282 				printf(" %ld,", dp->dup);
283 			printf("\n");
284 		}
285 		if (zlnhead != NULL) {
286 			printf("The following zero link count inodes remain:");
287 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
288 				printf(" %lu,", zlnp->zlncnt);
289 			printf("\n");
290 		}
291 	}
292 	zlnhead = (struct zlncnt *)0;
293 	duplist = (struct dups *)0;
294 	muldup = (struct dups *)0;
295 	inocleanup();
296 	if (fsmodified) {
297 		(void)time(&sblock.fs_time);
298 		sbdirty();
299 	}
300 	if (cvtlevel && sblk.b_dirty) {
301 		/*
302 		 * Write out the duplicate super blocks
303 		 */
304 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
305 			bwrite(fswritefd, (char *)&sblock,
306 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
307 	}
308 	if (!hotroot) {
309 		ckfini(1);
310 	} else {
311 		struct statfs stfs_buf;
312 		/*
313 		 * Check to see if root is mounted read-write.
314 		 */
315 		if (statfs("/", &stfs_buf) == 0)
316 			flags = stfs_buf.f_flags;
317 		else
318 			flags = 0;
319 		ckfini(flags & MNT_RDONLY);
320 	}
321 	free(blockmap);
322 	free(statemap);
323 	free((char *)lncntp);
324 	if (!fsmodified)
325 		return (0);
326 	if (!preen)
327 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
328 	if (rerun)
329 		printf("\n***** PLEASE RERUN FSCK *****\n");
330 	if (hotroot) {
331 		struct ufs_args args;
332 		int ret;
333 		/*
334 		 * We modified the root.  Do a mount update on
335 		 * it, unless it is read-write, so we can continue.
336 		 */
337 		if (flags & MNT_RDONLY) {
338 			args.fspec = 0;
339 			args.export.ex_flags = 0;
340 			args.export.ex_root = 0;
341 			flags |= MNT_UPDATE | MNT_RELOAD;
342 			ret = mount("ufs", "/", flags, &args);
343 			if (ret == 0)
344 				return (0);
345 		}
346 		if (!preen)
347 			printf("\n***** REBOOT NOW *****\n");
348 		sync();
349 		return (4);
350 	}
351 	return (0);
352 }
353