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