Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / e2fsck / unix.c
1 /*
2  * unix.c - The unix-specific code for e2fsck
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #ifdef HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #include <string.h>
17 #include <fcntl.h>
18 #include <ctype.h>
19 #include <time.h>
20 #ifdef HAVE_SIGNAL_H
21 #include <signal.h>
22 #endif
23 #ifdef HAVE_GETOPT_H
24 #include <getopt.h>
25 #else
26 extern char *optarg;
27 extern int optind;
28 #endif
29 #include <unistd.h>
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #ifdef HAVE_MNTENT_H
34 #include <mntent.h>
35 #endif
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_MALLOC_H
40 #include <malloc.h>
41 #endif
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
44 #endif
45 #ifdef HAVE_DIRENT_H
46 #include <dirent.h>
47 #endif
48
49 #include "et/com_err.h"
50 #include "e2fsck.h"
51 #include "problem.h"
52 #include "../version.h"
53
54 /* Command line options */
55 static int cflag;               /* check disk */
56 static int show_version_only;
57 static int verbose;
58
59 static int replace_bad_blocks;
60 static int keep_bad_blocks;
61 static char *bad_blocks_file;
62
63 e2fsck_t e2fsck_global_ctx;     /* Try your very best not to use this! */
64
65 #ifdef CONFIG_JBD_DEBUG         /* Enabled by configure --enable-jfs-debug */
66 int journal_enable_debug = -1;
67 #endif
68
69 static void usage(e2fsck_t ctx)
70 {
71         fprintf(stderr,
72                 _("Usage: %s [-panyrcdfvtDFV] [-b superblock] [-B blocksize]\n"
73                 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
74                 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
75                 "\t\t[-E extended-options] device\n"),
76                 ctx->program_name);
77
78         fprintf(stderr, _("\nEmergency help:\n"
79                 " -p                   Automatic repair (no questions)\n"
80                 " -n                   Make no changes to the filesystem\n"
81                 " -y                   Assume \"yes\" to all questions\n"
82                 " -c                   Check for bad blocks and add them to the badblock list\n"
83                 " -f                   Force checking even if filesystem is marked clean\n"));
84         fprintf(stderr, _(""
85                 " -v                   Be verbose\n"
86                 " -b superblock        Use alternative superblock\n"
87                 " -B blocksize         Force blocksize when looking for superblock\n"
88                 " -j external_journal  Set location of the external journal\n"
89                 " -l bad_blocks_file   Add to badblocks list\n"
90                 " -L bad_blocks_file   Set badblocks list\n"
91                 ));
92
93         exit(FSCK_USAGE);
94 }
95
96 static void show_stats(e2fsck_t ctx)
97 {
98         ext2_filsys fs = ctx->fs;
99         ext2_ino_t inodes, inodes_used;
100         blk_t blocks, blocks_used;
101         int dir_links;
102         int num_files, num_links;
103         int frag_percent;
104
105         dir_links = 2 * ctx->fs_directory_count - 1;
106         num_files = ctx->fs_total_count - dir_links;
107         num_links = ctx->fs_links_count - dir_links;
108         inodes = fs->super->s_inodes_count;
109         inodes_used = (fs->super->s_inodes_count -
110                        fs->super->s_free_inodes_count);
111         blocks = fs->super->s_blocks_count;
112         blocks_used = (fs->super->s_blocks_count -
113                        fs->super->s_free_blocks_count);
114
115         frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
116         frag_percent = (frag_percent + 5) / 10;
117         
118         if (!verbose) {
119                 printf(_("%s: %u/%u files (%0d.%d%% non-contiguous), %u/%u blocks\n"),
120                        ctx->device_name, inodes_used, inodes,
121                        frag_percent / 10, frag_percent % 10,
122                        blocks_used, blocks);
123                 return;
124         }
125         printf (P_("\n%8u inode used (%2.2f%%)\n", "\n%8u inodes used (%2.2f%%)\n",
126                    inodes_used), inodes_used, 100.0 * inodes_used / inodes);
127         printf (P_("%8u non-contiguous inode (%0d.%d%%)\n",
128                    "%8u non-contiguous inodes (%0d.%d%%)\n",
129                    ctx->fs_fragmented),
130                 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
131         printf (_("         # of inodes with ind/dind/tind blocks: %u/%u/%u\n"),
132                 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
133         printf (P_("%8u block used (%2.2f%%)\n", "%8u blocks used (%2.2f%%)\n",
134                    blocks_used), blocks_used, 100.0 * blocks_used / blocks);
135         printf (P_("%8u bad block\n", "%8u bad blocks\n",
136                    ctx->fs_badblocks_count), ctx->fs_badblocks_count);
137         printf (P_("%8u large file\n", "%8u large files\n",
138                    ctx->large_files), ctx->large_files);
139         printf (P_("\n%8u regular file\n", "\n%8u regular files\n",
140                    ctx->fs_regular_count), ctx->fs_regular_count);
141         printf (P_("%8u directory\n", "%8u directories\n",
142                    ctx->fs_directory_count), ctx->fs_directory_count);
143         printf (P_("%8u character device file\n",
144                    "%8u character device files\n", ctx->fs_chardev_count),
145                 ctx->fs_chardev_count);
146         printf (P_("%8u block device file\n", "%8u block device files\n",
147                    ctx->fs_blockdev_count), ctx->fs_blockdev_count);
148         printf (P_("%8u fifo\n", "%8u fifos\n", ctx->fs_fifo_count),
149                 ctx->fs_fifo_count);
150         printf (P_("%8u link\n", "%8u links\n",
151                    ctx->fs_links_count - dir_links),
152                 ctx->fs_links_count - dir_links);
153         printf (P_("%8u symbolic link", "%8u symbolic links",
154                    ctx->fs_symlinks_count), ctx->fs_symlinks_count);
155         printf (P_(" (%u fast symbolic link)\n", " (%u fast symbolic links)\n",
156                    ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
157         printf (P_("%8u socket\n", "%8u sockets\n", ctx->fs_sockets_count),
158                 ctx->fs_sockets_count);
159         printf ("--------\n");
160         printf (P_("%8u file\n", "%8u files\n",
161                    ctx->fs_total_count - dir_links),
162                 ctx->fs_total_count - dir_links);
163 }
164
165 static void check_mount(e2fsck_t ctx)
166 {
167         errcode_t       retval;
168         int             cont;
169
170         retval = ext2fs_check_if_mounted(ctx->filesystem_name,
171                                          &ctx->mount_flags);
172         if (retval) {
173                 com_err("ext2fs_check_if_mount", retval,
174                         _("while determining whether %s is mounted."),
175                         ctx->filesystem_name);
176                 return;
177         }
178
179         /*
180          * If the filesystem isn't mounted, or it's the root
181          * filesystem and it's mounted read-only, and we're not doing
182          * a read/write check, then everything's fine.
183          */
184         if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
185             ((ctx->mount_flags & EXT2_MF_ISROOT) &&
186              (ctx->mount_flags & EXT2_MF_READONLY) &&
187              !(ctx->options & E2F_OPT_WRITECHECK)))
188                 return;
189
190         if ((ctx->options & E2F_OPT_READONLY) &&
191             !(ctx->options & E2F_OPT_WRITECHECK)) {
192                 printf(_("Warning!  %s is mounted.\n"), ctx->filesystem_name);
193                 return;
194         }
195
196         printf(_("%s is mounted.  "), ctx->filesystem_name);
197         if (!ctx->interactive)
198                 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
199         printf(_("\n\n\007\007\007\007WARNING!!!  "
200                "Running e2fsck on a mounted filesystem may cause\n"
201                "SEVERE filesystem damage.\007\007\007\n\n"));
202         cont = ask_yn(_("Do you really want to continue"), -1);
203         if (!cont) {
204                 printf (_("check aborted.\n"));
205                 exit (0);
206         }
207         return;
208 }
209
210 static int is_on_batt(void)
211 {
212         FILE    *f;
213         DIR     *d;
214         char    tmp[80], tmp2[80], fname[80];
215         unsigned int    acflag;
216         struct dirent*  de;
217
218         f = fopen("/proc/apm", "r");
219         if (f) {
220                 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4) 
221                         acflag = 1;
222                 fclose(f);
223                 return (acflag != 1);
224         }
225         d = opendir("/proc/acpi/ac_adapter");
226         if (d) {
227                 while ((de=readdir(d)) != NULL) {
228                         if (!strncmp(".", de->d_name, 1))
229                                 continue;
230                         snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state", 
231                                  de->d_name);
232                         f = fopen(fname, "r");
233                         if (!f)
234                                 continue;
235                         if (fscanf(f, "%s %s", tmp2, tmp) != 2)
236                                 tmp[0] = 0;
237                         fclose(f);
238                         if (strncmp(tmp, "off-line", 8) == 0) {
239                                 closedir(d);
240                                 return 1;
241                         }
242                 }
243                 closedir(d);
244         }
245         return 0;
246 }
247
248 /*
249  * This routine checks to see if a filesystem can be skipped; if so,
250  * it will exit with E2FSCK_OK.  Under some conditions it will print a
251  * message explaining why a check is being forced.
252  */
253 static void check_if_skip(e2fsck_t ctx)
254 {
255         ext2_filsys fs = ctx->fs;
256         const char *reason = NULL;
257         unsigned int reason_arg = 0;
258         long next_check;
259         int batt = is_on_batt();
260         int defer_check_on_battery;
261         time_t lastcheck;
262
263         profile_get_boolean(ctx->profile, "options",
264                             "defer_check_on_battery", 0, 1, 
265                             &defer_check_on_battery);
266         if (!defer_check_on_battery)
267                 batt = 0;
268
269         if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || cflag)
270                 return;
271         
272         lastcheck = fs->super->s_lastcheck;
273         if (lastcheck > ctx->now)
274                 lastcheck -= ctx->time_fudge;
275         if ((fs->super->s_state & EXT2_ERROR_FS) ||
276             !ext2fs_test_valid(fs))
277                 reason = _(" contains a file system with errors");
278         else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
279                 reason = _(" was not cleanly unmounted");
280         else if (check_backup_super_block(ctx))
281                 reason = _(" primary superblock features different from backup");
282         else if ((fs->super->s_max_mnt_count > 0) &&
283                  (fs->super->s_mnt_count >=
284                   (unsigned) fs->super->s_max_mnt_count)) {
285                 reason = _(" has been mounted %u times without being checked");
286                 reason_arg = fs->super->s_mnt_count;
287                 if (batt && (fs->super->s_mnt_count < 
288                              (unsigned) fs->super->s_max_mnt_count*2))
289                         reason = 0;
290         } else if (fs->super->s_checkinterval &&
291                    ((ctx->now - lastcheck) >= fs->super->s_checkinterval)) {
292                 reason = _(" has gone %u days without being checked");
293                 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
294                 if (batt && ((ctx->now - fs->super->s_lastcheck) < 
295                              fs->super->s_checkinterval*2))
296                         reason = 0;
297         }
298         if (reason) {
299                 fputs(ctx->device_name, stdout);
300                 printf(reason, reason_arg);
301                 fputs(_(", check forced.\n"), stdout);
302                 return;
303         }
304         printf(_("%s: clean, %u/%u files, %u/%u blocks"), ctx->device_name,
305                fs->super->s_inodes_count - fs->super->s_free_inodes_count,
306                fs->super->s_inodes_count,
307                fs->super->s_blocks_count - fs->super->s_free_blocks_count,
308                fs->super->s_blocks_count);
309         next_check = 100000;
310         if (fs->super->s_max_mnt_count > 0) {
311                 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
312                 if (next_check <= 0) 
313                         next_check = 1;
314         }
315         if (fs->super->s_checkinterval &&
316             ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
317                 next_check = 1;
318         if (next_check <= 5) {
319                 if (next_check == 1) {
320                         if (batt) 
321                                 fputs(_(" (check deferred; on battery)"),
322                                       stdout);
323                         else
324                                 fputs(_(" (check after next mount)"), stdout);
325                 } else
326                         printf(_(" (check in %ld mounts)"), next_check);
327         }
328         fputc('\n', stdout);
329         ext2fs_close(fs);
330         ctx->fs = NULL;
331         e2fsck_free_context(ctx);
332         exit(FSCK_OK);
333 }
334
335 /*
336  * For completion notice
337  */
338 struct percent_tbl {
339         int     max_pass;
340         int     table[32];
341 };
342 struct percent_tbl e2fsck_tbl = {
343         5, { 0, 70, 90, 92,  95, 100 }
344 };
345 static char bar[128], spaces[128];
346
347 static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
348                           int max)
349 {
350         float   percent;
351         
352         if (pass <= 0)
353                 return 0.0;
354         if (pass > tbl->max_pass || max == 0)
355                 return 100.0;
356         percent = ((float) curr) / ((float) max);
357         return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
358                 + tbl->table[pass-1]);
359 }
360
361 extern void e2fsck_clear_progbar(e2fsck_t ctx)
362 {
363         if (!(ctx->flags & E2F_FLAG_PROG_BAR))
364                 return;
365         
366         printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
367                ctx->stop_meta);
368         fflush(stdout);
369         ctx->flags &= ~E2F_FLAG_PROG_BAR;
370 }
371
372 int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
373                            unsigned int dpynum)
374 {
375         static const char spinner[] = "\\|/-";
376         int     i;
377         unsigned int    tick;
378         struct timeval  tv;
379         int dpywidth;
380         int fixed_percent;
381
382         if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
383                 return 0;
384
385         /*
386          * Calculate the new progress position.  If the
387          * percentage hasn't changed, then we skip out right
388          * away. 
389          */
390         fixed_percent = (int) ((10 * percent) + 0.5);
391         if (ctx->progress_last_percent == fixed_percent)
392                 return 0;
393         ctx->progress_last_percent = fixed_percent;
394
395         /*
396          * If we've already updated the spinner once within
397          * the last 1/8th of a second, no point doing it
398          * again.
399          */
400         gettimeofday(&tv, NULL);
401         tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
402         if ((tick == ctx->progress_last_time) &&
403             (fixed_percent != 0) && (fixed_percent != 1000))
404                 return 0;
405         ctx->progress_last_time = tick;
406
407         /*
408          * Advance the spinner, and note that the progress bar
409          * will be on the screen
410          */
411         ctx->progress_pos = (ctx->progress_pos+1) & 3;
412         ctx->flags |= E2F_FLAG_PROG_BAR;
413
414         dpywidth = 66 - strlen(label);
415         dpywidth = 8 * (dpywidth / 8);
416         if (dpynum)
417                 dpywidth -= 8;
418
419         i = ((percent * dpywidth) + 50) / 100;
420         printf("%s%s: |%s%s", ctx->start_meta, label,
421                bar + (sizeof(bar) - (i+1)),
422                spaces + (sizeof(spaces) - (dpywidth - i + 1)));
423         if (fixed_percent == 1000)
424                 fputc('|', stdout);
425         else
426                 fputc(spinner[ctx->progress_pos & 3], stdout);
427         printf(" %4.1f%%  ", percent);
428         if (dpynum)
429                 printf("%u\r", dpynum);
430         else
431                 fputs(" \r", stdout);
432         fputs(ctx->stop_meta, stdout);
433         
434         if (fixed_percent == 1000)
435                 e2fsck_clear_progbar(ctx);
436         fflush(stdout);
437
438         return 0;
439 }
440
441 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
442                                   unsigned long cur, unsigned long max)
443 {
444         char buf[80];
445         float percent;
446
447         if (pass == 0)
448                 return 0;
449         
450         if (ctx->progress_fd) {
451                 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
452                 write(ctx->progress_fd, buf, strlen(buf));
453         } else {
454                 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
455                 e2fsck_simple_progress(ctx, ctx->device_name,
456                                        percent, 0);
457         }
458         return 0;
459 }
460
461 #define PATH_SET "PATH=/sbin"
462
463 static void reserve_stdio_fds(void)
464 {
465         int     fd;
466
467         while (1) {
468                 fd = open("/dev/null", O_RDWR);
469                 if (fd > 2)
470                         break;
471                 if (fd < 0) {
472                         fprintf(stderr, _("ERROR: Couldn't open "
473                                 "/dev/null (%s)\n"),
474                                 strerror(errno));
475                         break;
476                 }
477         }
478         close(fd);
479 }
480
481 #ifdef HAVE_SIGNAL_H
482 static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
483 {
484         e2fsck_t ctx = e2fsck_global_ctx;
485
486         if (!ctx)
487                 return;
488
489         ctx->progress = e2fsck_update_progress;
490         ctx->progress_fd = 0;
491 }
492
493 static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
494 {
495         e2fsck_t ctx = e2fsck_global_ctx;
496
497         if (!ctx)
498                 return;
499
500         e2fsck_clear_progbar(ctx);
501         ctx->progress = 0;
502 }
503
504 static void signal_cancel(int sig EXT2FS_ATTR((unused)))
505 {
506         e2fsck_t ctx = e2fsck_global_ctx;
507
508         if (!ctx)
509                 exit(FSCK_CANCELED);
510
511         ctx->flags |= E2F_FLAG_CANCEL;
512 }
513 #endif
514
515 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
516 {
517         char    *buf, *token, *next, *p, *arg;
518         int     ea_ver;
519         int     extended_usage = 0;
520
521         buf = string_copy(ctx, opts, 0);
522         for (token = buf; token && *token; token = next) {
523                 p = strchr(token, ',');
524                 next = 0;
525                 if (p) {
526                         *p = 0;
527                         next = p+1;
528                 }
529                 arg = strchr(token, '=');
530                 if (arg) {
531                         *arg = 0;
532                         arg++;
533                 }
534                 if (strcmp(token, "ea_ver") == 0) {
535                         if (!arg) {
536                                 extended_usage++;
537                                 continue;
538                         }
539                         ea_ver = strtoul(arg, &p, 0);
540                         if (*p ||
541                             ((ea_ver != 1) && (ea_ver != 2))) {
542                                 fprintf(stderr,
543                                         _("Invalid EA version.\n"));
544                                 extended_usage++;
545                                 continue;
546                         }
547                         ctx->ext_attr_ver = ea_ver;
548                 } else {
549                         fprintf(stderr, _("Unknown extended option: %s\n"),
550                                 token);
551                         extended_usage++;
552                 }
553         }
554         free(buf);
555
556         if (extended_usage) {
557                 fputs(("\nExtended options are separated by commas, "
558                        "and may take an argument which\n"
559                        "is set off by an equals ('=') sign.  "
560                         "Valid extended options are:\n"
561                        "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
562                 exit(1);
563         }
564 }
565
566 static void syntax_err_report(const char *filename, long err, int line_num)
567 {
568         fprintf(stderr, 
569                 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
570                 filename, line_num, error_message(err));
571         exit(FSCK_ERROR);
572 }
573
574 static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
575
576 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
577 {
578         int             flush = 0;
579         int             c, fd;
580 #ifdef MTRACE
581         extern void     *mallwatch;
582 #endif
583         e2fsck_t        ctx;
584         errcode_t       retval;
585 #ifdef HAVE_SIGNAL_H
586         struct sigaction        sa;
587 #endif
588         char            *extended_opts = 0;
589         char            *cp;
590         int             res;            /* result of sscanf */
591 #ifdef CONFIG_JBD_DEBUG
592         char            *jbd_debug;
593 #endif
594
595         retval = e2fsck_allocate_context(&ctx);
596         if (retval)
597                 return retval;
598
599         *ret_ctx = ctx;
600
601         setvbuf(stdout, NULL, _IONBF, BUFSIZ);
602         setvbuf(stderr, NULL, _IONBF, BUFSIZ);
603         if (isatty(0) && isatty(1)) {
604                 ctx->interactive = 1;
605         } else {
606                 ctx->start_meta[0] = '\001';
607                 ctx->stop_meta[0] = '\002';
608         }
609         memset(bar, '=', sizeof(bar)-1);
610         memset(spaces, ' ', sizeof(spaces)-1);
611         add_error_table(&et_ext2_error_table);
612         add_error_table(&et_prof_error_table);
613         blkid_get_cache(&ctx->blkid, NULL);
614         
615         if (argc && *argv)
616                 ctx->program_name = *argv;
617         else
618                 ctx->program_name = "e2fsck";
619         while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
620                 switch (c) {
621                 case 'C':
622                         ctx->progress = e2fsck_update_progress;
623                         res = sscanf(optarg, "%d", &ctx->progress_fd);
624                         if (res != 1)
625                                 goto sscanf_err;
626
627                         if (!ctx->progress_fd)
628                                 break;
629                         /* Validate the file descriptor to avoid disasters */
630                         fd = dup(ctx->progress_fd);
631                         if (fd < 0) {
632                                 fprintf(stderr,
633                                 _("Error validating file descriptor %d: %s\n"),
634                                         ctx->progress_fd,
635                                         error_message(errno));
636                                 fatal_error(ctx,
637                         _("Invalid completion information file descriptor"));
638                         } else
639                                 close(fd);
640                         break;
641                 case 'D':
642                         ctx->options |= E2F_OPT_COMPRESS_DIRS;
643                         break;
644                 case 'E':
645                         extended_opts = optarg;
646                         break;
647                 case 'p':
648                 case 'a':
649                         if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
650                         conflict_opt:
651                                 fatal_error(ctx, 
652         _("Only one of the options -p/-a, -n or -y may be specified."));
653                         }
654                         ctx->options |= E2F_OPT_PREEN;
655                         break;
656                 case 'n':
657                         if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
658                                 goto conflict_opt;
659                         ctx->options |= E2F_OPT_NO;
660                         break;
661                 case 'y':
662                         if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
663                                 goto conflict_opt;
664                         ctx->options |= E2F_OPT_YES;
665                         break;
666                 case 't':
667 #ifdef RESOURCE_TRACK
668                         if (ctx->options & E2F_OPT_TIME)
669                                 ctx->options |= E2F_OPT_TIME2;
670                         else
671                                 ctx->options |= E2F_OPT_TIME;
672 #else
673                         fprintf(stderr, _("The -t option is not "
674                                 "supported on this version of e2fsck.\n"));
675 #endif
676                         break;
677                 case 'c':
678                         if (cflag++)
679                                 ctx->options |= E2F_OPT_WRITECHECK;
680                         ctx->options |= E2F_OPT_CHECKBLOCKS;
681                         break;
682                 case 'r':
683                         /* What we do by default, anyway! */
684                         break;
685                 case 'b':
686                         res = sscanf(optarg, "%d", &ctx->use_superblock);
687                         if (res != 1)
688                                 goto sscanf_err;
689                         ctx->flags |= E2F_FLAG_SB_SPECIFIED;
690                         break;
691                 case 'B':
692                         ctx->blocksize = atoi(optarg);
693                         break;
694                 case 'I':
695                         res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
696                         if (res != 1)
697                                 goto sscanf_err;
698                         break;
699                 case 'j':
700                         ctx->journal_name = string_copy(ctx, optarg, 0);
701                         break;
702                 case 'P':
703                         res = sscanf(optarg, "%d", &ctx->process_inode_size);
704                         if (res != 1)
705                                 goto sscanf_err;
706                         break;
707                 case 'L':
708                         replace_bad_blocks++;
709                 case 'l':
710                         bad_blocks_file = string_copy(ctx, optarg, 0);
711                         break;
712                 case 'd':
713                         ctx->options |= E2F_OPT_DEBUG;
714                         break;
715                 case 'f':
716                         ctx->options |= E2F_OPT_FORCE;
717                         break;
718                 case 'F':
719                         flush = 1;
720                         break;
721                 case 'v':
722                         verbose = 1;
723                         break;
724                 case 'V':
725                         show_version_only = 1;
726                         break;
727 #ifdef MTRACE
728                 case 'M':
729                         mallwatch = (void *) strtol(optarg, NULL, 0);
730                         break;
731 #endif
732                 case 'N':
733                         ctx->device_name = optarg;
734                         break;
735                 case 'k':
736                         keep_bad_blocks++;
737                         break;
738                 default:
739                         usage(ctx);
740                 }
741         if (show_version_only)
742                 return 0;
743         if (optind != argc - 1)
744                 usage(ctx);
745         if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
746             !cflag && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
747                 ctx->options |= E2F_OPT_READONLY;
748         ctx->io_options = strchr(argv[optind], '?');
749         if (ctx->io_options) 
750                 *ctx->io_options++ = 0;
751         ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
752         if (!ctx->filesystem_name) {
753                 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"), 
754                         argv[optind]);
755                 fatal_error(ctx, 0);
756         }
757         if (extended_opts)
758                 parse_extended_opts(ctx, extended_opts);
759
760         if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
761                 config_fn[0] = cp;
762         profile_set_syntax_err_cb(syntax_err_report);
763         profile_init(config_fn, &ctx->profile);
764
765         if (flush) {
766                 fd = open(ctx->filesystem_name, O_RDONLY, 0);
767                 if (fd < 0) {
768                         com_err("open", errno,
769                                 _("while opening %s for flushing"),
770                                 ctx->filesystem_name);
771                         fatal_error(ctx, 0);
772                 }
773                 if ((retval = ext2fs_sync_device(fd, 1))) {
774                         com_err("ext2fs_sync_device", retval,
775                                 _("while trying to flush %s"),
776                                 ctx->filesystem_name);
777                         fatal_error(ctx, 0);
778                 }
779                 close(fd);
780         }
781         if (cflag && bad_blocks_file) {
782                 fprintf(stderr, _("The -c and the -l/-L options may "
783                                   "not be both used at the same time.\n"));
784                 exit(FSCK_USAGE);
785         }
786 #ifdef HAVE_SIGNAL_H
787         /*
788          * Set up signal action
789          */
790         memset(&sa, 0, sizeof(struct sigaction));
791         sa.sa_handler = signal_cancel;
792         sigaction(SIGINT, &sa, 0);
793         sigaction(SIGTERM, &sa, 0);
794 #ifdef SA_RESTART
795         sa.sa_flags = SA_RESTART;
796 #endif
797         e2fsck_global_ctx = ctx;
798         sa.sa_handler = signal_progress_on;
799         sigaction(SIGUSR1, &sa, 0);
800         sa.sa_handler = signal_progress_off;
801         sigaction(SIGUSR2, &sa, 0);
802 #endif
803
804         /* Update our PATH to include /sbin if we need to run badblocks  */
805         if (cflag) {
806                 char *oldpath = getenv("PATH");
807                 char *newpath;
808                 int len = sizeof(PATH_SET) + 1;
809
810                 if (oldpath)
811                         len += strlen(oldpath);
812
813                 newpath = malloc(len);
814                 if (!newpath)
815                         fatal_error(ctx, "Couldn't malloc() newpath");
816                 strcpy(newpath, PATH_SET);
817
818                 if (oldpath) {
819                         strcat(newpath, ":");
820                         strcat(newpath, oldpath);
821                 }
822                 putenv(newpath);
823         }
824 #ifdef CONFIG_JBD_DEBUG
825         jbd_debug = getenv("E2FSCK_JBD_DEBUG");
826         if (jbd_debug) {
827                 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
828                 if (res != 1) {
829                         fprintf(stderr,
830                                 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
831                                 jbd_debug);
832                         exit (1);
833                 }
834         }
835 #endif
836         return 0;
837
838 sscanf_err:
839         fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
840                 c, optarg);
841         exit (1);
842 }
843
844 static const char *my_ver_string = E2FSPROGS_VERSION;
845 static const char *my_ver_date = E2FSPROGS_DATE;
846                                         
847 int main (int argc, char *argv[])
848 {
849         errcode_t       retval = 0, orig_retval = 0;
850         int             exit_value = FSCK_OK;
851         ext2_filsys     fs = 0;
852         io_manager      io_ptr;
853         struct ext2_super_block *sb;
854         const char      *lib_ver_date;
855         int             my_ver, lib_ver;
856         e2fsck_t        ctx;
857         struct problem_context pctx;
858         int flags, run_result;
859         int journal_size;
860         int sysval, sys_page_size = 4096;
861         
862         clear_problem_context(&pctx);
863 #ifdef MTRACE
864         mtrace();
865 #endif
866 #ifdef MCHECK
867         mcheck(0);
868 #endif
869 #ifdef ENABLE_NLS
870         setlocale(LC_MESSAGES, "");
871         setlocale(LC_CTYPE, "");
872         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
873         textdomain(NLS_CAT_NAME);
874 #endif
875         my_ver = ext2fs_parse_version_string(my_ver_string);
876         lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
877         if (my_ver > lib_ver) {
878                 fprintf( stderr, _("Error: ext2fs library version "
879                         "out of date!\n"));
880                 show_version_only++;
881         }
882         
883         retval = PRS(argc, argv, &ctx);
884         if (retval) {
885                 com_err("e2fsck", retval,
886                         _("while trying to initialize program"));
887                 exit(FSCK_ERROR);
888         }
889         reserve_stdio_fds();
890         
891 #ifdef RESOURCE_TRACK
892         init_resource_track(&ctx->global_rtrack, NULL);
893 #endif
894
895         if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
896                 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
897                          my_ver_date);
898
899         if (show_version_only) {
900                 fprintf(stderr, _("\tUsing %s, %s\n"),
901                         error_message(EXT2_ET_BASE), lib_ver_date);
902                 exit(FSCK_OK);
903         }
904         
905         check_mount(ctx);
906         
907         if (!(ctx->options & E2F_OPT_PREEN) &&
908             !(ctx->options & E2F_OPT_NO) &&
909             !(ctx->options & E2F_OPT_YES)) {
910                 if (!ctx->interactive)
911                         fatal_error(ctx,
912                                     _("need terminal for interactive repairs"));
913         }
914         ctx->superblock = ctx->use_superblock;
915 restart:
916 #ifdef CONFIG_TESTIO_DEBUG
917         io_ptr = test_io_manager;
918         test_io_backing_manager = unix_io_manager;
919 #else
920         io_ptr = unix_io_manager;
921 #endif
922         flags = 0;
923         if ((ctx->options & E2F_OPT_READONLY) == 0)
924                 flags |= EXT2_FLAG_RW;
925         if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
926                 flags |= EXT2_FLAG_EXCLUSIVE;
927
928         if (ctx->superblock && ctx->blocksize) {
929                 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, 
930                                       flags, ctx->superblock, ctx->blocksize,
931                                       io_ptr, &fs);
932         } else if (ctx->superblock) {
933                 int blocksize;
934                 for (blocksize = EXT2_MIN_BLOCK_SIZE;
935                      blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
936                         retval = ext2fs_open2(ctx->filesystem_name, 
937                                               ctx->io_options, flags,
938                                               ctx->superblock, blocksize,
939                                               io_ptr, &fs);
940                         if (!retval)
941                                 break;
942                 }
943         } else 
944                 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options, 
945                                       flags, 0, 0, io_ptr, &fs);
946         if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
947             !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
948             ((retval == EXT2_ET_BAD_MAGIC) ||
949              (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
950              ((retval == 0) && ext2fs_check_desc(fs)))) {
951                 if (!fs || (fs->group_desc_count > 1)) {
952                         printf(_("%s: %s trying backup blocks...\n"),
953                                ctx->program_name, 
954                                retval ? _("Superblock invalid,") :
955                                _("Group descriptors look bad..."));
956                         get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
957                         if (fs)
958                                 ext2fs_close(fs);
959                         orig_retval = retval;
960                         goto restart;
961                 }
962         }
963         if (retval) {
964                 if (orig_retval)
965                         retval = orig_retval;
966                 com_err(ctx->program_name, retval, _("while trying to open %s"),
967                         ctx->filesystem_name);
968                 if (retval == EXT2_ET_REV_TOO_HIGH) {
969                         printf(_("The filesystem revision is apparently "
970                                "too high for this version of e2fsck.\n"
971                                "(Or the filesystem superblock "
972                                "is corrupt)\n\n"));
973                         fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
974                 } else if (retval == EXT2_ET_SHORT_READ)
975                         printf(_("Could this be a zero-length partition?\n"));
976                 else if ((retval == EPERM) || (retval == EACCES))
977                         printf(_("You must have %s access to the "
978                                "filesystem or be root\n"),
979                                (ctx->options & E2F_OPT_READONLY) ?
980                                "r/o" : "r/w");
981                 else if (retval == ENXIO)
982                         printf(_("Possibly non-existent or swap device?\n"));
983                 else if (retval == EBUSY)
984                         printf(_("Filesystem mounted or opened exclusively "
985                                  "by another program?\n"));
986 #ifdef EROFS
987                 else if (retval == EROFS)
988                         printf(_("Disk write-protected; use the -n option "
989                                "to do a read-only\n"
990                                "check of the device.\n"));
991 #endif
992                 else
993                         fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
994                 fatal_error(ctx, 0);
995         }
996         /*
997          * We only update the master superblock because (a) paranoia;
998          * we don't want to corrupt the backup superblocks, and (b) we
999          * don't need to update the mount count and last checked
1000          * fields in the backup superblock (the kernel doesn't update
1001          * the backup superblocks anyway).  With newer versions of the
1002          * library this flag is set by ext2fs_open2(), but we set this
1003          * here just to be sure.  (No, we don't support e2fsck running
1004          * with some other libext2fs than the one that it was shipped
1005          * with, but just in case....)
1006          */
1007         fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1008
1009         if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1010                 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1011                 int need_restart = 0;
1012
1013                 pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name,
1014                                                       blocksize,
1015                                                       &ctx->num_blocks);
1016                 /*
1017                  * The floppy driver refuses to allow anyone else to
1018                  * open the device if has been opened with O_EXCL;
1019                  * this is unlike other block device drivers in Linux.
1020                  * To handle this, we close the filesystem and then
1021                  * reopen the filesystem after we get the device size.
1022                  */
1023                 if (pctx.errcode == EBUSY) {
1024                         ext2fs_close(fs);
1025                         need_restart++;
1026                         pctx.errcode = 
1027                                 ext2fs_get_device_size(ctx->filesystem_name, 
1028                                                        blocksize,
1029                                                        &ctx->num_blocks);
1030                 }
1031                 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1032                         ctx->num_blocks = 0;
1033                 else if (pctx.errcode) {
1034                         fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1035                         ctx->flags |= E2F_FLAG_ABORT;
1036                         fatal_error(ctx, 0);
1037                 }
1038                 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1039                 if (need_restart)
1040                         goto restart;
1041         }
1042
1043         ctx->fs = fs;
1044         fs->priv_data = ctx;
1045         fs->now = ctx->now;
1046         sb = fs->super;
1047         if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1048                 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
1049                         _("while trying to open %s"),
1050                         ctx->filesystem_name);
1051         get_newer:
1052                 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1053         }
1054
1055         /*
1056          * Set the device name, which is used whenever we print error
1057          * or informational messages to the user.
1058          */
1059         if (ctx->device_name == 0 &&
1060             (sb->s_volume_name[0] != 0)) {
1061                 ctx->device_name = string_copy(ctx, sb->s_volume_name,
1062                                                sizeof(sb->s_volume_name));
1063         }
1064         if (ctx->device_name == 0)
1065                 ctx->device_name = ctx->filesystem_name;
1066
1067         /*
1068          * Make sure the ext3 superblock fields are consistent.
1069          */
1070         retval = e2fsck_check_ext3_journal(ctx);
1071         if (retval) {
1072                 com_err(ctx->program_name, retval,
1073                         _("while checking ext3 journal for %s"),
1074                         ctx->device_name);
1075                 fatal_error(ctx, 0);
1076         }
1077
1078         /*
1079          * Check to see if we need to do ext3-style recovery.  If so,
1080          * do it, and then restart the fsck.
1081          */
1082         if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
1083                 if (ctx->options & E2F_OPT_READONLY) {
1084                         printf(_("Warning: skipping journal recovery "
1085                                  "because doing a read-only filesystem "
1086                                  "check.\n"));
1087                         io_channel_flush(ctx->fs->io);
1088                 } else {
1089                         if (ctx->flags & E2F_FLAG_RESTARTED) {
1090                                 /*
1091                                  * Whoops, we attempted to run the
1092                                  * journal twice.  This should never
1093                                  * happen, unless the hardware or
1094                                  * device driver is being bogus.
1095                                  */
1096                                 com_err(ctx->program_name, 0,
1097                                         _("unable to set superblock flags on %s\n"), ctx->device_name);
1098                                 fatal_error(ctx, 0);
1099                         }
1100                         retval = e2fsck_run_ext3_journal(ctx);
1101                         if (retval) {
1102                                 com_err(ctx->program_name, retval,
1103                                 _("while recovering ext3 journal of %s"),
1104                                         ctx->device_name);
1105                                 fatal_error(ctx, 0);
1106                         }
1107                         ext2fs_close(ctx->fs);
1108                         ctx->fs = 0;
1109                         ctx->flags |= E2F_FLAG_RESTARTED;
1110                         goto restart;
1111                 }
1112         }
1113
1114         /*
1115          * Check for compatibility with the feature sets.  We need to
1116          * be more stringent than ext2fs_open().
1117          */
1118         if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1119             (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1120                 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1121                         "(%s)", ctx->device_name);
1122                 goto get_newer;
1123         }
1124         if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1125                 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1126                         "(%s)", ctx->device_name);
1127                 goto get_newer;
1128         }
1129 #ifdef ENABLE_COMPRESSION
1130         if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1131                 com_err(ctx->program_name, 0,
1132                         _("Warning: compression support is experimental.\n"));
1133 #endif
1134 #ifndef ENABLE_HTREE
1135         if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1136                 com_err(ctx->program_name, 0,
1137                         _("E2fsck not compiled with HTREE support,\n\t"
1138                           "but filesystem %s has HTREE directories.\n"),
1139                         ctx->device_name);
1140                 goto get_newer;
1141         }
1142 #endif
1143
1144         /*
1145          * If the user specified a specific superblock, presumably the
1146          * master superblock has been trashed.  So we mark the
1147          * superblock as dirty, so it can be written out.
1148          */
1149         if (ctx->superblock &&
1150             !(ctx->options & E2F_OPT_READONLY))
1151                 ext2fs_mark_super_dirty(fs);
1152
1153         /*
1154          * Calculate the number of filesystem blocks per pagesize.  If
1155          * fs->blocksize > page_size, set the number of blocks per
1156          * pagesize to 1 to avoid division by zero errors.
1157          */
1158 #ifdef _SC_PAGESIZE
1159         sysval = sysconf(_SC_PAGESIZE);
1160         if (sysval > 0)
1161                 sys_page_size = sysval;
1162 #endif /* _SC_PAGESIZE */
1163         ctx->blocks_per_page = sys_page_size / fs->blocksize;
1164         if (ctx->blocks_per_page == 0)
1165                 ctx->blocks_per_page = 1;
1166
1167         ehandler_init(fs->io);
1168
1169         if (ctx->superblock)
1170                 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1171         ext2fs_mark_valid(fs);
1172         check_super_block(ctx);
1173         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1174                 fatal_error(ctx, 0);
1175         check_if_skip(ctx);
1176         if (bad_blocks_file)
1177                 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1178         else if (cflag)
1179                 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1180         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1181                 fatal_error(ctx, 0);
1182
1183         /*
1184          * Mark the system as valid, 'til proven otherwise
1185          */
1186         ext2fs_mark_valid(fs);
1187
1188         retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1189         if (retval) {
1190                 com_err(ctx->program_name, retval,
1191                         _("while reading bad blocks inode"));
1192                 preenhalt(ctx);
1193                 printf(_("This doesn't bode well,"
1194                          " but we'll try to go on...\n"));
1195         }
1196
1197         /*
1198          * Save the journal size in megabytes.
1199          * Try and use the journal size from the backup else let e2fsck
1200          * find the default journal size.
1201          */
1202         if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS)
1203                 journal_size = sb->s_jnl_blocks[16] >> 20;
1204         else
1205                 journal_size = -1;
1206
1207         run_result = e2fsck_run(ctx);
1208         e2fsck_clear_progbar(ctx);
1209
1210         if (ctx->flags & E2F_FLAG_JOURNAL_INODE) {
1211                 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1212                         if (journal_size < 1024)
1213                                 journal_size = ext2fs_default_journal_size(fs->super->s_blocks_count);
1214                         if (journal_size < 0) {
1215                                 fs->super->s_feature_compat &=
1216                                         ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1217                                 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1218                                 com_err(ctx->program_name, 0, 
1219                                         _("Couldn't determine journal size"));
1220                                 goto no_journal;
1221                         }
1222                         printf(_("Creating journal (%d blocks): "),
1223                                journal_size);
1224                         fflush(stdout);
1225                         retval = ext2fs_add_journal_inode(fs,
1226                                                           journal_size, 0);
1227                         if (retval) {
1228                                 com_err("Error ", retval,
1229                                         _("\n\twhile trying to create journal"));
1230                                 goto no_journal;
1231                         }
1232                         printf(_(" Done.\n"));
1233                         printf(_("\n*** journal has been re-created - "
1234                                        "filesystem is now ext3 again ***\n"));
1235                 }
1236         }
1237 no_journal:
1238
1239         if (run_result == E2F_FLAG_RESTART) {
1240                 printf(_("Restarting e2fsck from the beginning...\n"));
1241                 retval = e2fsck_reset_context(ctx);
1242                 if (retval) {
1243                         com_err(ctx->program_name, retval,
1244                                 _("while resetting context"));
1245                         fatal_error(ctx, 0);
1246                 }
1247                 ext2fs_close(fs);
1248                 goto restart;
1249         }
1250         if (run_result & E2F_FLAG_CANCEL) {
1251                 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1252                        ctx->device_name : ctx->filesystem_name);
1253                 exit_value |= FSCK_CANCELED;
1254         }
1255         if (run_result & E2F_FLAG_ABORT)
1256                 fatal_error(ctx, _("aborted"));
1257         if (check_backup_super_block(ctx)) {
1258                 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1259                 ext2fs_mark_super_dirty(fs);
1260         }
1261
1262 #ifdef MTRACE
1263         mtrace_print("Cleanup");
1264 #endif
1265         if (ext2fs_test_changed(fs)) {
1266                 exit_value |= FSCK_NONDESTRUCT;
1267                 if (!(ctx->options & E2F_OPT_PREEN))
1268                     printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1269                                ctx->device_name);
1270                 if (ctx->mount_flags & EXT2_MF_ISROOT) {
1271                         printf(_("%s: ***** REBOOT LINUX *****\n"),
1272                                ctx->device_name);
1273                         exit_value |= FSCK_REBOOT;
1274                 }
1275         }
1276         if (!ext2fs_test_valid(fs) ||
1277             ((exit_value & FSCK_CANCELED) && 
1278              (sb->s_state & EXT2_ERROR_FS))) {
1279                 printf(_("\n%s: ********** WARNING: Filesystem still has "
1280                          "errors **********\n\n"), ctx->device_name);
1281                 exit_value |= FSCK_UNCORRECTED;
1282                 exit_value &= ~FSCK_NONDESTRUCT;
1283         }
1284         if (exit_value & FSCK_CANCELED) {
1285                 int     allow_cancellation;
1286
1287                 profile_get_boolean(ctx->profile, "options",
1288                                     "allow_cancellation", 0, 0, 
1289                                     &allow_cancellation);
1290                 exit_value &= ~FSCK_NONDESTRUCT;
1291                 if (allow_cancellation && ext2fs_test_valid(fs) &&
1292                     (sb->s_state & EXT2_VALID_FS) && 
1293                     !(sb->s_state & EXT2_ERROR_FS))
1294                         exit_value = 0;
1295         } else {
1296                 show_stats(ctx);
1297                 if (!(ctx->options & E2F_OPT_READONLY)) {
1298                         if (ext2fs_test_valid(fs)) {
1299                                 if (!(sb->s_state & EXT2_VALID_FS))
1300                                         exit_value |= FSCK_NONDESTRUCT;
1301                                 sb->s_state = EXT2_VALID_FS;
1302                         } else
1303                                 sb->s_state &= ~EXT2_VALID_FS;
1304                         sb->s_mnt_count = 0;
1305                         sb->s_lastcheck = ctx->now;
1306                         ext2fs_mark_super_dirty(fs);
1307                 }
1308         }
1309
1310         e2fsck_write_bitmaps(ctx);
1311 #ifdef RESOURCE_TRACK
1312         io_channel_flush(ctx->fs->io);
1313         if (ctx->options & E2F_OPT_TIME)
1314                 print_resource_track(NULL, &ctx->global_rtrack, ctx->fs->io);
1315 #endif
1316         ext2fs_close(fs);
1317         ctx->fs = NULL;
1318         free(ctx->filesystem_name);
1319         free(ctx->journal_name);
1320
1321         e2fsck_free_context(ctx);
1322         remove_error_table(&et_ext2_error_table);
1323         remove_error_table(&et_prof_error_table);
1324         return exit_value;
1325 }