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