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