xref: /netbsd-src/sbin/fsck_lfs/main.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /* $NetBSD: main.c,v 1.5 2000/05/23 01:48:53 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 #include <fstab.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <err.h>
50 
51 #include "fsck.h"
52 #include "extern.h"
53 #include "fsutil.h"
54 
55 int             returntosingle;
56 int             fake_cleanseg = -1;
57 
58 int             main(int, char *[]);
59 
60 static int      argtoi(int, char *, char *, int);
61 static int      checkfilesys(const char *, char *, long, int);
62 #if 0
63 static int      docheck(struct fstab *);
64 #endif
65 static void     usage(void);
66 
67 int
68 main(int argc, char **argv)
69 {
70 	int             ch;
71 	int             ret = 0;
72 	char           *optstring = "b:C:dm:npy";
73 
74 	sync();
75 	skipclean = 1;
76 	exitonfail = 0;
77 	while ((ch = getopt(argc, argv, optstring)) != EOF) {
78 		switch (ch) {
79 		case 'b':
80 			skipclean = 0;
81 			bflag = argtoi('b', "number", optarg, 10);
82 			printf("Alternate super block location: %d\n", bflag);
83 			break;
84 		case 'C':
85 			fake_cleanseg = atoi(optarg);
86 			break;
87 		case 'd':
88 			debug++;
89 			break;
90 		case 'e':
91 			exitonfail++;
92 			break;
93 		case 'm':
94 			lfmode = argtoi('m', "mode", optarg, 8);
95 			if (lfmode & ~07777)
96 				errexit("bad mode to -m: %o\n", lfmode);
97 			printf("** lost+found creation mode %o\n", lfmode);
98 			break;
99 
100 		case 'n':
101 			nflag++;
102 			yflag = 0;
103 			break;
104 
105 		case 'p':
106 			preen++;
107 			break;
108 
109 		case 'y':
110 			yflag++;
111 			nflag = 0;
112 			break;
113 
114 		default:
115 			usage();
116 		}
117 	}
118 #if 0
119 	if (nflag == 0) {
120 		errexit("fsck_lfs cannot write to the filesystem yet; the -n flag is required.\n");
121 	}
122 #endif
123 
124 	argc -= optind;
125 	argv += optind;
126 
127 	if (!argc)
128 		usage();
129 
130 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
131 		(void)signal(SIGINT, catch);
132 	if (preen)
133 		(void)signal(SIGQUIT, catchquit);
134 
135 	while (argc-- > 0)
136 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
137 
138 	if (returntosingle)
139 		ret = 2;
140 
141 	exit(ret);
142 }
143 
144 static int
145 argtoi(int flag, char *req, char *str, int base)
146 {
147 	char           *cp;
148 	int             ret;
149 
150 	ret = (int)strtol(str, &cp, base);
151 	if (cp == str || *cp)
152 		errexit("-%c flag requires a %s\n", flag, req);
153 	return (ret);
154 }
155 
156 #if 0
157 /*
158  * Determine whether a filesystem should be checked.
159  */
160 static int
161 docheck(struct fstab * fsp)
162 {
163 
164 	if ((strcmp(fsp->fs_vfstype, "ufs") &&
165 	     strcmp(fsp->fs_vfstype, "ffs")) ||
166 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
167 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
168 	    fsp->fs_passno == 0)
169 		return (0);
170 	return (1);
171 }
172 #endif
173 
174 /*
175  * Check the specified filesystem.
176  */
177 /* ARGSUSED */
178 static int
179 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
180 {
181 	daddr_t         n_ffree = 0, n_bfree = 0;
182 	struct dups    *dp;
183 	struct zlncnt  *zlnp;
184 
185 	if (preen && child)
186 		(void)signal(SIGQUIT, voidquit);
187 	setcdevname(filesys, preen);
188 	if (debug && preen)
189 		pwarn("starting\n");
190 	switch (setup(filesys)) {
191 	case 0:
192 		if (preen)
193 			pfatal("CAN'T CHECK FILE SYSTEM.");
194 	case -1:
195 		return (0);
196 	}
197 
198 	if (preen == 0) {
199 		printf("** Last Mounted on %s\n", sblock.lfs_fsmnt);
200 		if (hotroot())
201 			printf("** Root file system\n");
202 	}
203 	/*
204          * 0: check segment checksums, inode ranges
205          */
206 	if (preen == 0)
207 		printf("** Phase 0 - Check Segment Summaries and Inode Free List\n");
208 	pass0();
209 
210 	if (preen == 0) {
211 		/*
212 		 * 1: scan inodes tallying blocks used
213 		 */
214 		if (preen == 0)
215 			printf("** Phase 1 - Check Blocks and Sizes\n");
216 		pass1();
217 
218 		/*
219 		 * 2: traverse directories from root to mark all connected directories
220 		 */
221 		if (preen == 0)
222 			printf("** Phase 2 - Check Pathnames\n");
223 		pass2();
224 
225 		/*
226 		 * 3: scan inodes looking for disconnected directories
227 		 */
228 		if (preen == 0)
229 			printf("** Phase 3 - Check Connectivity\n");
230 		pass3();
231 
232 		/*
233 		 * 4: scan inodes looking for disconnected files; check reference counts
234 		 */
235 		if (preen == 0)
236 			printf("** Phase 4 - Check Reference Counts\n");
237 		pass4();
238 
239 
240 		/*
241 		 * 5: check segment byte totals and dirty flags
242 		 */
243 		if (preen == 0)
244 			printf("** Phase 5 - Check Segment Block Accounting\n");
245 		pass5();
246 
247 		/*
248 		 * print out summary statistics
249 		 */
250 		pwarn("%d files, %d used, %d free ",
251 		      n_files, n_blks, n_ffree + sblock.lfs_frag * n_bfree);
252 		putchar('\n');
253 	}
254 	if (debug) {
255 		if (duplist != NULL) {
256 			printf("The following duplicate blocks remain:");
257 			for (dp = duplist; dp; dp = dp->next)
258 				printf(" %d,", dp->dup);
259 			printf("\n");
260 		}
261 		if (zlnhead != NULL) {
262 			printf("The following zero link count inodes remain:");
263 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
264 				printf(" %u,", zlnp->zlncnt);
265 			printf("\n");
266 		}
267 	}
268 	zlnhead = (struct zlncnt *)0;
269 	duplist = (struct dups *)0;
270 	muldup = (struct dups *)0;
271 	inocleanup();
272 
273 	ckfini(1);
274 	free(blockmap);
275 	free(statemap);
276 	free((char *)lncntp);
277 	if (!fsmodified)
278 		return (0);
279 	if (!preen)
280 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
281 	if (rerun)
282 		printf("\n***** PLEASE RERUN FSCK *****\n");
283 	if (hotroot()) {
284 		struct statfs   stfs_buf;
285 		/*
286 		 * We modified the root.  Do a mount update on
287 		 * it, unless it is read-write, so we can continue.
288 		 */
289 		if (statfs("/", &stfs_buf) == 0) {
290 			long            flags = stfs_buf.f_flags;
291 			struct ufs_args args;
292 			int             ret;
293 
294 			if (flags & MNT_RDONLY) {
295 				args.fspec = 0;
296 				args.export.ex_flags = 0;
297 				args.export.ex_root = 0;
298 				flags |= MNT_UPDATE | MNT_RELOAD;
299 				ret = mount(MOUNT_LFS, "/", flags, &args);
300 				if (ret == 0)
301 					return (0);
302 			}
303 		}
304 		if (!preen)
305 			printf("\n***** REBOOT NOW *****\n");
306 		sync();
307 		return (4);
308 	}
309 	return (0);
310 }
311 
312 static void
313 usage()
314 {
315 	extern char    *__progname;
316 
317 	(void)fprintf(stderr,
318 		  "Usage: %s [-dnpy] [-b block] [-m mode] filesystem ...\n",
319 		       __progname);
320 	exit(1);
321 }
322