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