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