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