xref: /openbsd-src/sbin/fsck_ffs/main.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: main.c,v 1.33 2008/06/10 23:10:29 otto Exp $	*/
2 /*	$NetBSD: main.c,v 1.22 1996/10/11 20:15:48 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1986, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
42 #else
43 static const char rcsid[] = "$OpenBSD: main.c,v 1.33 2008/06/10 23:10:29 otto Exp $";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/mount.h>
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ffs/fs.h>
52 #include <fstab.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <unistd.h>
58 
59 #include "fsck.h"
60 #include "extern.h"
61 #include "fsutil.h"
62 
63 volatile sig_atomic_t returntosingle;
64 
65 int	argtoi(int, char *, char *, int);
66 int	checkfilesys(char *, char *, long, int);
67 int	docheck(struct fstab *);
68 int	main(int, char *[]);
69 
70 extern char *__progname;
71 
72 int
73 main(int argc, char *argv[])
74 {
75 	int ch;
76 	int ret = 0;
77 
78 	sync();
79 	skipclean = 1;
80 	while ((ch = getopt(argc, argv, "dfpnNyYb:c:m:")) != -1) {
81 		switch (ch) {
82 		case 'p':
83 			preen++;
84 			break;
85 
86 		case 'b':
87 			skipclean = 0;
88 			bflag = argtoi('b', "number", optarg, 10);
89 			printf("Alternate super block location: %d\n", bflag);
90 			break;
91 
92 		case 'c':
93 			skipclean = 0;
94 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
95 			break;
96 
97 		case 'd':
98 			debug++;
99 			break;
100 
101 		case 'f':
102 			skipclean = 0;
103 			break;
104 
105 		case 'm':
106 			lfmode = argtoi('m', "mode", optarg, 8);
107 			if (lfmode &~ 07777)
108 				errexit("bad mode to -m: %o\n", lfmode);
109 			printf("** lost+found creation mode %o\n", lfmode);
110 			break;
111 
112 		case 'n':
113 		case 'N':
114 			nflag++;
115 			yflag = 0;
116 			break;
117 
118 		case 'y':
119 		case 'Y':
120 			yflag++;
121 			nflag = 0;
122 			break;
123 
124 		default:
125 			errexit("usage: %s [-fnpy] [-b block#] [-c level] "
126 			    "[-m mode] filesystem ...\n", __progname);
127 		}
128 	}
129 	argc -= optind;
130 	argv += optind;
131 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
132 		(void)signal(SIGINT, catch);
133 	if (preen)
134 		(void)signal(SIGQUIT, catchquit);
135 	(void)signal(SIGINFO, catchinfo);
136 
137 	if (argc)
138 		while (argc-- > 0)
139 			(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
140 
141 	if (returntosingle)
142 		ret = 2;
143 
144 	exit(ret);
145 }
146 
147 int
148 argtoi(int flag, char *req, char *str, int base)
149 {
150 	char *cp;
151 	int ret;
152 
153 	ret = (int)strtol(str, &cp, base);
154 	if (cp == str || *cp)
155 		errexit("-%c flag requires a %s\n", flag, req);
156 	return (ret);
157 }
158 
159 /*
160  * Determine whether a filesystem should be checked.
161  */
162 int
163 docheck(struct fstab *fsp)
164 {
165 
166 	if ((strcmp(fsp->fs_vfstype, "ufs") &&
167 	     strcmp(fsp->fs_vfstype, "ffs")) ||
168 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
169 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
170 	    fsp->fs_passno == 0)
171 		return (0);
172 	return (1);
173 }
174 
175 /*
176  * Check the specified filesystem.
177  */
178 /* ARGSUSED */
179 int
180 checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
181 {
182 	daddr64_t n_ffree, n_bfree;
183 	struct dups *dp;
184 	struct zlncnt *zlnp;
185 	int cylno;
186 
187 	if (preen && child)
188 		(void)signal(SIGQUIT, voidquit);
189 	setcdevname(filesys, preen);
190 	if (debug && preen)
191 		pwarn("starting\n");
192 	switch (setup(filesys)) {
193 	case 0:
194 		if (preen)
195 			pfatal("CAN'T CHECK FILE SYSTEM.");
196 		/* FALLTHROUGH */
197 	case -1:
198 		if (fsreadfd != -1) {
199 			(void)close(fsreadfd);
200 			fsreadfd = -1;
201 		}
202 		if (fswritefd != -1) {
203 			(void)close(fswritefd);
204 			fswritefd = -1;
205 		}
206 		return (0);
207 	}
208 	info_filesys = filesys;
209 
210 	/*
211 	 * Cleared if any questions answered no. Used to decide if
212 	 * the superblock should be marked clean.
213 	 */
214 	resolved = 1;
215 
216 	/*
217 	 * 1: scan inodes tallying blocks used
218 	 */
219 	if (preen == 0) {
220 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
221 		if (hotroot())
222 			printf("** Root file system\n");
223 		printf("** Phase 1 - Check Blocks and Sizes\n");
224 	}
225 	pass1();
226 
227 	/*
228 	 * 1b: locate first references to duplicates, if any
229 	 */
230 	if (duplist) {
231 		if (preen || usedsoftdep)
232 			pfatal("INTERNAL ERROR: dups with -p");
233 		printf("** Phase 1b - Rescan For More DUPS\n");
234 		pass1b();
235 	}
236 
237 	/*
238 	 * 2: traverse directories from root to mark all connected directories
239 	 */
240 	if (preen == 0)
241 		printf("** Phase 2 - Check Pathnames\n");
242 	pass2();
243 
244 	/*
245 	 * 3: scan inodes looking for disconnected directories
246 	 */
247 	if (preen == 0)
248 		printf("** Phase 3 - Check Connectivity\n");
249 	pass3();
250 
251 	/*
252 	 * 4: scan inodes looking for disconnected files; check reference counts
253 	 */
254 	if (preen == 0)
255 		printf("** Phase 4 - Check Reference Counts\n");
256 	pass4();
257 
258 	/*
259 	 * 5: check and repair resource counts in cylinder groups
260 	 */
261 	if (preen == 0)
262 		printf("** Phase 5 - Check Cyl groups\n");
263 	pass5();
264 
265 	/*
266 	 * print out summary statistics
267 	 */
268 	n_ffree = sblock.fs_cstotal.cs_nffree;
269 	n_bfree = sblock.fs_cstotal.cs_nbfree;
270 	pwarn("%lld files, %lld used, %lld free ",
271 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
272 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
273 	    n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
274 	    ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
275 	if (debug &&
276 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
277 		printf("%lld files missing\n", n_files);
278 	if (debug) {
279 		n_blks += sblock.fs_ncg *
280 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
281 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
282 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
283 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
284 			printf("%lld blocks missing\n", n_blks);
285 		if (duplist != NULL) {
286 			printf("The following duplicate blocks remain:");
287 			for (dp = duplist; dp; dp = dp->next)
288 				printf(" %lld,", dp->dup);
289 			printf("\n");
290 		}
291 		if (zlnhead != NULL) {
292 			printf("The following zero link count inodes remain:");
293 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
294 				printf(" %u,", zlnp->zlncnt);
295 			printf("\n");
296 		}
297 	}
298 	zlnhead = NULL;
299 	duplist = NULL;
300 	muldup = NULL;
301 	inocleanup();
302 	if (fsmodified) {
303 		sblock.fs_time = (time_t)time(NULL);
304 		sbdirty();
305 	}
306 	if (cvtlevel && sblk.b_dirty) {
307 		/*
308 		 * Write out the duplicate super blocks
309 		 */
310 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
311 			bwrite(fswritefd, (char *)&sblock,
312 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
313 	}
314 	if (rerun)
315 		resolved = 0;
316 	ckfini(resolved); /* Don't mark fs clean if fsck needs to be re-run */
317 	free(blockmap);
318 	blockmap = NULL;
319 	free(stmap);
320 	stmap = NULL;
321 	free(lncntp);
322 	lncntp = NULL;
323 	free(sblock.fs_csp);
324 	free(sblk.b_un.b_buf);
325 	free(asblk.b_un.b_buf);
326 
327 	if (!fsmodified)
328 		return (0);
329 	if (!preen)
330 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
331 	if (rerun || !resolved)
332 		printf("\n***** PLEASE RERUN FSCK *****\n");
333 	if (hotroot()) {
334 		struct statfs stfs_buf;
335 		/*
336 		 * We modified the root.  Do a mount update on
337 		 * it, unless it is read-write, so we can continue.
338 		 */
339 		if (statfs("/", &stfs_buf) == 0) {
340 			long flags = stfs_buf.f_flags;
341 			struct ufs_args args;
342 			int ret;
343 
344 			if (flags & MNT_RDONLY) {
345 				args.fspec = 0;
346 				args.export_info.ex_flags = 0;
347 				args.export_info.ex_root = 0;
348 				flags |= MNT_UPDATE | MNT_RELOAD;
349 				ret = mount(MOUNT_FFS, "/", flags, &args);
350 				if (ret == 0)
351 					return(0);
352 			}
353 		}
354 		if (!preen)
355 			printf("\n***** REBOOT NOW *****\n");
356 		sync();
357 		return (4);
358 	}
359 	return (0);
360 }
361