xref: /dragonfly/sbin/fsck/main.c (revision 984263bc)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. 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 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1986, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/sbin/fsck/main.c,v 1.21.2.1 2001/01/23 23:11:07 iedowse Exp $";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <sys/mount.h>
52 #include <sys/resource.h>
53 
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ffs/fs.h>
57 
58 #include <err.h>
59 #include <errno.h>
60 #include <fstab.h>
61 #include <paths.h>
62 
63 #include "fsck.h"
64 
65 static int argtoi __P((int flag, char *req, char *str, int base));
66 static int docheck __P((struct fstab *fsp));
67 static int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
68 		int child));
69 static struct statfs *getmntpt __P((const char *));
70 int main __P((int argc, char *argv[]));
71 
72 int
73 main(argc, argv)
74 	int	argc;
75 	char	*argv[];
76 {
77 	int ch;
78 	int ret, maxrun = 0;
79 	struct rlimit rlimit;
80 
81 	sync();
82 	while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) {
83 		switch (ch) {
84 		case 'p':
85 			preen++;
86 			break;
87 
88 		case 'b':
89 			bflag = argtoi('b', "number", optarg, 10);
90 			printf("Alternate super block location: %d\n", bflag);
91 			break;
92 
93 		case 'c':
94 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
95 			break;
96 
97 		case 'd':
98 			debug++;
99 			break;
100 
101 		case 'f':
102 			fflag++;
103 			break;
104 
105 		case 'l':
106 			maxrun = argtoi('l', "number", optarg, 10);
107 			break;
108 
109 		case 'm':
110 			lfmode = argtoi('m', "mode", optarg, 8);
111 			if (lfmode &~ 07777)
112 				errx(EEXIT, "bad mode to -m: %o", lfmode);
113 			printf("** lost+found creation mode %o\n", lfmode);
114 			break;
115 
116 		case 'n':
117 		case 'N':
118 			nflag++;
119 			yflag = 0;
120 			break;
121 
122 		case 'y':
123 		case 'Y':
124 			yflag++;
125 			nflag = 0;
126 			break;
127 
128 		default:
129 			errx(EEXIT, "%c option?", ch);
130 		}
131 	}
132 	argc -= optind;
133 	argv += optind;
134 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
135 		(void)signal(SIGINT, catch);
136 	if (preen)
137 		(void)signal(SIGQUIT, catchquit);
138 	signal(SIGINFO, infohandler);
139 	/*
140 	 * Push up our allowed memory limit so we can cope
141 	 * with huge filesystems.
142 	 */
143 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
144 		rlimit.rlim_cur = rlimit.rlim_max;
145 		(void)setrlimit(RLIMIT_DATA, &rlimit);
146 	}
147 	if (argc) {
148 		while (argc-- > 0) {
149 			char *path = blockcheck(*argv);
150 
151 			if (path == NULL)
152 				pfatal("Can't check %s\n", *argv);
153 			else
154 				(void)checkfilesys(path, 0, 0L, 0);
155 			++argv;
156 		}
157 		exit(0);
158 	}
159 	ret = checkfstab(preen, maxrun, docheck, checkfilesys);
160 	if (returntosingle)
161 		exit(2);
162 	exit(ret);
163 }
164 
165 static int
166 argtoi(flag, req, str, base)
167 	int flag;
168 	char *req, *str;
169 	int base;
170 {
171 	char *cp;
172 	int ret;
173 
174 	ret = (int)strtol(str, &cp, base);
175 	if (cp == str || *cp)
176 		errx(EEXIT, "-%c flag requires a %s", flag, req);
177 	return (ret);
178 }
179 
180 /*
181  * Determine whether a filesystem should be checked.
182  */
183 static int
184 docheck(fsp)
185 	register struct fstab *fsp;
186 {
187 
188 	if (strcmp(fsp->fs_vfstype, "ufs") ||
189 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
190 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
191 	    fsp->fs_passno == 0)
192 		return (0);
193 	return (1);
194 }
195 
196 /*
197  * Check the specified filesystem.
198  */
199 /* ARGSUSED */
200 static int
201 checkfilesys(filesys, mntpt, auxdata, child)
202 	char *filesys, *mntpt;
203 	long auxdata;
204 	int child;
205 {
206 	ufs_daddr_t n_ffree, n_bfree;
207 	struct dups *dp;
208 	struct statfs *mntbuf;
209 	struct zlncnt *zlnp;
210 	int cylno;
211 
212 	if (preen && child)
213 		(void)signal(SIGQUIT, voidquit);
214 	cdevname = filesys;
215 	if (debug && preen)
216 		pwarn("starting\n");
217 	switch (setup(filesys)) {
218 	case 0:
219 		if (preen)
220 			pfatal("CAN'T CHECK FILE SYSTEM.");
221 		return (0);
222 	case -1:
223 		pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
224 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
225 		printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
226 		    sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
227 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
228 		return (0);
229 	}
230 	got_siginfo = 0;
231 
232 	/*
233 	 * Get the mount point information of the filesystem, if
234 	 * it is available.
235 	 */
236 	mntbuf = getmntpt(filesys);
237 
238 	/*
239 	 * Cleared if any questions answered no. Used to decide if
240 	 * the superblock should be marked clean.
241 	 */
242 	resolved = 1;
243 	/*
244 	 * 1: scan inodes tallying blocks used
245 	 */
246 	if (preen == 0) {
247 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
248 		if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
249 			printf("** Root file system\n");
250 		printf("** Phase 1 - Check Blocks and Sizes\n");
251 	}
252 	pass1();
253 
254 	/*
255 	 * 1b: locate first references to duplicates, if any
256 	 */
257 	if (duplist) {
258 		if (preen || usedsoftdep)
259 			pfatal("INTERNAL ERROR: dups with -p");
260 		printf("** Phase 1b - Rescan For More DUPS\n");
261 		pass1b();
262 	}
263 
264 	/*
265 	 * 2: traverse directories from root to mark all connected directories
266 	 */
267 	if (preen == 0)
268 		printf("** Phase 2 - Check Pathnames\n");
269 	pass2();
270 
271 	/*
272 	 * 3: scan inodes looking for disconnected directories
273 	 */
274 	if (preen == 0)
275 		printf("** Phase 3 - Check Connectivity\n");
276 	pass3();
277 
278 	/*
279 	 * 4: scan inodes looking for disconnected files; check reference counts
280 	 */
281 	if (preen == 0)
282 		printf("** Phase 4 - Check Reference Counts\n");
283 	pass4();
284 
285 	/*
286 	 * 5: check and repair resource counts in cylinder groups
287 	 */
288 	if (preen == 0)
289 		printf("** Phase 5 - Check Cyl groups\n");
290 	pass5();
291 
292 	/*
293 	 * print out summary statistics
294 	 */
295 	n_ffree = sblock.fs_cstotal.cs_nffree;
296 	n_bfree = sblock.fs_cstotal.cs_nbfree;
297 	pwarn("%ld files, %ld used, %ld free ",
298 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
299 	printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
300 	    n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
301 	if (debug &&
302 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
303 		printf("%d files missing\n", n_files);
304 	if (debug) {
305 		n_blks += sblock.fs_ncg *
306 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
307 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
308 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
309 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
310 			printf("%d blocks missing\n", n_blks);
311 		if (duplist != NULL) {
312 			printf("The following duplicate blocks remain:");
313 			for (dp = duplist; dp; dp = dp->next)
314 				printf(" %d,", dp->dup);
315 			printf("\n");
316 		}
317 		if (zlnhead != NULL) {
318 			printf("The following zero link count inodes remain:");
319 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
320 				printf(" %u,", zlnp->zlncnt);
321 			printf("\n");
322 		}
323 	}
324 	zlnhead = (struct zlncnt *)0;
325 	duplist = (struct dups *)0;
326 	muldup = (struct dups *)0;
327 	inocleanup();
328 	if (fsmodified) {
329 		sblock.fs_time = time(NULL);
330 		sbdirty();
331 	}
332 	if (cvtlevel && sblk.b_dirty) {
333 		/*
334 		 * Write out the duplicate super blocks
335 		 */
336 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
337 			bwrite(fswritefd, (char *)&sblock,
338 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
339 	}
340 	if (rerun)
341 		resolved = 0;
342 
343 	/*
344 	 * Check to see if the filesystem is mounted read-write.
345 	 */
346 	if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
347 		resolved = 0;
348 	ckfini(resolved);
349 
350 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
351 		if (inostathead[cylno].il_stat != NULL)
352 			free((char *)inostathead[cylno].il_stat);
353 	free((char *)inostathead);
354 	inostathead = NULL;
355 	if (fsmodified && !preen)
356 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
357 	if (rerun)
358 		printf("\n***** PLEASE RERUN FSCK *****\n");
359 	if (mntbuf != NULL) {
360 		struct ufs_args args;
361 		int ret;
362 		/*
363 		 * We modified a mounted filesystem.  Do a mount update on
364 		 * it unless it is read-write, so we can continue using it
365 		 * as safely as possible.
366 		 */
367 		if (mntbuf->f_flags & MNT_RDONLY) {
368 			args.fspec = 0;
369 			args.export.ex_flags = 0;
370 			args.export.ex_root = 0;
371 			ret = mount("ufs", mntbuf->f_mntonname,
372 			    mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
373 			if (ret == 0)
374 				return (0);
375 			pwarn("mount reload of '%s' failed: %s\n\n",
376 			    mntbuf->f_mntonname, strerror(errno));
377 		}
378 		if (!fsmodified)
379 			return (0);
380 		if (!preen)
381 			printf("\n***** REBOOT NOW *****\n");
382 		sync();
383 		return (4);
384 	}
385 	return (0);
386 }
387 
388 /*
389  * Get the directory that the device is mounted on.
390  */
391 static struct statfs *
392 getmntpt(name)
393 	const char *name;
394 {
395 	struct stat devstat, mntdevstat;
396 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
397 	char *devname;
398 	struct statfs *mntbuf;
399 	int i, mntsize;
400 
401 	if (stat(name, &devstat) != 0 ||
402 	    !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
403 		return (NULL);
404 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
405 	for (i = 0; i < mntsize; i++) {
406 		if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0)
407 			continue;
408 		devname = mntbuf[i].f_mntfromname;
409 		if (*devname != '/') {
410 			strcpy(device, _PATH_DEV);
411 			strcat(device, devname);
412 			devname = device;
413 		}
414 		if (stat(devname, &mntdevstat) == 0 &&
415 		    mntdevstat.st_rdev == devstat.st_rdev)
416 			return (&mntbuf[i]);
417 	}
418 	return (NULL);
419 }
420