GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/fsck_ext2fs/main.c Lines: 0 140 0.0 %
Date: 2017-11-13 Branches: 0 89 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: main.c,v 1.27 2016/03/16 15:41:10 krw Exp $	*/
2
/*	$NetBSD: main.c,v 1.1 1997/06/11 11:21:50 bouyer Exp $	*/
3
4
/*
5
 * Copyright (c) 1997 Manuel Bouyer.
6
 * Copyright (c) 1980, 1986, 1993
7
 *	The Regents of the University of California.  All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. 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
#include <sys/types.h>
35
#include <sys/signal.h>
36
#include <sys/time.h>
37
#include <sys/mount.h>
38
#include <ufs/ext2fs/ext2fs_dinode.h>
39
#include <ufs/ext2fs/ext2fs.h>
40
#include <fstab.h>
41
#include <stdlib.h>
42
#include <string.h>
43
#include <ctype.h>
44
#include <stdio.h>
45
#include <time.h>
46
#include <unistd.h>
47
#include <err.h>
48
49
#include "fsck.h"
50
#include "extern.h"
51
#include "fsutil.h"
52
53
volatile sig_atomic_t	returntosingle;
54
55
int	main(int, char *[]);
56
57
static int	argtoi(int, char *, char *, int);
58
static int	checkfilesys(char *, char *, long, int);
59
static  void usage(void);
60
61
62
int
63
main(int argc, char *argv[])
64
{
65
	int ch;
66
	int ret = 0;
67
68
	sync();
69
	skipclean = 1;
70
	while ((ch = getopt(argc, argv, "b:dfm:npy")) != -1) {
71
		switch (ch) {
72
		case 'b':
73
			skipclean = 0;
74
			bflag = argtoi('b', "number", optarg, 10);
75
			printf("Alternate super block location: %d\n", bflag);
76
			break;
77
78
		case 'd':
79
			debug = 1;
80
			break;
81
82
		case 'f':
83
			skipclean = 0;
84
			break;
85
86
		case 'm':
87
			lfmode = argtoi('m', "mode", optarg, 8);
88
			if (lfmode &~ 07777)
89
				errexit("bad mode to -m: %o\n", lfmode);
90
			printf("** lost+found creation mode %o\n", lfmode);
91
			break;
92
93
		case 'n':
94
			nflag = 1;
95
			yflag = 0;
96
			break;
97
98
		case 'p':
99
			preen = 1;
100
			break;
101
102
		case 'y':
103
			yflag = 1;
104
			nflag = 0;
105
			break;
106
107
		default:
108
			usage();
109
		}
110
	}
111
112
	argc -= optind;
113
	argv += optind;
114
115
	if (argc != 1)
116
		usage();
117
118
	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
119
		(void)signal(SIGINT, catch);
120
	if (preen)
121
		(void)signal(SIGQUIT, catchquit);
122
123
	(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
124
125
	if (returntosingle)
126
		ret = 2;
127
128
	exit(ret);
129
}
130
131
static int
132
argtoi(int flag, char *req, char *str, int base)
133
{
134
	char *cp;
135
	int ret;
136
137
	ret = (int)strtol(str, &cp, base);
138
	if (cp == str || *cp)
139
		errexit("-%c flag requires a %s\n", flag, req);
140
	return (ret);
141
}
142
143
/*
144
 * Check the specified filesystem.
145
 */
146
/* ARGSUSED */
147
static int
148
checkfilesys(char *filesys, char *mntpt, long auxdata, int child)
149
{
150
	daddr32_t n_bfree;
151
	struct dups *dp;
152
	struct zlncnt *zlnp;
153
	int i;
154
155
	if (preen && child)
156
		(void)signal(SIGQUIT, voidquit);
157
	setcdevname(filesys, NULL, preen);
158
	if (debug && preen)
159
		pwarn("starting\n");
160
161
	switch (setup(filesys)) {
162
	case 0:
163
		if (preen)
164
			pfatal("CAN'T CHECK FILE SYSTEM.");
165
	case -1:
166
		return (0);
167
	}
168
	/*
169
	 * 1: scan inodes tallying blocks used
170
	 */
171
	if (preen == 0) {
172
		if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
173
			printf("** Last Mounted on %s\n",
174
			    sblock.e2fs.e2fs_fsmnt);
175
		}
176
		if (hotroot())
177
			printf("** Root file system\n");
178
		printf("** Phase 1 - Check Blocks and Sizes\n");
179
	}
