blob: a787d398df8cb952cbcb2ba3b915c690ff7dea07 [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
Eric Sandeenfe65f1e2011-09-16 15:49:38 -0500545/*
546 * Make sure 0,1,2 file descriptors are open, so that we don't open
547 * the filesystem using the same file descriptor as stdout or stderr.
548 */
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000549static void reserve_stdio_fds(void)
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000550{
Eric Sandeenfe65f1e2011-09-16 15:49:38 -0500551 int fd = 0;
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000552
Eric Sandeenfe65f1e2011-09-16 15:49:38 -0500553 while (fd <= 2) {
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000554 fd = open("/dev/null", O_RDWR);
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000555 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000556 fprintf(stderr, _("ERROR: Couldn't open "
557 "/dev/null (%s)\n"),
Theodore Ts'o75d83be1999-05-18 03:16:36 +0000558 strerror(errno));
559 break;
560 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000561 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +0000562}
563
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000564#ifdef HAVE_SIGNAL_H
Theodore Ts'o54434922003-12-07 01:28:50 -0500565static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000566{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000567 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000568
569 if (!ctx)
570 return;
571
572 ctx->progress = e2fsck_update_progress;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000573}
574
Theodore Ts'o54434922003-12-07 01:28:50 -0500575static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o5596def1999-07-19 15:27:37 +0000576{
Theodore Ts'o243dc312000-08-22 21:37:47 +0000577 e2fsck_t ctx = e2fsck_global_ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000578
579 if (!ctx)
580 return;
581
582 e2fsck_clear_progbar(ctx);
583 ctx->progress = 0;
584}
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400585
Theodore Ts'o54434922003-12-07 01:28:50 -0500586static void signal_cancel(int sig EXT2FS_ATTR((unused)))
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400587{
588 e2fsck_t ctx = e2fsck_global_ctx;
589
590 if (!ctx)
591 exit(FSCK_CANCELED);
592
593 ctx->flags |= E2F_FLAG_CANCEL;
594}
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000595#endif
Theodore Ts'o5596def1999-07-19 15:27:37 +0000596
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400597static void parse_extended_opts(e2fsck_t ctx, const char *opts)
598{
599 char *buf, *token, *next, *p, *arg;
Theodore Ts'of3640932003-03-01 19:47:44 -0500600 int ea_ver;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400601 int extended_usage = 0;
602
Theodore Ts'of3640932003-03-01 19:47:44 -0500603 buf = string_copy(ctx, opts, 0);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400604 for (token = buf; token && *token; token = next) {
605 p = strchr(token, ',');
606 next = 0;
607 if (p) {
608 *p = 0;
609 next = p+1;
Brian Behlendorfcae542c2007-03-28 11:28:24 -0400610 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400611 arg = strchr(token, '=');
612 if (arg) {
613 *arg = 0;
614 arg++;
615 }
616 if (strcmp(token, "ea_ver") == 0) {
617 if (!arg) {
618 extended_usage++;
619 continue;
620 }
621 ea_ver = strtoul(arg, &p, 0);
622 if (*p ||
623 ((ea_ver != 1) && (ea_ver != 2))) {
624 fprintf(stderr,
625 _("Invalid EA version.\n"));
626 extended_usage++;
627 continue;
628 }
629 ctx->ext_attr_ver = ea_ver;
Theodore Ts'o63b5e352008-08-10 22:43:24 -0400630 } else if (strcmp(token, "fragcheck") == 0) {
631 ctx->options |= E2F_OPT_FRAGCHECK;
632 continue;
Bernd Schubert71873b12010-11-13 00:09:07 +0100633 } else if (strcmp(token, "journal_only") == 0) {
634 if (arg) {
635 extended_usage++;
636 continue;
637 }
638 ctx->options |= E2F_OPT_JOURNAL_ONLY;
Lukas Czernerefa1a352010-11-18 03:38:38 +0000639 } else if (strcmp(token, "discard") == 0) {
640 ctx->options |= E2F_OPT_DISCARD;
641 continue;
642 } else if (strcmp(token, "nodiscard") == 0) {
643 ctx->options &= ~E2F_OPT_DISCARD;
644 continue;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400645 } else {
646 fprintf(stderr, _("Unknown extended option: %s\n"),
647 token);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400648 extended_usage++;
Theodore Ts'obb145b02005-06-20 08:35:27 -0400649 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400650 }
Brian Behlendorfcae542c2007-03-28 11:28:24 -0400651 free(buf);
652
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400653 if (extended_usage) {
Theodore Ts'obb145b02005-06-20 08:35:27 -0400654 fputs(("\nExtended options are separated by commas, "
655 "and may take an argument which\n"
656 "is set off by an equals ('=') sign. "
Theodore Ts'o63b5e352008-08-10 22:43:24 -0400657 "Valid extended options are:\n"), stderr);
658 fputs(("\tea_ver=<ea_version (1 or 2)>\n"), stderr);
659 fputs(("\tfragcheck\n"), stderr);
Bernd Schubert71873b12010-11-13 00:09:07 +0100660 fputs(("\tjournal_only\n"), stderr);
Lukas Czernerefa1a352010-11-18 03:38:38 +0000661 fputs(("\tdiscard\n"), stderr);
662 fputs(("\tnodiscard\n"), stderr);
Theodore Ts'o63b5e352008-08-10 22:43:24 -0400663 fputc('\n', stderr);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400664 exit(1);
665 }
Brian Behlendorfcae542c2007-03-28 11:28:24 -0400666}
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400667
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500668static void syntax_err_report(const char *filename, long err, int line_num)
669{
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400670 fprintf(stderr,
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500671 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
672 filename, line_num, error_message(err));
673 exit(FSCK_ERROR);
674}
675
Matthias Andreeabcfdfd2006-06-10 16:08:18 +0200676static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400677
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000678static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
679{
680 int flush = 0;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000681 int c, fd;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000682#ifdef MTRACE
683 extern void *mallwatch;
684#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000685 e2fsck_t ctx;
686 errcode_t retval;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000687#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000688 struct sigaction sa;
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000689#endif
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400690 char *extended_opts = 0;
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500691 char *cp;
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400692 int res; /* result of sscanf */
693#ifdef CONFIG_JBD_DEBUG
694 char *jbd_debug;
695#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000696
697 retval = e2fsck_allocate_context(&ctx);
698 if (retval)
699 return retval;
700
701 *ret_ctx = ctx;
702
Theodore Ts'o908b7852003-04-16 15:20:13 -0400703 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
704 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
Theodore Ts'o54a31a32003-08-19 10:08:34 -0400705 if (isatty(0) && isatty(1)) {
706 ctx->interactive = 1;
707 } else {
708 ctx->start_meta[0] = '\001';
709 ctx->stop_meta[0] = '\002';
710 }
711 memset(bar, '=', sizeof(bar)-1);
712 memset(spaces, ' ', sizeof(spaces)-1);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500713 add_error_table(&et_ext2_error_table);
714 add_error_table(&et_prof_error_table);
Theodore Ts'of3640932003-03-01 19:47:44 -0500715 blkid_get_cache(&ctx->blkid, NULL);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400716
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000717 if (argc && *argv)
718 ctx->program_name = *argv;
719 else
720 ctx->program_name = "e2fsck";
Lukas Czernerefa1a352010-11-18 03:38:38 +0000721
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500722 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 +0000723 switch (c) {
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000724 case 'C':
725 ctx->progress = e2fsck_update_progress;
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400726 res = sscanf(optarg, "%d", &ctx->progress_fd);
727 if (res != 1)
728 goto sscanf_err;
729
Theodore Ts'obe625232008-03-26 08:26:01 -0400730 if (ctx->progress_fd < 0) {
731 ctx->progress = 0;
732 ctx->progress_fd = ctx->progress_fd * -1;
733 }
Theodore Ts'ofc9a69c2001-05-12 13:43:46 +0000734 if (!ctx->progress_fd)
735 break;
Theodore Ts'oae8160e2001-05-01 21:13:37 +0000736 /* Validate the file descriptor to avoid disasters */
737 fd = dup(ctx->progress_fd);
738 if (fd < 0) {
739 fprintf(stderr,
740 _("Error validating file descriptor %d: %s\n"),
741 ctx->progress_fd,
742 error_message(errno));
743 fatal_error(ctx,
744 _("Invalid completion information file descriptor"));
745 } else
746 close(fd);
Theodore Ts'oefac9a11998-05-07 05:02:00 +0000747 break;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400748 case 'D':
749 ctx->options |= E2F_OPT_COMPRESS_DIRS;
750 break;
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400751 case 'E':
752 extended_opts = optarg;
753 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000754 case 'p':
755 case 'a':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500756 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
757 conflict_opt:
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400758 fatal_error(ctx,
Theodore Ts'of4b6d2a2005-12-09 17:31:08 -0500759 _("Only one of the options -p/-a, -n or -y may be specified."));
Theodore Ts'o8161a742003-01-02 16:36:44 -0500760 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000761 ctx->options |= E2F_OPT_PREEN;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000762 break;
763 case 'n':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500764 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
765 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000766 ctx->options |= E2F_OPT_NO;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000767 break;
768 case 'y':
Theodore Ts'o8161a742003-01-02 16:36:44 -0500769 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
770 goto conflict_opt;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000771 ctx->options |= E2F_OPT_YES;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000772 break;
773 case 't':
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000774#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000775 if (ctx->options & E2F_OPT_TIME)
776 ctx->options |= E2F_OPT_TIME2;
777 else
778 ctx->options |= E2F_OPT_TIME;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000779#else
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000780 fprintf(stderr, _("The -t option is not "
781 "supported on this version of e2fsck.\n"));
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000782#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000783 break;
784 case 'c':
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500785 if (cflag++)
786 ctx->options |= E2F_OPT_WRITECHECK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000787 ctx->options |= E2F_OPT_CHECKBLOCKS;
788 break;
789 case 'r':
790 /* What we do by default, anyway! */
791 break;
792 case 'b':
Valerie Aurora Henson6dc64392010-06-13 17:00:00 -0400793 res = sscanf(optarg, "%llu", &ctx->use_superblock);
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400794 if (res != 1)
795 goto sscanf_err;
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -0400796 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000797 break;
798 case 'B':
Theodore Ts'of1a17612001-12-23 22:27:52 -0500799 ctx->blocksize = atoi(optarg);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000800 break;
801 case 'I':
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400802 res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
803 if (res != 1)
804 goto sscanf_err;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000805 break;
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400806 case 'j':
Theodore Ts'ob95ca922010-12-06 17:07:27 -0500807 ctx->journal_name = blkid_get_devname(ctx->blkid,
808 optarg, NULL);
809 if (!ctx->journal_name) {
810 com_err(ctx->program_name, 0,
811 _("Unable to resolve '%s'"),
812 optarg);
813 fatal_error(ctx, 0);
814 }
Theodore Ts'oadee8d72001-07-23 00:17:49 -0400815 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000816 case 'P':
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400817 res = sscanf(optarg, "%d", &ctx->process_inode_size);
818 if (res != 1)
819 goto sscanf_err;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000820 break;
821 case 'L':
822 replace_bad_blocks++;
823 case 'l':
Theodore Ts'of3640932003-03-01 19:47:44 -0500824 bad_blocks_file = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000825 break;
826 case 'd':
827 ctx->options |= E2F_OPT_DEBUG;
828 break;
829 case 'f':
Theodore Ts'od37066a2001-12-21 23:28:54 -0500830 ctx->options |= E2F_OPT_FORCE;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000831 break;
832 case 'F':
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000833 flush = 1;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000834 break;
835 case 'v':
836 verbose = 1;
837 break;
838 case 'V':
839 show_version_only = 1;
840 break;
841#ifdef MTRACE
842 case 'M':
843 mallwatch = (void *) strtol(optarg, NULL, 0);
844 break;
845#endif
846 case 'N':
Theodore Ts'oa2447f82008-03-29 14:46:48 -0400847 ctx->device_name = string_copy(ctx, optarg, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000848 break;
Theodore Ts'o4fb9d522004-02-24 00:16:09 -0500849 case 'k':
850 keep_bad_blocks++;
Theodore Ts'obc69f822004-02-27 10:39:27 -0500851 break;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000852 default:
853 usage(ctx);
854 }
855 if (show_version_only)
856 return 0;
857 if (optind != argc - 1)
858 usage(ctx);
Theodore Ts'o298c9c22010-03-12 19:18:20 -0500859 if ((ctx->options & E2F_OPT_NO) &&
860 (ctx->options & E2F_OPT_COMPRESS_DIRS)) {
861 com_err(ctx->program_name, 0,
862 _("The -n and -D options are incompatible."));
863 fatal_error(ctx, 0);
864 }
865 if ((ctx->options & E2F_OPT_NO) && cflag) {
866 com_err(ctx->program_name, 0,
867 _("The -n and -c options are incompatible."));
868 fatal_error(ctx, 0);
869 }
870 if ((ctx->options & E2F_OPT_NO) && bad_blocks_file) {
871 com_err(ctx->program_name, 0,
872 _("The -n and -l/-L options are incompatible."));
873 fatal_error(ctx, 0);
874 }
875 if (ctx->options & E2F_OPT_NO)
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000876 ctx->options |= E2F_OPT_READONLY;
Jose R. Santos49a73602007-10-21 21:04:03 -0500877
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500878 ctx->io_options = strchr(argv[optind], '?');
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400879 if (ctx->io_options)
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500880 *ctx->io_options++ = 0;
Theodore Ts'of3640932003-03-01 19:47:44 -0500881 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500882 if (!ctx->filesystem_name) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400883 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500884 argv[optind]);
885 fatal_error(ctx, 0);
886 }
Theodore Ts'o0684a4f2002-08-17 10:19:44 -0400887 if (extended_opts)
888 parse_extended_opts(ctx, extended_opts);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500889
Theodore Ts'o5dd2a6e2005-12-31 16:21:00 -0500890 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
891 config_fn[0] = cp;
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500892 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'o1017f652005-12-31 00:00:10 -0500893 profile_init(config_fn, &ctx->profile);
894
Theodore Ts'o28ffafb2000-02-08 19:14:02 +0000895 if (flush) {
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500896 fd = open(ctx->filesystem_name, O_RDONLY, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000897 if (fd < 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000898 com_err("open", errno,
899 _("while opening %s for flushing"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000900 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000901 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000902 }
Theodore Ts'o5df55d72001-06-11 07:00:04 +0000903 if ((retval = ext2fs_sync_device(fd, 1))) {
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +0000904 com_err("ext2fs_sync_device", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000905 _("while trying to flush %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000906 ctx->filesystem_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +0000907 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000908 }
909 close(fd);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000910 }
Theodore Ts'o3ed57c22001-12-24 15:01:59 -0500911 if (cflag && bad_blocks_file) {
912 fprintf(stderr, _("The -c and the -l/-L options may "
913 "not be both used at the same time.\n"));
914 exit(FSCK_USAGE);
915 }
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000916#ifdef HAVE_SIGNAL_H
Theodore Ts'o5596def1999-07-19 15:27:37 +0000917 /*
918 * Set up signal action
919 */
920 memset(&sa, 0, sizeof(struct sigaction));
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400921 sa.sa_handler = signal_cancel;
922 sigaction(SIGINT, &sa, 0);
923 sigaction(SIGTERM, &sa, 0);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000924#ifdef SA_RESTART
925 sa.sa_flags = SA_RESTART;
926#endif
Theodore Ts'o243dc312000-08-22 21:37:47 +0000927 e2fsck_global_ctx = ctx;
Theodore Ts'o5596def1999-07-19 15:27:37 +0000928 sa.sa_handler = signal_progress_on;
929 sigaction(SIGUSR1, &sa, 0);
930 sa.sa_handler = signal_progress_off;
931 sigaction(SIGUSR2, &sa, 0);
Theodore Ts'o9ecd8be1999-10-20 18:24:31 +0000932#endif
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400933
934 /* Update our PATH to include /sbin if we need to run badblocks */
935 if (cflag) {
936 char *oldpath = getenv("PATH");
Theodore Ts'o642935c2006-11-14 23:38:17 -0500937 char *newpath;
938 int len = sizeof(PATH_SET) + 1;
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400939
Theodore Ts'o642935c2006-11-14 23:38:17 -0500940 if (oldpath)
941 len += strlen(oldpath);
942
943 newpath = malloc(len);
944 if (!newpath)
945 fatal_error(ctx, "Couldn't malloc() newpath");
946 strcpy(newpath, PATH_SET);
947
948 if (oldpath) {
949 strcat(newpath, ":");
950 strcat(newpath, oldpath);
951 }
952 putenv(newpath);
Theodore Ts'o6d222f32001-07-29 12:06:58 -0400953 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500954#ifdef CONFIG_JBD_DEBUG
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400955 jbd_debug = getenv("E2FSCK_JBD_DEBUG");
Theodore Ts'o414025e2008-01-31 14:22:24 -0500956 if (jbd_debug) {
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400957 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
958 if (res != 1) {
959 fprintf(stderr,
960 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
961 jbd_debug);
962 exit (1);
963 }
Theodore Ts'o414025e2008-01-31 14:22:24 -0500964 }
Theodore Ts'o5e72cdb2002-11-08 15:35:13 -0500965#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000966 return 0;
Theodore Ts'o5845caa2007-08-20 22:55:33 -0400967
968sscanf_err:
969 fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
970 c, optarg);
971 exit (1);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000972}
973
Theodore Ts'o60663892009-05-28 23:40:18 -0400974static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr,
975 ext2_filsys *ret_fs)
976{
977 errcode_t retval;
978
979 *ret_fs = NULL;
980 if (ctx->superblock && ctx->blocksize) {
981 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
982 flags, ctx->superblock, ctx->blocksize,
983 io_ptr, ret_fs);
984 } else if (ctx->superblock) {
985 int blocksize;
986 for (blocksize = EXT2_MIN_BLOCK_SIZE;
987 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
988 if (*ret_fs) {
989 ext2fs_free(*ret_fs);
990 *ret_fs = NULL;
991 }
992 retval = ext2fs_open2(ctx->filesystem_name,
993 ctx->io_options, flags,
994 ctx->superblock, blocksize,
995 io_ptr, ret_fs);
996 if (!retval)
997 break;
998 }
999 } else
1000 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1001 flags, 0, 0, io_ptr, ret_fs);
1002 return retval;
1003}
1004
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001005static const char *my_ver_string = E2FSPROGS_VERSION;
1006static const char *my_ver_date = E2FSPROGS_DATE;
Jose R. Santos49a73602007-10-21 21:04:03 -05001007
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001008int main (int argc, char *argv[])
1009{
Theodore Ts'o82b59ca2009-09-01 20:01:38 -04001010 errcode_t retval = 0, retval2 = 0, orig_retval = 0;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001011 int exit_value = FSCK_OK;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001012 ext2_filsys fs = 0;
1013 io_manager io_ptr;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001014 struct ext2_super_block *sb;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001015 const char *lib_ver_date;
1016 int my_ver, lib_ver;
1017 e2fsck_t ctx;
Theodore Ts'o60663892009-05-28 23:40:18 -04001018 blk_t orig_superblock;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001019 struct problem_context pctx;
Theodore Ts'o08b21301997-11-03 19:42:40 +00001020 int flags, run_result;
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001021 int journal_size;
Theodore Ts'o9f0288d2007-08-03 20:43:37 -04001022 int sysval, sys_page_size = 4096;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -04001023 int old_bitmaps;
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001024 __u32 features[3];
Theodore Ts'o1dc506c2008-03-26 09:09:09 -04001025 char *cp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001026
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001027 clear_problem_context(&pctx);
Theodore Ts'o9b3018a2011-08-11 14:56:49 -04001028 sigcatcher_setup();
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001029#ifdef MTRACE
1030 mtrace();
1031#endif
1032#ifdef MCHECK
1033 mcheck(0);
1034#endif
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001035#ifdef ENABLE_NLS
1036 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001037 setlocale(LC_CTYPE, "");
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001038 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1039 textdomain(NLS_CAT_NAME);
1040#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001041 my_ver = ext2fs_parse_version_string(my_ver_string);
1042 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
1043 if (my_ver > lib_ver) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001044 fprintf( stderr, _("Error: ext2fs library version "
1045 "out of date!\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001046 show_version_only++;
1047 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001048
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001049 retval = PRS(argc, argv, &ctx);
1050 if (retval) {
1051 com_err("e2fsck", retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001052 _("while trying to initialize program"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001053 exit(FSCK_ERROR);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001054 }
Theodore Ts'o24fc5031998-08-26 15:23:31 +00001055 reserve_stdio_fds();
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001056
Theodore Ts'o6d96b002007-08-03 20:07:09 -04001057 init_resource_track(&ctx->global_rtrack, NULL);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001058 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
Theodore Ts'o908b7852003-04-16 15:20:13 -04001059 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001060 my_ver_date);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001061
1062 if (show_version_only) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001063 fprintf(stderr, _("\tUsing %s, %s\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001064 error_message(EXT2_ET_BASE), lib_ver_date);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001065 exit(FSCK_OK);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001066 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001067
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001068 check_mount(ctx);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001069
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001070 if (!(ctx->options & E2F_OPT_PREEN) &&
1071 !(ctx->options & E2F_OPT_NO) &&
1072 !(ctx->options & E2F_OPT_YES)) {
Theodore Ts'o54a31a32003-08-19 10:08:34 -04001073 if (!ctx->interactive)
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001074 fatal_error(ctx,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001075 _("need terminal for interactive repairs"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001076 }
1077 ctx->superblock = ctx->use_superblock;
1078restart:
Theodore Ts'o2a29f132003-05-05 12:08:47 -04001079#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001080 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1081 io_ptr = test_io_manager;
1082 test_io_backing_manager = unix_io_manager;
1083 } else
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001084#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001085 io_ptr = unix_io_manager;
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001086 flags = EXT2_FLAG_NOFREE_ON_ERROR;
Valerie Aurora Hensonc5d2f502009-08-22 22:29:02 -04001087 profile_get_boolean(ctx->profile, "options", "old_bitmaps", 0, 0,
1088 &old_bitmaps);
1089 if (!old_bitmaps)
1090 flags |= EXT2_FLAG_64BITS;
Theodore Ts'o17390c02000-07-07 04:13:21 +00001091 if ((ctx->options & E2F_OPT_READONLY) == 0)
1092 flags |= EXT2_FLAG_RW;
Theodore Ts'o2e14e0c2006-03-18 20:01:09 -05001093 if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
1094 flags |= EXT2_FLAG_EXCLUSIVE;
Theodore Ts'o17390c02000-07-07 04:13:21 +00001095
Theodore Ts'o60663892009-05-28 23:40:18 -04001096 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1097
Theodore Ts'oae6cdcf2001-09-19 15:17:25 -04001098 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
1099 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001100 ((retval == EXT2_ET_BAD_MAGIC) ||
Theodore Ts'ocd538082007-09-07 16:54:11 -04001101 (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
Theodore Ts'o82b59ca2009-09-01 20:01:38 -04001102 ((retval == 0) && (retval2 = ext2fs_check_desc(fs))))) {
1103 if (retval2 == ENOMEM) {
1104 retval = retval2;
1105 goto failure;
1106 }
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001107 if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) {
1108 ext2fs_free(fs);
1109 fs = NULL;
1110 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001111 if (!fs || (fs->group_desc_count > 1)) {
Theodore Ts'ocd538082007-09-07 16:54:11 -04001112 printf(_("%s: %s trying backup blocks...\n"),
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001113 ctx->program_name,
Theodore Ts'ocd538082007-09-07 16:54:11 -04001114 retval ? _("Superblock invalid,") :
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001115 _("Group descriptors look bad..."));
Theodore Ts'o60663892009-05-28 23:40:18 -04001116 orig_superblock = ctx->superblock;
Theodore Ts'of1a17612001-12-23 22:27:52 -05001117 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001118 if (fs)
1119 ext2fs_close(fs);
Theodore Ts'ocd538082007-09-07 16:54:11 -04001120 orig_retval = retval;
Theodore Ts'o60663892009-05-28 23:40:18 -04001121 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1122 if ((orig_retval == 0) && retval != 0) {
Theodore Ts'o22ff06d2010-07-05 15:06:37 -04001123 if (fs)
1124 ext2fs_close(fs);
Theodore Ts'o60663892009-05-28 23:40:18 -04001125 com_err(ctx->program_name, retval,
1126 "when using the backup blocks");
1127 printf(_("%s: going back to original "
1128 "superblock\n"), ctx->program_name);
1129 ctx->superblock = orig_superblock;
1130 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1131 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001132 }
1133 }
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001134 if (((retval == EXT2_ET_UNSUPP_FEATURE) ||
1135 (retval == EXT2_ET_RO_UNSUPP_FEATURE)) &&
1136 fs && fs->super) {
1137 sb = fs->super;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001138 features[0] = (sb->s_feature_compat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001139 ~EXT2_LIB_FEATURE_COMPAT_SUPP);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001140 features[1] = (sb->s_feature_incompat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001141 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001142 features[2] = (sb->s_feature_ro_compat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001143 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1144 if (features[0] || features[1] || features[2])
1145 goto print_unsupp_features;
1146 }
Theodore Ts'o82b59ca2009-09-01 20:01:38 -04001147failure:
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001148 if (retval) {
Theodore Ts'ocd538082007-09-07 16:54:11 -04001149 if (orig_retval)
1150 retval = orig_retval;
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001151 com_err(ctx->program_name, retval, _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001152 ctx->filesystem_name);
Theodore Ts'o24dd4021998-02-01 00:16:40 +00001153 if (retval == EXT2_ET_REV_TOO_HIGH) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001154 printf(_("The filesystem revision is apparently "
Theodore Ts'o24dd4021998-02-01 00:16:40 +00001155 "too high for this version of e2fsck.\n"
1156 "(Or the filesystem superblock "
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001157 "is corrupt)\n\n"));
Theodore Ts'o24dd4021998-02-01 00:16:40 +00001158 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1159 } else if (retval == EXT2_ET_SHORT_READ)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001160 printf(_("Could this be a zero-length partition?\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001161 else if ((retval == EPERM) || (retval == EACCES))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001162 printf(_("You must have %s access to the "
1163 "filesystem or be root\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001164 (ctx->options & E2F_OPT_READONLY) ?
1165 "r/o" : "r/w");
1166 else if (retval == ENXIO)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001167 printf(_("Possibly non-existent or swap device?\n"));
Theodore Ts'o2e14e0c2006-03-18 20:01:09 -05001168 else if (retval == EBUSY)
1169 printf(_("Filesystem mounted or opened exclusively "
1170 "by another program?\n"));
Jan Kara32f66cc2010-09-08 16:12:08 +02001171 else if (retval == ENOENT)
1172 printf(_("Possibly non-existent device?\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +00001173#ifdef EROFS
1174 else if (retval == EROFS)
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001175 printf(_("Disk write-protected; use the -n option "
Theodore Ts'o68227541997-11-04 04:25:22 +00001176 "to do a read-only\n"
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001177 "check of the device.\n"));
Theodore Ts'o68227541997-11-04 04:25:22 +00001178#endif
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001179 else
1180 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001181 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001182 }
Theodore Ts'o058ad1c2007-06-18 18:26:50 -04001183 /*
1184 * We only update the master superblock because (a) paranoia;
1185 * we don't want to corrupt the backup superblocks, and (b) we
1186 * don't need to update the mount count and last checked
1187 * fields in the backup superblock (the kernel doesn't update
1188 * the backup superblocks anyway). With newer versions of the
1189 * library this flag is set by ext2fs_open2(), but we set this
1190 * here just to be sure. (No, we don't support e2fsck running
1191 * with some other libext2fs than the one that it was shipped
1192 * with, but just in case....)
1193 */
1194 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001195
1196 if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1197 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1198 int need_restart = 0;
1199
Theodore Ts'o59119642010-03-14 19:26:48 -04001200 pctx.errcode = ext2fs_get_device_size2(ctx->filesystem_name,
1201 blocksize,
1202 &ctx->num_blocks);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001203 /*
1204 * The floppy driver refuses to allow anyone else to
1205 * open the device if has been opened with O_EXCL;
1206 * this is unlike other block device drivers in Linux.
1207 * To handle this, we close the filesystem and then
1208 * reopen the filesystem after we get the device size.
1209 */
1210 if (pctx.errcode == EBUSY) {
1211 ext2fs_close(fs);
1212 need_restart++;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001213 pctx.errcode =
Theodore Ts'o59119642010-03-14 19:26:48 -04001214 ext2fs_get_device_size2(ctx->filesystem_name,
1215 blocksize,
1216 &ctx->num_blocks);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001217 }
1218 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1219 ctx->num_blocks = 0;
1220 else if (pctx.errcode) {
1221 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1222 ctx->flags |= E2F_FLAG_ABORT;
1223 fatal_error(ctx, 0);
Theodore Ts'od2af1bd2007-06-04 01:14:52 -04001224 }
1225 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1226 if (need_restart)
1227 goto restart;
1228 }
1229
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001230 ctx->fs = fs;
Theodore Ts'o54dc7ca1998-01-19 14:50:49 +00001231 fs->priv_data = ctx;
Theodore Ts'o8dceb922005-09-24 21:59:45 -04001232 fs->now = ctx->now;
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001233 sb = fs->super;
1234 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001235 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001236 _("while trying to open %s"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001237 ctx->filesystem_name);
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001238 get_newer:
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001239 fatal_error(ctx, _("Get a newer version of e2fsck!"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001240 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001241
Theodore Ts'o17390c02000-07-07 04:13:21 +00001242 /*
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001243 * Set the device name, which is used whenever we print error
1244 * or informational messages to the user.
Theodore Ts'o17390c02000-07-07 04:13:21 +00001245 */
Theodore Ts'o5596def1999-07-19 15:27:37 +00001246 if (ctx->device_name == 0 &&
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001247 (sb->s_volume_name[0] != 0)) {
Theodore Ts'of3640932003-03-01 19:47:44 -05001248 ctx->device_name = string_copy(ctx, sb->s_volume_name,
1249 sizeof(sb->s_volume_name));
Theodore Ts'o5596def1999-07-19 15:27:37 +00001250 }
1251 if (ctx->device_name == 0)
Theodore Ts'o1dc506c2008-03-26 09:09:09 -04001252 ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
1253 for (cp = ctx->device_name; *cp; cp++)
1254 if (isspace(*cp) || *cp == ':')
1255 *cp = '_';
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001256
Andreas Dilger14c5af32009-05-17 23:03:04 -04001257 ehandler_init(fs->io);
1258
Theodore Ts'o47c1b8e2009-04-28 09:09:44 -04001259 if ((ctx->mount_flags & EXT2_MF_MOUNTED) &&
1260 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER))
1261 goto skip_journal;
1262
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001263 /*
Theodore Ts'o9b565752000-12-13 18:50:22 +00001264 * Make sure the ext3 superblock fields are consistent.
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001265 */
1266 retval = e2fsck_check_ext3_journal(ctx);
1267 if (retval) {
1268 com_err(ctx->program_name, retval,
1269 _("while checking ext3 journal for %s"),
1270 ctx->device_name);
Theodore Ts'o243dc312000-08-22 21:37:47 +00001271 fatal_error(ctx, 0);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001272 }
1273
Theodore Ts'o9b565752000-12-13 18:50:22 +00001274 /*
1275 * Check to see if we need to do ext3-style recovery. If so,
1276 * do it, and then restart the fsck.
1277 */
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001278 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001279 if (ctx->options & E2F_OPT_READONLY) {
1280 printf(_("Warning: skipping journal recovery "
1281 "because doing a read-only filesystem "
1282 "check.\n"));
1283 io_channel_flush(ctx->fs->io);
1284 } else {
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001285 if (ctx->flags & E2F_FLAG_RESTARTED) {
1286 /*
1287 * Whoops, we attempted to run the
1288 * journal twice. This should never
1289 * happen, unless the hardware or
1290 * device driver is being bogus.
1291 */
1292 com_err(ctx->program_name, 0,
1293 _("unable to set superblock flags on %s\n"), ctx->device_name);
1294 fatal_error(ctx, 0);
1295 }
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001296 retval = e2fsck_run_ext3_journal(ctx);
1297 if (retval) {
1298 com_err(ctx->program_name, retval,
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001299 _("while recovering ext3 journal of %s"),
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001300 ctx->device_name);
1301 fatal_error(ctx, 0);
1302 }
1303 ext2fs_close(ctx->fs);
1304 ctx->fs = 0;
Theodore Ts'ob92ae152003-01-02 16:53:54 -05001305 ctx->flags |= E2F_FLAG_RESTARTED;
Theodore Ts'o2575fb02000-08-22 21:50:04 +00001306 goto restart;
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001307 }
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001308 }
1309
Theodore Ts'o47c1b8e2009-04-28 09:09:44 -04001310skip_journal:
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001311 /*
1312 * Check for compatibility with the feature sets. We need to
1313 * be more stringent than ext2fs_open().
1314 */
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001315 features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP;
1316 features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001317 features[2] = (sb->s_feature_ro_compat &
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001318 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1319print_unsupp_features:
1320 if (features[0] || features[1] || features[2]) {
1321 int i, j;
1322 __u32 *mask = features, m;
1323
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001324 fprintf(stderr, _("%s has unsupported feature(s):"),
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001325 ctx->filesystem_name);
1326
1327 for (i=0; i <3; i++,mask++) {
1328 for (j=0,m=1; j < 32; j++, m<<=1) {
1329 if (*mask & m)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001330 fprintf(stderr, " %s",
Theodore Ts'odcc91e12008-02-26 20:59:56 -05001331 e2p_feature2string(i, m));
1332 }
1333 }
1334 putc('\n', stderr);
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001335 goto get_newer;
1336 }
1337#ifdef ENABLE_COMPRESSION
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001338 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
Theodore Ts'o3b5386d2000-08-14 14:25:19 +00001339 com_err(ctx->program_name, 0,
1340 _("Warning: compression support is experimental.\n"));
1341#endif
Theodore Ts'o8fdc9982002-06-25 23:26:34 -04001342#ifndef ENABLE_HTREE
1343 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1344 com_err(ctx->program_name, 0,
1345 _("E2fsck not compiled with HTREE support,\n\t"
1346 "but filesystem %s has HTREE directories.\n"),
1347 ctx->device_name);
1348 goto get_newer;
1349 }
1350#endif
1351
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001352 /*
1353 * If the user specified a specific superblock, presumably the
1354 * master superblock has been trashed. So we mark the
1355 * superblock as dirty, so it can be written out.
1356 */
1357 if (ctx->superblock &&
1358 !(ctx->options & E2F_OPT_READONLY))
1359 ext2fs_mark_super_dirty(fs);
1360
Theodore Ts'o9f0288d2007-08-03 20:43:37 -04001361 /*
1362 * Calculate the number of filesystem blocks per pagesize. If
1363 * fs->blocksize > page_size, set the number of blocks per
1364 * pagesize to 1 to avoid division by zero errors.
1365 */
1366#ifdef _SC_PAGESIZE
1367 sysval = sysconf(_SC_PAGESIZE);
1368 if (sysval > 0)
1369 sys_page_size = sysval;
1370#endif /* _SC_PAGESIZE */
1371 ctx->blocks_per_page = sys_page_size / fs->blocksize;
1372 if (ctx->blocks_per_page == 0)
1373 ctx->blocks_per_page = 1;
1374
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001375 if (ctx->superblock)
1376 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
Theodore Ts'o550a4af2005-01-25 03:09:24 -05001377 ext2fs_mark_valid(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001378 check_super_block(ctx);
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001379 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001380 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001381 check_if_skip(ctx);
Theodore Ts'o69d0edf2009-04-23 00:39:06 -04001382 check_resize_inode(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001383 if (bad_blocks_file)
1384 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1385 else if (cflag)
Theodore Ts'o4fb9d522004-02-24 00:16:09 -05001386 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +00001387 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Theodore Ts'o243dc312000-08-22 21:37:47 +00001388 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001389
1390 /*
1391 * Mark the system as valid, 'til proven otherwise
1392 */
1393 ext2fs_mark_valid(fs);
1394
1395 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1396 if (retval) {
1397 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001398 _("while reading bad blocks inode"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001399 preenhalt(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001400 printf(_("This doesn't bode well,"
1401 " but we'll try to go on...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001402 }
Theodore Ts'o08b21301997-11-03 19:42:40 +00001403
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001404 /*
1405 * Save the journal size in megabytes.
1406 * Try and use the journal size from the backup else let e2fsck
1407 * find the default journal size.
1408 */
1409 if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS)
Andreas Dilger931b58e2011-06-11 12:17:29 -04001410 journal_size = (sb->s_jnl_blocks[15] << (32 - 20)) |
1411 (sb->s_jnl_blocks[16] >> 20);
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001412 else
1413 journal_size = -1;
1414
Aditya Kali624e4a62011-07-20 11:40:06 -07001415 if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA) {
1416 int qtype;
1417 /* Quotas were enabled. Do quota accounting during fsck. */
1418 if ((sb->s_usr_quota_inum && sb->s_grp_quota_inum) ||
1419 (!sb->s_usr_quota_inum && !sb->s_grp_quota_inum))
1420 qtype = -1;
1421 else
1422 qtype = sb->s_usr_quota_inum ? USRQUOTA : GRPQUOTA;
1423
1424 init_quota_context(&ctx->qctx, ctx->fs, qtype);
1425 }
1426
Theodore Ts'o08b21301997-11-03 19:42:40 +00001427 run_result = e2fsck_run(ctx);
Theodore Ts'o5596def1999-07-19 15:27:37 +00001428 e2fsck_clear_progbar(ctx);
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001429
1430 if (ctx->flags & E2F_FLAG_JOURNAL_INODE) {
1431 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1432 if (journal_size < 1024)
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -04001433 journal_size = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001434 if (journal_size < 0) {
1435 fs->super->s_feature_compat &=
1436 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'o0cfce7f2007-10-06 12:37:08 -04001437 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001438 com_err(ctx->program_name, 0,
Kalpak Shah5107d0d2007-06-21 11:59:06 -04001439 _("Couldn't determine journal size"));
1440 goto no_journal;
1441 }
1442 printf(_("Creating journal (%d blocks): "),
1443 journal_size);
1444 fflush(stdout);
1445 retval = ext2fs_add_journal_inode(fs,
1446 journal_size, 0);
1447 if (retval) {
1448 com_err("Error ", retval,
1449 _("\n\twhile trying to create journal"));
1450 goto no_journal;
1451 }
1452 printf(_(" Done.\n"));
1453 printf(_("\n*** journal has been re-created - "
1454 "filesystem is now ext3 again ***\n"));
1455 }
1456 }
1457no_journal:
1458
Aditya Kali624e4a62011-07-20 11:40:06 -07001459 if (ctx->qctx) {
1460 write_quota_inode(ctx->qctx, -1);
1461 release_quota_context(&ctx->qctx);
1462 }
1463
Theodore Ts'o08b21301997-11-03 19:42:40 +00001464 if (run_result == E2F_FLAG_RESTART) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001465 printf(_("Restarting e2fsck from the beginning...\n"));
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001466 retval = e2fsck_reset_context(ctx);
1467 if (retval) {
1468 com_err(ctx->program_name, retval,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001469 _("while resetting context"));
Theodore Ts'o243dc312000-08-22 21:37:47 +00001470 fatal_error(ctx, 0);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001471 }
Theodore Ts'o73f17cf1999-01-04 07:35:45 +00001472 ext2fs_close(fs);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001473 goto restart;
1474 }
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001475 if (run_result & E2F_FLAG_CANCEL) {
1476 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1477 ctx->device_name : ctx->filesystem_name);
1478 exit_value |= FSCK_CANCELED;
1479 }
1480 if (run_result & E2F_FLAG_ABORT)
1481 fatal_error(ctx, _("aborted"));
Theodore Ts'o0c37f452007-10-01 09:18:54 -04001482 if (check_backup_super_block(ctx)) {
1483 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1484 ext2fs_mark_super_dirty(fs);
1485 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001486
1487#ifdef MTRACE
1488 mtrace_print("Cleanup");
1489#endif
1490 if (ext2fs_test_changed(fs)) {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001491 exit_value |= FSCK_NONDESTRUCT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001492 if (!(ctx->options & E2F_OPT_PREEN))
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001493 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001494 ctx->device_name);
Theodore Ts'oee895132002-11-07 16:16:55 -05001495 if (ctx->mount_flags & EXT2_MF_ISROOT) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +00001496 printf(_("%s: ***** REBOOT LINUX *****\n"),
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001497 ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001498 exit_value |= FSCK_REBOOT;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001499 }
1500 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001501 if (!ext2fs_test_valid(fs) ||
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001502 ((exit_value & FSCK_CANCELED) &&
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001503 (sb->s_state & EXT2_ERROR_FS))) {
Theodore Ts'od3124012001-07-20 14:13:49 -04001504 printf(_("\n%s: ********** WARNING: Filesystem still has "
1505 "errors **********\n\n"), ctx->device_name);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001506 exit_value |= FSCK_UNCORRECTED;
1507 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'od3124012001-07-20 14:13:49 -04001508 }
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001509 if (exit_value & FSCK_CANCELED) {
1510 int allow_cancellation;
1511
1512 profile_get_boolean(ctx->profile, "options",
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001513 "allow_cancellation", 0, 0,
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001514 &allow_cancellation);
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001515 exit_value &= ~FSCK_NONDESTRUCT;
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001516 if (allow_cancellation && ext2fs_test_valid(fs) &&
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001517 (sb->s_state & EXT2_VALID_FS) &&
Theodore Ts'oeb065cc2005-12-31 00:52:23 -05001518 !(sb->s_state & EXT2_ERROR_FS))
1519 exit_value = 0;
1520 } else {
Theodore Ts'o4cae0452002-07-21 14:14:03 -04001521 show_stats(ctx);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001522 if (!(ctx->options & E2F_OPT_READONLY)) {
1523 if (ext2fs_test_valid(fs)) {
1524 if (!(sb->s_state & EXT2_VALID_FS))
1525 exit_value |= FSCK_NONDESTRUCT;
1526 sb->s_state = EXT2_VALID_FS;
1527 } else
1528 sb->s_state &= ~EXT2_VALID_FS;
1529 sb->s_mnt_count = 0;
Theodore Ts'o177839e2010-05-13 17:36:36 -04001530 if (!(ctx->flags & E2F_FLAG_TIME_INSANE))
1531 sb->s_lastcheck = ctx->now;
Theodore Ts'o993988f2010-06-25 10:53:13 -04001532 memset(((char *) sb) + EXT4_S_ERR_START, 0,
1533 EXT4_S_ERR_LEN);
Theodore Ts'oc1637bd2002-11-08 15:55:17 -05001534 ext2fs_mark_super_dirty(fs);
1535 }
1536 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001537
Theodore Ts'o2e6436d2010-05-17 22:54:06 -04001538 if ((run_result & E2F_FLAG_CANCEL) == 0 &&
1539 sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM &&
Andreas Dilgerf628ace2008-03-31 10:50:19 -04001540 !(ctx->options & E2F_OPT_READONLY)) {
1541 retval = ext2fs_set_gdt_csum(ctx->fs);
1542 if (retval) {
1543 com_err(ctx->program_name, retval,
1544 _("while setting block group checksum info"));
1545 fatal_error(ctx, 0);
1546 }
1547 }
Jose R. Santos49a73602007-10-21 21:04:03 -05001548
Theodore Ts'of8188ff1997-11-14 05:23:04 +00001549 e2fsck_write_bitmaps(ctx);
Theodore Ts'o6d96b002007-08-03 20:07:09 -04001550 io_channel_flush(ctx->fs->io);
Ken Chen9facd072009-05-28 09:55:10 -04001551 print_resource_track(ctx, NULL, &ctx->global_rtrack, ctx->fs->io);
1552
Theodore Ts'o0628ae32001-07-29 12:26:46 -04001553 ext2fs_close(fs);
1554 ctx->fs = NULL;
Theodore Ts'of3640932003-03-01 19:47:44 -05001555 free(ctx->journal_name);
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001556
Theodore Ts'ob28a6e92005-07-25 11:36:43 -05001557 e2fsck_free_context(ctx);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001558 remove_error_table(&et_ext2_error_table);
1559 remove_error_table(&et_prof_error_table);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +00001560 return exit_value;
1561}