blob: 1c29e4c08069dda27360801bb5cfee419d0b0873 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * e2fsck.c - a consistency checker for the new extended file system.
3 *
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
6 */
7
8/* Usage: e2fsck [-dfpnsvy] device
9 * -d -- debugging this program
10 * -f -- check the fs even if it is marked valid
11 * -p -- "preen" the filesystem
12 * -n -- open the filesystem r/o mode; never try to fix problems
13 * -v -- verbose (tells how many files)
14 * -y -- always answer yes to questions
15 *
16 * The device may be a block device or a image of one, but this isn't
17 * enforced (but it's not much fun on a character device :-).
18 */
19
20#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <termios.h>
24#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000025#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000027#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <unistd.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000029#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
32#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <mntent.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000034#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include <sys/ioctl.h>
36#include <malloc.h>
37
38#include "et/com_err.h"
39#include "e2fsck.h"
40#include "../version.h"
41
42extern int isatty(int);
43
44const char * program_name = "e2fsck";
45const char * device_name = NULL;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000046const char * filesystem_name = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000047
48/* Command line options */
49int nflag = 0;
50int yflag = 0;
51int tflag = 0; /* Do timing */
52int cflag = 0; /* check disk */
53int preen = 0;
54int rwflag = 1;
55int inode_buffer_blocks = 0;
56blk_t superblock;
57int blocksize = 0;
58int verbose = 0;
59int list = 0;
60int debug = 0;
61int force = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000062int invalid_bitmaps = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000063static int show_version_only = 0;
64
65static int replace_bad_blocks = 0;
66static char *bad_blocks_file = 0;
67
68static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
69
70struct resource_track global_rtrack;
71
72static int root_filesystem = 0;
73static int read_only_root = 0;
74
Theodore Ts'of3db3561997-04-26 13:34:30 +000075int *invalid_inode_bitmap;
76int *invalid_block_bitmap;
77int *invalid_inode_table;
78int restart_e2fsck = 0;
79
Theodore Ts'o3839e651997-04-26 13:21:57 +000080static void usage(NOARGS)
81{
82 fprintf(stderr,
Theodore Ts'of3db3561997-04-26 13:34:30 +000083 "Usage: %s [-panyrcdfvtFV] [-b superblock] [-B blocksize]\n"
84 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
85 "\t\t[-l|-L bad_blocks_file] device\n", program_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 exit(FSCK_USAGE);
87}
88
89static void show_stats(ext2_filsys fs)
90{
91 int inodes, inodes_used, blocks, blocks_used;
92 int dir_links;
93 int num_files, num_links;
94
95 dir_links = 2 * fs_directory_count - 1;
96 num_files = fs_total_count - dir_links;
97 num_links = fs_links_count - dir_links;
98 inodes = fs->super->s_inodes_count;
99 inodes_used = (fs->super->s_inodes_count -
100 fs->super->s_free_inodes_count);
101 blocks = fs->super->s_blocks_count;
102 blocks_used = (fs->super->s_blocks_count -
103 fs->super->s_free_blocks_count);
104
105 if (!verbose) {
Theodore Ts'o74becf31997-04-26 14:37:06 +0000106 printf("%s: %d/%d files (%3.1f%% non-contiguous), %d/%d blocks\n",
107 device_name, inodes_used, inodes,
108 100.00 * fs_fragmented / inodes_used,
109 blocks_used, blocks);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 return;
111 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000112 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113 (inodes_used != 1) ? "s" : "",
114 100 * inodes_used / inodes);
Theodore Ts'o74becf31997-04-26 14:37:06 +0000115 printf ("%8d non-contiguous inodes (%3.1f%%)\n",
116 fs_fragmented, 100.00 * fs_fragmented / inodes_used);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000117 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
118 fs_ind_count, fs_dind_count, fs_tind_count);
119 printf ("%8d block%s used (%d%%)\n"
120 "%8d bad block%s\n", blocks_used,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121 (blocks_used != 1) ? "s" : "",
122 100 * blocks_used / blocks, fs_badblocks_count,
123 fs_badblocks_count != 1 ? "s" : "");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000124 printf ("\n%8d regular file%s\n"
125 "%8d director%s\n"
126 "%8d character device file%s\n"
127 "%8d block device file%s\n"
128 "%8d fifo%s\n"
129 "%8d link%s\n"
130 "%8d symbolic link%s (%d fast symbolic link%s)\n"
131 "%8d socket%s\n"
132 "--------\n"
133 "%8d file%s\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 fs_regular_count, (fs_regular_count != 1) ? "s" : "",
135 fs_directory_count, (fs_directory_count != 1) ? "ies" : "y",
136 fs_chardev_count, (fs_chardev_count != 1) ? "s" : "",
137 fs_blockdev_count, (fs_blockdev_count != 1) ? "s" : "",
138 fs_fifo_count, (fs_fifo_count != 1) ? "s" : "",
139 fs_links_count - dir_links,
140 ((fs_links_count - dir_links) != 1) ? "s" : "",
141 fs_symlinks_count, (fs_symlinks_count != 1) ? "s" : "",
142 fs_fast_symlinks_count, (fs_fast_symlinks_count != 1) ? "s" : "",
143 fs_sockets_count, (fs_sockets_count != 1) ? "s" : "",
144 fs_total_count - dir_links,
145 ((fs_total_count - dir_links) != 1) ? "s" : "");
146}
147
148static void check_mount(NOARGS)
149{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000150 errcode_t retval;
Theodore Ts'o297f47a1997-04-26 14:25:20 +0000151 int mount_flags, cont, fd;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000153 retval = ext2fs_check_if_mounted(filesystem_name, &mount_flags);
154 if (retval) {
155 com_err("ext2fs_check_if_mount", retval,
156 "while determining whether %s is mounted.",
157 filesystem_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000159 }
160 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 return;
Theodore Ts'o297f47a1997-04-26 14:25:20 +0000162
Theodore Ts'o74becf31997-04-26 14:37:06 +0000163#if (defined(__linux__) && defined(HAVE_MNTENT_H))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 /*
165 * If the root is mounted read-only, then /etc/mtab is
166 * probably not correct; so we won't issue a warning based on
167 * it.
168 */
Theodore Ts'o297f47a1997-04-26 14:25:20 +0000169 fd = open(MOUNTED, O_RDWR);
170 if (fd < 0) {
171 if (errno == EROFS)
172 return;
173 } else
174 close(fd);
175#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176
177 if (!rwflag) {
178 printf("Warning! %s is mounted.\n", device_name);
179 return;
180 }
181
182 printf ("%s is mounted. ", device_name);
183 if (isatty (0) && isatty (1))
184 cont = ask_yn("Do you really want to continue", -1);
185 else
186 cont = 0;
187 if (!cont) {
188 printf ("check aborted.\n");
189 exit (0);
190 }
191 return;
192}
193
194static void sync_disks(NOARGS)
195{
196 sync();
197 sync();
198 sleep(1);
199 sync();
200}
201
Theodore Ts'of3db3561997-04-26 13:34:30 +0000202#define MIN_CHECK 1
203#define MAX_CHECK 2
204
205static const char *corrupt_msg = "\nThe filesystem superblock is corrupt. "
206 "Try running e2fsck with an alternate\n"
207 "superblock using the -b option. "
208 "(8193 is commonly an alternate superblock;\n"
209 "Hence, 'e2fsck -b 8193 <device>' may recover the filesystem.)\n\n";
210
211static void check_super_value(const char *descr, unsigned long value,
212 int flags, unsigned long min, unsigned long max)
213{
214 if (((flags & MIN_CHECK) && (value < min)) ||
215 ((flags & MAX_CHECK) && (value > max))) {
216 printf("Corruption found in superblock. (%s = %lu).\n",
217 descr, value);
218 printf(corrupt_msg);
219 fatal_error(0);
220 }
221}
222
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223static void check_super_block(ext2_filsys fs)
224{
225 blk_t first_block, last_block;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000226 struct ext2_super_block *s = fs->super;
227 blk_t blocks_per_group = fs->super->s_blocks_per_group;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 int i;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000229 blk_t should_be;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000230 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000231
Theodore Ts'of3db3561997-04-26 13:34:30 +0000232 /*
233 * Verify the super block constants...
234 */
235 check_super_value("inodes_count", s->s_inodes_count,
236 MIN_CHECK, 1, 0);
237 check_super_value("blocks_count", s->s_blocks_count,
238 MIN_CHECK, 1, 0);
239 check_super_value("first_data_block", s->s_first_data_block,
240 MAX_CHECK, 0, s->s_blocks_count);
241 check_super_value("log_frag_size", s->s_log_frag_size,
242 MAX_CHECK, 0, 2);
243 check_super_value("log_block_size", s->s_log_block_size,
244 MIN_CHECK | MAX_CHECK, s->s_log_frag_size,
245 2);
246 check_super_value("frags_per_group", s->s_frags_per_group,
247 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
248 check_super_value("blocks_per_group", s->s_blocks_per_group,
249 MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s));
250 check_super_value("inodes_per_group", s->s_inodes_per_group,
251 MIN_CHECK, 1, 0);
252 check_super_value("r_blocks_count", s->s_r_blocks_count,
253 MAX_CHECK, 0, s->s_blocks_count);
254
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000255 retval = ext2fs_get_device_size(filesystem_name, EXT2_BLOCK_SIZE(s),
256 &should_be);
257 if (retval) {
258 com_err("ext2fs_get_device_size", retval,
259 "while trying to check physical size of filesystem");
260 fatal_error(0);
261 }
262 if (should_be < s->s_blocks_count) {
263 printf("The filesystem size (according to the superblock) is %d blocks\n", s->s_blocks_count);
264 printf("The physical size of the device is %d blocks\n",
265 should_be);
266 printf("Either the superblock or the partition table is likely to be corrupt!\n");
267 preenhalt(fs);
268 if (ask("Abort", 1))
269 fatal_error(0);
270 }
271
Theodore Ts'of3db3561997-04-26 13:34:30 +0000272 if (s->s_log_block_size != s->s_log_frag_size) {
273 printf("Superblock block_size = %d, fragsize = %d.\n",
274 EXT2_BLOCK_SIZE(s), EXT2_FRAG_SIZE(s));
275 printf("This version of e2fsck does not support fragment "
276 "sizes different\n"
277 "from the block size.\n");
278 fatal_error(0);
279 }
280
281 should_be = s->s_frags_per_group /
282 (s->s_log_block_size - s->s_log_frag_size + 1);
283 if (s->s_blocks_per_group != should_be) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000284 printf("Superblock blocks_per_group = %u, should "
285 "have been %u\n", s->s_blocks_per_group,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286 should_be);
287 printf(corrupt_msg);
288 }
289
290 should_be = (s->s_log_block_size == 0) ? 1 : 0;
291 if (s->s_first_data_block != should_be) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000292 printf("Superblock first_data_block = %u, should "
293 "have been %u\n", s->s_first_data_block,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000294 should_be);
295 printf(corrupt_msg);
296 }
297
298 /*
299 * Verify the group descriptors....
300 */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 first_block = fs->super->s_first_data_block;
302 last_block = first_block + blocks_per_group;
303
304 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000305 if (i == fs->group_desc_count - 1)
306 last_block = fs->super->s_blocks_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 if ((fs->group_desc[i].bg_block_bitmap < first_block) ||
308 (fs->group_desc[i].bg_block_bitmap >= last_block)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000309 printf("Block bitmap for group %d is not in group. "
310 "(block %u)\n",
311 i, fs->group_desc[i].bg_block_bitmap);
312 preenhalt(fs);
313 if (!ask("Relocate", 1)) {
314 fatal_error("Block bitmap not in group");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000315 }
316 fs->group_desc[i].bg_block_bitmap = 0;
317 invalid_block_bitmap[i]++;
318 invalid_bitmaps++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 }
320 if ((fs->group_desc[i].bg_inode_bitmap < first_block) ||
321 (fs->group_desc[i].bg_inode_bitmap >= last_block)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000322 printf("Inode bitmap group %d not in group. "
323 "(block %u)\n",
324 i, fs->group_desc[i].bg_inode_bitmap);
325 preenhalt(fs);
326 if (!ask("Relocate", 1)) {
327 fatal_error("Inode bitmap not in group");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000328 }
329 fs->group_desc[i].bg_inode_bitmap = 0;
330 invalid_inode_bitmap[i]++;
331 invalid_bitmaps++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332 }
333 if ((fs->group_desc[i].bg_inode_table < first_block) ||
334 ((fs->group_desc[i].bg_inode_table +
335 fs->inode_blocks_per_group - 1) >= last_block)) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000336 printf("Inode table for group %d not in group. "
337 "(block %u)\n",
338 i, fs->group_desc[i].bg_inode_table);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000339 printf("WARNING: SEVERE DATA LOSS POSSIBLE.\n");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000340 preenhalt(fs);
341 if (!ask("Relocate", 1)) {
342 fatal_error("Inode table not in group");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000343 }
344 fs->group_desc[i].bg_inode_table = 0;
345 invalid_inode_table[i]++;
346 invalid_bitmaps++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347 }
348 first_block += fs->super->s_blocks_per_group;
349 last_block += fs->super->s_blocks_per_group;
350 }
351 return;
352}
353
354/*
355 * This routine checks to see if a filesystem can be skipped; if so,
356 * it will exit with E2FSCK_OK. Under some conditions it will print a
357 * message explaining why a check is being forced.
358 */
359static void check_if_skip(ext2_filsys fs)
360{
361 const char *reason = NULL;
362
363 if (force || bad_blocks_file || cflag)
364 return;
365
366 if (fs->super->s_state & EXT2_ERROR_FS)
367 reason = "contains a file system with errors";
368 else if (fs->super->s_mnt_count >=
369 (unsigned) fs->super->s_max_mnt_count)
370 reason = "has reached maximal mount count";
371 else if (fs->super->s_checkinterval &&
372 time(0) >= (fs->super->s_lastcheck +
373 fs->super->s_checkinterval))
374 reason = "has gone too long without being checked";
375 if (reason) {
376 printf("%s %s, check forced.\n", device_name, reason);
377 return;
378 }
379 if (fs->super->s_state & EXT2_VALID_FS) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000380 printf("%s: clean, %d/%d files, %d/%d blocks\n", device_name,
381 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
382 fs->super->s_inodes_count,
383 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
384 fs->super->s_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 exit(FSCK_OK);
386 }
387}
388
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000389#define PATH_SET "PATH=/sbin"
390
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391static void PRS(int argc, char *argv[])
392{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000393 int flush = 0;
394 char c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395#ifdef MTRACE
Theodore Ts'of3db3561997-04-26 13:34:30 +0000396 extern void *mallwatch;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000398 char *oldpath = getenv("PATH");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399
400 /* Update our PATH to include /sbin */
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000401 if (oldpath) {
402 char *newpath;
403
404 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
405 if (!newpath)
406 fatal_error("Couldn't malloc() newpath");
407 strcpy (newpath, PATH_SET);
408 strcat (newpath, ":");
409 strcat (newpath, oldpath);
410 putenv (newpath);
411 } else
412 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413
414 setbuf(stdout, NULL);
415 setbuf(stderr, NULL);
416 initialize_ext2_error_table();
417
418 if (argc && *argv)
419 program_name = *argv;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000420 while ((c = getopt (argc, argv, "panyrcB:dfvtFVM:b:I:P:l:L:N:")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 switch (c) {
422 case 'p':
423 case 'a':
424 preen = 1;
425 yflag = nflag = 0;
426 break;
427 case 'n':
428 nflag = 1;
429 preen = yflag = 0;
430 break;
431 case 'y':
432 yflag = 1;
433 preen = nflag = 0;
434 break;
435 case 't':
436 tflag++;
437 break;
438 case 'c':
439 cflag++;
440 break;
441 case 'r':
442 /* What we do by default, anyway! */
443 break;
444 case 'b':
445 superblock = atoi(optarg);
446 break;
447 case 'B':
448 blocksize = atoi(optarg);
449 break;
450 case 'I':
451 inode_buffer_blocks = atoi(optarg);
452 break;
453 case 'P':
454 process_inode_size = atoi(optarg);
455 break;
456 case 'L':
457 replace_bad_blocks++;
458 case 'l':
459 bad_blocks_file = malloc(strlen(optarg)+1);
460 if (!bad_blocks_file)
461 fatal_error("Couldn't malloc bad_blocks_file");
462 strcpy(bad_blocks_file, optarg);
463 break;
464 case 'd':
465 debug = 1;
466 break;
467 case 'f':
468 force = 1;
469 break;
470 case 'F':
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000471#ifdef BLKFLSBUF
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 flush = 1;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000473#else
474 fatal_error ("-F not supported");
475#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000476 break;
477 case 'v':
478 verbose = 1;
479 break;
480 case 'V':
481 show_version_only = 1;
482 break;
483#ifdef MTRACE
484 case 'M':
485 mallwatch = (void *) strtol(optarg, NULL, 0);
486 break;
487#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000488 case 'N':
489 device_name = optarg;
490 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 default:
492 usage ();
493 }
494 if (show_version_only)
495 return;
496 if (optind != argc - 1)
497 usage ();
498 if (nflag && !bad_blocks_file && !cflag)
499 rwflag = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000500 filesystem_name = argv[optind];
501 if (device_name == 0)
502 device_name = filesystem_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 if (flush) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000504#ifdef BLKFLSBUF
505 int fd = open(filesystem_name, O_RDONLY, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506
507 if (fd < 0) {
508 com_err("open", errno, "while opening %s for flushing",
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000509 filesystem_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 exit(FSCK_ERROR);
511 }
512 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
513 com_err("BLKFLSBUF", errno, "while trying to flush %s",
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000514 filesystem_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 exit(FSCK_ERROR);
516 }
517 close(fd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000518#else
519 fatal_error ("BLKFLSBUF not supported");
520#endif /* BLKFLSBUF */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 }
522}
523
524int main (int argc, char *argv[])
525{
526 errcode_t retval = 0;
527 int exit_value = FSCK_OK;
528 int i;
529 ext2_filsys fs;
530
531#ifdef MTRACE
532 mtrace();
533#endif
534#ifdef MCHECK
535 mcheck(0);
536#endif
537
538 init_resource_track(&global_rtrack);
539
540 PRS(argc, argv);
541
542 if (!preen)
543 fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n",
544 E2FSPROGS_VERSION, E2FSPROGS_DATE,
545 EXT2FS_VERSION, EXT2FS_DATE);
546
547 if (show_version_only)
548 exit(0);
549
550 check_mount();
551
552 if (!preen && !nflag && !yflag) {
553 if (!isatty (0) || !isatty (1))
554 die ("need terminal for interactive repairs");
555 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000556restart:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557 sync_disks();
558 if (superblock && blocksize) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000559 retval = ext2fs_open(filesystem_name,
560 rwflag ? EXT2_FLAG_RW : 0,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 superblock, blocksize, unix_io_manager,
562 &fs);
563 } else if (superblock) {
564 for (i=0; possible_block_sizes[i]; i++) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000565 retval = ext2fs_open(filesystem_name,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566 rwflag ? EXT2_FLAG_RW : 0,
567 superblock,
568 possible_block_sizes[i],
569 unix_io_manager, &fs);
570 if (!retval)
571 break;
572 }
573 } else
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000574 retval = ext2fs_open(filesystem_name,
575 rwflag ? EXT2_FLAG_RW : 0,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 0, 0, unix_io_manager, &fs);
577 if (retval) {
578 com_err(program_name, retval, "while trying to open %s",
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000579 filesystem_name);
580 switch (retval) {
581 case EXT2_ET_REV_TOO_HIGH:
Theodore Ts'of3db3561997-04-26 13:34:30 +0000582 printf ("Get a newer version of e2fsck!\n");
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000583 break;
584 case EXT2_ET_SHORT_READ:
585 printf ("Could this be a zero-length partition?\n");
586 break;
587 case EPERM:
588 case EACCES:
589 printf("You must have %s access to the "
590 "filesystem or be root\n",
591 rwflag ? "r/w" : "r/o");
592 break;
593 case ENXIO:
594 printf("Possibly non-existent or swap device?\n");
595 break;
596 default:
Theodore Ts'of3db3561997-04-26 13:34:30 +0000597 printf(corrupt_msg);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000598 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000599 fatal_error(0);
600 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000601
602#ifdef EXT2_CURRENT_REV
603 if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) {
604 com_err(program_name, retval, "while trying to open %s",
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000605 filesystem_name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000606 printf ("Get a newer version of e2fsck!\n");
607 fatal_error(0);
608 }
609#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 /*
611 * If the user specified a specific superblock, presumably the
612 * master superblock has been trashed. So we mark the
613 * superblock as dirty, so it can be written out.
614 */
615 if (superblock && rwflag)
616 ext2fs_mark_super_dirty(fs);
617
618 ehandler_init(fs->io);
619
Theodore Ts'of3db3561997-04-26 13:34:30 +0000620 invalid_inode_bitmap = allocate_memory(sizeof(int) *
621 fs->group_desc_count,
622 "invalid_inode_bitmap");
623 invalid_block_bitmap = allocate_memory(sizeof(int) *
624 fs->group_desc_count,
625 "invalid_block_bitmap");
626 invalid_inode_table = allocate_memory(sizeof(int) *
627 fs->group_desc_count,
628 "invalid_inode_table");
629
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630 check_super_block(fs);
631 check_if_skip(fs);
632 if (bad_blocks_file)
633 read_bad_blocks_file(fs, bad_blocks_file, replace_bad_blocks);
634 else if (cflag)
635 test_disk(fs);
636
637 /*
638 * Mark the system as valid, 'til proven otherwise
639 */
640 ext2fs_mark_valid(fs);
641
642 pass1(fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000643 free(invalid_inode_bitmap);
644 free(invalid_block_bitmap);
645 free(invalid_inode_table);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000646 if (restart_e2fsck) {
647 ext2fs_close(fs);
648 printf("Restarting e2fsck from the beginning...\n");
649 restart_e2fsck = 0;
650 goto restart;
651 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 pass2(fs);
653 pass3(fs);
654 pass4(fs);
655 pass5(fs);
656
657#ifdef MTRACE
658 mtrace_print("Cleanup");
659#endif
660 if (ext2fs_test_changed(fs)) {
661 exit_value = FSCK_NONDESTRUCT;
662 if (!preen)
663 printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n",
664 device_name);
665 if (root_filesystem && !read_only_root) {
666 printf("%s: ***** REBOOT LINUX *****\n", device_name);
667 exit_value = FSCK_REBOOT;
668 }
669 }
670 if (!ext2fs_test_valid(fs))
671 exit_value = FSCK_UNCORRECTED;
672 if (rwflag) {
673 if (ext2fs_test_valid(fs))
674 fs->super->s_state = EXT2_VALID_FS;
675 else
676 fs->super->s_state &= ~EXT2_VALID_FS;
677 fs->super->s_mnt_count = 0;
678 fs->super->s_lastcheck = time(NULL);
679 ext2fs_mark_super_dirty(fs);
680 }
681 show_stats(fs);
682
683 write_bitmaps(fs);
684 ext2fs_close(fs);
685 sync_disks();
686
687 if (tflag)
688 print_resource_track(&global_rtrack);
689
690 return exit_value;
691}