]>
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 | |
36 | #include <sys/ioctl.h> | |
37 | #include <malloc.h> | |
38 | ||
39 | #include "et/com_err.h" | |
40 | #include "e2fsck.h" | |
41 | #include "problem.h" | |
42 | #include "../version.h" | |
43 | ||
1b6bf175 TT |
44 | /* Command line options */ |
45 | static int blocksize = 0; | |
46 | static int swapfs = 0; | |
47 | static int normalize_swapfs = 0; | |
48 | static int cflag = 0; /* check disk */ | |
49 | static int show_version_only = 0; | |
50 | static int force = 0; | |
51 | static int verbose = 0; | |
52 | ||
53 | static int replace_bad_blocks = 0; | |
54 | static char *bad_blocks_file = 0; | |
55 | ||
56 | static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0}; | |
57 | ||
58 | static int root_filesystem = 0; | |
59 | static int read_only_root = 0; | |
60 | ||
243dc31f TT |
61 | e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */ |
62 | ||
1b6bf175 TT |
63 | static void usage(e2fsck_t ctx) |
64 | { | |
65 | fprintf(stderr, | |
0c4a0726 | 66 | _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n" |
1b6bf175 | 67 | "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n" |
0c4a0726 | 68 | "\t\t[-l|-L bad_blocks_file] [-C fd] device\n"), |
5596defa | 69 | ctx->program_name); |
b55199ea | 70 | |
0c4a0726 | 71 | fprintf(stderr, _("\nEmergency help:\n" |
b55199ea TT |
72 | " -p Automatic repair (no questions)\n" |
73 | " -n Make no changes to the filesystem\n" | |
74 | " -y Assume \"yes\" to all questions\n" | |
75 | " -c Check for bad blocks\n" | |
53ef44c4 TT |
76 | " -f Force checking even if filesystem is marked clean\n")); |
77 | fprintf(stderr, _("" | |
b55199ea TT |
78 | " -v Be verbose\n" |
79 | " -b superblock Use alternative superblock\n" | |
80 | " -B blocksize Force blocksize when looking for superblock\n" | |
81 | " -l bad_blocks_file Add to badblocks list\n" | |
82 | " -L bad_blocks_file Set badblocks list\n" | |
0c4a0726 | 83 | )); |
b55199ea | 84 | |
1b6bf175 TT |
85 | exit(FSCK_USAGE); |
86 | } | |
87 | ||
88 | static void show_stats(e2fsck_t ctx) | |
89 | { | |
90 | ext2_filsys fs = ctx->fs; | |
91 | int inodes, inodes_used, blocks, blocks_used; | |
92 | int dir_links; | |
93 | int num_files, num_links; | |
94 | int frag_percent; | |
95 | ||
96 | dir_links = 2 * ctx->fs_directory_count - 1; | |
97 | num_files = ctx->fs_total_count - dir_links; | |
98 | num_links = ctx->fs_links_count - dir_links; | |
99 | inodes = fs->super->s_inodes_count; | |
100 | inodes_used = (fs->super->s_inodes_count - | |
101 | fs->super->s_free_inodes_count); | |
102 | blocks = fs->super->s_blocks_count; | |
103 | blocks_used = (fs->super->s_blocks_count - | |
104 | fs->super->s_free_blocks_count); | |
105 | ||
106 | frag_percent = (10000 * ctx->fs_fragmented) / inodes_used; | |
107 | frag_percent = (frag_percent + 5) / 10; | |
108 | ||
109 | if (!verbose) { | |
0c4a0726 | 110 | printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"), |
1b6bf175 TT |
111 | ctx->device_name, inodes_used, inodes, |
112 | frag_percent / 10, frag_percent % 10, | |
113 | blocks_used, blocks); | |
114 | return; | |
115 | } | |
0c4a0726 TT |
116 | /* |
117 | * This is a bit ugly. But I think there will nearly always be more | |
118 | * than one "thing" to report about, so I won't try writing complex | |
119 | * code to handle one/two/many forms of all words. | |
120 | * Some languages (Italian, at least) never uses the plural form | |
121 | * of foreign words, so in real life this could not be a problem. | |
122 | * md@linux.it - 2000-1-15 | |
123 | */ | |
124 | #ifdef ENABLE_NLS | |
125 | printf (_("\n%8d inodes used (%d%%)\n"), inodes_used, | |
126 | (inodes_used != 1), 100 * inodes_used / inodes); | |
127 | printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"), | |
128 | ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); | |
129 | printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"), | |
130 | ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); | |
131 | printf (_("%8d blocks used (%d%%)\n" | |
132 | "%8d bad blocks\n"), blocks_used, | |
636a954a TT |
133 | (int) ((long long) 100 * blocks_used / blocks), |
134 | ctx->fs_badblocks_count); | |
0c4a0726 TT |
135 | printf (_("\n%8d regular files\n" |
136 | "%8d directories\n" | |
137 | "%8d character device files\n" | |
138 | "%8d block device files\n" | |
139 | "%8d fifos\n" | |
140 | "%8d links\n" | |
141 | "%8d symbolic links (%d fast symbolic links)\n" | |
142 | "%8d sockets\n" | |
143 | "--------\n" | |
144 | "%8d files\n"), | |
145 | ctx->fs_regular_count, | |
146 | ctx->fs_directory_count, | |
147 | ctx->fs_chardev_count, | |
148 | ctx->fs_blockdev_count, | |
149 | ctx->fs_fifo_count, | |
150 | ctx->fs_links_count - dir_links, | |
151 | ctx->fs_symlinks_count, | |
152 | ctx->fs_fast_symlinks_count, | |
153 | ctx->fs_sockets_count, | |
154 | ctx->fs_total_count - dir_links); | |
155 | #else | |
1b6bf175 TT |
156 | printf ("\n%8d inode%s used (%d%%)\n", inodes_used, |
157 | (inodes_used != 1) ? "s" : "", | |
158 | 100 * inodes_used / inodes); | |
159 | printf ("%8d non-contiguous inodes (%0d.%d%%)\n", | |
160 | ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); | |
161 | printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n", | |
162 | ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); | |
163 | printf ("%8d block%s used (%d%%)\n" | |
164 | "%8d bad block%s\n", blocks_used, | |
165 | (blocks_used != 1) ? "s" : "", | |
166 | 100 * blocks_used / blocks, ctx->fs_badblocks_count, | |
167 | ctx->fs_badblocks_count != 1 ? "s" : ""); | |
168 | printf ("\n%8d regular file%s\n" | |
169 | "%8d director%s\n" | |
170 | "%8d character device file%s\n" | |
171 | "%8d block device file%s\n" | |
172 | "%8d fifo%s\n" | |
173 | "%8d link%s\n" | |
174 | "%8d symbolic link%s (%d fast symbolic link%s)\n" | |
175 | "%8d socket%s\n" | |
176 | "--------\n" | |
177 | "%8d file%s\n", | |
178 | ctx->fs_regular_count, | |
179 | (ctx->fs_regular_count != 1) ? "s" : "", | |
180 | ctx->fs_directory_count, | |
181 | (ctx->fs_directory_count != 1) ? "ies" : "y", | |
182 | ctx->fs_chardev_count, | |
183 | (ctx->fs_chardev_count != 1) ? "s" : "", | |
184 | ctx->fs_blockdev_count, | |
185 | (ctx->fs_blockdev_count != 1) ? "s" : "", | |
186 | ctx->fs_fifo_count, | |
187 | (ctx->fs_fifo_count != 1) ? "s" : "", | |
188 | ctx->fs_links_count - dir_links, | |
189 | ((ctx->fs_links_count - dir_links) != 1) ? "s" : "", | |
190 | ctx->fs_symlinks_count, | |
191 | (ctx->fs_symlinks_count != 1) ? "s" : "", | |
192 | ctx->fs_fast_symlinks_count, | |
193 | (ctx->fs_fast_symlinks_count != 1) ? "s" : "", | |
194 | ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "", | |
195 | ctx->fs_total_count - dir_links, | |
196 | ((ctx->fs_total_count - dir_links) != 1) ? "s" : ""); | |
0c4a0726 | 197 | #endif |
1b6bf175 TT |
198 | } |
199 | ||
200 | static void check_mount(e2fsck_t ctx) | |
201 | { | |
202 | errcode_t retval; | |
203 | int mount_flags, cont, fd; | |
204 | ||
205 | retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); | |
206 | if (retval) { | |
207 | com_err("ext2fs_check_if_mount", retval, | |
0c4a0726 | 208 | _("while determining whether %s is mounted."), |
1b6bf175 TT |
209 | ctx->filesystem_name); |
210 | return; | |
211 | } | |
212 | if (!(mount_flags & EXT2_MF_MOUNTED)) | |
213 | return; | |
214 | ||
215 | #if (defined(__linux__) && defined(HAVE_MNTENT_H)) | |
216 | /* | |
217 | * If the root is mounted read-only, then /etc/mtab is | |
218 | * probably not correct; so we won't issue a warning based on | |
219 | * it. | |
220 | */ | |
221 | fd = open(MOUNTED, O_RDWR); | |
222 | if (fd < 0) { | |
223 | if (errno == EROFS) | |
224 | return; | |
225 | } else | |
226 | close(fd); | |
227 | #endif | |
228 | ||
229 | if (ctx->options & E2F_OPT_READONLY) { | |
0c4a0726 | 230 | printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name); |
1b6bf175 TT |
231 | return; |
232 | } | |
233 | ||
0c4a0726 | 234 | printf(_("%s is mounted. "), ctx->filesystem_name); |
243dc31f TT |
235 | if (!isatty(0) || !isatty(1)) |
236 | fatal_error(ctx, _("Cannot continue, aborting.\n\n")); | |
0c4a0726 | 237 | printf(_("\n\n\007\007\007\007WARNING!!! " |
74033350 | 238 | "Running e2fsck on a mounted filesystem may cause\n" |
0c4a0726 TT |
239 | "SEVERE filesystem damage.\007\007\007\n\n")); |
240 | cont = ask_yn(_("Do you really want to continue"), -1); | |
1b6bf175 | 241 | if (!cont) { |
0c4a0726 | 242 | printf (_("check aborted.\n")); |
1b6bf175 TT |
243 | exit (0); |
244 | } | |
245 | return; | |
246 | } | |
247 | ||
1b6bf175 TT |
248 | /* |
249 | * This routine checks to see if a filesystem can be skipped; if so, | |
250 | * it will exit with E2FSCK_OK. Under some conditions it will print a | |
251 | * message explaining why a check is being forced. | |
252 | */ | |
253 | static void check_if_skip(e2fsck_t ctx) | |
254 | { | |
255 | ext2_filsys fs = ctx->fs; | |
256 | const char *reason = NULL; | |
b6a0807b | 257 | unsigned int reason_arg = 0; |
1b6bf175 TT |
258 | |
259 | if (force || bad_blocks_file || cflag || swapfs) | |
260 | return; | |
261 | ||
262 | if (fs->super->s_state & EXT2_ERROR_FS) | |
b6a0807b | 263 | reason = _(" contains a file system with errors"); |
24fc5032 | 264 | else if ((fs->super->s_state & EXT2_VALID_FS) == 0) |
b6a0807b | 265 | reason = _(" was not cleanly unmounted"); |
bc57f153 | 266 | else if ((fs->super->s_max_mnt_count > 0) && |
17390c04 | 267 | (fs->super->s_mnt_count >= |
b6a0807b TT |
268 | (unsigned) fs->super->s_max_mnt_count)) { |
269 | reason = _(" has been mounted %u times without being checked"); | |
270 | reason_arg = fs->super->s_mnt_count; | |
271 | } else if (fs->super->s_checkinterval && | |
1b6bf175 | 272 | time(0) >= (fs->super->s_lastcheck + |
b6a0807b TT |
273 | fs->super->s_checkinterval)) { |
274 | reason = _(" has gone %u days without being checked"); | |
275 | reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24); | |
276 | } | |
1b6bf175 | 277 | if (reason) { |
b6a0807b TT |
278 | fputs(ctx->device_name, stdout); |
279 | printf(reason, reason_arg); | |
280 | fputs(_(", check forced.\n"), stdout); | |
1b6bf175 TT |
281 | return; |
282 | } | |
0c4a0726 | 283 | printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name, |
1b6bf175 TT |
284 | fs->super->s_inodes_count - fs->super->s_free_inodes_count, |
285 | fs->super->s_inodes_count, | |
286 | fs->super->s_blocks_count - fs->super->s_free_blocks_count, | |
287 | fs->super->s_blocks_count); | |
288 | ext2fs_close(fs); | |
289 | exit(FSCK_OK); | |
290 | } | |
291 | ||
efac9a1b TT |
292 | /* |
293 | * For completion notice | |
294 | */ | |
5596defa TT |
295 | struct percent_tbl { |
296 | int max_pass; | |
297 | int table[32]; | |
298 | }; | |
299 | struct percent_tbl e2fsck_tbl = { | |
300 | 5, { 0, 70, 90, 92, 95, 100 } | |
301 | }; | |
5596defa TT |
302 | static char bar[] = |
303 | "===============================================================" | |
304 | "==============================================================="; | |
305 | static char spaces[] = | |
306 | " " | |
307 | " "; | |
308 | ||
309 | static float calc_percent(struct percent_tbl *tbl, int pass, int curr, | |
310 | int max) | |
311 | { | |
312 | float percent; | |
313 | ||
314 | if (pass <= 0) | |
315 | return 0.0; | |
b8d164cd | 316 | if (pass > tbl->max_pass || max == 0) |
5596defa TT |
317 | return 100.0; |
318 | percent = ((float) curr) / ((float) max); | |
319 | return ((percent * (tbl->table[pass] - tbl->table[pass-1])) | |
320 | + tbl->table[pass-1]); | |
321 | } | |
322 | ||
323 | extern void e2fsck_clear_progbar(e2fsck_t ctx) | |
324 | { | |
325 | if (!(ctx->flags & E2F_FLAG_PROG_BAR)) | |
326 | return; | |
327 | ||
328 | printf("%s\r", spaces + (sizeof(spaces) - 80)); | |
329 | ctx->flags &= ~E2F_FLAG_PROG_BAR; | |
330 | } | |
331 | ||
efac9a1b TT |
332 | static int e2fsck_update_progress(e2fsck_t ctx, int pass, |
333 | unsigned long cur, unsigned long max) | |
334 | { | |
53ef44c4 | 335 | static const char spinner[] = "\\|/-"; |
efac9a1b | 336 | char buf[80]; |
5596defa TT |
337 | int i; |
338 | float percent; | |
06012323 TT |
339 | int tick; |
340 | struct timeval tv; | |
17390c04 | 341 | static int dpywidth = 0; |
f75c28de TT |
342 | |
343 | if (pass == 0) | |
344 | return 0; | |
efac9a1b TT |
345 | |
346 | if (ctx->progress_fd) { | |
347 | sprintf(buf, "%d %lu %lu\n", pass, cur, max); | |
348 | write(ctx->progress_fd, buf, strlen(buf)); | |
349 | } else { | |
5596defa TT |
350 | if (ctx->flags & E2F_FLAG_PROG_SUPPRESS) |
351 | return 0; | |
17390c04 TT |
352 | if (dpywidth == 0) { |
353 | dpywidth = 66 - strlen(ctx->device_name); | |
354 | dpywidth = 8 * (dpywidth / 8); | |
355 | } | |
e4c8e885 TT |
356 | /* |
357 | * Calculate the new progress position. If the | |
358 | * percentage hasn't changed, then we skip out right | |
359 | * away. | |
360 | */ | |
361 | percent = calc_percent(&e2fsck_tbl, pass, cur, max); | |
362 | if (ctx->progress_last_percent == (int) 10 * percent) | |
363 | return 0; | |
364 | ctx->progress_last_percent = (int) 10 * percent; | |
365 | ||
366 | /* | |
367 | * If we've already updated the spinner once within | |
368 | * the last 1/8th of a second, no point doing it | |
369 | * again. | |
370 | */ | |
06012323 TT |
371 | gettimeofday(&tv, NULL); |
372 | tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8)); | |
373 | if ((tick == ctx->progress_last_time) && | |
374 | (cur != max) && (cur != 0)) | |
375 | return 0; | |
376 | ctx->progress_last_time = tick; | |
e4c8e885 TT |
377 | |
378 | /* | |
379 | * Advance the spinner, and note that the progress bar | |
380 | * will be on the screen | |
381 | */ | |
efac9a1b | 382 | ctx->progress_pos = (ctx->progress_pos+1) & 3; |
5596defa | 383 | ctx->flags |= E2F_FLAG_PROG_BAR; |
e4c8e885 | 384 | |
5596defa TT |
385 | i = ((percent * dpywidth) + 50) / 100; |
386 | printf("%s: |%s%s", ctx->device_name, | |
387 | bar + (sizeof(bar) - (i+1)), | |
388 | spaces + (sizeof(spaces) - (dpywidth - i + 1))); | |
389 | if (percent == 100.0) | |
390 | fputc('|', stdout); | |
391 | else | |
392 | fputc(spinner[ctx->progress_pos & 3], stdout); | |
393 | printf(" %4.1f%% \r", percent); | |
394 | if (percent == 100.0) | |
395 | e2fsck_clear_progbar(ctx); | |
efac9a1b TT |
396 | fflush(stdout); |
397 | } | |
398 | return 0; | |
399 | } | |
1b6bf175 TT |
400 | |
401 | #define PATH_SET "PATH=/sbin" | |
402 | ||
5ba23cb1 | 403 | static void reserve_stdio_fds(void) |
24fc5032 TT |
404 | { |
405 | int fd; | |
406 | ||
407 | while (1) { | |
408 | fd = open("/dev/null", O_RDWR); | |
409 | if (fd > 2) | |
410 | break; | |
75d83bec | 411 | if (fd < 0) { |
0c4a0726 TT |
412 | fprintf(stderr, _("ERROR: Couldn't open " |
413 | "/dev/null (%s)\n"), | |
75d83bec TT |
414 | strerror(errno)); |
415 | break; | |
416 | } | |
24fc5032 TT |
417 | } |
418 | close(fd); | |
419 | } | |
420 | ||
9ecd8bec | 421 | #ifdef HAVE_SIGNAL_H |
5596defa TT |
422 | static void signal_progress_on(int sig) |
423 | { | |
243dc31f | 424 | e2fsck_t ctx = e2fsck_global_ctx; |
5596defa TT |
425 | |
426 | if (!ctx) | |
427 | return; | |
428 | ||
429 | ctx->progress = e2fsck_update_progress; | |
430 | ctx->progress_fd = 0; | |
431 | } | |
432 | ||
433 | static void signal_progress_off(int sig) | |
434 | { | |
243dc31f | 435 | e2fsck_t ctx = e2fsck_global_ctx; |
5596defa TT |
436 | |
437 | if (!ctx) | |
438 | return; | |
439 | ||
440 | e2fsck_clear_progbar(ctx); | |
441 | ctx->progress = 0; | |
442 | } | |
9ecd8bec | 443 | #endif |
5596defa | 444 | |
1b6bf175 TT |
445 | static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx) |
446 | { | |
447 | int flush = 0; | |
ae8160e6 | 448 | int c, fd; |
1b6bf175 TT |
449 | #ifdef MTRACE |
450 | extern void *mallwatch; | |
451 | #endif | |
452 | char *oldpath = getenv("PATH"); | |
453 | e2fsck_t ctx; | |
454 | errcode_t retval; | |
9ecd8bec | 455 | #ifdef HAVE_SIGNAL_H |
5596defa | 456 | struct sigaction sa; |
9ecd8bec | 457 | #endif |
1b6bf175 TT |
458 | |
459 | retval = e2fsck_allocate_context(&ctx); | |
460 | if (retval) | |
461 | return retval; | |
462 | ||
463 | *ret_ctx = ctx; | |
464 | ||
465 | /* Update our PATH to include /sbin */ | |
466 | if (oldpath) { | |
467 | char *newpath; | |
468 | ||
54dc7ca2 TT |
469 | newpath = (char *) malloc(sizeof (PATH_SET) + 1 + |
470 | strlen (oldpath)); | |
1b6bf175 | 471 | if (!newpath) |
f8188fff | 472 | fatal_error(ctx, "Couldn't malloc() newpath"); |
1b6bf175 TT |
473 | strcpy (newpath, PATH_SET); |
474 | strcat (newpath, ":"); | |
475 | strcat (newpath, oldpath); | |
476 | putenv (newpath); | |
477 | } else | |
478 | putenv (PATH_SET); | |
479 | ||
480 | setbuf(stdout, NULL); | |
481 | setbuf(stderr, NULL); | |
482 | initialize_ext2_error_table(); | |
483 | ||
484 | if (argc && *argv) | |
485 | ctx->program_name = *argv; | |
486 | else | |
487 | ctx->program_name = "e2fsck"; | |
efac9a1b | 488 | while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF) |
1b6bf175 | 489 | switch (c) { |
efac9a1b TT |
490 | case 'C': |
491 | ctx->progress = e2fsck_update_progress; | |
492 | ctx->progress_fd = atoi(optarg); | |
fc9a69ca TT |
493 | if (!ctx->progress_fd) |
494 | break; | |
ae8160e6 TT |
495 | /* Validate the file descriptor to avoid disasters */ |
496 | fd = dup(ctx->progress_fd); | |
497 | if (fd < 0) { | |
498 | fprintf(stderr, | |
499 | _("Error validating file descriptor %d: %s\n"), | |
500 | ctx->progress_fd, | |
501 | error_message(errno)); | |
502 | fatal_error(ctx, | |
503 | _("Invalid completion information file descriptor")); | |
504 | } else | |
505 | close(fd); | |
efac9a1b | 506 | break; |
1b6bf175 TT |
507 | case 'p': |
508 | case 'a': | |
509 | ctx->options |= E2F_OPT_PREEN; | |
510 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO); | |
511 | break; | |
512 | case 'n': | |
513 | ctx->options |= E2F_OPT_NO; | |
514 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN); | |
515 | break; | |
516 | case 'y': | |
517 | ctx->options |= E2F_OPT_YES; | |
518 | ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO); | |
519 | break; | |
520 | case 't': | |
8bf191e8 | 521 | #ifdef RESOURCE_TRACK |
1b6bf175 TT |
522 | if (ctx->options & E2F_OPT_TIME) |
523 | ctx->options |= E2F_OPT_TIME2; | |
524 | else | |
525 | ctx->options |= E2F_OPT_TIME; | |
8bf191e8 | 526 | #else |
0c4a0726 TT |
527 | fprintf(stderr, _("The -t option is not " |
528 | "supported on this version of e2fsck.\n")); | |
8bf191e8 | 529 | #endif |
1b6bf175 TT |
530 | break; |
531 | case 'c': | |
532 | cflag++; | |
533 | ctx->options |= E2F_OPT_CHECKBLOCKS; | |
534 | break; | |
535 | case 'r': | |
536 | /* What we do by default, anyway! */ | |
537 | break; | |
538 | case 'b': | |
539 | ctx->use_superblock = atoi(optarg); | |
540 | break; | |
541 | case 'B': | |
542 | blocksize = atoi(optarg); | |
543 | break; | |
544 | case 'I': | |
545 | ctx->inode_buffer_blocks = atoi(optarg); | |
546 | break; | |
547 | case 'P': | |
548 | ctx->process_inode_size = atoi(optarg); | |
549 | break; | |
550 | case 'L': | |
551 | replace_bad_blocks++; | |
552 | case 'l': | |
54dc7ca2 | 553 | bad_blocks_file = (char *) malloc(strlen(optarg)+1); |
1b6bf175 | 554 | if (!bad_blocks_file) |
f8188fff TT |
555 | fatal_error(ctx, |
556 | "Couldn't malloc bad_blocks_file"); | |
1b6bf175 TT |
557 | strcpy(bad_blocks_file, optarg); |
558 | break; | |
559 | case 'd': | |
560 | ctx->options |= E2F_OPT_DEBUG; | |
561 | break; | |
562 | case 'f': | |
563 | force = 1; | |
564 | break; | |
565 | case 'F': | |
1b6bf175 | 566 | flush = 1; |
1b6bf175 TT |
567 | break; |
568 | case 'v': | |
569 | verbose = 1; | |
570 | break; | |
571 | case 'V': | |
572 | show_version_only = 1; | |
573 | break; | |
574 | #ifdef MTRACE | |
575 | case 'M': | |
576 | mallwatch = (void *) strtol(optarg, NULL, 0); | |
577 | break; | |
578 | #endif | |
579 | case 'N': | |
580 | ctx->device_name = optarg; | |
581 | break; | |
5df55d7f | 582 | #ifdef ENABLE_SWAPFS |
1b6bf175 TT |
583 | case 's': |
584 | normalize_swapfs = 1; | |
585 | case 'S': | |
586 | swapfs = 1; | |
587 | break; | |
5df55d7f TT |
588 | #else |
589 | case 's': | |
590 | case 'S': | |
591 | fprintf(stderr, _("Byte-swapping filesystems " | |
592 | "not compiled in this version " | |
593 | "of e2fsck\n")); | |
594 | exit(1); | |
595 | #endif | |
1b6bf175 TT |
596 | default: |
597 | usage(ctx); | |
598 | } | |
599 | if (show_version_only) | |
600 | return 0; | |
601 | if (optind != argc - 1) | |
602 | usage(ctx); | |
603 | if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file && | |
604 | !cflag && !swapfs) | |
605 | ctx->options |= E2F_OPT_READONLY; | |
606 | ctx->filesystem_name = argv[optind]; | |
28ffafb0 | 607 | if (flush) { |
1b6bf175 TT |
608 | int fd = open(ctx->filesystem_name, O_RDONLY, 0); |
609 | ||
610 | if (fd < 0) { | |
0c4a0726 TT |
611 | com_err("open", errno, |
612 | _("while opening %s for flushing"), | |
1b6bf175 | 613 | ctx->filesystem_name); |
243dc31f | 614 | fatal_error(ctx, 0); |
1b6bf175 | 615 | } |
5df55d7f | 616 | if ((retval = ext2fs_sync_device(fd, 1))) { |
5ba23cb1 | 617 | com_err("ext2fs_sync_device", retval, |
0c4a0726 | 618 | _("while trying to flush %s"), |
1b6bf175 | 619 | ctx->filesystem_name); |
243dc31f | 620 | fatal_error(ctx, 0); |
1b6bf175 TT |
621 | } |
622 | close(fd); | |
1b6bf175 | 623 | } |
5df55d7f | 624 | #ifdef ENABLE_SWAPFS |
1b6bf175 TT |
625 | if (swapfs) { |
626 | if (cflag || bad_blocks_file) { | |
0c4a0726 TT |
627 | fprintf(stderr, _("Incompatible options not " |
628 | "allowed when byte-swapping.\n")); | |
243dc31f | 629 | exit(FSCK_USAGE); |
1b6bf175 TT |
630 | } |
631 | } | |
5df55d7f | 632 | #endif |
9ecd8bec | 633 | #ifdef HAVE_SIGNAL_H |
5596defa TT |
634 | /* |
635 | * Set up signal action | |
636 | */ | |
637 | memset(&sa, 0, sizeof(struct sigaction)); | |
638 | #ifdef SA_RESTART | |
639 | sa.sa_flags = SA_RESTART; | |
640 | #endif | |
243dc31f | 641 | e2fsck_global_ctx = ctx; |
5596defa TT |
642 | sa.sa_handler = signal_progress_on; |
643 | sigaction(SIGUSR1, &sa, 0); | |
644 | sa.sa_handler = signal_progress_off; | |
645 | sigaction(SIGUSR2, &sa, 0); | |
9ecd8bec | 646 | #endif |
1b6bf175 TT |
647 | return 0; |
648 | } | |
649 | ||
650 | static const char *my_ver_string = E2FSPROGS_VERSION; | |
651 | static const char *my_ver_date = E2FSPROGS_DATE; | |
652 | ||
653 | int main (int argc, char *argv[]) | |
654 | { | |
655 | errcode_t retval = 0; | |
656 | int exit_value = FSCK_OK; | |
657 | int i; | |
658 | ext2_filsys fs = 0; | |
659 | io_manager io_ptr; | |
5dd8f963 | 660 | struct ext2_super_block *sb; |
1b6bf175 TT |
661 | const char *lib_ver_date; |
662 | int my_ver, lib_ver; | |
663 | e2fsck_t ctx; | |
664 | struct problem_context pctx; | |
08b21301 | 665 | int flags, run_result; |
1b6bf175 TT |
666 | |
667 | clear_problem_context(&pctx); | |
668 | #ifdef MTRACE | |
669 | mtrace(); | |
670 | #endif | |
671 | #ifdef MCHECK | |
672 | mcheck(0); | |
0c4a0726 TT |
673 | #endif |
674 | #ifdef ENABLE_NLS | |
675 | setlocale(LC_MESSAGES, ""); | |
676 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | |
677 | textdomain(NLS_CAT_NAME); | |
1b6bf175 TT |
678 | #endif |
679 | my_ver = ext2fs_parse_version_string(my_ver_string); | |
680 | lib_ver = ext2fs_get_library_version(0, &lib_ver_date); | |
681 | if (my_ver > lib_ver) { | |
0c4a0726 TT |
682 | fprintf( stderr, _("Error: ext2fs library version " |
683 | "out of date!\n")); | |
1b6bf175 TT |
684 | show_version_only++; |
685 | } | |
686 | ||
687 | retval = PRS(argc, argv, &ctx); | |
688 | if (retval) { | |
689 | com_err("e2fsck", retval, | |
0c4a0726 | 690 | _("while trying to initialize program")); |
243dc31f | 691 | exit(FSCK_ERROR); |
1b6bf175 | 692 | } |
24fc5032 TT |
693 | reserve_stdio_fds(); |
694 | ||
8bf191e8 | 695 | #ifdef RESOURCE_TRACK |
1b6bf175 | 696 | init_resource_track(&ctx->global_rtrack); |
8bf191e8 | 697 | #endif |
1b6bf175 TT |
698 | |
699 | if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) | |
0c4a0726 | 700 | fprintf (stderr, _("e2fsck %s, %s for EXT2 FS %s, %s\n"), |
1b6bf175 TT |
701 | my_ver_string, my_ver_date, EXT2FS_VERSION, |
702 | EXT2FS_DATE); | |
703 | ||
704 | if (show_version_only) { | |
0c4a0726 | 705 | fprintf(stderr, _("\tUsing %s, %s\n"), |
1b6bf175 | 706 | error_message(EXT2_ET_BASE), lib_ver_date); |
243dc31f | 707 | exit(FSCK_OK); |
1b6bf175 TT |
708 | } |
709 | ||
710 | check_mount(ctx); | |
711 | ||
712 | if (!(ctx->options & E2F_OPT_PREEN) && | |
713 | !(ctx->options & E2F_OPT_NO) && | |
714 | !(ctx->options & E2F_OPT_YES)) { | |
715 | if (!isatty (0) || !isatty (1)) | |
f8188fff | 716 | fatal_error(ctx, |
0c4a0726 | 717 | _("need terminal for interactive repairs")); |
1b6bf175 TT |
718 | } |
719 | ctx->superblock = ctx->use_superblock; | |
720 | restart: | |
721 | #if 1 | |
722 | io_ptr = unix_io_manager; | |
723 | #else | |
724 | io_ptr = test_io_manager; | |
725 | test_io_backing_manager = unix_io_manager; | |
726 | #endif | |
17390c04 TT |
727 | flags = 0; |
728 | if ((ctx->options & E2F_OPT_READONLY) == 0) | |
729 | flags |= EXT2_FLAG_RW; | |
730 | ||
1b6bf175 TT |
731 | if (ctx->superblock && blocksize) { |
732 | retval = ext2fs_open(ctx->filesystem_name, flags, | |
733 | ctx->superblock, blocksize, io_ptr, &fs); | |
734 | } else if (ctx->superblock) { | |
735 | for (i=0; possible_block_sizes[i]; i++) { | |
736 | retval = ext2fs_open(ctx->filesystem_name, flags, | |
737 | ctx->superblock, | |
738 | possible_block_sizes[i], | |
739 | io_ptr, &fs); | |
740 | if (!retval) | |
741 | break; | |
742 | } | |
743 | } else | |
744 | retval = ext2fs_open(ctx->filesystem_name, flags, | |
745 | 0, 0, io_ptr, &fs); | |
746 | if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && | |
747 | ((retval == EXT2_ET_BAD_MAGIC) || | |
748 | ((retval == 0) && ext2fs_check_desc(fs)))) { | |
749 | if (!fs || (fs->group_desc_count > 1)) { | |
0c4a0726 TT |
750 | printf(_("%s trying backup blocks...\n"), |
751 | retval ? _("Couldn't find ext2 superblock,") : | |
752 | _("Group descriptors look bad...")); | |
1b6bf175 TT |
753 | ctx->superblock = get_backup_sb(fs); |
754 | if (fs) | |
755 | ext2fs_close(fs); | |
756 | goto restart; | |
757 | } | |
758 | } | |
759 | if (retval) { | |
0c4a0726 | 760 | com_err(ctx->program_name, retval, _("while trying to open %s"), |
1b6bf175 | 761 | ctx->filesystem_name); |
24dd4028 | 762 | if (retval == EXT2_ET_REV_TOO_HIGH) { |
0c4a0726 | 763 | printf(_("The filesystem revision is apparently " |
24dd4028 TT |
764 | "too high for this version of e2fsck.\n" |
765 | "(Or the filesystem superblock " | |
0c4a0726 | 766 | "is corrupt)\n\n")); |
24dd4028 TT |
767 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); |
768 | } else if (retval == EXT2_ET_SHORT_READ) | |
0c4a0726 | 769 | printf(_("Could this be a zero-length partition?\n")); |
1b6bf175 | 770 | else if ((retval == EPERM) || (retval == EACCES)) |
0c4a0726 TT |
771 | printf(_("You must have %s access to the " |
772 | "filesystem or be root\n"), | |
1b6bf175 TT |
773 | (ctx->options & E2F_OPT_READONLY) ? |
774 | "r/o" : "r/w"); | |
775 | else if (retval == ENXIO) | |
0c4a0726 | 776 | printf(_("Possibly non-existent or swap device?\n")); |
68227542 TT |
777 | #ifdef EROFS |
778 | else if (retval == EROFS) | |
0c4a0726 | 779 | printf(_("Disk write-protected; use the -n option " |
68227542 | 780 | "to do a read-only\n" |
0c4a0726 | 781 | "check of the device.\n")); |
68227542 | 782 | #endif |
1b6bf175 TT |
783 | else |
784 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); | |
243dc31f | 785 | fatal_error(ctx, 0); |
1b6bf175 TT |
786 | } |
787 | ctx->fs = fs; | |
54dc7ca2 | 788 | fs->priv_data = ctx; |
5dd8f963 TT |
789 | sb = fs->super; |
790 | if (sb->s_rev_level > E2FSCK_CURRENT_REV) { | |
1b6bf175 | 791 | com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, |
0c4a0726 | 792 | _("while trying to open %s"), |
1b6bf175 | 793 | ctx->filesystem_name); |
f8188fff | 794 | get_newer: |
0c4a0726 | 795 | fatal_error(ctx, _("Get a newer version of e2fsck!")); |
1b6bf175 | 796 | } |
3b5386dc TT |
797 | |
798 | /* | |
799 | * Set the device name, which is used whenever we print error | |
800 | * or informational messages to the user. | |
801 | */ | |
802 | if (ctx->device_name == 0 && | |
5dd8f963 TT |
803 | (sb->s_volume_name[0] != 0)) { |
804 | char *cp = malloc(sizeof(sb->s_volume_name)+1); | |
3b5386dc | 805 | if (cp) { |
5dd8f963 TT |
806 | strncpy(cp, sb->s_volume_name, |
807 | sizeof(sb->s_volume_name)); | |
808 | cp[sizeof(sb->s_volume_name)] = 0; | |
3b5386dc TT |
809 | ctx->device_name = cp; |
810 | } | |
811 | } | |
812 | if (ctx->device_name == 0) | |
813 | ctx->device_name = ctx->filesystem_name; | |
814 | ||
17390c04 | 815 | /* |
9b565759 | 816 | * Make sure the ext3 superblock fields are consistent. |
17390c04 | 817 | */ |
3b5386dc TT |
818 | retval = e2fsck_check_ext3_journal(ctx); |
819 | if (retval) { | |
820 | com_err(ctx->program_name, retval, | |
821 | _("while checking ext3 journal for %s"), | |
822 | ctx->device_name); | |
823 | ext2fs_close(ctx->fs); | |
243dc31f | 824 | fatal_error(ctx, 0); |
3b5386dc TT |
825 | } |
826 | ||
9b565759 TT |
827 | /* |
828 | * Check to see if we need to do ext3-style recovery. If so, | |
829 | * do it, and then restart the fsck. | |
830 | */ | |
5dd8f963 | 831 | if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) { |
2575fb04 TT |
832 | if (ctx->options & E2F_OPT_READONLY) { |
833 | printf(_("Warning: skipping journal recovery " | |
834 | "because doing a read-only filesystem " | |
835 | "check.\n")); | |
836 | io_channel_flush(ctx->fs->io); | |
837 | } else { | |
838 | retval = e2fsck_run_ext3_journal(ctx); | |
839 | if (retval) { | |
840 | com_err(ctx->program_name, retval, | |
3b5386dc | 841 | _("while recovering ext3 journal of %s"), |
2575fb04 TT |
842 | ctx->device_name); |
843 | fatal_error(ctx, 0); | |
844 | } | |
845 | ext2fs_close(ctx->fs); | |
846 | ctx->fs = 0; | |
847 | goto restart; | |
17390c04 | 848 | } |
17390c04 | 849 | } |
3b5386dc | 850 | |
1b6bf175 TT |
851 | /* |
852 | * Check for compatibility with the feature sets. We need to | |
853 | * be more stringent than ext2fs_open(). | |
854 | */ | |
5dd8f963 TT |
855 | if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || |
856 | (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { | |
1b6bf175 | 857 | com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE, |
3b5386dc | 858 | "(%s)", ctx->device_name); |
f8188fff | 859 | goto get_newer; |
1b6bf175 | 860 | } |
5dd8f963 | 861 | if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { |
1b6bf175 | 862 | com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE, |
3b5386dc | 863 | "(%s)", ctx->device_name); |
1b6bf175 TT |
864 | goto get_newer; |
865 | } | |
1917875f | 866 | #ifdef ENABLE_COMPRESSION |
5dd8f963 | 867 | if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION) |
1917875f TT |
868 | com_err(ctx->program_name, 0, |
869 | _("Warning: compression support is experimental.\n")); | |
870 | #endif | |
1b6bf175 TT |
871 | |
872 | /* | |
873 | * If the user specified a specific superblock, presumably the | |
874 | * master superblock has been trashed. So we mark the | |
875 | * superblock as dirty, so it can be written out. | |
876 | */ | |
877 | if (ctx->superblock && | |
878 | !(ctx->options & E2F_OPT_READONLY)) | |
879 | ext2fs_mark_super_dirty(fs); | |
880 | ||
881 | /* | |
882 | * Don't overwrite the backup superblock and block | |
883 | * descriptors, until we're sure the filesystem is OK.... | |
884 | */ | |
885 | fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; | |
886 | ||
887 | ehandler_init(fs->io); | |
888 | ||
889 | if (ctx->superblock) | |
890 | set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); | |
891 | check_super_block(ctx); | |
a02ce9df | 892 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
243dc31f | 893 | fatal_error(ctx, 0); |
1b6bf175 TT |
894 | check_if_skip(ctx); |
895 | if (bad_blocks_file) | |
896 | read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); | |
897 | else if (cflag) | |
898 | test_disk(ctx); | |
a02ce9df | 899 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
243dc31f | 900 | fatal_error(ctx, 0); |
5df55d7f | 901 | #ifdef ENABLE_SWAPFS |
1b6bf175 TT |
902 | if (normalize_swapfs) { |
903 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == | |
904 | ext2fs_native_flag()) { | |
0c4a0726 TT |
905 | fprintf(stderr, _("%s: Filesystem byte order " |
906 | "already normalized.\n"), ctx->device_name); | |
243dc31f | 907 | fatal_error(ctx, 0); |
1b6bf175 TT |
908 | } |
909 | } | |
f8188fff | 910 | if (swapfs) { |
1b6bf175 | 911 | swap_filesys(ctx); |
a02ce9df | 912 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
243dc31f | 913 | fatal_error(ctx, 0); |
f8188fff | 914 | } |
5df55d7f | 915 | #endif |
1b6bf175 TT |
916 | |
917 | /* | |
918 | * Mark the system as valid, 'til proven otherwise | |
919 | */ | |
920 | ext2fs_mark_valid(fs); | |
921 | ||
922 | retval = ext2fs_read_bb_inode(fs, &fs->badblocks); | |
923 | if (retval) { | |
924 | com_err(ctx->program_name, retval, | |
0c4a0726 | 925 | _("while reading bad blocks inode")); |
1b6bf175 | 926 | preenhalt(ctx); |
0c4a0726 TT |
927 | printf(_("This doesn't bode well," |
928 | " but we'll try to go on...\n")); | |
1b6bf175 | 929 | } |
08b21301 TT |
930 | |
931 | run_result = e2fsck_run(ctx); | |
5596defa | 932 | e2fsck_clear_progbar(ctx); |
08b21301 | 933 | if (run_result == E2F_FLAG_RESTART) { |
0c4a0726 | 934 | printf(_("Restarting e2fsck from the beginning...\n")); |
1b6bf175 TT |
935 | retval = e2fsck_reset_context(ctx); |
936 | if (retval) { | |
937 | com_err(ctx->program_name, retval, | |
0c4a0726 | 938 | _("while resetting context")); |
243dc31f | 939 | fatal_error(ctx, 0); |
1b6bf175 | 940 | } |
73f17cfc | 941 | ext2fs_close(fs); |
1b6bf175 TT |
942 | goto restart; |
943 | } | |
a02ce9df | 944 | if (run_result & E2F_FLAG_SIGNAL_MASK) |
243dc31f | 945 | fatal_error(ctx, 0); |
08b21301 TT |
946 | if (run_result & E2F_FLAG_CANCEL) |
947 | ext2fs_unmark_valid(fs); | |
1b6bf175 TT |
948 | |
949 | #ifdef MTRACE | |
950 | mtrace_print("Cleanup"); | |
951 | #endif | |
952 | if (ext2fs_test_changed(fs)) { | |
953 | exit_value = FSCK_NONDESTRUCT; | |
954 | if (!(ctx->options & E2F_OPT_PREEN)) | |
0c4a0726 | 955 | printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"), |
1b6bf175 TT |
956 | ctx->device_name); |
957 | if (root_filesystem && !read_only_root) { | |
0c4a0726 | 958 | printf(_("%s: ***** REBOOT LINUX *****\n"), |
1b6bf175 TT |
959 | ctx->device_name); |
960 | exit_value = FSCK_REBOOT; | |
961 | } | |
962 | } | |
963 | if (ext2fs_test_valid(fs)) | |
964 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; | |
965 | else | |
966 | exit_value = FSCK_UNCORRECTED; | |
967 | if (!(ctx->options & E2F_OPT_READONLY)) { | |
968 | if (ext2fs_test_valid(fs)) { | |
5dd8f963 | 969 | if (!(sb->s_state & EXT2_VALID_FS)) |
1b6bf175 | 970 | exit_value = FSCK_NONDESTRUCT; |
5dd8f963 | 971 | sb->s_state = EXT2_VALID_FS; |
1b6bf175 | 972 | } else |
5dd8f963 TT |
973 | sb->s_state &= ~EXT2_VALID_FS; |
974 | sb->s_mnt_count = 0; | |
975 | sb->s_lastcheck = time(NULL); | |
1b6bf175 TT |
976 | ext2fs_mark_super_dirty(fs); |
977 | } | |
978 | show_stats(ctx); | |
979 | ||
f8188fff | 980 | e2fsck_write_bitmaps(ctx); |
1b6bf175 | 981 | |
8bf191e8 | 982 | #ifdef RESOURCE_TRACK |
1b6bf175 TT |
983 | if (ctx->options & E2F_OPT_TIME) |
984 | print_resource_track(NULL, &ctx->global_rtrack); | |
8bf191e8 | 985 | #endif |
1b6bf175 | 986 | |
1dde43f0 | 987 | ext2fs_close(fs); |
7142db08 TT |
988 | ctx->fs = NULL; |
989 | e2fsck_free_context(ctx); | |
1b6bf175 TT |
990 | |
991 | return exit_value; | |
992 | } |