xref: /netbsd-src/sbin/fsck_lfs/main.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* $NetBSD: main.c,v 1.53 2019/02/03 03:19:26 mrg Exp $	 */
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/mount.h>
35 
36 #include <ufs/lfs/lfs.h>
37 #include <ufs/lfs/lfs_accessors.h>
38 
39 #include <fstab.h>
40 #include <stdbool.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <err.h>
48 #include <util.h>
49 #include <signal.h>
50 
51 #include "fsck.h"
52 #include "extern.h"
53 #include "fsutil.h"
54 #include "exitvalues.h"
55 
56 volatile sig_atomic_t returntosingle = 0;
57 
58 static int argtoi(int, const char *, const char *, int);
59 static int checkfilesys(const char *, char *, long, int);
60 static void usage(void);
61 static void efun(int, const char *, ...);
62 extern void (*panic_func)(int, const char *, va_list);
63 
64 static void
65 efun(int eval, const char *fmt, ...)
66 {
67 	va_list ap;
68 	va_start(ap, fmt);
69 	verr(EEXIT, fmt, ap);
70 	va_end(ap);
71 }
72 
73 int
74 main(int argc, char **argv)
75 {
76 	int ch;
77 	int ret = FSCK_EXIT_OK;
78 	const char *optstring = "b:dfi:m:npPqUy";
79 	bool reallypreen;
80 
81 	reallypreen = false;
82 	ckfinish = ckfini;
83 	skipclean = 1;
84 	exitonfail = 0;
85 	idaddr = 0x0;
86 	panic_func = vmsg;
87 	esetfunc(efun);
88 	while ((ch = getopt(argc, argv, optstring)) != -1) {
89 		switch (ch) {
90 		case 'b':
91 			skipclean = 0;
92 			bflag = argtoi('b', "number", optarg, 0);
93 			printf("Alternate super block location: %d\n", bflag);
94 			break;
95 		case 'd':
96 			debug++;
97 			break;
98 		case 'e':
99 			exitonfail++;
100 			break;
101 		case 'f':
102 			skipclean = 0;
103 			reallypreen = true;
104 			break;
105 		case 'i':
106 			idaddr = strtol(optarg, NULL, 0);
107 			break;
108 		case 'm':
109 			lfmode = argtoi('m', "mode", optarg, 8);
110 			if (lfmode & ~07777)
111 				err(1, "bad mode to -m: %o", lfmode);
112 			printf("** lost+found creation mode %o\n", lfmode);
113 			break;
114 
115 		case 'n':
116 			nflag++;
117 			yflag = 0;
118 			break;
119 
120 		case 'p':
121 			preen++;
122 			break;
123 
124 		case 'P':		/* Progress meter not implemented. */
125 			break;
126 
127 		case 'q':
128 			quiet++;
129 			break;
130 #ifndef SMALL
131 		case 'U':
132 			Uflag++;
133 			break;
134 #endif
135 		case 'y':
136 			yflag++;
137 			nflag = 0;
138 			break;
139 
140 		default:
141 			usage();
142 		}
143 	}
144 
145 	argc -= optind;
146 	argv += optind;
147 
148 	if (!argc)
149 		usage();
150 
151 	/*
152 	 * Don't do anything in preen mode. This is a replacement for
153 	 * version 1.111 of src/distrib/utils/sysinst/disks.c, which
154 	 * disabled fsck on installer-generated lfs partitions. That
155 	 * isn't the right way to do it; better to run fsck but have
156 	 * it not do anything, so that when the issues in fsck get
157 	 * resolved it can be turned back on.
158 	 *
159 	 * If you really want to run fsck in preen mode you can do:
160 	 *    fsck_lfs -p -f image
161 	 *
162 	 * This was prompted by
163 	 * http://mail-index.netbsd.org/tech-kern/2010/02/09/msg007306.html.
164 	 *
165 	 * It would be nice if someone prepared a more detailed report
166 	 * of the problems.
167 	 *
168 	 * XXX.
169 	 */
170 	if (preen && !reallypreen) {
171 		return ret;
172 	}
173 
174 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
175 		(void) signal(SIGINT, catch);
176 	if (preen)
177 		(void) signal(SIGQUIT, catchquit);
178 
179 	while (argc-- > 0) {
180 		int nret = checkfilesys(blockcheck(*argv++), 0, 0L, 0);
181 		if (ret < nret)
182 			ret = nret;
183 	}
184 
185 	return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
186 }
187 
188 static int
189 argtoi(int flag, const char *req, const char *str, int base)
190 {
191 	char *cp;
192 	int ret;
193 
194 	ret = (int) strtol(str, &cp, base);
195 	if (cp == str || *cp)
196 		err(FSCK_EXIT_USAGE, "-%c flag requires a %s", flag, req);
197 	return (ret);
198 }
199 
200 /*
201  * Check the specified filesystem.
202  */
203 
204 /* ARGSUSED */
205 static int
206 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
207 {
208 	struct dups *dp;
209 	struct zlncnt *zlnp;
210 
211 	if (preen && child)
212 		(void) signal(SIGQUIT, voidquit);
213 	setcdevname(filesys, preen);
214 	if (debug && preen)
215 		pwarn("starting\n");
216 	switch (setup(filesys)) {
217 	case 0:
218 		if (preen)
219 			pfatal("CAN'T CHECK FILE SYSTEM.");
220 		/* FALLTHROUGH */
221 	case -1:
222 		return FSCK_EXIT_OK;
223 	}
224 
225 	/*
226 	 * For LFS, "preen" means "roll forward".  We don't check anything
227 	 * else.
228 	 */
229 	if (preen == 0) {
230 		printf("** Last Mounted on %s\n", lfs_sb_getfsmnt(fs));
231 		if (hotroot())
232 			printf("** Root file system\n");
233 		/*
234 		 * 0: check segment checksums, inode ranges
235 		 */
236 		printf("** Phase 0 - Check Inode Free List\n");
237 	}
238 
239 	/*
240 	 * Check inode free list - we do this even if idaddr is set,
241 	 * since if we're writing we don't want to write a bad list.
242 	 */
243 	pass0();
244 
245 	if (preen == 0) {
246 		/*
247 		 * 1: scan inodes tallying blocks used
248 		 */
249 		printf("** Phase 1 - Check Blocks and Sizes\n");
250 		pass1();
251 
252 		/*
253 		 * 2: traverse directories from root to mark all connected directories
254 		 */
255 		printf("** Phase 2 - Check Pathnames\n");
256 		pass2();
257 
258 		/*
259 		 * 3: scan inodes looking for disconnected directories
260 		 */
261 		printf("** Phase 3 - Check Connectivity\n");
262 		pass3();
263 
264 		/*
265 		 * 4: scan inodes looking for disconnected files; check reference counts
266 		 */
267 		printf("** Phase 4 - Check Reference Counts\n");
268 		pass4();
269 	}
270 
271 	/*
272 	 * 5: check segment byte totals and dirty flags, and cleanerinfo
273 	 */
274 	if (!preen)
275 		printf("** Phase 5 - Check Segment Block Accounting\n");
276 	pass5();
277 
278 	if (debug && !preen) {
279 		if (duplist != NULL) {
280 			printf("The following duplicate blocks remain:");
281 			for (dp = duplist; dp; dp = dp->next)
282 				printf(" %lld,", (long long) dp->dup);
283 			printf("\n");
284 		}
285 		if (zlnhead != NULL) {
286 			printf("The following zero link count inodes remain:");
287 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
288 				printf(" %llu,",
289 				    (unsigned long long)zlnp->zlncnt);
290 			printf("\n");
291 		}
292 	}
293 
294 	if (!rerun) {
295 		if (!preen) {
296 			if (reply("ROLL FILESYSTEM FORWARD") == 1) {
297 				printf("** Phase 6 - Roll Forward\n");
298 				pass6();
299 			}
300 		}
301 		else {
302 			pass6();
303 		}
304 	}
305 	zlnhead = (struct zlncnt *) 0;
306 	orphead = (struct zlncnt *) 0;
307 	duplist = (struct dups *) 0;
308 	muldup = (struct dups *) 0;
309 	inocleanup();
310 
311 	/*
312 	 * print out summary statistics
313 	 */
314 	pwarn("%ju files, %jd used, %jd free\n",
315 	    (uintmax_t) n_files, (intmax_t) n_blks,
316 	    (intmax_t) lfs_sb_getbfree(fs));
317 
318 	ckfini(1);
319 
320 	free(blockmap);
321 	free(statemap);
322 	free((char *)lncntp);
323 	if (!fsmodified) {
324 		return FSCK_EXIT_OK;
325 	}
326 	if (!preen)
327 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
328 	if (rerun)
329 		printf("\n***** PLEASE RERUN FSCK *****\n");
330 	if (hotroot()) {
331 		struct statvfs stfs_buf;
332 		/*
333 		 * We modified the root.  Do a mount update on
334 		 * it, unless it is read-write, so we can continue.
335 		 */
336 		if (statvfs("/", &stfs_buf) == 0) {
337 			long flags = stfs_buf.f_flag;
338 			struct ulfs_args args;
339 
340 			if (flags & MNT_RDONLY) {
341 				args.fspec = 0;
342 				flags |= MNT_UPDATE | MNT_RELOAD;
343 				if (mount(MOUNT_LFS, "/", flags,
344 				    &args, sizeof args) == 0)
345 					return FSCK_EXIT_OK;
346 			}
347 		}
348 		if (!preen)
349 			printf("\n***** REBOOT NOW *****\n");
350 		sync();
351 		return FSCK_EXIT_ROOT_CHANGED;
352 	}
353 	return FSCK_EXIT_OK;
354 }
355 
356 static void
357 usage(void)
358 {
359 
360 	(void) fprintf(stderr,
361 	    "Usage: %s [-dfpqU] [-b block] [-m mode] [-y | -n] filesystem ...\n",
362 	    getprogname());
363 	exit(FSCK_EXIT_USAGE);
364 }
365