180
	pass1();
181
182
	/*
183
	 * 1b: locate first references to duplicates, if any
184
	 */
185
	if (duplist) {
186
		if (preen)
187
			pfatal("INTERNAL ERROR: dups with -p");
188
		printf("** Phase 1b - Rescan For More DUPS\n");
189
		pass1b();
190
	}
191
192
	/*
193
	 * 2: traverse directories from root to mark all connected directories
194
	 */
195
	if (preen == 0)
196
		printf("** Phase 2 - Check Pathnames\n");
197
	pass2();
198
199
	/*
200
	 * 3: scan inodes looking for disconnected directories
201
	 */
202
	if (preen == 0)
203
		printf("** Phase 3 - Check Connectivity\n");
204
	pass3();
205
206
	/*
207
	 * 4: scan inodes looking for disconnected files; check reference counts
208
	 */
209
	if (preen == 0)
210
		printf("** Phase 4 - Check Reference Counts\n");
211
	pass4();
212
213
	/*
214
	 * 5: check and repair resource counts in cylinder groups
215
	 */
216
	if (preen == 0)
217
		printf("** Phase 5 - Check Cyl groups\n");
218
	pass5();
219
220
	/*
221
	 * print out summary statistics
222
	 */
223
	n_bfree = sblock.e2fs.e2fs_fbcount;
224
225
	pwarn("%d files, %d used, %d free\n",
226
	    n_files, n_blks, n_bfree);
227
	if (debug &&
228
		/* 9 reserved and unused inodes in FS */
229
	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
230
		printf("%d files missing\n", n_files);
231
	if (debug) {
232
		for (i = 0; i < sblock.e2fs_ncg; i++)
233
			n_blks +=  cgoverhead(i);
234
		n_blks += sblock.e2fs.e2fs_first_dblock;
235
		if (n_blks -= maxfsblock - n_bfree)
236
			printf("%d blocks missing\n", n_blks);
237
		if (duplist != NULL) {
238
			printf("The following duplicate blocks remain:");
239
			for (dp = duplist; dp; dp = dp->next)
240
				printf(" %d,", dp->dup);
241
			printf("\n");
242
		}
243
		if (zlnhead != NULL) {
244
			printf("The following zero link count inodes remain:");
245
			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
246
				printf(" %llu,",
247
				    (unsigned long long)zlnp->zlncnt);
248
			printf("\n");
249
		}
250
	}
251
	zlnhead = NULL;
252
	duplist = NULL;
253
	muldup = NULL;
254
	inocleanup();
255
	if (fsmodified) {
256
		time_t t;
257
		(void)time(&t);
258
		sblock.e2fs.e2fs_wtime = t;
259
		sblock.e2fs.e2fs_lastfsck = t;
260
		sbdirty();
261
	}
262
	ckfini(1);
263
	free(blockmap);
264
	free(statemap);
265
	free((char *)lncntp);
266
	if (!fsmodified)
267
		return (0);
268
	if (!preen)
269
		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
270
	if (rerun)
271
		printf("\n***** PLEASE RERUN FSCK *****\n");
272
	if (hotroot()) {
273
		struct statfs stfs_buf;
274
		/*
275
		 * We modified the root.  Do a mount update on
276
		 * it, unless it is read-write, so we can continue.
277
		 */
278
		if (statfs("/", &stfs_buf) == 0) {
279
			long flags = stfs_buf.f_flags;
280
			struct ufs_args args;
281
			int ret;
282
283
			if (flags & MNT_RDONLY) {
284
				args.fspec = 0;
285
				args.export_info.ex_flags = 0;
286
				args.export_info.ex_root = 0;
287
				flags |= MNT_UPDATE | MNT_RELOAD;
288
				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
289
				if (ret == 0)
290
					return(0);
291
			}
292
		}
293
		if (!preen)
294
			printf("\n***** REBOOT NOW *****\n");
295
		sync();
296
		return (4);
297
	}
298
	return (0);
299
}
300
301
static void
302
usage(void)
303
{
304
	extern char *__progname;
305
306
	(void) fprintf(stderr,
307
	    "usage: %s [-dfnpy] [-b block#] [-m mode] filesystem\n",
308
	    __progname);
309
	exit(1);
310
}