blob: bc18d41cfdb358a4532755dd68b9eaa496bf5003 [file] [log] [blame]
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001/*
2 * unix.c - The unix-specific code for e2fsck
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00004 * 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
Theodore Ts'oebabf2a2008-07-13 15:32:37 -040012#define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
13
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000014#include <stdio.h>
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18#include <string.h>
19#include <fcntl.h>
20#include <ctype.h>
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000021#include <time.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000022#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +000023#include <signal.h>
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +000024#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000025#ifdef HAVE_GETOPT_H
26#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000027#else
28extern char *optarg;
29extern int optind;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000030#endif
31#include <unistd.h>
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#ifdef HAVE_MNTENT_H
36#include <mntent.h>
37#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040038#ifdef HAVE_SYS_IOCTL_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000039#include <sys/ioctl.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040040#endif
Theodore Ts'oe71d8732003-03-14 02:13:48 -050041#ifdef HAVE_MALLOC_H
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000042#include <malloc.h>
Theodore Ts'oe71d8732003-03-14 02:13:48 -050043#endif
Theodore Ts'oc07f9f22004-04-12 00:16:44 -040044#ifdef HAVE_SYS_TYPES_H
45#include <sys/types.h>
46#endif
47#ifdef HAVE_DIRENT_H
48#include <dirent.h>
49#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000050
Andreas Dilger864b8d42008-08-24 20:37:39 -040051#include "e2p/e2p.h"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000052#include "et/com_err.h"
Eric Sandeen01c196b2008-03-29 23:01:52 -050053#include "e2p/e2p.h"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000054#include "e2fsck.h"
55#include "problem.h"
56#include "../version.h"
57
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000058/* Command line options */
Theodore Ts'o54a31a32003-08-19 10:08:34 -040059static int cflag; /* check disk */
60static int show_version_only;
61static int verbose;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000062
Theodore Ts'o54a31a32003-08-19 10:08:34 -040063static int replace_bad_blocks;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -050064static int keep_bad_blocks;
Theodore Ts'o54a31a32003-08-19 10:08:34 -040065static char *bad_blocks_file;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000066
Theodore Ts'o243dc312000-08-22 21:37:47 +000067e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
68
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -050069#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
70int journal_enable_debug = -1;
71#endif
72
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000073static void usage(e2fsck_t ctx)
74{
75 fprintf(stderr,
Theodore Ts'o1a855cb2007-08-10 23:58:56 -040076 _("Usage: %s [-panyrcdfvtDFV] [-b superblock] [-B blocksize]\n"
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000077 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -040078 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040079 "\t\t[-E extended-options] device\n"),
Theodore Ts'o5596def1999-07-19 15:27:37 +000080 ctx->program_name);
Theodore Ts'ob55199e1999-07-19 15:37:46 +000081
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000082 fprintf(stderr, _("\nEmergency help:\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000083 " -p Automatic repair (no questions)\n"
84 " -n Make no changes to the filesystem\n"
85 " -y Assume \"yes\" to all questions\n"
Theodore Ts'o19445ef2003-01-29 21:04:52 -050086 " -c Check for bad blocks and add them to the badblock list\n"
Theodore Ts'o53ef44c2001-01-06 05:55:58 +000087 " -f Force checking even if filesystem is marked clean\n"));
88 fprintf(stderr, _(""
Theodore Ts'ob55199e1999-07-19 15:37:46 +000089 " -v Be verbose\n"
90 " -b superblock Use alternative superblock\n"
91 " -B blocksize Force blocksize when looking for superblock\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -040092 " -j external_journal Set location of the external journal\n"
Theodore Ts'ob55199e1999-07-19 15:37:46 +000093 " -l bad_blocks_file Add to badblocks list\n"
94 " -L bad_blocks_file Set badblocks list\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000095 ));
Theodore Ts'ob55199e1999-07-19 15:37:46 +000096
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000097 exit(FSCK_USAGE);
98}
99
100static void show_stats(e2fsck_t ctx)
101{
102 ext2_filsys fs = ctx->fs;
Eric Sandeenf3358642006-09-12 14:56:17 -0400103 ext2_ino_t inodes, inodes_used;
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400104 blk64_t blocks, blocks_used;
105 unsigned int dir_links;
106 unsigned int num_files, num_links;
Theodore Ts'oce44d8c2008-12-08 21:33:11 -0500107 int frag_percent_file, frag_percent_dir, frag_percent_total;
Theodore Ts'o8da6d1a2008-08-14 09:48:07 -0400108 int i, j;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000109
110 dir_links = 2 * ctx->fs_directory_count - 1;
111 num_files = ctx->fs_total_count - dir_links;
112 num_links = ctx->fs_links_count - dir_links;
113 inodes = fs->super->s_inodes_count;
114 inodes_used = (fs->super->s_inodes_count -
115 fs->super->s_free_inodes_count);
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400116 blocks = ext2fs_blocks_count(fs->super);
117 blocks_used = (ext2fs_blocks_count(fs->super) -
118 ext2fs_free_blocks_count(fs->super));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000119
Theodore Ts'oce44d8c2008-12-08 21:33:11 -0500120 frag_percent_file = (10000 * ctx->fs_fragmented) / inodes_used;
121 frag_percent_file = (frag_percent_file + 5) / 10;
122
123 frag_percent_dir = (10000 * ctx->fs_fragmented_dir) / inodes_used;
124 frag_percent_dir = (frag_percent_dir + 5) / 10;
125
126 frag_percent_total = ((10000 * (ctx->fs_fragmented +
127 ctx->fs_fragmented_dir))
128 / inodes_used);
129 frag_percent_total = (frag_percent_total + 5) / 10;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400130
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000131 if (!verbose) {
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400132 printf(_("%s: %u/%u files (%0d.%d%% non-contiguous), %llu/%llu blocks\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000133 ctx->device_name, inodes_used, inodes,
Theodore Ts'oce44d8c2008-12-08 21:33:11 -0500134 frag_percent_total / 10, frag_percent_total % 10,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000135 blocks_used, blocks);
136 return;
137 }
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400138 printf (P_("\n%8u inode used (%2.2f%%)\n", "\n%8u inodes used (%2.2f%%)\n",
139 inodes_used), inodes_used, 100.0 * inodes_used / inodes);
Theodore Ts'oce44d8c2008-12-08 21:33:11 -0500140 printf (P_("%8u non-contiguous file (%0d.%d%%)\n",
141 "%8u non-contiguous files (%0d.%d%%)\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400142 ctx->fs_fragmented),
Theodore Ts'oce44d8c2008-12-08 21:33:11 -0500143 ctx->fs_fragmented, frag_percent_file / 10,
144 frag_percent_file % 10);
145 printf (P_("%8u non-contiguous directory (%0d.%d%%)\n",
146 "%8u non-contiguous directories (%0d.%d%%)\n",
147 ctx->fs_fragmented_dir),
148 ctx->fs_fragmented_dir, frag_percent_dir / 10,
149 frag_percent_dir % 10);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400150 printf (_(" # of inodes with ind/dind/tind blocks: %u/%u/%u\n"),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000151 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
Theodore Ts'o8da6d1a2008-08-14 09:48:07 -0400152
153 for (j=MAX_EXTENT_DEPTH_COUNT-1; j >=0; j--)
154 if (ctx->extent_depth_count[j])
155 break;
156 if (++j) {
157 printf (_(" Extent depth histogram: "));
158 for (i=0; i < j; i++) {
159 if (i)
160 fputc('/', stdout);
161 printf("%u", ctx->extent_depth_count[i]);
162 }
163 fputc('\n', stdout);
164 }
165
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400166 printf (P_("%8llu block used (%2.2f%%)\n",
167 "%8llu blocks used (%2.2f%%)\n",
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400168 blocks_used), blocks_used, 100.0 * blocks_used / blocks);
169 printf (P_("%8u bad block\n", "%8u bad blocks\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400170 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400171 printf (P_("%8u large file\n", "%8u large files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400172 ctx->large_files), ctx->large_files);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400173 printf (P_("\n%8u regular file\n", "\n%8u regular files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400174 ctx->fs_regular_count), ctx->fs_regular_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400175 printf (P_("%8u directory\n", "%8u directories\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400176 ctx->fs_directory_count), ctx->fs_directory_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400177 printf (P_("%8u character device file\n",
178 "%8u character device files\n", ctx->fs_chardev_count),
Theodore Ts'o113e4052003-05-17 21:00:46 -0400179 ctx->fs_chardev_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400180 printf (P_("%8u block device file\n", "%8u block device files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400181 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400182 printf (P_("%8u fifo\n", "%8u fifos\n", ctx->fs_fifo_count),
Theodore Ts'o113e4052003-05-17 21:00:46 -0400183 ctx->fs_fifo_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400184 printf (P_("%8u link\n", "%8u links\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400185 ctx->fs_links_count - dir_links),
186 ctx->fs_links_count - dir_links);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400187 printf (P_("%8u symbolic link", "%8u symbolic links",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400188 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400189 printf (P_(" (%u fast symbolic link)\n", " (%u fast symbolic links)\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400190 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400191 printf (P_("%8u socket\n", "%8u sockets\n", ctx->fs_sockets_count),
Theodore Ts'o113e4052003-05-17 21:00:46 -0400192 ctx->fs_sockets_count);
193 printf ("--------\n");
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400194 printf (P_("%8u file\n", "%8u files\n",
Theodore Ts'o113e4052003-05-17 21:00:46 -0400195 ctx->fs_total_count - dir_links),
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000196 ctx->fs_total_count - dir_links);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000197}
198
199static void check_mount(e2fsck_t ctx)
200{
201 errcode_t retval;
Theodore Ts'oee895132002-11-07 16:16:55 -0500202 int cont;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000203
Theodore Ts'oee895132002-11-07 16:16:55 -0500204 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
205 &ctx->mount_flags);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000206 if (retval) {
207 com_err("ext2fs_check_if_mount", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000208 _("while determining whether %s is mounted."),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000209 ctx->filesystem_name);
210 return;
211 }
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400212
213 /*
Theodore Ts'oae1182c2005-12-09 18:11:16 -0500214 * If the filesystem isn't mounted, or it's the root
215 * filesystem and it's mounted read-only, and we're not doing
216 * a read/write check, then everything's fine.
Theodore Ts'o83e6ac82001-07-30 16:29:52 -0400217 */
Theodore Ts'oee895132002-11-07 16:16:55 -0500218 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
219 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
Theodore Ts'oae1182c2005-12-09 18:11:16 -0500220 (ctx->mount_flags & EXT2_MF_READONLY) &&
221 !(ctx->options & E2F_OPT_WRITECHECK)))
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000222 return;
223
Theodore Ts'oae1182c2005-12-09 18:11:16 -0500224 if ((ctx->options & E2F_OPT_READONLY) &&
225 !(ctx->options & E2F_OPT_WRITECHECK)) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000226 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000227 return;
228 }
229
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000230 printf(_("%s is mounted. "), ctx->filesystem_name);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400231 if (!ctx->interactive)
Theodore Ts'o243dc312000-08-22 21:37:47 +0000232 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000233 printf(_("\n\n\007\007\007\007WARNING!!! "
Theodore Ts'o751265b2010-03-12 19:25:33 -0500234 "The filesystem is mounted. If you continue you ***WILL***\n"
235 "cause ***SEVERE*** filesystem damage.\007\007\007\n\n"));
Theodore Ts'o1760d162011-05-07 23:14:06 -0400236 cont = ask_yn(_("Do you really want to continue"), 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000237 if (!cont) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000238 printf (_("check aborted.\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000239 exit (0);
240 }
241 return;
242}
243
Theodore Ts'o54434922003-12-07 01:28:50 -0500244static int is_on_batt(void)
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500245{
246 FILE *f;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400247 DIR *d;
248 char tmp[80], tmp2[80], fname[80];
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500249 unsigned int acflag;
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400250 struct dirent* de;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500251
252 f = fopen("/proc/apm", "r");
253 if (f) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400254 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500255 acflag = 1;
256 fclose(f);
257 return (acflag != 1);
258 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400259 d = opendir("/proc/acpi/ac_adapter");
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500260 if (d) {
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500261 while ((de=readdir(d)) != NULL) {
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500262 if (!strncmp(".", de->d_name, 1))
263 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400264 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500265 de->d_name);
266 f = fopen(fname, "r");
267 if (!f)
268 continue;
269 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
270 tmp[0] = 0;
271 fclose(f);
272 if (strncmp(tmp, "off-line", 8) == 0) {
273 closedir(d);
274 return 1;
275 }
Theodore Ts'oc07f9f22004-04-12 00:16:44 -0400276 }
Matthias Andree4b137042005-01-13 03:35:29 +0100277 closedir(d);
Theodore Ts'oecd0d8f2005-01-17 13:59:18 -0500278 }
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500279 return 0;
280}
281
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000282/*
283 * This routine checks to see if a filesystem can be skipped; if so,
284 * it will exit with E2FSCK_OK. Under some conditions it will print a
285 * message explaining why a check is being forced.
286 */
287static void check_if_skip(e2fsck_t ctx)
288{
289 ext2_filsys fs = ctx->fs;
Theodore Ts'oa3efe482011-06-16 01:13:42 -0400290 struct problem_context pctx;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000291 const char *reason = NULL;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000292 unsigned int reason_arg = 0;
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500293 long next_check;
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500294 int batt = is_on_batt();
Theodore Ts'oa5f37a92006-01-29 05:15:36 -0500295 int defer_check_on_battery;
Theodore Ts'o177839e2010-05-13 17:36:36 -0400296 int broken_system_clock;
Theodore Ts'o60702c22007-09-22 20:42:04 -0400297 time_t lastcheck;
Theodore Ts'oa5f37a92006-01-29 05:15:36 -0500298
Theodore Ts'o177839e2010-05-13 17:36:36 -0400299 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
300 0, 0, &broken_system_clock);
301 if (ctx->flags & E2F_FLAG_TIME_INSANE)
302 broken_system_clock = 1;
Theodore Ts'oa5f37a92006-01-29 05:15:36 -0500303 profile_get_boolean(ctx->profile, "options",
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400304 "defer_check_on_battery", 0, 1,
Theodore Ts'oa5f37a92006-01-29 05:15:36 -0500305 &defer_check_on_battery);
306 if (!defer_check_on_battery)
307 batt = 0;
308
Theodore Ts'o1a855cb2007-08-10 23:58:56 -0400309 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || cflag)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000310 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400311
Bernd Schubert71873b12010-11-13 00:09:07 +0100312 if (ctx->options & E2F_OPT_JOURNAL_ONLY)
313 goto skip;
314
Theodore Ts'o60702c22007-09-22 20:42:04 -0400315 lastcheck = fs->super->s_lastcheck;
316 if (lastcheck > ctx->now)
317 lastcheck -= ctx->time_fudge;
Theodore Ts'o550a4af2005-01-25 03:09:24 -0500318 if ((fs->super->s_state & EXT2_ERROR_FS) ||
319 !ext2fs_test_valid(fs))
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000320 reason = _(" contains a file system with errors");
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000321 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000322 reason = _(" was not cleanly unmounted");
Theodore Ts'o0c37f452007-10-01 09:18:54 -0400323 else if (check_backup_super_block(ctx))
324 reason = _(" primary superblock features different from backup");
Theodore Ts'obc57f152001-04-26 04:11:46 +0000325 else if ((fs->super->s_max_mnt_count > 0) &&
Theodore Ts'o17390c02000-07-07 04:13:21 +0000326 (fs->super->s_mnt_count >=
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000327 (unsigned) fs->super->s_max_mnt_count)) {
328 reason = _(" has been mounted %u times without being checked");
329 reason_arg = fs->super->s_mnt_count;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400330 if (batt && (fs->super->s_mnt_count <
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500331 (unsigned) fs->super->s_max_mnt_count*2))
332 reason = 0;
Theodore Ts'o177839e2010-05-13 17:36:36 -0400333 } else if (!broken_system_clock && fs->super->s_checkinterval &&
334 (ctx->now < lastcheck)) {
Theodore Ts'o68eb0922008-06-07 12:19:08 -0400335 reason = _(" has filesystem last checked time in the future");
336 if (batt)
337 reason = 0;
Theodore Ts'o177839e2010-05-13 17:36:36 -0400338 } else if (!broken_system_clock && fs->super->s_checkinterval &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400339 ((ctx->now - lastcheck) >=
Theodore Ts'o2acad6b2008-06-07 11:04:10 -0400340 ((time_t) fs->super->s_checkinterval))) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000341 reason = _(" has gone %u days without being checked");
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400342 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400343 if (batt && ((ctx->now - fs->super->s_lastcheck) <
Theodore Ts'o54434922003-12-07 01:28:50 -0500344 fs->super->s_checkinterval*2))
Theodore Ts'o015b03d2003-11-21 11:02:22 -0500345 reason = 0;
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000346 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000347 if (reason) {
Theodore Ts'ob6a08072001-06-14 01:16:17 +0000348 fputs(ctx->device_name, stdout);
349 printf(reason, reason_arg);
350 fputs(_(", check forced.\n"), stdout);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000351 return;
352 }
Theodore Ts'oa3efe482011-06-16 01:13:42 -0400353
354 /*
355 * Update the global counts from the block group counts. This
356 * is needed since modern kernels don't update the global
357 * counts so as to avoid locking the entire file system. So
358 * if the filesystem is not unmounted cleanly, the global
359 * counts may not be accurate. Update them here if we can,
360 * for the benefit of users who might examine the file system
361 * using dumpe2fs. (This is for cosmetic reasons only.)
362 */
363 clear_problem_context(&pctx);
364 pctx.ino = fs->super->s_free_inodes_count;
365 pctx.ino2 = ctx->free_inodes;
366 if ((pctx.ino != pctx.ino2) &&
367 !(ctx->options & E2F_OPT_READONLY) &&
368 fix_problem(ctx, PR_0_FREE_INODE_COUNT, &pctx)) {
369 fs->super->s_free_inodes_count = ctx->free_inodes;
370 ext2fs_mark_super_dirty(fs);
371 }
372 clear_problem_context(&pctx);
373 pctx.blk = ext2fs_free_blocks_count(fs->super);
374 pctx.blk2 = ctx->free_blocks;
375 if ((pctx.blk != pctx.blk2) &&
376 !(ctx->options & E2F_OPT_READONLY) &&
377 fix_problem(ctx, PR_0_FREE_BLOCK_COUNT, &pctx)) {
378 ext2fs_free_blocks_count_set(fs->super, ctx->free_blocks);
379 ext2fs_mark_super_dirty(fs);
380 }
381
382 /* Print the summary message when we're skipping a full check */
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400383 printf(_("%s: clean, %u/%u files, %llu/%llu blocks"), ctx->device_name,
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000384 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
385 fs->super->s_inodes_count,
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400386 ext2fs_blocks_count(fs->super) -
387 ext2fs_free_blocks_count(fs->super),
388 ext2fs_blocks_count(fs->super));
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500389 next_check = 100000;
390 if (fs->super->s_max_mnt_count > 0) {
391 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400392 if (next_check <= 0)
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500393 next_check = 1;
394 }
Theodore Ts'o177839e2010-05-13 17:36:36 -0400395 if (!broken_system_clock && fs->super->s_checkinterval &&
Theodore Ts'o1f3ad142005-04-14 14:07:53 -0400396 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500397 next_check = 1;
398 if (next_check <= 5) {
Theodore Ts'obc3392c2006-01-29 05:05:31 -0500399 if (next_check == 1) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400400 if (batt)
Theodore Ts'obc3392c2006-01-29 05:05:31 -0500401 fputs(_(" (check deferred; on battery)"),
402 stdout);
403 else
404 fputs(_(" (check after next mount)"), stdout);
405 } else
Theodore Ts'o54434922003-12-07 01:28:50 -0500406 printf(_(" (check in %ld mounts)"), next_check);
Theodore Ts'o6de289c2003-11-21 10:54:54 -0500407 }
408 fputc('\n', stdout);
Bernd Schubert71873b12010-11-13 00:09:07 +0100409skip:
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000410 ext2fs_close(fs);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400411 ctx->fs = NULL;
412 e2fsck_free_context(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000413 exit(FSCK_OK);
414}
415
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000416/*
417 * For completion notice
418 */
Theodore Ts'o5596def1999-07-19 15:27:37 +0000419struct percent_tbl {
420 int max_pass;
421 int table[32];
422};
423struct percent_tbl e2fsck_tbl = {
424 5, { 0, 70, 90, 92, 95, 100 }
425};
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400426static char bar[128], spaces[128];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000427
428static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
429 int max)
430{
431 float percent;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400432
Theodore Ts'o5596def1999-07-19 15:27:37 +0000433 if (pass <= 0)
434 return 0.0;
Theodore Ts'ob8d164c2000-08-08 03:17:04 +0000435 if (pass > tbl->max_pass || max == 0)
Theodore Ts'o5596def1999-07-19 15:27:37 +0000436 return 100.0;
437 percent = ((float) curr) / ((float) max);
438 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
439 + tbl->table[pass-1]);
440}
441
442extern void e2fsck_clear_progbar(e2fsck_t ctx)
443{
444 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
445 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400446
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400447 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
448 ctx->stop_meta);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400449 fflush(stdout);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000450 ctx->flags &= ~E2F_FLAG_PROG_BAR;
451}
452
Theodore Ts'o520ead32003-04-19 13:48:27 -0400453int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500454 unsigned int dpynum)
455{
456 static const char spinner[] = "\\|/-";
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500457 int i;
Theodore Ts'o54434922003-12-07 01:28:50 -0500458 unsigned int tick;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500459 struct timeval tv;
460 int dpywidth;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500461 int fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500462
463 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
464 return 0;
465
466 /*
467 * Calculate the new progress position. If the
468 * percentage hasn't changed, then we skip out right
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400469 * away.
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500470 */
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500471 fixed_percent = (int) ((10 * percent) + 0.5);
472 if (ctx->progress_last_percent == fixed_percent)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500473 return 0;
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500474 ctx->progress_last_percent = fixed_percent;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500475
476 /*
477 * If we've already updated the spinner once within
478 * the last 1/8th of a second, no point doing it
479 * again.
480 */
481 gettimeofday(&tv, NULL);
482 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
483 if ((tick == ctx->progress_last_time) &&
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500484 (fixed_percent != 0) && (fixed_percent != 1000))
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500485 return 0;
486 ctx->progress_last_time = tick;
487
488 /*
489 * Advance the spinner, and note that the progress bar
490 * will be on the screen
491 */
492 ctx->progress_pos = (ctx->progress_pos+1) & 3;
493 ctx->flags |= E2F_FLAG_PROG_BAR;
494
495 dpywidth = 66 - strlen(label);
496 dpywidth = 8 * (dpywidth / 8);
497 if (dpynum)
498 dpywidth -= 8;
499
500 i = ((percent * dpywidth) + 50) / 100;
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400501 printf("%s%s: |%s%s", ctx->start_meta, label,
502 bar + (sizeof(bar) - (i+1)),
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500503 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500504 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500505 fputc('|', stdout);
506 else
507 fputc(spinner[ctx->progress_pos & 3], stdout);
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500508 printf(" %4.1f%% ", percent);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500509 if (dpynum)
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500510 printf("%u\r", dpynum);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500511 else
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500512 fputs(" \r", stdout);
513 fputs(ctx->stop_meta, stdout);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400514
Theodore Ts'o9214dcc2005-01-19 13:57:40 -0500515 if (fixed_percent == 1000)
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500516 e2fsck_clear_progbar(ctx);
517 fflush(stdout);
518
519 return 0;
520}
521
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000522static int e2fsck_update_progress(e2fsck_t ctx, int pass,
523 unsigned long cur, unsigned long max)
524{
Theodore Ts'o1dc506c2008-03-26 09:09:09 -0400525 char buf[1024];
Theodore Ts'o5596def1999-07-19 15:27:37 +0000526 float percent;
Theodore Ts'of75c28d1998-08-01 04:18:06 +0000527
528 if (pass == 0)
529 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400530
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000531 if (ctx->progress_fd) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400532 snprintf(buf, sizeof(buf), "%d %lu %lu %s\n",
Theodore Ts'o1dc506c2008-03-26 09:09:09 -0400533 pass, cur, max, ctx->device_name);
Theodore Ts'ob0258cb2009-04-22 15:09:41 -0400534 write_all(ctx->progress_fd, buf, strlen(buf));
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000535 } else {
Theodore Ts'oe4c8e882000-07-05 23:54:46 +0000536 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500537 e2fsck_simple_progress(ctx, ctx->device_name,
538 percent, 0);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000539 }
540 return 0;
541}
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000542
543#define PATH_SET "PATH=/sbin"
544
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000545static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000546{
547 int fd;
548
549 while (1) {
550 fd = open("/dev/null", O_RDWR);
551 if (fd > 2)
552 break;
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000553 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000554 fprintf(stderr, _("ERROR: Couldn't open "
555 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000556 strerror(errno));
557 break;
558 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000559 }
560 close(fd);
561}
562
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000563#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500564static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000565{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000566 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000567
568 if (!ctx)
569 return;
570
571 ctx->progress = e2fsck_update_progress;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000572}
573
Theodore Ts'o54434922003-12-07 01:28:50 -0500574static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000575{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000576 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000577
578 if (!ctx)
579 return;
580
581 e2fsck_clear_progbar(ctx);
582 ctx->progress = 0;
583}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400584
Theodore Ts'o54434922003-12-07 01:28:50 -0500585static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400586{
587 e2fsck_t ctx = e2fsck_global_ctx;
588
589 if (!ctx)
590 exit(FSCK_CANCELED);
591
592 ctx->flags |= E2F_FLAG_CANCEL;
593}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000594#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000595
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400596static void parse_extended_opts(e2fsck_t ctx, const char *opts)
597{
598 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500599 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400600 int extended_usage = 0;
601
Theodore Ts'of3640932003-03-01 19:47:44 -0500602 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400603 for (token = buf; token && *token; token = next) {
604 p = strchr(token, ',');
605 next = 0;
606 if (p) {
607 *p = 0;
608 next = p+1;
Brian Behlendorfcae542c2007-03-28 11:28:24 -0400609 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400610 arg = strchr(token, '=');
611 if (arg) {
612 *arg = 0;
613 arg++;
614 }
615 if (strcmp(token, "ea_ver") == 0) {
616 if (!arg) {
617 extended_usage++;
618 continue;
619 }
620 ea_ver = strtoul(arg, &p, 0);
621 if (*p ||
622 ((ea_ver != 1) && (ea_ver != 2))) {
623 fprintf(stderr,
624 _("Invalid EA version.\n"));
625 extended_usage++;
626 continue;
627 }
628 ctx->ext_attr_ver = ea_ver;
Theodore Ts'o63b5e352008-08-10 22:43:24 -0400629 } else if (strcmp(token, "fragcheck") == 0) {
630 ctx->options |= E2F_OPT_FRAGCHECK;
631 continue;
Bernd Schubert71873b12010-11-13 00:09:07 +0100632 } else if (strcmp(token, "journal_only") == 0) {
633 if (arg) {
634 extended_usage++;
635 continue;
636 }
637 ctx->options |= E2F_OPT_JOURNAL_ONLY;
Lukas Czernerefa1a352010-11-18 03:38:38 +0000638 } else if (strcmp(token, "discard") == 0) {
639 ctx->options |= E2F_OPT_DISCARD;
640 continue;
641 } else if (strcmp(token, "nodiscard") == 0) {
642 ctx->options &= ~E2F_OPT_DISCARD;
643 continue;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400644 } else {
645 fprintf(stderr, _("Unknown extended option: %s\n"),
646 token);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400647 extended_usage++;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400648 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400649 }
Brian Behlendorfcae542c2007-03-28 11:28:24 -0400650 free(buf);
651
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400652 if (extended_usage) {
Theodore Ts'obb145b02005-06-20 08:35:27 -0400653 fputs(("\nExtended options are separated by commas, "
654 "and may take an argument which\n"
655 "is set off by an equals ('=') sign. "
Theodore Ts'o63b5e352008-08-10 22:43:24 -0400656 "Valid extended options are:\n"), stderr);
657 fputs(("\tea_ver=<ea_version (1 or 2)>\n"), stderr);
658 fputs(("\tfragcheck\n"), stderr);
Bernd Schubert71873b12010-11-13 00:09:07 +0100659 fputs(("\tjournal_only\n"), stderr);
Lukas Czernerefa1a352010-11-18 03:38:38 +0000660 fputs(("\tdiscard\n"), stderr);
661 fputs(("\tnodiscard\n"), stderr);
Theodore Ts'o63b5e352008-08-10 22:43:24 -0400662 fputc('\n', stderr);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400663 exit(1);
664 }
Brian Behlendorfcae542c2007-03-28 11:28:24 -0400665}
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400666
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500667static void syntax_err_report(const char *filename, long err, int line_num)
668{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400669 fprintf(stderr,
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500670 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
671 filename, line_num, error_message(err));
672 exit(FSCK_ERROR);
673}
674
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200675static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400676
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000677static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
678{
679 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000680 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000681#ifdef MTRACE
682 extern void *mallwatch;
683#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000684 e2fsck_t ctx;
685 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000686#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000687 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000688#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400689 char *extended_opts = 0;
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500690 char *cp;
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400691 int res; /* result of sscanf */
692#ifdef CONFIG_JBD_DEBUG
693 char *jbd_debug;
694#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000695
696 retval = e2fsck_allocate_context(&ctx);
697 if (retval)
698 return retval;
699
700 *ret_ctx = ctx;
701
Theodore Ts'o908b7852003-04-16 15:20:13 -0400702 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
703 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400704 if (isatty(0) && isatty(1)) {
705 ctx->interactive = 1;
706 } else {
707 ctx->start_meta[0] = '\001';
708 ctx->stop_meta[0] = '\002';
709 }
710 memset(bar, '=', sizeof(bar)-1);
711 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500712 add_error_table(&et_ext2_error_table);
713 add_error_table(&et_prof_error_table);
Theodore Ts'of3640932003-03-01 19:47:44 -0500714 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400715
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000716 if (argc && *argv)
717 ctx->program_name = *argv;
718 else
719 ctx->program_name = "e2fsck";
Lukas Czernerefa1a352010-11-18 03:38:38 +0000720
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500721 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 +0000722 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000723 case 'C':
724 ctx->progress = e2fsck_update_progress;
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400725 res = sscanf(optarg, "%d", &ctx->progress_fd);
726 if (res != 1)
727 goto sscanf_err;
728
Theodore Ts'obe625232008-03-26 08:26:01 -0400729 if (ctx->progress_fd < 0) {
730 ctx->progress = 0;
731 ctx->progress_fd = ctx->progress_fd * -1;
732 }
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000733 if (!ctx->progress_fd)
734 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000735 /* Validate the file descriptor to avoid disasters */
736 fd = dup(ctx->progress_fd);
737 if (fd < 0) {
738 fprintf(stderr,
739 _("Error validating file descriptor %d: %s\n"),
740 ctx->progress_fd,
741 error_message(errno));
742 fatal_error(ctx,
743 _("Invalid completion information file descriptor"));
744 } else
745 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000746 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400747 case 'D':
748 ctx->options |= E2F_OPT_COMPRESS_DIRS;
749 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400750 case 'E':
751 extended_opts = optarg;
752 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000753 case 'p':
754 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500755 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
756 conflict_opt:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400757 fatal_error(ctx,
Theodore Ts'of4b6d2a2005-12-09 17:31:08 -0500758 _("Only one of the options -p/-a, -n or -y may be specified."));
Theodore Ts'o8161a742003-01-02 16:36:44 -0500759 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000760 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000761 break;
762 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500763 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
764 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000765 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000766 break;
767 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500768 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
769 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000770 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000771 break;
772 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000773#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000774 if (ctx->options & E2F_OPT_TIME)
775 ctx->options |= E2F_OPT_TIME2;
776 else
777 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000778#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000779 fprintf(stderr, _("The -t option is not "
780 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000781#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000782 break;
783 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500784 if (cflag++)
785 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000786 ctx->options |= E2F_OPT_CHECKBLOCKS;
787 break;
788 case 'r':
789 /* What we do by default, anyway! */
790 break;
791 case 'b':
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400792 res = sscanf(optarg, "%llu", &ctx->use_superblock);
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400793 if (res != 1)
794 goto sscanf_err;
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400795 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000796 break;
797 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500798 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000799 break;
800 case 'I':
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400801 res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
802 if (res != 1)
803 goto sscanf_err;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000804 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400805 case 'j':
Theodore Ts'ob95ca922010-12-06 17:07:27 -0500806 ctx->journal_name = blkid_get_devname(ctx->blkid,
807 optarg, NULL);
808 if (!ctx->journal_name) {
809 com_err(ctx->program_name, 0,
810 _("Unable to resolve '%s'"),
811 optarg);
812 fatal_error(ctx, 0);
813 }
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400814 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000815 case 'P':
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400816 res = sscanf(optarg, "%d", &ctx->process_inode_size);
817 if (res != 1)
818 goto sscanf_err;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000819 break;
820 case 'L':
821 replace_bad_blocks++;
822 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500823 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000824 break;
825 case 'd':
826 ctx->options |= E2F_OPT_DEBUG;
827 break;
828 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500829 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000830 break;
831 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000832 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000833 break;
834 case 'v':
835 verbose = 1;
836 break;
837 case 'V':
838 show_version_only = 1;
839 break;
840#ifdef MTRACE
841 case 'M':
842 mallwatch = (void *) strtol(optarg, NULL, 0);
843 break;
844#endif
845 case 'N':
Theodore Ts'oa2447f82008-03-29 14:46:48 -0400846 ctx->device_name = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000847 break;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500848 case 'k':
849 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500850 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000851 default:
852 usage(ctx);
853 }
854 if (show_version_only)
855 return 0;
856 if (optind != argc - 1)
857 usage(ctx);
Theodore Ts'o298c9c22010-03-12 19:18:20 -0500858 if ((ctx->options & E2F_OPT_NO) &&
859 (ctx->options & E2F_OPT_COMPRESS_DIRS)) {
860 com_err(ctx->program_name, 0,
861 _("The -n and -D options are incompatible."));
862 fatal_error(ctx, 0);
863 }
864 if ((ctx->options & E2F_OPT_NO) && cflag) {
865 com_err(ctx->program_name, 0,
866 _("The -n and -c options are incompatible."));
867 fatal_error(ctx, 0);
868 }
869 if ((ctx->options & E2F_OPT_NO) && bad_blocks_file) {
870 com_err(ctx->program_name, 0,
871 _("The -n and -l/-L options are incompatible."));
872 fatal_error(ctx, 0);
873 }
874 if (ctx->options & E2F_OPT_NO)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000875 ctx->options |= E2F_OPT_READONLY;
Jose R. Santos49a73602007-10-21 21:04:03 -0500876
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500877 ctx->io_options = strchr(argv[optind], '?');
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400878 if (ctx->io_options)
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500879 *ctx->io_options++ = 0;
Theodore Ts'of3640932003-03-01 19:47:44 -0500880 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500881 if (!ctx->filesystem_name) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400882 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500883 argv[optind]);
884 fatal_error(ctx, 0);
885 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400886 if (extended_opts)
887 parse_extended_opts(ctx, extended_opts);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500888
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500889 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
890 config_fn[0] = cp;
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500891 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500892 profile_init(config_fn, &ctx->profile);
893
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000894 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500895 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000896 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000897 com_err("open", errno,
898 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000899 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000900 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000901 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000902 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000903 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000904 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000905 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000906 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000907 }
908 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000909 }
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500910 if (cflag && bad_blocks_file) {
911 fprintf(stderr, _("The -c and the -l/-L options may "
912 "not be both used at the same time.\n"));
913 exit(FSCK_USAGE);
914 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000915#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000916 /*
917 * Set up signal action
918 */
919 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400920 sa.sa_handler = signal_cancel;
921 sigaction(SIGINT, &sa, 0);
922 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000923#ifdef SA_RESTART
924 sa.sa_flags = SA_RESTART;
925#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000926 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000927 sa.sa_handler = signal_progress_on;
928 sigaction(SIGUSR1, &sa, 0);
929 sa.sa_handler = signal_progress_off;
930 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000931#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400932
933 /* Update our PATH to include /sbin if we need to run badblocks */
934 if (cflag) {
935 char *oldpath = getenv("PATH");
Theodore Ts'o642935c2006-11-14 23:38:17 -0500936 char *newpath;
937 int len = sizeof(PATH_SET) + 1;
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400938
Theodore Ts'o642935c2006-11-14 23:38:17 -0500939 if (oldpath)
940 len += strlen(oldpath);
941
942 newpath = malloc(len);
943 if (!newpath)
944 fatal_error(ctx, "Couldn't malloc() newpath");
945 strcpy(newpath, PATH_SET);
946
947 if (oldpath) {
948 strcat(newpath, ":");
949 strcat(newpath, oldpath);
950 }
951 putenv(newpath);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400952 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500953#ifdef CONFIG_JBD_DEBUG
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400954 jbd_debug = getenv("E2FSCK_JBD_DEBUG");
Theodore Ts'o414025e2008-01-31 14:22:24 -0500955 if (jbd_debug) {
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400956 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
957 if (res != 1) {
958 fprintf(stderr,
959 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
960 jbd_debug);
961 exit (1);
962 }
Theodore Ts'o414025e2008-01-31 14:22:24 -0500963 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500964#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000965 return 0;
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400966
967sscanf_err:
968 fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
969 c, optarg);
970 exit (1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000971}
972
Theodore Ts'o60663892009-05-28 23:40:18 -0400973static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr,
974 ext2_filsys *ret_fs)
975{
976 errcode_t retval;
977
978 *ret_fs = NULL;
979 if (ctx->superblock && ctx->blocksize) {
980 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
981 flags, ctx->superblock, ctx->blocksize,
982 io_ptr, ret_fs);
983 } else if (ctx->superblock) {
984 int blocksize;
985 for (blocksize = EXT2_MIN_BLOCK_SIZE;
986 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
987 if (*ret_fs) {
988 ext2fs_free(*ret_fs);
989 *ret_fs = NULL;
990 }
991 retval = ext2fs_open2(ctx->filesystem_name,
992 ctx->io_options, flags,
993 ctx->superblock, blocksize,
994 io_ptr, ret_fs);
995 if (!retval)
996 break;
997 }
998 } else
999 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1000 flags, 0, 0, io_ptr, ret_fs);
1001 return retval;
1002}
1003
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001004static const char *my_ver_string = E2FSPROGS_VERSION;
1005static const char *my_ver_date = E2FSPROGS_DATE;
Jose R. Santos49a73602007-10-21 21:04:03 -05001006
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001007int main (int argc, char *argv[])
1008{
Theodore Ts'o82b59ca2009-09-01 20:01:38 -04001009 errcode_t retval = 0, retval2 = 0, orig_retval = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001010 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001011 ext2_filsys fs = 0;
1012 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001013 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001014 const char *lib_ver_date;
1015 int my_ver, lib_ver;
1016 e2fsck_t ctx;
Theodore Ts'o60663892009-05-28 23:40:18 -04001017 blk_t orig_superblock;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001018 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +00001019 int flags, run_result;
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001020 int journal_size;
Theodore Ts'o9f0288d2007-08-03 20:43:37 -04001021 int sysval, sys_page_size = 4096;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -04001022 int old_bitmaps;
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001023 __u32 features[3];
Theodore Ts'o1dc506c2008-03-26 09:09:09 -04001024 char *cp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001025
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001026 clear_problem_context(&pctx);
Theodore Ts'o9b3018a2011-08-11 14:56:49 -04001027 sigcatcher_setup();
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001028#ifdef MTRACE
1029 mtrace();
1030#endif
1031#ifdef MCHECK
1032 mcheck(0);
1033#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001034#ifdef ENABLE_NLS
1035 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001036 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001037 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1038 textdomain(NLS_CAT_NAME);
1039#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001040 my_ver = ext2fs_parse_version_string(my_ver_string);
1041 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
1042 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001043 fprintf( stderr, _("Error: ext2fs library version "
1044 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001045 show_version_only++;
1046 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001047
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001048 retval = PRS(argc, argv, &ctx);
1049 if (retval) {
1050 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001051 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001052 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001053 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +00001054 reserve_stdio_fds();
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001055
Theodore Ts'o6d96b002007-08-03 20:07:09 -04001056 init_resource_track(&ctx->global_rtrack, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001057 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -04001058 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001059 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001060
1061 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001062 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001063 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001064 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001065 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001066
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001067 check_mount(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001068
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001069 if (!(ctx->options & E2F_OPT_PREEN) &&
1070 !(ctx->options & E2F_OPT_NO) &&
1071 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -04001072 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001073 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001074 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001075 }
1076 ctx->superblock = ctx->use_superblock;
1077restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -04001078#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001079 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1080 io_ptr = test_io_manager;
1081 test_io_backing_manager = unix_io_manager;
1082 } else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001083#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001084 io_ptr = unix_io_manager;
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001085 flags = EXT2_FLAG_NOFREE_ON_ERROR;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -04001086 profile_get_boolean(ctx->profile, "options", "old_bitmaps", 0, 0,
1087 &old_bitmaps);
1088 if (!old_bitmaps)
1089 flags |= EXT2_FLAG_64BITS;
Theodore Ts'o17390c02000-07-07 04:13:21 +00001090 if ((ctx->options & E2F_OPT_READONLY) == 0)
1091 flags |= EXT2_FLAG_RW;
Theodore Ts'o2e14e0c2006-03-18 20:01:09 -05001092 if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
1093 flags |= EXT2_FLAG_EXCLUSIVE;
Theodore Ts'o17390c02000-07-07 04:13:21 +00001094
Theodore Ts'o60663892009-05-28 23:40:18 -04001095 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1096
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -04001097 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
1098 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001099 ((retval == EXT2_ET_BAD_MAGIC) ||
Theodore Ts'ocd538082007-09-07 16:54:11 -04001100 (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
Theodore Ts'o82b59ca2009-09-01 20:01:38 -04001101 ((retval == 0) && (retval2 = ext2fs_check_desc(fs))))) {
1102 if (retval2 == ENOMEM) {
1103 retval = retval2;
1104 goto failure;
1105 }
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001106 if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) {
1107 ext2fs_free(fs);
1108 fs = NULL;
1109 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001110 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'ocd538082007-09-07 16:54:11 -04001111 printf(_("%s: %s trying backup blocks...\n"),
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001112 ctx->program_name,
Theodore Ts'ocd538082007-09-07 16:54:11 -04001113 retval ? _("Superblock invalid,") :
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001114 _("Group descriptors look bad..."));
Theodore Ts'o60663892009-05-28 23:40:18 -04001115 orig_superblock = ctx->superblock;
Theodore Ts'of1a17612001-12-23 22:27:52 -05001116 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001117 if (fs)
1118 ext2fs_close(fs);
Theodore Ts'ocd538082007-09-07 16:54:11 -04001119 orig_retval = retval;
Theodore Ts'o60663892009-05-28 23:40:18 -04001120 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1121 if ((orig_retval == 0) && retval != 0) {
Theodore Ts'o22ff06d2010-07-05 15:06:37 -04001122 if (fs)
1123 ext2fs_close(fs);
Theodore Ts'o60663892009-05-28 23:40:18 -04001124 com_err(ctx->program_name, retval,
1125 "when using the backup blocks");
1126 printf(_("%s: going back to original "
1127 "superblock\n"), ctx->program_name);
1128 ctx->superblock = orig_superblock;
1129 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1130 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001131 }
1132 }
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001133 if (((retval == EXT2_ET_UNSUPP_FEATURE) ||
1134 (retval == EXT2_ET_RO_UNSUPP_FEATURE)) &&
1135 fs && fs->super) {
1136 sb = fs->super;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001137 features[0] = (sb->s_feature_compat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001138 ~EXT2_LIB_FEATURE_COMPAT_SUPP);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001139 features[1] = (sb->s_feature_incompat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001140 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001141 features[2] = (sb->s_feature_ro_compat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001142 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1143 if (features[0] || features[1] || features[2])
1144 goto print_unsupp_features;
1145 }
Theodore Ts'o82b59ca2009-09-01 20:01:38 -04001146failure:
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001147 if (retval) {
Theodore Ts'ocd538082007-09-07 16:54:11 -04001148 if (orig_retval)
1149 retval = orig_retval;
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001150 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001151 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +00001152 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001153 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +00001154 "too high for this version of e2fsck.\n"
1155 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001156 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +00001157 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1158 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001159 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001160 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001161 printf(_("You must have %s access to the "
1162 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001163 (ctx->options & E2F_OPT_READONLY) ?
1164 "r/o" : "r/w");
1165 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001166 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o2e14e0c2006-03-18 20:01:09 -05001167 else if (retval == EBUSY)
1168 printf(_("Filesystem mounted or opened exclusively "
1169 "by another program?\n"));
Jan Kara32f66cc2010-09-08 16:12:08 +02001170 else if (retval == ENOENT)
1171 printf(_("Possibly non-existent device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +00001172#ifdef EROFS
1173 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001174 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +00001175 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001176 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +00001177#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001178 else
1179 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001180 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001181 }
Theodore Ts'o058ad1c2007-06-18 18:26:50 -04001182 /*
1183 * We only update the master superblock because (a) paranoia;
1184 * we don't want to corrupt the backup superblocks, and (b) we
1185 * don't need to update the mount count and last checked
1186 * fields in the backup superblock (the kernel doesn't update
1187 * the backup superblocks anyway). With newer versions of the
1188 * library this flag is set by ext2fs_open2(), but we set this
1189 * here just to be sure. (No, we don't support e2fsck running
1190 * with some other libext2fs than the one that it was shipped
1191 * with, but just in case....)
1192 */
1193 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001194
1195 if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1196 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1197 int need_restart = 0;
1198
Theodore Ts'o59119642010-03-14 19:26:48 -04001199 pctx.errcode = ext2fs_get_device_size2(ctx->filesystem_name,
1200 blocksize,
1201 &ctx->num_blocks);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001202 /*
1203 * The floppy driver refuses to allow anyone else to
1204 * open the device if has been opened with O_EXCL;
1205 * this is unlike other block device drivers in Linux.
1206 * To handle this, we close the filesystem and then
1207 * reopen the filesystem after we get the device size.
1208 */
1209 if (pctx.errcode == EBUSY) {
1210 ext2fs_close(fs);
1211 need_restart++;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001212 pctx.errcode =
Theodore Ts'o59119642010-03-14 19:26:48 -04001213 ext2fs_get_device_size2(ctx->filesystem_name,
1214 blocksize,
1215 &ctx->num_blocks);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001216 }
1217 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1218 ctx->num_blocks = 0;
1219 else if (pctx.errcode) {
1220 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1221 ctx->flags |= E2F_FLAG_ABORT;
1222 fatal_error(ctx, 0);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001223 }
1224 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1225 if (need_restart)
1226 goto restart;
1227 }
1228
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001229 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +00001230 fs->priv_data = ctx;
Theodore Ts'o8dceb922005-09-24 21:59:45 -04001231 fs->now = ctx->now;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001232 sb = fs->super;
1233 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001234 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001235 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001236 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001237 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001238 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001239 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001240
Theodore Ts'o17390c02000-07-07 04:13:21 +00001241 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001242 * Set the device name, which is used whenever we print error
1243 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +00001244 */
Theodore Ts'o5596def1999-07-19 15:27:37 +00001245 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001246 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -05001247 ctx->device_name = string_copy(ctx, sb->s_volume_name,
1248 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +00001249 }
1250 if (ctx->device_name == 0)
Theodore Ts'o1dc506c2008-03-26 09:09:09 -04001251 ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
1252 for (cp = ctx->device_name; *cp; cp++)
1253 if (isspace(*cp) || *cp == ':')
1254 *cp = '_';
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001255
Andreas Dilger14c5af32009-05-17 23:03:04 -04001256 ehandler_init(fs->io);
1257
Theodore Ts'o47c1b8e2009-04-28 09:09:44 -04001258 if ((ctx->mount_flags & EXT2_MF_MOUNTED) &&
1259 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER))
1260 goto skip_journal;
1261
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001262 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +00001263 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001264 */
1265 retval = e2fsck_check_ext3_journal(ctx);
1266 if (retval) {
1267 com_err(ctx->program_name, retval,
1268 _("while checking ext3 journal for %s"),
1269 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001270 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001271 }
1272
Theodore Ts'o9b565752000-12-13 18:50:22 +00001273 /*
1274 * Check to see if we need to do ext3-style recovery. If so,
1275 * do it, and then restart the fsck.
1276 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001277 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001278 if (ctx->options & E2F_OPT_READONLY) {
1279 printf(_("Warning: skipping journal recovery "
1280 "because doing a read-only filesystem "
1281 "check.\n"));
1282 io_channel_flush(ctx->fs->io);
1283 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001284 if (ctx->flags & E2F_FLAG_RESTARTED) {
1285 /*
1286 * Whoops, we attempted to run the
1287 * journal twice. This should never
1288 * happen, unless the hardware or
1289 * device driver is being bogus.
1290 */
1291 com_err(ctx->program_name, 0,
1292 _("unable to set superblock flags on %s\n"), ctx->device_name);
1293 fatal_error(ctx, 0);
1294 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001295 retval = e2fsck_run_ext3_journal(ctx);
1296 if (retval) {
1297 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001298 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001299 ctx->device_name);
1300 fatal_error(ctx, 0);
1301 }
1302 ext2fs_close(ctx->fs);
1303 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001304 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001305 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001306 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001307 }
1308
Theodore Ts'o47c1b8e2009-04-28 09:09:44 -04001309skip_journal:
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001310 /*
1311 * Check for compatibility with the feature sets. We need to
1312 * be more stringent than ext2fs_open().
1313 */
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001314 features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP;
1315 features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001316 features[2] = (sb->s_feature_ro_compat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001317 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1318print_unsupp_features:
1319 if (features[0] || features[1] || features[2]) {
1320 int i, j;
1321 __u32 *mask = features, m;
1322
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001323 fprintf(stderr, _("%s has unsupported feature(s):"),
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001324 ctx->filesystem_name);
1325
1326 for (i=0; i <3; i++,mask++) {
1327 for (j=0,m=1; j < 32; j++, m<<=1) {
1328 if (*mask & m)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001329 fprintf(stderr, " %s",
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001330 e2p_feature2string(i, m));
1331 }
1332 }
1333 putc('\n', stderr);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001334 goto get_newer;
1335 }
1336#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001337 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001338 com_err(ctx->program_name, 0,
1339 _("Warning: compression support is experimental.\n"));
1340#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001341#ifndef ENABLE_HTREE
1342 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1343 com_err(ctx->program_name, 0,
1344 _("E2fsck not compiled with HTREE support,\n\t"
1345 "but filesystem %s has HTREE directories.\n"),
1346 ctx->device_name);
1347 goto get_newer;
1348 }
1349#endif
1350
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001351 /*
1352 * If the user specified a specific superblock, presumably the
1353 * master superblock has been trashed. So we mark the
1354 * superblock as dirty, so it can be written out.
1355 */
1356 if (ctx->superblock &&
1357 !(ctx->options & E2F_OPT_READONLY))
1358 ext2fs_mark_super_dirty(fs);
1359
Theodore Ts'o9f0288d2007-08-03 20:43:37 -04001360 /*
1361 * Calculate the number of filesystem blocks per pagesize. If
1362 * fs->blocksize > page_size, set the number of blocks per
1363 * pagesize to 1 to avoid division by zero errors.
1364 */
1365#ifdef _SC_PAGESIZE
1366 sysval = sysconf(_SC_PAGESIZE);
1367 if (sysval > 0)
1368 sys_page_size = sysval;
1369#endif /* _SC_PAGESIZE */
1370 ctx->blocks_per_page = sys_page_size / fs->blocksize;
1371 if (ctx->blocks_per_page == 0)
1372 ctx->blocks_per_page = 1;
1373
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001374 if (ctx->superblock)
1375 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
Theodore Ts'o550a4af2005-01-25 03:09:24 -05001376 ext2fs_mark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001377 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001378 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001379 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001380 check_if_skip(ctx);
Theodore Ts'o69d0edf2009-04-23 00:39:06 -04001381 check_resize_inode(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001382 if (bad_blocks_file)
1383 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1384 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001385 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001386 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001387 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001388
1389 /*
1390 * Mark the system as valid, 'til proven otherwise
1391 */
1392 ext2fs_mark_valid(fs);
1393
1394 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1395 if (retval) {
1396 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001397 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001398 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001399 printf(_("This doesn't bode well,"
1400 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001401 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001402
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001403 /*
1404 * Save the journal size in megabytes.
1405 * Try and use the journal size from the backup else let e2fsck
1406 * find the default journal size.
1407 */
1408 if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS)
Andreas Dilger931b58e2011-06-11 12:17:29 -04001409 journal_size = (sb->s_jnl_blocks[15] << (32 - 20)) |
1410 (sb->s_jnl_blocks[16] >> 20);
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001411 else
1412 journal_size = -1;
1413
Aditya Kali624e4a62011-07-20 11:40:06 -07001414 if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA) {
1415 int qtype;
1416 /* Quotas were enabled. Do quota accounting during fsck. */
1417 if ((sb->s_usr_quota_inum && sb->s_grp_quota_inum) ||
1418 (!sb->s_usr_quota_inum && !sb->s_grp_quota_inum))
1419 qtype = -1;
1420 else
1421 qtype = sb->s_usr_quota_inum ? USRQUOTA : GRPQUOTA;
1422
1423 init_quota_context(&ctx->qctx, ctx->fs, qtype);
1424 }
1425
Theodore Ts'o08b21301997-11-03 19:42:40 +00001426 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001427 e2fsck_clear_progbar(ctx);
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001428
1429 if (ctx->flags & E2F_FLAG_JOURNAL_INODE) {
1430 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1431 if (journal_size < 1024)
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001432 journal_size = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001433 if (journal_size < 0) {
1434 fs->super->s_feature_compat &=
1435 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'o0cfce7f2007-10-06 12:37:08 -04001436 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001437 com_err(ctx->program_name, 0,
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001438 _("Couldn't determine journal size"));
1439 goto no_journal;
1440 }
1441 printf(_("Creating journal (%d blocks): "),
1442 journal_size);
1443 fflush(stdout);
1444 retval = ext2fs_add_journal_inode(fs,
1445 journal_size, 0);
1446 if (retval) {
1447 com_err("Error ", retval,
1448 _("\n\twhile trying to create journal"));
1449 goto no_journal;
1450 }
1451 printf(_(" Done.\n"));
1452 printf(_("\n*** journal has been re-created - "
1453 "filesystem is now ext3 again ***\n"));
1454 }
1455 }
1456no_journal:
1457
Aditya Kali624e4a62011-07-20 11:40:06 -07001458 if (ctx->qctx) {
1459 write_quota_inode(ctx->qctx, -1);
1460 release_quota_context(&ctx->qctx);
1461 }
1462
Theodore Ts'o08b21301997-11-03 19:42:40 +00001463 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001464 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001465 retval = e2fsck_reset_context(ctx);
1466 if (retval) {
1467 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001468 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001469 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001470 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001471 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001472 goto restart;
1473 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001474 if (run_result & E2F_FLAG_CANCEL) {
1475 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1476 ctx->device_name : ctx->filesystem_name);
1477 exit_value |= FSCK_CANCELED;
1478 }
1479 if (run_result & E2F_FLAG_ABORT)
1480 fatal_error(ctx, _("aborted"));
Theodore Ts'o0c37f452007-10-01 09:18:54 -04001481 if (check_backup_super_block(ctx)) {
1482 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1483 ext2fs_mark_super_dirty(fs);
1484 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001485
1486#ifdef MTRACE
1487 mtrace_print("Cleanup");
1488#endif
1489 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001490 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001491 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001492 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001493 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001494 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001495 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001496 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001497 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001498 }
1499 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001500 if (!ext2fs_test_valid(fs) ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001501 ((exit_value & FSCK_CANCELED) &&
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001502 (sb->s_state & EXT2_ERROR_FS))) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001503 printf(_("\n%s: ********** WARNING: Filesystem still has "
1504 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001505 exit_value |= FSCK_UNCORRECTED;
1506 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001507 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001508 if (exit_value & FSCK_CANCELED) {
1509 int allow_cancellation;
1510
1511 profile_get_boolean(ctx->profile, "options",
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001512 "allow_cancellation", 0, 0,
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001513 &allow_cancellation);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001514 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001515 if (allow_cancellation && ext2fs_test_valid(fs) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001516 (sb->s_state & EXT2_VALID_FS) &&
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001517 !(sb->s_state & EXT2_ERROR_FS))
1518 exit_value = 0;
1519 } else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001520 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001521 if (!(ctx->options & E2F_OPT_READONLY)) {
1522 if (ext2fs_test_valid(fs)) {
1523 if (!(sb->s_state & EXT2_VALID_FS))
1524 exit_value |= FSCK_NONDESTRUCT;
1525 sb->s_state = EXT2_VALID_FS;
1526 } else
1527 sb->s_state &= ~EXT2_VALID_FS;
1528 sb->s_mnt_count = 0;
Theodore Ts'o177839e2010-05-13 17:36:36 -04001529 if (!(ctx->flags & E2F_FLAG_TIME_INSANE))
1530 sb->s_lastcheck = ctx->now;
Theodore Ts'o993988f2010-06-25 10:53:13 -04001531 memset(((char *) sb) + EXT4_S_ERR_START, 0,
1532 EXT4_S_ERR_LEN);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001533 ext2fs_mark_super_dirty(fs);
1534 }
1535 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001536
Theodore Ts'o2e6436d2010-05-17 22:54:06 -04001537 if ((run_result & E2F_FLAG_CANCEL) == 0 &&
1538 sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM &&
Andreas Dilgerf628ace2008-03-31 10:50:19 -04001539 !(ctx->options & E2F_OPT_READONLY)) {
1540 retval = ext2fs_set_gdt_csum(ctx->fs);
1541 if (retval) {
1542 com_err(ctx->program_name, retval,
1543 _("while setting block group checksum info"));
1544 fatal_error(ctx, 0);
1545 }
1546 }
Jose R. Santos49a73602007-10-21 21:04:03 -05001547
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001548 e2fsck_write_bitmaps(ctx);
Theodore Ts'o6d96b002007-08-03 20:07:09 -04001549 io_channel_flush(ctx->fs->io);
Ken Chen9facd072009-05-28 09:55:10 -04001550 print_resource_track(ctx, NULL, &ctx->global_rtrack, ctx->fs->io);
1551
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001552 ext2fs_close(fs);
1553 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001554 free(ctx->journal_name);
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001555
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001556 e2fsck_free_context(ctx);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001557 remove_error_table(&et_ext2_error_table);
1558 remove_error_table(&et_prof_error_table);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001559 return exit_value;
1560}