blob: 5f1aaa23185bd3def96ceaa6c24b305a4eec90db [file] [log] [blame]
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001/*
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>
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000019#include <time.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000020#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +000021#include <signal.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000022#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000023#ifdef HAVE_GETOPT_H
24#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000025#else
26extern char *optarg;
27extern int optind;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000028#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
Theodore Ts'offf45482003-04-13 00:44:19 -040036#ifdef HAVE_SYS_IOCTL_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000037#include <sys/ioctl.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040038#endif
Theodore Ts'oe71d8732003-03-14 02:13:48 -050039#ifdef HAVE_MALLOC_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000040#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050041#endif
Theodore Ts'oc07f9f22004-04-12 00:16:44 -040042#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_DIRENT_H
46#include <dirent.h>
47#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000048
49#include "et/com_err.h"
50#include "e2fsck.h"
51#include "problem.h"
52#include "../version.h"
53
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054/* Command line options */
Theodore Ts'o54a31a32003-08-19 10:08:34 -040055static int swapfs;
56static int normalize_swapfs;
57static int cflag; /* check disk */
58static int show_version_only;
59static int verbose;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000060
Theodore Ts'o54a31a32003-08-19 10:08:34 -040061static int replace_bad_blocks;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -050062static int keep_bad_blocks;
Theodore Ts'o54a31a32003-08-19 10:08:34 -040063static char *bad_blocks_file;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000064
Theodore Ts'o243dc312000-08-22 21:37:47 +000065e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
66
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -050067#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
68int journal_enable_debug = -1;
69#endif
70
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000071static void usage(e2fsck_t ctx)
72{
73 fprintf(stderr,
Theodore Ts'o4fd68342002-10-31 19:39:03 -050074 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000075 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -040076 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040077 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000078 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000079
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000080 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000081 " -p Automatic repair (no questions)\n"
82 " -n Make no changes to the filesystem\n"
83 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050084 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000085 " -f Force checking even if filesystem is marked clean\n"));
86 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000087 " -v Be verbose\n"
88 " -b superblock Use alternative superblock\n"
89 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -040090 " -j external_journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000091 " -l bad_blocks_file Add to badblocks list\n"
92 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000093 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000094
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000095 exit(FSCK_USAGE);
96}
97
98static void show_stats(e2fsck_t ctx)
99{
100 ext2_filsys fs = ctx->fs;
101 int inodes, inodes_used, blocks, blocks_used;
102 int dir_links;
103 int num_files, num_links;
104 int frag_percent;
105
106 dir_links = 2 * ctx->fs_directory_count - 1;
107 num_files = ctx->fs_total_count - dir_links;
108 num_links = ctx->fs_links_count - dir_links;
109 inodes = fs->super->s_inodes_count;
110 inodes_used = (fs->super->s_inodes_count -
111 fs->super->s_free_inodes_count);
112 blocks = fs->super->s_blocks_count;
113 blocks_used = (fs->super->s_blocks_count -
114 fs->super->s_free_blocks_count);
115
116 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
117 frag_percent = (frag_percent + 5) / 10;
118
119 if (!verbose) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000120 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000121 ctx->device_name, inodes_used, inodes,
122 frag_percent / 10, frag_percent % 10,
123 blocks_used, blocks);
124 return;
125 }
Theodore Ts'o113e4052003-05-17 21:00:46 -0400126 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
127 inodes_used), inodes_used, 100 * inodes_used / inodes);
128 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
129 "%8d non-contiguous inodes (%0d.%d%%)\n",
130 ctx->fs_fragmented),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000131 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
132 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
133 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Theodore Ts'o113e4052003-05-17 21:00:46 -0400134 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
135 blocks_used),
136 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
137 printf (P_("%8d bad block\n", "%8d bad blocks\n",
138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
139 printf (P_("%8d large file\n", "%8d large files\n",
140 ctx->large_files), ctx->large_files);
141 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
142 ctx->fs_regular_count), ctx->fs_regular_count);
143 printf (P_("%8d directory\n", "%8d directories\n",
144 ctx->fs_directory_count), ctx->fs_directory_count);
145 printf (P_("%8d character device file\n",
146 "%8d character device files\n", ctx->fs_chardev_count),
147 ctx->fs_chardev_count);
148 printf (P_("%8d block device file\n", "%8d block device files\n",
149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
150 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
151 ctx->fs_fifo_count);
152 printf (P_("%8d link\n", "%8d links\n",
153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
155 printf (P_("%8d symbolic link", "%8d symbolic links",
156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
157 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
159 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
160 ctx->fs_sockets_count);
161 printf ("--------\n");
162 printf (P_("%8d file\n", "%8d files\n",
163 ctx->fs_total_count - dir_links),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000164 ctx->fs_total_count - dir_links);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000165}
166
167static void check_mount(e2fsck_t ctx)
168{
169 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500170 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000171
Theodore Ts'oee895132002-11-07 16:16:55 -0500172 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
173 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000174 if (retval) {
175 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000176 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000177 ctx->filesystem_name);
178 return;
179 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400180
181 /*
182 * If the filesystem isn't mounted, or it's the root filesystem
183 * and it's mounted read-only, then everything's fine.
184 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500185 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
186 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
187 (ctx->mount_flags & EXT2_MF_READONLY)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000188 return;
189
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000190 if (ctx->options & E2F_OPT_READONLY) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000191 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000192 return;
193 }
194
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000195 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400196 if (!ctx->interactive)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000197 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000198 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o74033351999-07-01 03:00:47 +0000199 "Running e2fsck on a mounted filesystem may cause\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000200 "SEVERE filesystem damage.\007\007\007\n\n"));
201 cont = ask_yn(_("Do you really want to continue"), -1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000202 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000203 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000204 exit (0);
205 }
206 return;
207}
208
Theodore Ts'o54434922003-12-07 01:28:50 -0500209static int is_on_batt(void)
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500210{
211 FILE *f;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400212 DIR *d;
213 char tmp[80], tmp2[80], fname[80];
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500214 unsigned int acflag;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400215 struct dirent* de;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500216
217 f = fopen("/proc/apm", "r");
218 if (f) {
219 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
220 acflag = 1;
221 fclose(f);
222 return (acflag != 1);
223 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400224 d = opendir("/proc/acpi/ac_adapter");
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500225 if (d) {
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500226 while ((de=readdir(d)) != NULL) {
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500227 if (!strncmp(".", de->d_name, 1))
228 continue;
229 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
230 de->d_name);
231 f = fopen(fname, "r");
232 if (!f)
233 continue;
234 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
235 tmp[0] = 0;
236 fclose(f);
237 if (strncmp(tmp, "off-line", 8) == 0) {
238 closedir(d);
239 return 1;
240 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400241 }
Matthias Andree4b137042005-01-13 03:35:29 +0100242 closedir(d);
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500243 }
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500244 return 0;
245}
246
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000247/*
248 * This routine checks to see if a filesystem can be skipped; if so,
249 * it will exit with E2FSCK_OK. Under some conditions it will print a
250 * message explaining why a check is being forced.
251 */
252static void check_if_skip(e2fsck_t ctx)
253{
254 ext2_filsys fs = ctx->fs;
255 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000256 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500257 long next_check;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500258 int batt = is_on_batt();
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000259
Theodore Ts'od37066a2001-12-21 23:28:54 -0500260 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
261 cflag || swapfs)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000262 return;
263
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500264 if ((fs->super->s_state & EXT2_ERROR_FS) ||
265 !ext2fs_test_valid(fs))
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000266 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000267 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000268 reason = _(" was not cleanly unmounted");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000269 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000270 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000271 (unsigned) fs->super->s_max_mnt_count)) {
272 reason = _(" has been mounted %u times without being checked");
273 reason_arg = fs->super->s_mnt_count;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500274 if (batt && (fs->super->s_mnt_count <
275 (unsigned) fs->super->s_max_mnt_count*2))
276 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000277 } else if (fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400278 ((ctx->now - fs->super->s_lastcheck) >=
Theodore Ts'o54434922003-12-07 01:28:50 -0500279 fs->super->s_checkinterval)) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000280 reason = _(" has gone %u days without being checked");
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400281 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
282 if (batt && ((ctx->now - fs->super->s_lastcheck) <
Theodore Ts'o54434922003-12-07 01:28:50 -0500283 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500284 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000285 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000286 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000287 fputs(ctx->device_name, stdout);
288 printf(reason, reason_arg);
289 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000290 return;
291 }
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500292 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000293 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
294 fs->super->s_inodes_count,
295 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
296 fs->super->s_blocks_count);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500297 next_check = 100000;
298 if (fs->super->s_max_mnt_count > 0) {
299 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
300 if (next_check <= 0)
301 next_check = 1;
302 }
Theodore Ts'o66fbee82004-05-04 20:38:17 -0400303 if (fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400304 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500305 next_check = 1;
306 if (next_check <= 5) {
307 if (next_check == 1)
308 fputs(_(" (check after next mount)"), stdout);
309 else
Theodore Ts'o54434922003-12-07 01:28:50 -0500310 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500311 }
312 fputc('\n', stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000313 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400314 ctx->fs = NULL;
315 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000316 exit(FSCK_OK);
317}
318
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000319/*
320 * For completion notice
321 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000322struct percent_tbl {
323 int max_pass;
324 int table[32];
325};
326struct percent_tbl e2fsck_tbl = {
327 5, { 0, 70, 90, 92, 95, 100 }
328};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400329static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000330
331static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
332 int max)
333{
334 float percent;
335
336 if (pass <= 0)
337 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000338 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000339 return 100.0;
340 percent = ((float) curr) / ((float) max);
341 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
342 + tbl->table[pass-1]);
343}
344
345extern void e2fsck_clear_progbar(e2fsck_t ctx)
346{
347 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
348 return;
349
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400350 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
351 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400352 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000353 ctx->flags &= ~E2F_FLAG_PROG_BAR;
354}
355
Theodore Ts'o520ead32003-04-19 13:48:27 -0400356int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500357 unsigned int dpynum)
358{
359 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500360 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500361 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500362 struct timeval tv;
363 int dpywidth;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500364 int fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500365
366 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
367 return 0;
368
369 /*
370 * Calculate the new progress position. If the
371 * percentage hasn't changed, then we skip out right
372 * away.
373 */
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500374 fixed_percent = (int) ((10 * percent) + 0.5);
375 if (ctx->progress_last_percent == fixed_percent)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500376 return 0;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500377 ctx->progress_last_percent = fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500378
379 /*
380 * If we've already updated the spinner once within
381 * the last 1/8th of a second, no point doing it
382 * again.
383 */
384 gettimeofday(&tv, NULL);
385 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
386 if ((tick == ctx->progress_last_time) &&
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500387 (fixed_percent != 0) && (fixed_percent != 1000))
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500388 return 0;
389 ctx->progress_last_time = tick;
390
391 /*
392 * Advance the spinner, and note that the progress bar
393 * will be on the screen
394 */
395 ctx->progress_pos = (ctx->progress_pos+1) & 3;
396 ctx->flags |= E2F_FLAG_PROG_BAR;
397
398 dpywidth = 66 - strlen(label);
399 dpywidth = 8 * (dpywidth / 8);
400 if (dpynum)
401 dpywidth -= 8;
402
403 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400404 printf("%s%s: |%s%s", ctx->start_meta, label,
405 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500406 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500407 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500408 fputc('|', stdout);
409 else
410 fputc(spinner[ctx->progress_pos & 3], stdout);
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500411 printf(" %4.1f%% ", percent);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500412 if (dpynum)
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500413 printf("%u\r", dpynum);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500414 else
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500415 fputs(" \r", stdout);
416 fputs(ctx->stop_meta, stdout);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500417
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500418 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500419 e2fsck_clear_progbar(ctx);
420 fflush(stdout);
421
422 return 0;
423}
424
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000425static int e2fsck_update_progress(e2fsck_t ctx, int pass,
426 unsigned long cur, unsigned long max)
427{
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000428 char buf[80];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000429 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000430
431 if (pass == 0)
432 return 0;
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000433
434 if (ctx->progress_fd) {
435 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
436 write(ctx->progress_fd, buf, strlen(buf));
437 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000438 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500439 e2fsck_simple_progress(ctx, ctx->device_name,
440 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000441 }
442 return 0;
443}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000444
445#define PATH_SET "PATH=/sbin"
446
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000447static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000448{
449 int fd;
450
451 while (1) {
452 fd = open("/dev/null", O_RDWR);
453 if (fd > 2)
454 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000455 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000456 fprintf(stderr, _("ERROR: Couldn't open "
457 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000458 strerror(errno));
459 break;
460 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000461 }
462 close(fd);
463}
464
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000465#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500466static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000467{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000468 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000469
470 if (!ctx)
471 return;
472
473 ctx->progress = e2fsck_update_progress;
474 ctx->progress_fd = 0;
475}
476
Theodore Ts'o54434922003-12-07 01:28:50 -0500477static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000478{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000479 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000480
481 if (!ctx)
482 return;
483
484 e2fsck_clear_progbar(ctx);
485 ctx->progress = 0;
486}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400487
Theodore Ts'o54434922003-12-07 01:28:50 -0500488static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400489{
490 e2fsck_t ctx = e2fsck_global_ctx;
491
492 if (!ctx)
493 exit(FSCK_CANCELED);
494
495 ctx->flags |= E2F_FLAG_CANCEL;
496}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000497#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000498
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400499static void parse_extended_opts(e2fsck_t ctx, const char *opts)
500{
501 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500502 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400503 int extended_usage = 0;
504
Theodore Ts'of3640932003-03-01 19:47:44 -0500505 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400506 for (token = buf; token && *token; token = next) {
507 p = strchr(token, ',');
508 next = 0;
509 if (p) {
510 *p = 0;
511 next = p+1;
512 }
513 arg = strchr(token, '=');
514 if (arg) {
515 *arg = 0;
516 arg++;
517 }
518 if (strcmp(token, "ea_ver") == 0) {
519 if (!arg) {
520 extended_usage++;
521 continue;
522 }
523 ea_ver = strtoul(arg, &p, 0);
524 if (*p ||
525 ((ea_ver != 1) && (ea_ver != 2))) {
526 fprintf(stderr,
527 _("Invalid EA version.\n"));
528 extended_usage++;
529 continue;
530 }
531 ctx->ext_attr_ver = ea_ver;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400532 } else {
533 fprintf(stderr, _("Unknown extended option: %s\n"),
534 token);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400535 extended_usage++;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400536 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400537 }
538 if (extended_usage) {
Theodore Ts'obb145b02005-06-20 08:35:27 -0400539 fputs(("\nExtended options are separated by commas, "
540 "and may take an argument which\n"
541 "is set off by an equals ('=') sign. "
542 "Valid extended options are:\n"
543 "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400544 exit(1);
545 }
546}
547
548
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000549static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
550{
551 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000552 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000553#ifdef MTRACE
554 extern void *mallwatch;
555#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000556 e2fsck_t ctx;
557 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000558#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000559 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000560#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400561 char *extended_opts = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000562
563 retval = e2fsck_allocate_context(&ctx);
564 if (retval)
565 return retval;
566
567 *ret_ctx = ctx;
568
Theodore Ts'o908b7852003-04-16 15:20:13 -0400569 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
570 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400571 if (isatty(0) && isatty(1)) {
572 ctx->interactive = 1;
573 } else {
574 ctx->start_meta[0] = '\001';
575 ctx->stop_meta[0] = '\002';
576 }
577 memset(bar, '=', sizeof(bar)-1);
578 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000579 initialize_ext2_error_table();
Theodore Ts'of3640932003-03-01 19:47:44 -0500580 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000581
582 if (argc && *argv)
583 ctx->program_name = *argv;
584 else
585 ctx->program_name = "e2fsck";
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500586 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000587 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000588 case 'C':
589 ctx->progress = e2fsck_update_progress;
590 ctx->progress_fd = atoi(optarg);
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000591 if (!ctx->progress_fd)
592 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000593 /* Validate the file descriptor to avoid disasters */
594 fd = dup(ctx->progress_fd);
595 if (fd < 0) {
596 fprintf(stderr,
597 _("Error validating file descriptor %d: %s\n"),
598 ctx->progress_fd,
599 error_message(errno));
600 fatal_error(ctx,
601 _("Invalid completion information file descriptor"));
602 } else
603 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000604 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400605 case 'D':
606 ctx->options |= E2F_OPT_COMPRESS_DIRS;
607 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400608 case 'E':
609 extended_opts = optarg;
610 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000611 case 'p':
612 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500613 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
614 conflict_opt:
615 fatal_error(ctx,
616 _("Only one the options -p/-a, -n or -y may be specified."));
617 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000618 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000619 break;
620 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500621 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
622 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000623 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000624 break;
625 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500626 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
627 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000628 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000629 break;
630 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000631#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000632 if (ctx->options & E2F_OPT_TIME)
633 ctx->options |= E2F_OPT_TIME2;
634 else
635 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000636#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000637 fprintf(stderr, _("The -t option is not "
638 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000639#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000640 break;
641 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500642 if (cflag++)
643 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000644 ctx->options |= E2F_OPT_CHECKBLOCKS;
645 break;
646 case 'r':
647 /* What we do by default, anyway! */
648 break;
649 case 'b':
650 ctx->use_superblock = atoi(optarg);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400651 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000652 break;
653 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500654 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000655 break;
656 case 'I':
657 ctx->inode_buffer_blocks = atoi(optarg);
658 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400659 case 'j':
Theodore Ts'of3640932003-03-01 19:47:44 -0500660 ctx->journal_name = string_copy(ctx, optarg, 0);
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400661 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000662 case 'P':
663 ctx->process_inode_size = atoi(optarg);
664 break;
665 case 'L':
666 replace_bad_blocks++;
667 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500668 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000669 break;
670 case 'd':
671 ctx->options |= E2F_OPT_DEBUG;
672 break;
673 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500674 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000675 break;
676 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000677 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000678 break;
679 case 'v':
680 verbose = 1;
681 break;
682 case 'V':
683 show_version_only = 1;
684 break;
685#ifdef MTRACE
686 case 'M':
687 mallwatch = (void *) strtol(optarg, NULL, 0);
688 break;
689#endif
690 case 'N':
691 ctx->device_name = optarg;
692 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000693#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000694 case 's':
695 normalize_swapfs = 1;
696 case 'S':
697 swapfs = 1;
698 break;
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000699#else
700 case 's':
701 case 'S':
702 fprintf(stderr, _("Byte-swapping filesystems "
703 "not compiled in this version "
704 "of e2fsck\n"));
705 exit(1);
706#endif
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500707 case 'k':
708 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500709 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000710 default:
711 usage(ctx);
712 }
713 if (show_version_only)
714 return 0;
715 if (optind != argc - 1)
716 usage(ctx);
717 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400718 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000719 ctx->options |= E2F_OPT_READONLY;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500720 ctx->io_options = strchr(argv[optind], '?');
721 if (ctx->io_options)
722 *ctx->io_options++ = 0;
Theodore Ts'of3640932003-03-01 19:47:44 -0500723 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500724 if (!ctx->filesystem_name) {
725 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
726 argv[optind]);
727 fatal_error(ctx, 0);
728 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400729 if (extended_opts)
730 parse_extended_opts(ctx, extended_opts);
731
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000732 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500733 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000734 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000735 com_err("open", errno,
736 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000737 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000738 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000739 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000740 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000741 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000742 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000743 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000744 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000745 }
746 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000747 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000748#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000749 if (swapfs) {
750 if (cflag || bad_blocks_file) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000751 fprintf(stderr, _("Incompatible options not "
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500752 "allowed when byte-swapping.\n"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000753 exit(FSCK_USAGE);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000754 }
755 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000756#endif
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500757 if (cflag && bad_blocks_file) {
758 fprintf(stderr, _("The -c and the -l/-L options may "
759 "not be both used at the same time.\n"));
760 exit(FSCK_USAGE);
761 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000762#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000763 /*
764 * Set up signal action
765 */
766 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400767 sa.sa_handler = signal_cancel;
768 sigaction(SIGINT, &sa, 0);
769 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000770#ifdef SA_RESTART
771 sa.sa_flags = SA_RESTART;
772#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000773 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000774 sa.sa_handler = signal_progress_on;
775 sigaction(SIGUSR1, &sa, 0);
776 sa.sa_handler = signal_progress_off;
777 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000778#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400779
780 /* Update our PATH to include /sbin if we need to run badblocks */
781 if (cflag) {
782 char *oldpath = getenv("PATH");
783 if (oldpath) {
784 char *newpath;
785
786 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
787 strlen (oldpath));
788 if (!newpath)
789 fatal_error(ctx, "Couldn't malloc() newpath");
790 strcpy (newpath, PATH_SET);
791 strcat (newpath, ":");
792 strcat (newpath, oldpath);
793 putenv (newpath);
794 } else
795 putenv (PATH_SET);
796 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500797#ifdef CONFIG_JBD_DEBUG
798 if (getenv("E2FSCK_JBD_DEBUG"))
799 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
800#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000801 return 0;
802}
803
804static const char *my_ver_string = E2FSPROGS_VERSION;
805static const char *my_ver_date = E2FSPROGS_DATE;
806
807int main (int argc, char *argv[])
808{
809 errcode_t retval = 0;
810 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000811 ext2_filsys fs = 0;
812 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000813 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000814 const char *lib_ver_date;
815 int my_ver, lib_ver;
816 e2fsck_t ctx;
817 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000818 int flags, run_result;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000819
820 clear_problem_context(&pctx);
821#ifdef MTRACE
822 mtrace();
823#endif
824#ifdef MCHECK
825 mcheck(0);
826#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000827#ifdef ENABLE_NLS
828 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500829 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000830 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
831 textdomain(NLS_CAT_NAME);
832#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000833 my_ver = ext2fs_parse_version_string(my_ver_string);
834 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
835 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000836 fprintf( stderr, _("Error: ext2fs library version "
837 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000838 show_version_only++;
839 }
840
841 retval = PRS(argc, argv, &ctx);
842 if (retval) {
843 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000844 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +0000845 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000846 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000847 reserve_stdio_fds();
848
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000849#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000850 init_resource_track(&ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000851#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000852
853 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -0400854 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400855 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000856
857 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000858 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000859 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000860 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000861 }
862
863 check_mount(ctx);
864
865 if (!(ctx->options & E2F_OPT_PREEN) &&
866 !(ctx->options & E2F_OPT_NO) &&
867 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400868 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000869 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000870 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000871 }
872 ctx->superblock = ctx->use_superblock;
873restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400874#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000875 io_ptr = test_io_manager;
876 test_io_backing_manager = unix_io_manager;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400877#else
878 io_ptr = unix_io_manager;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000879#endif
Theodore Ts'o17390c02000-07-07 04:13:21 +0000880 flags = 0;
881 if ((ctx->options & E2F_OPT_READONLY) == 0)
882 flags |= EXT2_FLAG_RW;
883
Theodore Ts'of1a17612001-12-23 22:27:52 -0500884 if (ctx->superblock && ctx->blocksize) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500885 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
886 flags, ctx->superblock, ctx->blocksize,
887 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000888 } else if (ctx->superblock) {
Andreas Dilger932a4892002-05-16 03:20:07 -0600889 int blocksize;
890 for (blocksize = EXT2_MIN_BLOCK_SIZE;
891 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500892 retval = ext2fs_open2(ctx->filesystem_name,
893 ctx->io_options, flags,
894 ctx->superblock, blocksize,
895 io_ptr, &fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000896 if (!retval)
897 break;
898 }
899 } else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500900 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
901 flags, 0, 0, io_ptr, &fs);
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400902 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
903 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000904 ((retval == EXT2_ET_BAD_MAGIC) ||
905 ((retval == 0) && ext2fs_check_desc(fs)))) {
906 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000907 printf(_("%s trying backup blocks...\n"),
908 retval ? _("Couldn't find ext2 superblock,") :
909 _("Group descriptors look bad..."));
Theodore Ts'of1a17612001-12-23 22:27:52 -0500910 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000911 if (fs)
912 ext2fs_close(fs);
913 goto restart;
914 }
915 }
916 if (retval) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000917 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000918 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000919 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000920 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000921 "too high for this version of e2fsck.\n"
922 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000923 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +0000924 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
925 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000926 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000927 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000928 printf(_("You must have %s access to the "
929 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000930 (ctx->options & E2F_OPT_READONLY) ?
931 "r/o" : "r/w");
932 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000933 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000934#ifdef EROFS
935 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000936 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +0000937 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000938 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +0000939#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000940 else
941 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000942 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000943 }
944 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +0000945 fs->priv_data = ctx;
Theodore Ts'o8dceb922005-09-24 21:59:45 -0400946 fs->now = ctx->now;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000947 sb = fs->super;
948 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000949 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000950 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000951 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000952 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000953 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000954 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000955
Theodore Ts'o17390c02000-07-07 04:13:21 +0000956 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000957 * Set the device name, which is used whenever we print error
958 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +0000959 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000960 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000961 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -0500962 ctx->device_name = string_copy(ctx, sb->s_volume_name,
963 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +0000964 }
965 if (ctx->device_name == 0)
966 ctx->device_name = ctx->filesystem_name;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000967
968 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +0000969 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000970 */
971 retval = e2fsck_check_ext3_journal(ctx);
972 if (retval) {
973 com_err(ctx->program_name, retval,
974 _("while checking ext3 journal for %s"),
975 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000976 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +0000977 }
978
Theodore Ts'o9b565752000-12-13 18:50:22 +0000979 /*
980 * Check to see if we need to do ext3-style recovery. If so,
981 * do it, and then restart the fsck.
982 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000983 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +0000984 if (ctx->options & E2F_OPT_READONLY) {
985 printf(_("Warning: skipping journal recovery "
986 "because doing a read-only filesystem "
987 "check.\n"));
988 io_channel_flush(ctx->fs->io);
989 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -0500990 if (ctx->flags & E2F_FLAG_RESTARTED) {
991 /*
992 * Whoops, we attempted to run the
993 * journal twice. This should never
994 * happen, unless the hardware or
995 * device driver is being bogus.
996 */
997 com_err(ctx->program_name, 0,
998 _("unable to set superblock flags on %s\n"), ctx->device_name);
999 fatal_error(ctx, 0);
1000 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001001 retval = e2fsck_run_ext3_journal(ctx);
1002 if (retval) {
1003 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001004 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001005 ctx->device_name);
1006 fatal_error(ctx, 0);
1007 }
1008 ext2fs_close(ctx->fs);
1009 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001010 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001011 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001012 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001013 }
1014
1015 /*
1016 * Check for compatibility with the feature sets. We need to
1017 * be more stringent than ext2fs_open().
1018 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001019 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1020 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001021 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1022 "(%s)", ctx->device_name);
1023 goto get_newer;
1024 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001025 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001026 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1027 "(%s)", ctx->device_name);
1028 goto get_newer;
1029 }
1030#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001031 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001032 com_err(ctx->program_name, 0,
1033 _("Warning: compression support is experimental.\n"));
1034#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001035#ifndef ENABLE_HTREE
1036 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1037 com_err(ctx->program_name, 0,
1038 _("E2fsck not compiled with HTREE support,\n\t"
1039 "but filesystem %s has HTREE directories.\n"),
1040 ctx->device_name);
1041 goto get_newer;
1042 }
1043#endif
1044
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001045 /*
1046 * If the user specified a specific superblock, presumably the
1047 * master superblock has been trashed. So we mark the
1048 * superblock as dirty, so it can be written out.
1049 */
1050 if (ctx->superblock &&
1051 !(ctx->options & E2F_OPT_READONLY))
1052 ext2fs_mark_super_dirty(fs);
1053
1054 /*
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001055 * We only update the master superblock because (a) paranoia;
1056 * we don't want to corrupt the backup superblocks, and (b) we
1057 * don't need to update the mount count and last checked
1058 * fields in the backup superblock (the kernel doesn't
1059 * update the backup superblocks anyway).
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001060 */
1061 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1062
1063 ehandler_init(fs->io);
1064
1065 if (ctx->superblock)
1066 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
Theodore Ts'o550a4af2005-01-25 03:09:24 -05001067 ext2fs_mark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001068 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001069 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001070 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001071 check_if_skip(ctx);
1072 if (bad_blocks_file)
1073 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1074 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001075 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001076 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001077 fatal_error(ctx, 0);
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001078#ifdef ENABLE_SWAPFS
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001079 if (normalize_swapfs) {
1080 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1081 ext2fs_native_flag()) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001082 fprintf(stderr, _("%s: Filesystem byte order "
1083 "already normalized.\n"), ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001084 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001085 }
1086 }
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001087 if (swapfs) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001088 swap_filesys(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001089 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001090 fatal_error(ctx, 0);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001091 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +00001092#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001093
1094 /*
1095 * Mark the system as valid, 'til proven otherwise
1096 */
1097 ext2fs_mark_valid(fs);
1098
1099 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1100 if (retval) {
1101 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001102 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001103 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001104 printf(_("This doesn't bode well,"
1105 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001106 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001107
1108 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001109 e2fsck_clear_progbar(ctx);
Theodore Ts'o08b21301997-11-03 19:42:40 +00001110 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001111 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001112 retval = e2fsck_reset_context(ctx);
1113 if (retval) {
1114 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001115 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001116 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001117 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001118 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001119 goto restart;
1120 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001121 if (run_result & E2F_FLAG_CANCEL) {
1122 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1123 ctx->device_name : ctx->filesystem_name);
1124 exit_value |= FSCK_CANCELED;
1125 }
1126 if (run_result & E2F_FLAG_ABORT)
1127 fatal_error(ctx, _("aborted"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001128
1129#ifdef MTRACE
1130 mtrace_print("Cleanup");
1131#endif
1132 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001133 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001134 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001135 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001136 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001137 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001138 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001139 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001140 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001141 }
1142 }
Theodore Ts'oe70ae992002-09-28 09:16:28 -04001143 if (!ext2fs_test_valid(fs)) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001144 printf(_("\n%s: ********** WARNING: Filesystem still has "
1145 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001146 exit_value |= FSCK_UNCORRECTED;
1147 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001148 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001149 if (exit_value & FSCK_CANCELED)
1150 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001151 else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001152 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001153 if (!(ctx->options & E2F_OPT_READONLY)) {
1154 if (ext2fs_test_valid(fs)) {
1155 if (!(sb->s_state & EXT2_VALID_FS))
1156 exit_value |= FSCK_NONDESTRUCT;
1157 sb->s_state = EXT2_VALID_FS;
1158 } else
1159 sb->s_state &= ~EXT2_VALID_FS;
1160 sb->s_mnt_count = 0;
Theodore Ts'o1f3ad142005-04-14 14:07:53 -04001161 sb->s_lastcheck = ctx->now;
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001162 ext2fs_mark_super_dirty(fs);
1163 }
1164 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001165
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001166 e2fsck_write_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001167
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001168 ext2fs_close(fs);
1169 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001170 free(ctx->filesystem_name);
1171 free(ctx->journal_name);
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001172
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001173#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001174 if (ctx->options & E2F_OPT_TIME)
1175 print_resource_track(NULL, &ctx->global_rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +00001176#endif
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001177 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001178 return exit_value;
1179}