xref: /openbsd-src/sbin/fsck_ext2fs/main.c (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 /*	$OpenBSD: main.c,v 1.29 2021/01/27 05:03:23 deraadt Exp $	*/
2 /*	$NetBSD: main.c,v 1.1 1997/06/11 11:21:50 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1980, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/signal.h>
36 #include <sys/time.h>
37 #include <sys/mount.h>
38 #include <ufs/ext2fs/ext2fs_dinode.h>
39 #include <ufs/ext2fs/ext2fs.h>
40 #include <fstab.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <err.h>
48 
49 #include "fsck.h"
50 #include "extern.h"
51 #include "fsutil.h"
52 
53 volatile sig_atomic_t	returntosingle;
54 
55 int	main(int, char *[]);
56 
57 static int	argtoi(int, char *, char *, int);
58 static int	checkfilesys(char *, char *, long, int);
59 static  void usage(void);
60 
61 struct bufarea bufhead;		/* head of list of other blks in filesys */
62 struct bufarea sblk;		/* file system superblock */
63 struct bufarea asblk;		/* first alternate superblock */
64 struct bufarea *pdirbp;		/* current directory contents */
65 struct bufarea *pbp;		/* current inode block */
66 struct bufarea *getdatablk(daddr32_t, long);
67 struct m_ext2fs sblock;
68 
69 struct dups *duplist;		/* head of dup list */
70 struct dups *muldup;		/* end of unique duplicate dup block numbers */
71 
72 struct zlncnt *zlnhead;		/* head of zero link count list */
73 
74 struct inoinfo **inphead, **inpsort;
75 long numdirs, listmax, inplast;
76 
77 long	secsize;		/* actual disk sector size */
78 char	nflag;			/* assume a no response */
79 char	yflag;			/* assume a yes response */
80 int	bflag;			/* location of alternate super block */
81 int	debug;			/* output debugging info */
82 int	preen;			/* just fix normal inconsistencies */
83 char	havesb;			/* superblock has been read */
84 char	skipclean;		/* skip clean file systems if preening */
85 int	fsmodified;		/* 1 => write done to file system */
86 int	fsreadfd;		/* file descriptor for reading file system */
87 int	fswritefd;		/* file descriptor for writing file system */
88 int	rerun;			/* rerun fsck.  Only used in non-preen mode */
89 
90 daddr32_t	maxfsblock;		/* number of blocks in the file system */
91 char	*blockmap;		/* ptr to primary blk allocation map */
92 ino_t	maxino;			/* number of inodes in file system */
93 ino_t	lastino;		/* last inode in use */
94 char	*statemap;		/* ptr to inode state table */
95 u_char	*typemap;		/* ptr to inode type table */
96 int16_t	*lncntp;		/* ptr to link count table */
97 
98 ino_t	lfdir;			/* lost & found directory inode number */
99 
100 daddr32_t	n_blks;			/* number of blocks in use */
101 daddr32_t	n_files;		/* number of files in use */
102 
103 struct	ext2fs_dinode zino;
104 
105 int
106 main(int argc, char *argv[])
107 {
108 	int ch;
109 	int ret = 0;
110 
111 	checkroot();
112 
113 	sync();
114 	skipclean = 1;
115 	while ((ch = getopt(argc, argv, "b:dfm:npy")) != -1) {
116 		switch (ch) {
117 		case 'b':
118 			skipclean = 0;
119 			bflag = argtoi('b', "number", optarg, 10);
120 			printf("Alternate super block location: %d\n", bflag);
121 			break;
122 
123 		case 'd':
124 			debug = 1;
125 			break;
126 
127 		case 'f':
128 			skipclean = 0;
129 			break;
130 
131 		case 'm':
132 			lfmode = argtoi('m', "mode", optarg, 8);
133 			if (lfmode &~ 07777)
134 				errexit("bad mode to -m: %o\n", lfmode);
135 			printf("** lost+found creation mode %o\n", lfmode);
136 			break;
137 
138 		case 'n':
139 			nflag = 1;
140 			yflag = 0;
141 			break;
142 
143 		case 'p':
144 			preen = 1;
145 			break;
146 
147 		case 'y':
148 			yflag = 1;
149 			nflag = 0;
150 			break;
151 
152 		default:
153 			usage();
154 		}
155 	}
156 
157 	argc -= optind;
158 	argv += optind;
159 
160 	if (argc != 1)
161 		usage();
162 
163 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
164 		(void)signal(SIGINT, catch);
165 	if (preen)
166 		(void)signal(SIGQUIT, catchquit);
167 
168 	(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
169 
170 	if (returntosingle)
171 		ret = 2;
172 
173 	exit(ret);
174 }
175 
176 static int
177 argtoi(int flag, char *req, char *str, int base)
178 {
179 	char *cp;
180 	int ret;
181 
182 	ret = (int)strtol(str, &cp, base);
183 	if (cp == str || *cp)
184 		errexit("-%c flag requires a %s\n", flag, req);
185 	return (ret);
186 }
187 
188 /*
189  * Check the specified filesystem.
190  */
191 /* ARGSUSED */
192 static int
193 checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
194 {
195 	daddr32_t n_bfree;
196 	struct dups *dp;
197 	struct zlncnt *zlnp;
198 	int i;
199 
200 	if (preen && child)
201 		(void)signal(SIGQUIT, voidquit);
202 	setcdevname(filesys, NULL, preen);
203 	if (debug && preen)
204 		pwarn("starting\n");
205 
206 	switch (setup(filesys)) {
207 	case 0:
208 		if (preen)
209 			pfatal("CAN'T CHECK FILE SYSTEM.");
210 	case -1:
211 		return (0);
212 	}
213 	/*
214 	 * 1: scan inodes tallying blocks used
215 	 */
216 	if (preen == 0) {
217 		if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
218 			printf("** Last Mounted on %s\n",
219 			    sblock.e2fs.e2fs_fsmnt);
220 		}
221 		if (hotroot())
222 			printf("** Root file system\n");
223 		printf("** Phase 1 - Check Blocks and Sizes\n");
224 	}
225 	pass1();
226 
227 	/*
228 	 * 1b: locate first references to duplicates, if any
229 	 */
230 	if (duplist) {
231 		if (preen)
232 			pfatal("INTERNAL ERROR: dups with -p");
233 		printf("** Phase 1b - Rescan For More DUPS\n");
234 		pass1b();
235 	}
236 
237 	/*
238 	 * 2: traverse directories from root to mark all connected directories
239 	 */
240 	if (preen == 0)
241 		printf("** Phase 2 - Check Pathnames\n");
242 	pass2();
243 
244 	/*
245 	 * 3: scan inodes looking for disconnected directories
246 	 */
247 	if (preen == 0)
248 		printf("** Phase 3 - Check Connectivity\n");
249 	pass3();
250 
251 	/*
252 	 * 4: scan inodes looking for disconnected files; check reference counts
253 	 */
254 	if (preen == 0)
255 		printf("** Phase 4 - Check Reference Counts\n");
256 	pass4();
257 
258 	/*
259 	 * 5: check and repair resource counts in cylinder groups
260 	 */
261 	if (preen == 0)
262 		printf("** Phase 5 - Check Cyl groups\n");
263 	pass5();
264 
265 	/*
266 	 * print out summary statistics
267 	 */
268 	n_bfree = sblock.e2fs.e2fs_fbcount;
269 
270 	pwarn("%d files, %d used, %d free\n",
271 	    n_files, n_blks, n_bfree);
272 	if (debug &&
273 		/* 9 reserved and unused inodes in FS */
274 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
275 		printf("%d files missing\n", n_files);
276 	if (debug) {
277 		for (i = 0; i < sblock.e2fs_ncg; i++)
278 			n_blks +=  cgoverhead(i);
279 		n_blks += sblock.e2fs.e2fs_first_dblock;
280 		if (n_blks -= maxfsblock - n_bfree)
281 			printf("%d blocks missing\n", n_blks);
282 		if (duplist != NULL) {
283 			printf("The following duplicate blocks remain:");
284 			for (dp = duplist; dp; dp = dp->next)
285 				printf(" %d,", dp->dup);
286 			printf("\n");
287 		}
288 		if (zlnhead != NULL) {
289 			printf("The following zero link count inodes remain:");
290 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
291 				printf(" %llu,",
292 				    (unsigned long long)zlnp->zlncnt);
293 			printf("\n");
294 		}
295 	}
296 	zlnhead = NULL;
297 	duplist = NULL;
298 	muldup = NULL;
299 	inocleanup();
300 	if (fsmodified) {
301 		time_t t;
302 		(void)time(&t);
303 		sblock.e2fs.e2fs_wtime = t;
304 		sblock.e2fs.e2fs_lastfsck = t;
305 		sbdirty();
306 	}
307 	ckfini(1);
308 	free(blockmap);
309 	free(statemap);
310 	free((char *)lncntp);
311 	if (!fsmodified)
312 		return (0);
313 	if (!preen)
314 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
315 	if (rerun)
316 		printf("\n***** PLEASE RERUN FSCK *****\n");
317 	if (hotroot()) {
318 		struct statfs stfs_buf;
319 		/*
320 		 * We modified the root.  Do a mount update on
321 		 * it, unless it is read-write, so we can continue.
322 		 */
323 		if (statfs("/", &stfs_buf) == 0) {
324 			long flags = stfs_buf.f_flags;
325 			struct ufs_args args;
326 			int ret;
327 
328 			if (flags & MNT_RDONLY) {
329 				args.fspec = 0;
330 				args.export_info.ex_flags = 0;
331 				args.export_info.ex_root = 0;
332 				flags |= MNT_UPDATE | MNT_RELOAD;
333 				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
334 				if (ret == 0)
335 					return(0);
336 			}
337 		}
338 		if (!preen)
339 			printf("\n***** REBOOT NOW *****\n");
340 		sync();
341 		return (4);
342 	}
343 	return (0);
344 }
345 
346 static void
347 usage(void)
348 {
349 	extern char *__progname;
350 
351 	(void) fprintf(stderr,
352 	    "usage: %s [-dfnpy] [-b block#] [-m mode] filesystem\n",
353 	    __progname);
354 	exit(1);
355 }
356