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