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