GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/fsck_ffs/main.c Lines: 74 170 43.5 %
Date: 2017-11-07 Branches: 26 106 24.5 %

Line Branch Exec Source
1
/*	$OpenBSD: main.c,v 1.50 2016/09/09 15:37:15 tb 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
int	argtoi(int, char *, char *, int);
52
int	checkfilesys(char *, char *, long, int);
53
int	main(int, char *[]);
54
55
extern char *__progname;
56
57
void
58
usage(void)
59
{
60
	fprintf(stderr, "usage: %s [-fnpy] [-b block#] [-c level] "
61
	    "[-m mode] filesystem\n", __progname);
62
	exit(1);
63
}
64
int
65
main(int argc, char *argv[])
66
{
67
	int ch;
68
	int ret = 0;
69
70
16
	sync();
71
8
	skipclean = 1;
72
32
	while ((ch = getopt(argc, argv, "dfpnNyYb:c:m:")) != -1) {
73


16
		switch (ch) {
74
		case 'p':
75
			preen = 1;
76
			break;
77
78
		case 'b':
79
			skipclean = 0;
80
			bflag = argtoi('b', "number", optarg, 10);
81
			printf("Alternate super block location: %d\n", bflag);
82
			break;
83
84
		case 'c':
85
			skipclean = 0;
86
			cvtlevel = argtoi('c', "conversion level", optarg, 10);
87
			if (cvtlevel < 3)
88
				errexit("cannot do level %d conversion\n",
89
				    cvtlevel);
90
			break;
91
92
		case 'd':
93
			debug = 1;
94
			break;
95
96
		case 'f':
97
8
			skipclean = 0;
98
8
			break;
99
100
		case 'm':
101
			lfmode = argtoi('m', "mode", optarg, 8);
102
			if (lfmode &~ 07777)
103
				errexit("bad mode to -m: %o\n", lfmode);
104
			printf("** lost+found creation mode %o\n", lfmode);
105
			break;
106
107
		case 'n':
108
		case 'N':
109
8
			nflag = 1;
110
8
			yflag = 0;
111
8
			break;
112
113
		case 'y':
114
		case 'Y':
115
			yflag = 1;
116
			nflag = 0;
117
			break;
118
119
		default:
120
			usage();
121
		}
122
	}
123
8
	argc -= optind;
124
8
	argv += optind;
125
126
8
	if (argc != 1)
127
		usage();
128
129
8
	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
130
8
		(void)signal(SIGINT, catch);
131
8
	if (preen)
132
		(void)signal(SIGQUIT, catchquit);
133
8
	catchinfo(0);
134
135
8
	(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
136
137
8
	if (returntosingle)
138
		ret = 2;
139
140
	exit(ret);
141
}
142
143
int
144
argtoi(int flag, char *req, char *str, int base)
145
{
146
	char *cp;
147
	int ret;
148
149
	ret = (int)strtol(str, &cp, base);
150
	if (cp == str || *cp)
151
		errexit("-%c flag requires a %s\n", flag, req);
152
	return (ret);
153
}
154
155
/*
156
 * Check the specified filesystem.
157
 */
