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