xref: /openbsd-src/sbin/fsck_ffs/main.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /*	$OpenBSD: main.c,v 1.54 2023/03/08 04:43:06 guenther Exp $	*/
2 /*	$NetBSD: main.c,v 1.22 1996/10/11 20:15:48 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/time.h>
34 #include <sys/signal.h>
35 #include <sys/mount.h>
36 #include <ufs/ufs/dinode.h>
37 #include <ufs/ffs/fs.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <err.h>
44 
45 #include "fsck.h"
46 #include "extern.h"
47 #include "fsutil.h"
48 
49 volatile sig_atomic_t returntosingle;
50 
51 long long argtoi(int, char *, char *, int);
52 int	checkfilesys(char *, char *, long, int);
53 int	main(int, char *[]);
54 
55 extern char *__progname;
56 
57 struct inostatlist *inostathead;
58 
59 struct bufarea bufhead;		/* head of list of other blks in filesys */
60 struct bufarea sblk;		/* file system superblock */
61 struct bufarea asblk;		/* alternate file system superblock */
62 struct bufarea *pdirbp;		/* current directory contents */
63 struct bufarea *pbp;		/* current inode block */
64 
65 struct dups *duplist;		/* head of dup list */
66 struct dups *muldup;		/* end of unique duplicate dup block numbers */
67 
68 struct zlncnt *zlnhead;		/* head of zero link count list */
69 
70 struct inoinfo **inphead, **inpsort;
71 
72 extern long numdirs, listmax, inplast;
73 
74 long	secsize;		/* actual disk sector size */
75 char	nflag;			/* assume a no response */
76 char	yflag;			/* assume a yes response */
77 daddr_t	bflag;			/* location of alternate super block */
78 int	debug;			/* output debugging info */
79 int	cvtlevel;		/* convert to newer file system format */
80 char    usedsoftdep;            /* just fix soft dependency inconsistencies */
81 int	preen;			/* just fix normal inconsistencies */
82 char    resolved;               /* cleared if unresolved changes => not clean */
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 daddr_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 
95 ino_t	lfdir;			/* lost & found directory inode number */
96 
97 daddr_t	n_blks;			/* number of blocks in use */
98 int64_t	n_files;		/* number of files in use */
99 
100 struct ufs1_dinode ufs1_zino;
101 struct ufs2_dinode ufs2_zino;
102 
103 void
104 usage(void)
105 {
106 	fprintf(stderr, "usage: %s [-fnpy] [-b block#] [-c level] "
107 	    "[-m mode] filesystem\n", __progname);
108 	exit(1);
109 }
110 int
111 main(int argc, char *argv[])
112 {
113 	int ch;
114 	int ret = 0;
115 
116 	checkroot();
117 
118 	sync();
119 	skipclean = 1;
120 	while ((ch = getopt(argc, argv, "dfpnNyYb:c:m:")) != -1) {
121 		switch (ch) {
122 		case 'p':
123 			preen = 1;
124 			break;
125 
126 		case 'b':
127 			skipclean = 0;
128 			bflag = argtoi('b', "number", optarg, 10);
129 			printf("Alternate super block location: %lld\n",
130 			    (long long)bflag);
131 			break;
132 
133 		case 'c':
134 			skipclean = 0;
135 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
136 			if (cvtlevel < 3)
137 				errexit("cannot do level %d conversion\n",
138 				    cvtlevel);
139 			break;
140 
141 		case 'd':
142 			debug = 1;
143 			break;
144 
145 		case 'f':
146 			skipclean = 0;
147 			break;
148 
149 		case 'm':
150 			lfmode = argtoi('m', "mode", optarg, 8);
151 			if (lfmode &~ 07777)
152 				errexit("bad mode to -m: %o\n", lfmode);
153 			printf("** lost+found creation mode %o\n", lfmode);
154 			break;
155 
156 		case 'n':
157 		case 'N':
158 			nflag = 1;
159 			yflag = 0;
160 			break;
161 
162 		case 'y':
163 		case 'Y':
164 			yflag = 1;
165 			nflag = 0;
166 			break;
167 
168 		default:
169 			usage();
170 		}
171 	}
172 	argc -= optind;
173 	argv += optind;
174 
175 	if (argc != 1)
176 		usage();
177 
178 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
179 		(void)signal(SIGINT, catch);
180 	if (preen)
181 		(void)signal(SIGQUIT, catchquit);
182 	catchinfo(0);
183 
184 	(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
185 
186 	if (returntosingle)
187 		ret = 2;
188 
189 	exit(ret);
190 }
191 
192 long long
193 argtoi(int flag, char *req, char *str, int base)
194 {
195 	char *cp;
196 	long long ret;
197 
198 	ret = strtoll(str, &cp, base);
199 	if (cp == str || *cp)
200 		errexit("-%c flag requires a %s\n", flag, req);
201 	return (ret);
202 }
203 
204 /*
205  * Check the specified filesystem.
206  */
207 int
208 checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
209 {
210 	daddr_t n_ffree, n_bfree;
211 	struct dups *dp;
212 	struct zlncnt *zlnp;
213 	int cylno;
214 
215 	if (preen && child)
216 		(void)signal(SIGQUIT, voidquit);
217 	setcdevname(filesys, NULL, preen);
218 	if (debug && preen)
219 		pwarn("starting\n");
220 
221 	switch (setup(filesys, 0)) {
222 	case 0:
223 		if (preen)
224 			pfatal("CAN'T CHECK FILE SYSTEM.");
225 		/* FALLTHROUGH */
226 	case -1:
227 		if (fsreadfd != -1) {
228 			(void)close(fsreadfd);
229 			fsreadfd = -1;
230 		}
231 		if (fswritefd != -1) {
232 			(void)close(fswritefd);
233 			fswritefd = -1;
234 		}
235 		return (0);
236 	}
237 	info_filesys = filesys;
238 
239 	/*
240 	 * Cleared if any questions answered no. Used to decide if
241 	 * the superblock should be marked clean.
242 	 */
243 	resolved = 1;
244 
245 	/*
246 	 * 1: scan inodes tallying blocks used
247 	 */
248 	if (preen == 0) {
249 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
250 		if (hotroot())
251 			printf("** Root file system\n");
252 		printf("** Phase 1 - Check Blocks and Sizes\n");
253 	}
254 	pass1();
255 
256 	/*
257 	 * 1b: locate first references to duplicates, if any
258 	 */
259 	if (duplist) {
260 		if (preen || usedsoftdep)
261 			pfatal("INTERNAL ERROR: dups with -p");
262 		printf("** Phase 1b - Rescan For More DUPS\n");
263 		pass1b();
264 	}
265 
266 	/*
267 	 * 2: traverse directories from root to mark all connected directories
268 	 */
269 	if (preen == 0)
270 		printf("** Phase 2 - Check Pathnames\n");
271 	pass2();
272 
273 	/*
274 	 * 3: scan inodes looking for disconnected directories
275 	 */
276 	if (preen == 0)
277 		printf("** Phase 3 - Check Connectivity\n");
278 	pass3();
279 
280 	/*
281 	 * 4: scan inodes looking for disconnected files; check reference counts
282 	 */
283 	if (preen == 0)
284 		printf("** Phase 4 - Check Reference Counts\n");
285 	pass4();
286 
287 	/*
288 	 * 5: check and repair resource counts in cylinder groups
289 	 */
290 	if (preen == 0)
291 		printf("** Phase 5 - Check Cyl groups\n");
292 	pass5();
293 
294 	/*
295 	 * print out summary statistics
296 	 */
297 	n_ffree = sblock.fs_cstotal.cs_nffree;
298 	n_bfree = sblock.fs_cstotal.cs_nbfree;
299 	pwarn("%lld files, %lld used, %lld free ",
300 	    n_files, (long long)n_blks,
301 	    (long long)(n_ffree + sblock.fs_frag * n_bfree));
302 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
303 	    (long long)n_ffree, (long long)n_bfree,
304 	    (long long)((n_ffree * 100) / sblock.fs_dsize),
305 	    (long long)(((n_ffree * 1000 + sblock.fs_dsize / 2) /
306 	    sblock.fs_dsize) % 10));
307 	if (debug &&
308 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
309 		printf("%lld files missing\n", n_files);
310 	if (debug) {
311 		n_blks += sblock.fs_ncg *
312 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
313 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
314 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
315 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
316 			printf("%lld blocks missing\n", (long long)n_blks);
317 		if (duplist != NULL) {
318 			printf("The following duplicate blocks remain:");
319 			for (dp = duplist; dp; dp = dp->next)
320 				printf(" %lld,", (long long)dp->dup);
321 			printf("\n");
322 		}
323 		if (zlnhead != NULL) {
324 			printf("The following zero link count inodes remain:");
325 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
326 				printf(" %llu,",
327 				    (unsigned long long)zlnp->zlncnt);
328 			printf("\n");
329 		}
330 	}
331 	zlnhead = NULL;
332 	duplist = NULL;
333 	muldup = NULL;
334 	inocleanup();
335 	if (fsmodified) {
336 		sblock.fs_time = (time_t)time(NULL);
337 		sbdirty();
338 	}
339 	if (cvtlevel && sblk.b_dirty) {
340 		/*
341 		 * Write out the duplicate super blocks
342 		 */
343 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
344 			bwrite(fswritefd, (char *)&sblock,
345 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
346 	}
347 	if (rerun)
348 		resolved = 0;
349 	ckfini(resolved); /* Don't mark fs clean if fsck needs to be re-run */
350 
351 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
352 		free(inostathead[cylno].il_stat);
353 	free(inostathead);
354 	inostathead = NULL;
355 
356 	free(blockmap);
357 	blockmap = NULL;
358 	free(sblock.fs_csp);
359 	free(sblk.b_un.b_buf);
360 	free(asblk.b_un.b_buf);
361 
362 	if (!fsmodified)
363 		return (0);
364 	if (!preen)
365 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
366 	if (rerun || !resolved)
367 		printf("\n***** PLEASE RERUN FSCK *****\n");
368 	if (hotroot()) {
369 		struct statfs stfs_buf;
370 		/*
371 		 * We modified the root.  Do a mount update on
372 		 * it, unless it is read-write, so we can continue.
373 		 */
374 		if (statfs("/", &stfs_buf) == 0) {
375 			long flags = stfs_buf.f_flags;
376 			struct ufs_args args;
377 			int ret;
378 
379 			if (flags & MNT_RDONLY) {
380 				args.fspec = 0;
381 				args.export_info.ex_flags = 0;
382 				args.export_info.ex_root = 0;
383 				flags |= MNT_UPDATE | MNT_RELOAD;
384 				ret = mount(MOUNT_FFS, "/", flags, &args);
385 				if (ret == 0)
386 					return(0);
387 			}
388 		}
389 		if (!preen)
390 			printf("\n***** REBOOT NOW *****\n");
391 		sync();
392 		return (4);
393 	}
394 	return (0);
395 }
396