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