rezize2fs: fix memory leak when fixing up the orphan file inode
[e2fsprogs.git] / misc / fsck.c
blob64d0e7c06375faf731f144986937b1edb1a337c8
1 /*
2 * pfsck --- A generic, parallelizing front-end for the fsck program.
3 * It will automatically try to run fsck programs in parallel if the
4 * devices are on separate spindles. It is based on the same ideas as
5 * the generic front end for fsck by David Engel and Fred van Kempen,
6 * but it has been completely rewritten from scratch to support
7 * parallel execution.
9 * Written by Theodore Ts'o, <tytso@mit.edu>
11 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
12 * o Changed -t fstype to behave like with mount when -A (all file
13 * systems) or -M (like mount) is specified.
14 * o fsck looks if it can find the fsck.type program to decide
15 * if it should ignore the fs type. This way more fsck programs
16 * can be added without changing this front-end.
17 * o -R flag skip root file system.
19 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
22 * %Begin-Header%
23 * This file may be redistributed under the terms of the GNU Public
24 * License.
25 * %End-Header%
28 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
30 #include "config.h"
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/stat.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <time.h>
39 #if HAVE_STDLIB_H
40 #include <stdlib.h>
41 #endif
42 #if HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
45 #if HAVE_PATHS_H
46 #include <paths.h>
47 #endif
48 #if HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #if HAVE_ERRNO_H
52 #include <errno.h>
53 #endif
54 #if HAVE_MALLOC_H
55 #include <malloc.h>
56 #endif
57 #ifdef HAVE_SIGNAL_H
58 #include <signal.h>
59 #endif
61 #include "../version.h"
62 #include "support/devname.h"
63 #include "support/nls-enable.h"
64 #include "fsck.h"
65 #include "blkid/blkid.h"
67 #ifndef _PATH_MNTTAB
68 #define _PATH_MNTTAB "/etc/fstab"
69 #endif
71 static const char *ignored_types[] = {
72 "ignore",
73 "iso9660",
74 "nfs",
75 "proc",
76 "sw",
77 "swap",
78 "tmpfs",
79 "devpts",
80 NULL
83 static const char *really_wanted[] = {
84 "minix",
85 "ext2",
86 "ext3",
87 "ext4",
88 "ext4dev",
89 "jfs",
90 "reiserfs",
91 "xiafs",
92 "xfs",
93 NULL
96 #define BASE_MD "/dev/md"
99 * Global variables for options
101 static char *devices[MAX_DEVICES];
102 static char *args[MAX_ARGS];
103 static int num_devices, num_args;
105 static int verbose = 0;
106 static int doall = 0;
107 static int noexecute = 0;
108 static int serialize = 0;
109 static int skip_root = 0;
110 static int ignore_mounted = 0;
111 static int notitle = 0;
112 static int parallel_root = 0;
113 static int progress = 0;
114 static int progress_fd = 0;
115 static int force_all_parallel = 0;
116 static int num_running = 0;
117 static int max_running = 0;
118 static volatile int cancel_requested = 0;
119 static int kill_sent = 0;
120 static char *progname;
121 static char *fstype = NULL;
122 static struct fs_info *filesys_info = NULL, *filesys_last = NULL;
123 static struct fsck_instance *instance_list;
124 static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
125 static char *fsck_path = 0;
126 static blkid_cache cache = NULL;
128 static char *string_copy(const char *s)
130 char *ret;
132 if (!s)
133 return 0;
134 ret = malloc(strlen(s)+1);
135 if (ret)
136 strcpy(ret, s);
137 return ret;
140 static int string_to_int(const char *s)
142 long l;
143 char *p;
145 l = strtol(s, &p, 0);
146 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
147 return -1;
148 else
149 return (int) l;
152 static int ignore(struct fs_info *);
154 static char *skip_over_blank(char *cp)
156 while (*cp && isspace(*cp))
157 cp++;
158 return cp;
161 static char *skip_over_word(char *cp)
163 while (*cp && !isspace(*cp))
164 cp++;
165 return cp;
168 static void strip_line(char *line)
170 char *p;
172 while (*line) {
173 p = line + strlen(line) - 1;
174 if ((*p == '\n') || (*p == '\r'))
175 *p = 0;
176 else
177 break;
181 static char *parse_word(char **buf)
183 char *word, *next;
185 word = *buf;
186 if (*word == 0)
187 return 0;
189 word = skip_over_blank(word);
190 next = skip_over_word(word);
191 if (*next)
192 *next++ = 0;
193 *buf = next;
194 return word;
197 static void parse_escape(char *word)
199 char *p, *q;
200 int ac, i;
202 if (!word)
203 return;
205 for (p = word, q = word; *p; p++, q++) {
206 *q = *p;
207 if (*p != '\\')
208 continue;
209 if (*++p == 0)
210 break;
211 if (*p == 't') {
212 *q = '\t';
213 continue;
215 if (*p == 'n') {
216 *q = '\n';
217 continue;
219 if (!isdigit(*p)) {
220 *q = *p;
221 continue;
223 ac = 0;
224 for (i = 0; i < 3; i++, p++) {
225 if (!isdigit(*p))
226 break;
227 ac = (ac * 8) + (*p - '0');
229 *q = ac;
230 p--;
232 *q = 0;
235 static void free_instance(struct fsck_instance *i)
237 free(i->prog);
238 free(i->device);
239 free(i->base_device);
240 free(i);
241 return;
244 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
245 const char *type, const char *opts,
246 int freq, int passno)
248 struct fs_info *fs;
250 if (!(fs = malloc(sizeof(struct fs_info))))
251 return NULL;
253 fs->device = string_copy(device);
254 fs->mountpt = string_copy(mntpnt);
255 fs->type = string_copy(type);
256 fs->opts = string_copy(opts ? opts : "");
257 fs->freq = freq;
258 fs->passno = passno;
259 fs->flags = 0;
260 fs->next = NULL;
262 if (!filesys_info)
263 filesys_info = fs;
264 else
265 filesys_last->next = fs;
266 filesys_last = fs;
268 return fs;
273 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
275 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
276 struct fs_info *fs;
278 *ret_fs = 0;
279 strip_line(line);
280 cp = line;
282 device = parse_word(&cp);
283 if (!device || *device == '#')
284 return 0; /* Ignore blank lines and comments */
285 mntpnt = parse_word(&cp);
286 type = parse_word(&cp);
287 opts = parse_word(&cp);
288 freq = parse_word(&cp);
289 passno = parse_word(&cp);
291 if (!mntpnt || !type)
292 return -1;
294 parse_escape(device);
295 parse_escape(mntpnt);
296 parse_escape(type);
297 parse_escape(opts);
298 parse_escape(freq);
299 parse_escape(passno);
301 dev = get_devname(cache, device, NULL);
302 if (dev)
303 device = dev;
305 if (strchr(type, ','))
306 type = 0;
308 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
309 freq ? atoi(freq) : -1,
310 passno ? atoi(passno) : -1);
311 free(dev);
313 if (!fs)
314 return -1;
315 *ret_fs = fs;
316 return 0;
319 static void interpret_type(struct fs_info *fs)
321 char *t;
323 if (strcmp(fs->type, "auto") != 0)
324 return;
325 t = blkid_get_tag_value(cache, "TYPE", fs->device);
326 if (t) {
327 free(fs->type);
328 fs->type = t;
333 * Load the filesystem database from /etc/fstab
335 static void load_fs_info(const char *filename)
337 FILE *f;
338 char buf[1024];
339 int lineno = 0;
340 int old_fstab = 1;
341 struct fs_info *fs;
343 if ((f = fopen(filename, "r")) == NULL) {
344 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
345 filename, strerror(errno));
346 return;
348 while (!feof(f)) {
349 lineno++;
350 if (!fgets(buf, sizeof(buf), f))
351 break;
352 buf[sizeof(buf)-1] = 0;
353 if (parse_fstab_line(buf, &fs) < 0) {
354 fprintf(stderr, _("WARNING: bad format "
355 "on line %d of %s\n"), lineno, filename);
356 continue;
358 if (!fs)
359 continue;
360 if (fs->passno < 0)
361 fs->passno = 0;
362 else
363 old_fstab = 0;
366 fclose(f);
368 if (old_fstab && filesys_info) {
369 fputs("\007\007\007", stderr);
370 fputs(_(
371 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
372 " field. I will kludge around things for you, but you\n"
373 " should fix your /etc/fstab file as soon as you can.\n\n"), stderr);
375 for (fs = filesys_info; fs; fs = fs->next) {
376 fs->passno = 1;
381 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
382 static struct fs_info *lookup(char *filesys)
384 struct fs_info *fs;
386 /* No filesys name given. */
387 if (filesys == NULL)
388 return NULL;
390 for (fs = filesys_info; fs; fs = fs->next) {
391 if (!strcmp(filesys, fs->device) ||
392 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
393 break;
396 return fs;
399 /* Find fsck program for a given fs type. */
400 static char *find_fsck(char *type)
402 char *s;
403 const char *tpl;
404 static char prog[256];
405 char *p = string_copy(fsck_path);
406 struct stat st;
408 /* Are we looking for a program or just a type? */
409 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
411 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
412 int len = snprintf(prog, sizeof(prog), tpl, s, type);
414 if ((len < 0) || (len >= (int) sizeof(prog)))
415 continue;
416 if (stat(prog, &st) == 0)
417 break;
419 free(p);
420 return(s ? prog : NULL);
423 static int progress_active(NOARGS)
425 struct fsck_instance *inst;
427 for (inst = instance_list; inst; inst = inst->next) {
428 if (inst->flags & FLAG_DONE)
429 continue;
430 if (inst->flags & FLAG_PROGRESS)
431 return 1;
433 return 0;
437 * Execute a particular fsck program, and link it into the list of
438 * child processes we are waiting for.
440 static int execute(const char *type, const char *device, const char *mntpt,
441 int interactive)
443 char *s, *argv[80], prog[256];
444 int argc, i, len;
445 struct fsck_instance *inst, *p;
446 pid_t pid;
448 len = snprintf(prog, sizeof(prog), "fsck.%s", type);
449 if ((len < 0) || (len >= (int) sizeof(prog)))
450 return EINVAL;
452 inst = malloc(sizeof(struct fsck_instance));
453 if (!inst)
454 return ENOMEM;
455 memset(inst, 0, sizeof(struct fsck_instance));
457 argv[0] = string_copy(prog);
458 argc = 1;
460 for (i=0; i <num_args; i++)
461 argv[argc++] = string_copy(args[i]);
463 if (progress) {
464 if ((strcmp(type, "ext2") == 0) ||
465 (strcmp(type, "ext3") == 0) ||
466 (strcmp(type, "ext4") == 0) ||
467 (strcmp(type, "ext4dev") == 0)) {
468 char tmp[80];
470 tmp[0] = 0;
471 if (!progress_active()) {
472 snprintf(tmp, 80, "-C%d", progress_fd);
473 inst->flags |= FLAG_PROGRESS;
474 } else if (progress_fd)
475 snprintf(tmp, 80, "-C%d", progress_fd * -1);
476 if (tmp[0])
477 argv[argc++] = string_copy(tmp);
481 argv[argc++] = string_copy(device);
482 argv[argc] = 0;
484 s = find_fsck(prog);
485 if (s == NULL) {
486 fprintf(stderr, _("fsck: %s: not found\n"), prog);
487 free(inst);
488 return ENOENT;
491 if (verbose || noexecute) {
492 printf("[%s (%d) -- %s] ", s, num_running,
493 mntpt ? mntpt : device);
494 for (i=0; i < argc; i++)
495 printf("%s ", argv[i]);
496 printf("\n");
499 /* Fork and execute the correct program. */
500 if (noexecute)
501 pid = -1;
502 else if ((pid = fork()) < 0) {
503 perror("fork");
504 free(inst);
505 return errno;
506 } else if (pid == 0) {
507 if (!interactive)
508 close(0);
509 (void) execv(s, argv);
510 perror(argv[0]);
511 free(inst);
512 exit(EXIT_ERROR);
515 for (i=0; i < argc; i++)
516 free(argv[i]);
518 inst->pid = pid;
519 inst->prog = string_copy(prog);
520 inst->type = string_copy(type);
521 inst->device = string_copy(device);
522 inst->base_device = base_device(device);
523 inst->start_time = time(0);
524 inst->next = NULL;
527 * Find the end of the list, so we add the instance on at the end.
529 for (p = instance_list; p && p->next; p = p->next);
531 if (p)
532 p->next = inst;
533 else
534 instance_list = inst;
536 return 0;
540 * Send a signal to all outstanding fsck child processes
542 static int kill_all(int signum)
544 struct fsck_instance *inst;
545 int n = 0;
547 for (inst = instance_list; inst; inst = inst->next) {
548 if (inst->flags & FLAG_DONE)
549 continue;
550 if (inst->pid <= 0)
551 continue;
552 kill(inst->pid, signum);
553 n++;
555 return n;
559 * Wait for one child process to exit; when it does, unlink it from
560 * the list of executing child processes, and return it.
562 static struct fsck_instance *wait_one(int flags)
564 int status;
565 int sig;
566 struct fsck_instance *inst, *inst2, *prev;
567 pid_t pid;
569 if (!instance_list)
570 return NULL;
572 if (noexecute) {
573 inst = instance_list;
574 prev = 0;
575 #ifdef RANDOM_DEBUG
576 while (inst->next && (random() & 1)) {
577 prev = inst;
578 inst = inst->next;
580 #endif
581 inst->exit_status = 0;
582 goto ret_inst;
586 * gcc -Wall fails saving throw against stupidity
587 * (inst and prev are thought to be uninitialized variables)
589 inst = prev = NULL;
591 do {
592 pid = waitpid(-1, &status, flags);
593 if (cancel_requested && !kill_sent) {
594 kill_all(SIGTERM);
595 kill_sent++;
597 if ((pid == 0) && (flags & WNOHANG))
598 return NULL;
599 if (pid < 0) {
600 if ((errno == EINTR) || (errno == EAGAIN))
601 continue;
602 if (errno == ECHILD) {
603 fprintf(stderr,
604 _("%s: wait: No more child process?!?\n"),
605 progname);
606 return NULL;
608 perror("wait");
609 continue;
611 for (prev = 0, inst = instance_list;
612 inst;
613 prev = inst, inst = inst->next) {
614 if (inst->pid == pid)
615 break;
617 } while (!inst);
619 if (WIFEXITED(status))
620 status = WEXITSTATUS(status);
621 else if (WIFSIGNALED(status)) {
622 sig = WTERMSIG(status);
623 if (sig == SIGINT) {
624 status = EXIT_UNCORRECTED;
625 } else {
626 printf(_("Warning... %s for device %s exited "
627 "with signal %d.\n"),
628 inst->prog, inst->device, sig);
629 status = EXIT_ERROR;
631 } else {
632 printf(_("%s %s: status is %x, should never happen.\n"),
633 inst->prog, inst->device, status);
634 status = EXIT_ERROR;
636 inst->exit_status = status;
637 inst->flags |= FLAG_DONE;
638 if (progress && (inst->flags & FLAG_PROGRESS) &&
639 !progress_active()) {
640 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
641 if (inst2->flags & FLAG_DONE)
642 continue;
643 if (strcmp(inst2->type, "ext2") &&
644 strcmp(inst2->type, "ext3") &&
645 strcmp(inst2->type, "ext4") &&
646 strcmp(inst2->type, "ext4dev"))
647 continue;
649 * If we've just started the fsck, wait a tiny
650 * bit before sending the kill, to give it
651 * time to set up the signal handler
653 if (inst2->start_time < time(0)+2) {
654 if (fork() == 0) {
655 sleep(1);
656 kill(inst2->pid, SIGUSR1);
657 exit(0);
659 } else
660 kill(inst2->pid, SIGUSR1);
661 inst2->flags |= FLAG_PROGRESS;
662 break;
665 ret_inst:
666 if (prev)
667 prev->next = inst->next;
668 else
669 instance_list = inst->next;
670 if (verbose > 1)
671 printf(_("Finished with %s (exit status %d)\n"),
672 inst->device, inst->exit_status);
673 num_running--;
674 return inst;
677 #define FLAG_WAIT_ALL 0
678 #define FLAG_WAIT_ATLEAST_ONE 1
680 * Wait until all executing child processes have exited; return the
681 * logical OR of all of their exit code values.
683 static int wait_many(int flags)
685 struct fsck_instance *inst;
686 int global_status = 0;
687 int wait_flags = 0;
689 while ((inst = wait_one(wait_flags))) {
690 global_status |= inst->exit_status;
691 free_instance(inst);
692 #ifdef RANDOM_DEBUG
693 if (noexecute && (flags & WNOHANG) && !(random() % 3))
694 break;
695 #endif
696 if (flags & FLAG_WAIT_ATLEAST_ONE)
697 wait_flags = WNOHANG;
699 return global_status;
703 * Run the fsck program on a particular device
705 * If the type is specified using -t, and it isn't prefixed with "no"
706 * (as in "noext2") and only one filesystem type is specified, then
707 * use that type regardless of what is specified in /etc/fstab.
709 * If the type isn't specified by the user, then use either the type
710 * specified in /etc/fstab, or DEFAULT_FSTYPE.
712 static void fsck_device(struct fs_info *fs, int interactive)
714 const char *type;
715 int retval;
717 interpret_type(fs);
719 if (strcmp(fs->type, "auto") != 0)
720 type = fs->type;
721 else if (fstype && strncmp(fstype, "no", 2) &&
722 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
723 !strchr(fstype, ','))
724 type = fstype;
725 else
726 type = DEFAULT_FSTYPE;
728 num_running++;
729 retval = execute(type, fs->device, fs->mountpt, interactive);
730 if (retval) {
731 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
732 "for %s\n"), progname, retval, type, fs->device);
733 num_running--;
739 * Deal with the fsck -t argument.
741 static struct fs_type_compile {
742 char **list;
743 int *type;
744 int negate;
745 } fs_type_compiled;
747 #define FS_TYPE_NORMAL 0
748 #define FS_TYPE_OPT 1
749 #define FS_TYPE_NEGOPT 2
751 static const char *fs_type_syntax_error =
752 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
753 "with 'no' or '!'.\n");
755 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
757 char *cp, *list, *s;
758 int num = 2;
759 int negate, first_negate = 1;
761 if (fs_type) {
762 for (cp=fs_type; *cp; cp++) {
763 if (*cp == ',')
764 num++;
768 cmp->list = malloc(num * sizeof(char *));
769 cmp->type = malloc(num * sizeof(int));
770 if (!cmp->list || !cmp->type) {
771 fputs(_("Couldn't allocate memory for filesystem types\n"),
772 stderr);
773 exit(EXIT_ERROR);
775 memset(cmp->list, 0, num * sizeof(char *));
776 memset(cmp->type, 0, num * sizeof(int));
777 cmp->negate = 0;
779 if (!fs_type)
780 return;
782 list = string_copy(fs_type);
783 num = 0;
784 s = strtok(list, ",");
785 while(s) {
786 negate = 0;
787 if (strncmp(s, "no", 2) == 0) {
788 s += 2;
789 negate = 1;
790 } else if (*s == '!') {
791 s++;
792 negate = 1;
794 if (strcmp(s, "loop") == 0)
795 /* loop is really short-hand for opts=loop */
796 goto loop_special_case;
797 else if (strncmp(s, "opts=", 5) == 0) {
798 s += 5;
799 loop_special_case:
800 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
801 } else {
802 if (first_negate) {
803 cmp->negate = negate;
804 first_negate = 0;
806 if ((negate && !cmp->negate) ||
807 (!negate && cmp->negate)) {
808 fputs(_(fs_type_syntax_error), stderr);
809 free(list);
810 exit(EXIT_USAGE);
813 #if 0
814 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
815 #endif
816 cmp->list[num++] = string_copy(s);
817 s = strtok(NULL, ",");
819 free(list);
823 * This function returns true if a particular option appears in a
824 * comma-delimited options list
826 static int opt_in_list(const char *opt, char *optlist)
828 char *list, *s;
830 if (!optlist)
831 return 0;
832 list = string_copy(optlist);
834 s = strtok(list, ",");
835 while(s) {
836 if (strcmp(s, opt) == 0) {
837 free(list);
838 return 1;
840 s = strtok(NULL, ",");
842 free(list);
843 return 0;
846 /* See if the filesystem matches the criteria given by the -t option */
847 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
849 int n, ret = 0, checked_type = 0;
850 char *cp;
852 if (cmp->list == 0 || cmp->list[0] == 0)
853 return 1;
855 for (n=0; (cp = cmp->list[n]); n++) {
856 switch (cmp->type[n]) {
857 case FS_TYPE_NORMAL:
858 checked_type++;
859 if (strcmp(cp, fs->type) == 0) {
860 ret = 1;
862 break;
863 case FS_TYPE_NEGOPT:
864 if (opt_in_list(cp, fs->opts))
865 return 0;
866 break;
867 case FS_TYPE_OPT:
868 if (!opt_in_list(cp, fs->opts))
869 return 0;
870 break;
873 if (checked_type == 0)
874 return 1;
875 return (cmp->negate ? !ret : ret);
878 /* Check if we should ignore this filesystem. */
879 static int ignore(struct fs_info *fs)
881 const char **ip;
882 int wanted = 0;
885 * If the pass number is 0, ignore it.
887 if (fs->passno == 0)
888 return 1;
891 * If this is a bind mount, ignore it.
893 if (opt_in_list("bind", fs->opts)) {
894 fprintf(stderr,
895 _("%s: skipping bad line in /etc/fstab: bind mount with nonzero fsck pass number\n"),
896 fs->mountpt);
897 return 1;
900 interpret_type(fs);
903 * If a specific fstype is specified, and it doesn't match,
904 * ignore it.
906 if (!fs_match(fs, &fs_type_compiled)) return 1;
908 /* Are we ignoring this type? */
909 for(ip = ignored_types; *ip; ip++)
910 if (strcmp(fs->type, *ip) == 0) return 1;
912 /* Do we really really want to check this fs? */
913 for(ip = really_wanted; *ip; ip++)
914 if (strcmp(fs->type, *ip) == 0) {
915 wanted = 1;
916 break;
919 /* See if the <fsck.fs> program is available. */
920 if (find_fsck(fs->type) == NULL) {
921 if (wanted)
922 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
923 fs->device, fs->type);
924 return 1;
927 /* We can and want to check this file system type. */
928 return 0;
932 * Returns TRUE if a partition on the same disk is already being
933 * checked.
935 static int device_already_active(char *device)
937 struct fsck_instance *inst;
938 char *base;
940 if (force_all_parallel)
941 return 0;
943 #ifdef BASE_MD
944 /* Don't check a soft raid disk with any other disk */
945 if (instance_list &&
946 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
947 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
948 return 1;
949 #endif
951 base = base_device(device);
953 * If we don't know the base device, assume that the device is
954 * already active if there are any fsck instances running.
956 if (!base)
957 return (instance_list != 0);
958 for (inst = instance_list; inst; inst = inst->next) {
959 if (!inst->base_device || !strcmp(base, inst->base_device)) {
960 free(base);
961 return 1;
964 free(base);
965 return 0;
968 /* Check all file systems, using the /etc/fstab table. */
969 static int check_all(NOARGS)
971 struct fs_info *fs = NULL;
972 int status = EXIT_OK;
973 int not_done_yet = 1;
974 int passno = 1;
975 int pass_done;
977 if (verbose)
978 fputs(_("Checking all file systems.\n"), stdout);
981 * Do an initial scan over the filesystem; mark filesystems
982 * which should be ignored as done, and resolve any "auto"
983 * filesystem types (done as a side-effect of calling ignore()).
985 for (fs = filesys_info; fs; fs = fs->next) {
986 if (ignore(fs))
987 fs->flags |= FLAG_DONE;
991 * Find and check the root filesystem.
993 if (!parallel_root) {
994 for (fs = filesys_info; fs; fs = fs->next) {
995 if (!strcmp(fs->mountpt, "/"))
996 break;
998 if (fs) {
999 if (!skip_root && !ignore(fs) &&
1000 !(ignore_mounted && is_mounted(fs->device))) {
1001 fsck_device(fs, 1);
1002 status |= wait_many(FLAG_WAIT_ALL);
1003 if (status > EXIT_NONDESTRUCT)
1004 return status;
1006 fs->flags |= FLAG_DONE;
1010 * This is for the bone-headed user who enters the root
1011 * filesystem twice. Skip root will skip all root entries.
1013 if (skip_root)
1014 for (fs = filesys_info; fs; fs = fs->next)
1015 if (!strcmp(fs->mountpt, "/"))
1016 fs->flags |= FLAG_DONE;
1018 while (not_done_yet) {
1019 not_done_yet = 0;
1020 pass_done = 1;
1022 for (fs = filesys_info; fs; fs = fs->next) {
1023 if (cancel_requested)
1024 break;
1025 if (fs->flags & FLAG_DONE)
1026 continue;
1028 * If the filesystem's pass number is higher
1029 * than the current pass number, then we don't
1030 * do it yet.
1032 if (fs->passno > passno) {
1033 not_done_yet++;
1034 continue;
1036 if (ignore_mounted && is_mounted(fs->device)) {
1037 fs->flags |= FLAG_DONE;
1038 continue;
1041 * If a filesystem on a particular device has
1042 * already been spawned, then we need to defer
1043 * this to another pass.
1045 if (device_already_active(fs->device)) {
1046 pass_done = 0;
1047 continue;
1050 * Spawn off the fsck process
1052 fsck_device(fs, serialize);
1053 fs->flags |= FLAG_DONE;
1056 * Only do one filesystem at a time, or if we
1057 * have a limit on the number of fsck's extant
1058 * at one time, apply that limit.
1060 if (serialize ||
1061 (max_running && (num_running >= max_running))) {
1062 pass_done = 0;
1063 break;
1066 if (cancel_requested)
1067 break;
1068 if (verbose > 1)
1069 printf(_("--waiting-- (pass %d)\n"), passno);
1070 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1071 FLAG_WAIT_ATLEAST_ONE);
1072 if (pass_done) {
1073 if (verbose > 1)
1074 printf("----------------------------------\n");
1075 passno++;
1076 } else
1077 not_done_yet++;
1079 if (cancel_requested && !kill_sent) {
1080 kill_all(SIGTERM);
1081 kill_sent++;
1083 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1084 return status;
1087 static void usage(NOARGS)
1089 fputs(_("Usage: fsck [-AMNPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n"), stderr);
1090 exit(EXIT_USAGE);
1093 #ifdef HAVE_SIGNAL_H
1094 static void signal_cancel(int sig FSCK_ATTR((unused)))
1096 cancel_requested++;
1098 #endif
1100 static void PRS(int argc, char *argv[])
1102 int i, j;
1103 char *arg, *dev, *tmp = 0;
1104 char options[128];
1105 int opt = 0;
1106 int opts_for_fsck = 0;
1107 #ifdef HAVE_SIGNAL_H
1108 struct sigaction sa;
1111 * Set up signal action
1113 memset(&sa, 0, sizeof(struct sigaction));
1114 sa.sa_handler = signal_cancel;
1115 sigaction(SIGINT, &sa, 0);
1116 sigaction(SIGTERM, &sa, 0);
1117 #endif
1119 num_devices = 0;
1120 num_args = 0;
1121 instance_list = 0;
1123 progname = argv[0];
1125 for (i=1; i < argc; i++) {
1126 arg = argv[i];
1127 if (!arg)
1128 continue;
1129 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1130 if (num_devices >= MAX_DEVICES) {
1131 fprintf(stderr, _("%s: too many devices\n"),
1132 progname);
1133 exit(EXIT_ERROR);
1135 dev = get_devname(cache, arg, NULL);
1136 if (!dev && strchr(arg, '=')) {
1138 * Check to see if we failed because
1139 * /proc/partitions isn't found.
1141 if (access("/proc/partitions", R_OK) < 0) {
1142 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
1143 strerror(errno));
1144 fprintf(stderr, "Is /proc mounted?\n");
1145 exit(EXIT_ERROR);
1148 * Check to see if this is because
1149 * we're not running as root
1151 if (geteuid())
1152 fprintf(stderr,
1153 "Must be root to scan for matching filesystems: %s\n", arg);
1154 else
1155 fprintf(stderr,
1156 "Couldn't find matching filesystem: %s\n", arg);
1157 exit(EXIT_ERROR);
1159 devices[num_devices++] = dev ? dev : string_copy(arg);
1160 continue;
1162 if (arg[0] != '-' || opts_for_fsck) {
1163 if (num_args >= MAX_ARGS) {
1164 fprintf(stderr, _("%s: too many arguments\n"),
1165 progname);
1166 exit(EXIT_ERROR);
1168 args[num_args++] = string_copy(arg);
1169 continue;
1171 for (j=1; arg[j]; j++) {
1172 if (opts_for_fsck) {
1173 options[++opt] = arg[j];
1174 continue;
1176 switch (arg[j]) {
1177 case 'A':
1178 doall++;
1179 break;
1180 case 'C':
1181 progress++;
1182 if (arg[j+1]) {
1183 progress_fd = string_to_int(arg+j+1);
1184 if (progress_fd < 0)
1185 progress_fd = 0;
1186 else
1187 goto next_arg;
1188 } else if (argc > i + 1 &&
1189 argv[i + 1][0] != '-') {
1190 progress_fd = string_to_int(argv[i]);
1191 if (progress_fd < 0)
1192 progress_fd = 0;
1193 else {
1194 ++i;
1195 goto next_arg;
1198 break;
1199 case 'V':
1200 verbose++;
1201 break;
1202 case 'N':
1203 noexecute++;
1204 break;
1205 case 'R':
1206 skip_root++;
1207 break;
1208 case 'T':
1209 notitle++;
1210 break;
1211 case 'M':
1212 ignore_mounted++;
1213 break;
1214 case 'P':
1215 parallel_root++;
1216 break;
1217 case 's':
1218 serialize++;
1219 break;
1220 case 't':
1221 tmp = 0;
1222 if (fstype)
1223 usage();
1224 if (arg[j+1])
1225 tmp = arg+j+1;
1226 else if ((i+1) < argc)
1227 tmp = argv[++i];
1228 else
1229 usage();
1230 fstype = string_copy(tmp);
1231 compile_fs_type(fstype, &fs_type_compiled);
1232 goto next_arg;
1233 case '-':
1234 opts_for_fsck++;
1235 break;
1236 case '?':
1237 usage();
1238 break;
1239 default:
1240 options[++opt] = arg[j];
1241 break;
1244 next_arg:
1245 if (opt) {
1246 options[0] = '-';
1247 options[++opt] = '\0';
1248 if (num_args >= MAX_ARGS) {
1249 fprintf(stderr,
1250 _("%s: too many arguments\n"),
1251 progname);
1252 exit(EXIT_ERROR);
1254 args[num_args++] = string_copy(options);
1255 opt = 0;
1258 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1259 force_all_parallel++;
1260 if ((tmp = getenv("FSCK_MAX_INST")))
1261 max_running = atoi(tmp);
1264 int main(int argc, char *argv[])
1266 int i, status = 0;
1267 int interactive = 0;
1268 char *oldpath = getenv("PATH");
1269 const char *fstab;
1270 struct fs_info *fs;
1272 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1273 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1275 #ifdef ENABLE_NLS
1276 setlocale(LC_MESSAGES, "");
1277 setlocale(LC_CTYPE, "");
1278 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1279 textdomain(NLS_CAT_NAME);
1280 #endif
1281 blkid_get_cache(&cache, NULL);
1282 PRS(argc, argv);
1284 if (!notitle)
1285 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1287 fstab = getenv("FSTAB_FILE");
1288 if (!fstab)
1289 fstab = _PATH_MNTTAB;
1290 load_fs_info(fstab);
1292 /* Update our search path to include uncommon directories. */
1293 if (oldpath) {
1294 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1295 strlen (oldpath) + 1);
1296 if (!fsck_path) {
1297 fprintf(stderr, "%s: Unable to allocate memory for fsck_path\n", progname);
1298 exit(EXIT_ERROR);
1300 strcpy (fsck_path, fsck_prefix_path);
1301 strcat (fsck_path, ":");
1302 strcat (fsck_path, oldpath);
1303 } else {
1304 fsck_path = string_copy(fsck_prefix_path);
1307 if ((num_devices == 1) || (serialize))
1308 interactive = 1;
1310 /* If -A was specified ("check all"), do that! */
1311 if (doall)
1312 return check_all();
1314 if (num_devices == 0) {
1315 serialize++;
1316 interactive++;
1317 return check_all();
1319 for (i = 0 ; i < num_devices; i++) {
1320 if (cancel_requested) {
1321 if (!kill_sent) {
1322 kill_all(SIGTERM);
1323 kill_sent++;
1325 break;
1327 fs = lookup(devices[i]);
1328 if (!fs) {
1329 fs = create_fs_device(devices[i], 0, "auto",
1330 0, -1, -1);
1331 if (!fs)
1332 continue;
1334 if (ignore_mounted && is_mounted(fs->device))
1335 continue;
1336 fsck_device(fs, interactive);
1337 if (serialize ||
1338 (max_running && (num_running >= max_running))) {
1339 struct fsck_instance *inst;
1341 inst = wait_one(0);
1342 if (inst) {
1343 status |= inst->exit_status;
1344 free_instance(inst);
1346 if (verbose > 1)
1347 printf("----------------------------------\n");
1350 status |= wait_many(FLAG_WAIT_ALL);
1351 free(fsck_path);
1352 blkid_put_cache(cache);
1353 return status;