158
/* ARGSUSED */
159
int
160
checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
161
{
162
	daddr_t n_ffree, n_bfree;
163
	struct dups *dp;
164
	struct zlncnt *zlnp;
165
	int cylno;
166
167
16
	if (preen && child)
168
		(void)signal(SIGQUIT, voidquit);
169
8
	setcdevname(filesys, NULL, preen);
170
8
	if (debug && preen)
171
		pwarn("starting\n");
172
173
8
	switch (setup(filesys, 0)) {
174
	case 0:
175
		if (preen)
176
			pfatal("CAN'T CHECK FILE SYSTEM.");
177
		/* FALLTHROUGH */
178
	case -1:
179
		if (fsreadfd != -1) {
180
			(void)close(fsreadfd);
181
			fsreadfd = -1;
182
		}
183
		if (fswritefd != -1) {
184
			(void)close(fswritefd);
185
			fswritefd = -1;
186
		}
187
		return (0);
188
	}
189
8
	info_filesys = filesys;
190
191
	/*
192
	 * Cleared if any questions answered no. Used to decide if
193
	 * the superblock should be marked clean.
194
	 */
195
8
	resolved = 1;
196
197
	/*
198
	 * 1: scan inodes tallying blocks used
199
	 */
200
8
	if (preen == 0) {
201
8
		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
202
8
		if (hotroot())
203
			printf("** Root file system\n");
204
8
		printf("** Phase 1 - Check Blocks and Sizes\n");
205
8
	}
206
8
	pass1();
207
208
	/*
209
	 * 1b: locate first references to duplicates, if any
210
	 */
211
8
	if (duplist) {
212
		if (preen || usedsoftdep)
213
			pfatal("INTERNAL ERROR: dups with -p");
214
		printf("** Phase 1b - Rescan For More DUPS\n");
215
		pass1b();
216
	}
217
218
	/*
219
	 * 2: traverse directories from root to mark all connected directories
220
	 */
221
8
	if (preen == 0)
222
8
		printf("** Phase 2 - Check Pathnames\n");
223
8
	pass2();
224
225
	/*
226
	 * 3: scan inodes looking for disconnected directories
227
	 */
228
8
	if (preen == 0)
229
8
		printf("** Phase 3 - Check Connectivity\n");
230
8
	pass3();
231
232
	/*
233
	 * 4: scan inodes looking for disconnected files; check reference counts
234
	 */
235
8
	if (preen == 0)
236
8
		printf("** Phase 4 - Check Reference Counts\n");
237
8
	pass4();
238
239
	/*
240
	 * 5: check and repair resource counts in cylinder groups
241
	 */
242
8
	if (preen == 0)
243
8
		printf("** Phase 5 - Check Cyl groups\n");
244
8
	pass5();
245
246
	/*
247
	 * print out summary statistics
248
	 */
249
8
	n_ffree = sblock.fs_cstotal.cs_nffree;
250
8
	n_bfree = sblock.fs_cstotal.cs_nbfree;
251
8
	pwarn("%lld files, %lld used, %lld free ",
252
8
	    n_files, (long long)n_blks,
253
8
	    (long long)(n_ffree + sblock.fs_frag * n_bfree));
254
8
	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
255
	    (long long)n_ffree, (long long)n_bfree,
256
8
	    (long long)((n_ffree * 100) / sblock.fs_dsize),
257
8
	    (long long)(((n_ffree * 1000 + sblock.fs_dsize / 2) /
258
8
	    sblock.fs_dsize) % 10));
259

8
	if (debug &&
260
	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
261
		printf("%lld files missing\n", n_files);
262
8
	if (debug) {
263
		n_blks += sblock.fs_ncg *
264
			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
265
		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
266
		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
267
		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
268
			printf("%lld blocks missing\n", (long long)n_blks);
269
		if (duplist != NULL) {
270
			printf("The following duplicate blocks remain:");
271
			for (dp = duplist; dp; dp = dp->next)
272
				printf(" %lld,", (long long)dp->dup);
273
			printf("\n");
274
		}
275
		if (zlnhead != NULL) {
276
			printf("The following zero link count inodes remain:");
277
			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
278
				printf(" %llu,",
279
				    (unsigned long long)zlnp->zlncnt);
280
			printf("\n");
281
		}
282
	}
283
8
	zlnhead = NULL;
284
8
	duplist = NULL;
285
8
	muldup = NULL;
286
8
	inocleanup();
287
8
	if (fsmodified) {
288
		sblock.fs_time = (time_t)time(NULL);
289
		sbdirty();
290
	}
291

8
	if (cvtlevel && sblk.b_dirty) {
292
		/*
293
		 * Write out the duplicate super blocks
294
		 */
295
		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
296
			bwrite(fswritefd, (char *)&sblock,
297
			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
298
	}
299
8
	if (rerun)
300
		resolved = 0;
301
8
	ckfini(resolved); /* Don't mark fs clean if fsck needs to be re-run */
302
303
40
	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
304
12
		free(inostathead[cylno].il_stat);
305
8
	free(inostathead);
306
8
	inostathead = NULL;
307
308
8
	free(blockmap);
309
8
	blockmap = NULL;
310
8
	free(sblock.fs_csp);
311
8
	free(sblk.b_un.b_buf);
312
8
	free(asblk.b_un.b_buf);
313
314
8
	if (!fsmodified)
315
8
		return (0);
316
	if (!preen)
317
		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
318
	if (rerun || !resolved)
319
		printf("\n***** PLEASE RERUN FSCK *****\n");
320
	if (hotroot()) {
321
		struct statfs stfs_buf;
322
		/*
323
		 * We modified the root.  Do a mount update on
324
		 * it, unless it is read-write, so we can continue.
325
		 */
326
		if (statfs("/", &stfs_buf) == 0) {
327
			long flags = stfs_buf.f_flags;
328
			struct ufs_args args;
329
			int ret;
330
331
			if (flags & MNT_RDONLY) {
332
				args.fspec = 0;
333
				args.export_info.ex_flags = 0;
334
				args.export_info.ex_root = 0;
335
				flags |= MNT_UPDATE | MNT_RELOAD;
336
				ret = mount(MOUNT_FFS, "/", flags, &args);
337
				if (ret == 0)
338
					return(0);
339
			}
340
		}
341
		if (!preen)
342
			printf("\n***** REBOOT NOW *****\n");
343
		sync();
344
		return (4);
345
	}
346
	return (0);
347
8
}