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