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