xref: /netbsd-src/sbin/fsck_lfs/main.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /* $NetBSD: main.c,v 1.14 2003/03/29 00:09:43 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/mount.h>
39 #include <ufs/ufs/dinode.h>
40 #include <sys/mount.h>
41 #include <ufs/ufs/ufsmount.h>
42 #include <ufs/lfs/lfs.h>
43 
44 #include <fstab.h>
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <err.h>
52 
53 #include "fsck.h"
54 #include "extern.h"
55 #include "fsutil.h"
56 
57 int returntosingle;
58 
59 int main(int, char *[]);
60 
61 static int argtoi(int, char *, char *, int);
62 static int checkfilesys(const char *, char *, long, int);
63 static void usage(void);
64 extern void (*panic_func)(int, const char *, va_list);
65 
66 int
67 main(int argc, char **argv)
68 {
69 	int ch;
70 	int ret = 0;
71 	char *optstring = "b:dfi:m:npy";
72 
73 	sync();
74 	skipclean = 1;
75 	exitonfail = 0;
76 	idaddr = 0x0;
77 	panic_func = vmsg;
78 	while ((ch = getopt(argc, argv, optstring)) != -1) {
79 		switch (ch) {
80 		case 'b':
81 			skipclean = 0;
82 			bflag = argtoi('b', "number", optarg, 0);
83 			printf("Alternate super block location: %d\n", bflag);
84 			break;
85 		case 'd':
86 			debug++;
87 			break;
88 		case 'e':
89 			exitonfail++;
90 			break;
91 		case 'f':
92 			skipclean = 0;
93 			break;
94 		case 'i':
95 			idaddr = strtol(optarg, NULL, 0);
96 			break;
97 		case 'm':
98 			lfmode = argtoi('m', "mode", optarg, 8);
99 			if (lfmode & ~07777)
100 				err(1, "bad mode to -m: %o\n", lfmode);
101 			printf("** lost+found creation mode %o\n", lfmode);
102 			break;
103 
104 		case 'n':
105 			nflag++;
106 			yflag = 0;
107 			break;
108 
109 		case 'p':
110 			preen++;
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, char *req, 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 Segment Summaries and Inode Free List\n");
191 	}
192 	if (idaddr)
193 		pwarn("-i given, skipping free list check\n");
194 	else
195 		pass0();
196 
197 	if (preen == 0) {
198 		/*
199 		 * 1: scan inodes tallying blocks used
200 		 */
201 		printf("** Phase 1 - Check Blocks and Sizes\n");
202 		pass1();
203 
204 		/*
205 		 * 2: traverse directories from root to mark all connected directories
206 		 */
207 		printf("** Phase 2 - Check Pathnames\n");
208 		pass2();
209 
210 		/*
211 		 * 3: scan inodes looking for disconnected directories
212 		 */
213 		printf("** Phase 3 - Check Connectivity\n");
214 		pass3();
215 
216 		/*
217 		 * 4: scan inodes looking for disconnected files; check reference counts
218 		 */
219 		printf("** Phase 4 - Check Reference Counts\n");
220 		pass4();
221 
222 		/*
223 		 * 5: check segment byte totals and dirty flags
224 		 */
225 		printf("** Phase 5 - Check Segment Block Accounting\n");
226 		pass5();
227 
228 		if (debug) {
229 			if (duplist != NULL) {
230 				printf("The following duplicate blocks remain:");
231 				for (dp = duplist; dp; dp = dp->next)
232 					printf(" %lld,", (long long) dp->dup);
233 				printf("\n");
234 			}
235 			if (zlnhead != NULL) {
236 				printf("The following zero link count inodes remain:");
237 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
238 					printf(" %u,", zlnp->zlncnt);
239 				printf("\n");
240 			}
241 		}
242 	}
243 	if (!rerun) {
244 		if (!preen)
245 			printf("** Phase 6 - Roll Forward\n");
246 		pass6();
247 	}
248 	zlnhead = (struct zlncnt *) 0;
249 	duplist = (struct dups *) 0;
250 	muldup = (struct dups *) 0;
251 	inocleanup();
252 
253 	/*
254 	 * print out summary statistics
255 	 */
256 	pwarn("%d files, %lld used, %lld free\n",
257 	    n_files, (long long) n_blks,
258 	    (long long) fs->lfs_bfree);
259 
260 	ckfini(1);
261 
262 	free(blockmap);
263 	free(statemap);
264 	free((char *)lncntp);
265 	if (!fsmodified) {
266 		return (0);
267 	}
268 	if (!preen)
269 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
270 	if (rerun)
271 		printf("\n***** PLEASE RERUN FSCK *****\n");
272 	if (hotroot()) {
273 		struct statfs stfs_buf;
274 		/*
275 		 * We modified the root.  Do a mount update on
276 		 * it, unless it is read-write, so we can continue.
277 		 */
278 		if (statfs("/", &stfs_buf) == 0) {
279 			long flags = stfs_buf.f_flags;
280 			struct ufs_args args;
281 			int ret;
282 
283 			if (flags & MNT_RDONLY) {
284 				args.fspec = 0;
285 				args.export.ex_flags = 0;
286 				args.export.ex_root = 0;
287 				flags |= MNT_UPDATE | MNT_RELOAD;
288 				ret = mount(MOUNT_LFS, "/", flags, &args);
289 				if (ret == 0)
290 					return (0);
291 			}
292 		}
293 		if (!preen)
294 			printf("\n***** REBOOT NOW *****\n");
295 		sync();
296 		return (4);
297 	}
298 	return (0);
299 }
300 
301 static void
302 usage()
303 {
304 
305 	(void) fprintf(stderr,
306 	    "Usage: %s [-dnpy] [-b block] [-m mode] filesystem ...\n",
307 	    getprogname());
308 	exit(1);
309 }
310