]> git.ipfire.org Git - thirdparty/util-linux.git/blob - fsck/fsck.c
fsck: improve whole-disk check, detect stacked devices
[thirdparty/util-linux.git] / fsck / fsck.c
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.
8 *
9 * Written by Theodore Ts'o, <tytso@mit.edu>
10 *
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.
18 *
19 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
21 *
22 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
23 *
24 * This file may be redistributed under the terms of the GNU Public
25 * License.
26 */
27
28 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
29
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <sys/signal.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 #include <stdlib.h>
40 #include <paths.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <malloc.h>
44 #include <signal.h>
45 #include <dirent.h>
46 #include <blkid.h>
47
48 #include "fsprobe.h"
49
50 #include "nls.h"
51 #include "pathnames.h"
52 #include "ismounted.h"
53
54 #include "fsck.h"
55
56 static const char *ignored_types[] = {
57 "ignore",
58 "iso9660",
59 "nfs",
60 "proc",
61 "sw",
62 "swap",
63 "tmpfs",
64 "devpts",
65 NULL
66 };
67
68 static const char *really_wanted[] = {
69 "minix",
70 "ext2",
71 "ext3",
72 "ext4",
73 "ext4dev",
74 "jfs",
75 "reiserfs",
76 "xiafs",
77 "xfs",
78 NULL
79 };
80
81 #define BASE_MD "/dev/md"
82
83 /*
84 * Global variables for options
85 */
86 char *devices[MAX_DEVICES];
87 char *args[MAX_ARGS];
88 int num_devices, num_args;
89
90 int verbose = 0;
91 int doall = 0;
92 int noexecute = 0;
93 int serialize = 0;
94 int skip_root = 0;
95 int ignore_mounted = 0;
96 int notitle = 0;
97 int parallel_root = 0;
98 int progress = 0;
99 int progress_fd = 0;
100 int force_all_parallel = 0;
101 int num_running = 0;
102 int max_running = 0;
103 volatile int cancel_requested = 0;
104 int kill_sent = 0;
105 char *progname;
106 char *fstype = NULL;
107 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
108 struct fsck_instance *instance_list;
109 const char fsck_prefix_path[] = FS_SEARCH_PATH;
110 char *fsck_path = 0;
111
112 static char *string_copy(const char *s)
113 {
114 char *ret;
115
116 if (!s)
117 return 0;
118 ret = malloc(strlen(s)+1);
119 if (ret)
120 strcpy(ret, s);
121 return ret;
122 }
123
124 static int string_to_int(const char *s)
125 {
126 long l;
127 char *p;
128
129 l = strtol(s, &p, 0);
130 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
131 return -1;
132 else
133 return (int) l;
134 }
135
136 static int ignore(struct fs_info *);
137
138 static char *skip_over_blank(char *cp)
139 {
140 while (*cp && isspace(*cp))
141 cp++;
142 return cp;
143 }
144
145 static char *skip_over_word(char *cp)
146 {
147 while (*cp && !isspace(*cp))
148 cp++;
149 return cp;
150 }
151
152 static void strip_line(char *line)
153 {
154 char *p;
155
156 while (*line) {
157 p = line + strlen(line) - 1;
158 if ((*p == '\n') || (*p == '\r'))
159 *p = 0;
160 else
161 break;
162 }
163 }
164
165 static char *parse_word(char **buf)
166 {
167 char *word, *next;
168
169 word = *buf;
170 if (*word == 0)
171 return 0;
172
173 word = skip_over_blank(word);
174 next = skip_over_word(word);
175 if (*next)
176 *next++ = 0;
177 *buf = next;
178 return word;
179 }
180
181 static void parse_escape(char *word)
182 {
183 char *p, *q;
184 int ac, i;
185
186 if (!word)
187 return;
188
189 for (p = word, q = word; *p; p++, q++) {
190 *q = *p;
191 if (*p != '\\')
192 continue;
193 if (*++p == 0)
194 break;
195 if (*p == 't') {
196 *q = '\t';
197 continue;
198 }
199 if (*p == 'n') {
200 *q = '\n';
201 continue;
202 }
203 if (!isdigit(*p)) {
204 *q = *p;
205 continue;
206 }
207 ac = 0;
208 for (i = 0; i < 3; i++, p++) {
209 if (!isdigit(*p))
210 break;
211 ac = (ac * 8) + (*p - '0');
212 }
213 *q = ac;
214 p--;
215 }
216 *q = 0;
217 }
218
219 static void free_instance(struct fsck_instance *i)
220 {
221 free(i->prog);
222 free(i);
223 return;
224 }
225
226 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
227 const char *type, const char *opts,
228 int freq, int passno)
229 {
230 struct fs_info *fs;
231
232 if (!(fs = malloc(sizeof(struct fs_info))))
233 return NULL;
234
235 fs->device = string_copy(device);
236 fs->mountpt = string_copy(mntpnt);
237 fs->type = string_copy(type);
238 fs->opts = string_copy(opts ? opts : "");
239 fs->freq = freq;
240 fs->passno = passno;
241 fs->flags = 0;
242 fs->next = NULL;
243 fs->disk = 0;
244 fs->stacked = 0;
245
246 if (!filesys_info)
247 filesys_info = fs;
248 else
249 filesys_last->next = fs;
250 filesys_last = fs;
251
252 return fs;
253 }
254
255
256
257 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
258 {
259 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
260 struct fs_info *fs;
261
262 *ret_fs = 0;
263 strip_line(line);
264 cp = line;
265
266 device = parse_word(&cp);
267 if (!device || *device == '#')
268 return 0; /* Ignore blank lines and comments */
269 mntpnt = parse_word(&cp);
270 type = parse_word(&cp);
271 opts = parse_word(&cp);
272 freq = parse_word(&cp);
273 passno = parse_word(&cp);
274
275 if (!mntpnt || !type)
276 return -1;
277
278 parse_escape(device);
279 parse_escape(mntpnt);
280 parse_escape(type);
281 parse_escape(opts);
282 parse_escape(freq);
283 parse_escape(passno);
284
285 dev = fsprobe_get_devname_by_spec(device);
286 if (dev)
287 device = dev;
288
289 if (strchr(type, ','))
290 type = 0;
291
292 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
293 freq ? atoi(freq) : -1,
294 passno ? atoi(passno) : -1);
295 free(dev);
296
297 if (!fs)
298 return -1;
299 *ret_fs = fs;
300 return 0;
301 }
302
303 static void interpret_type(struct fs_info *fs)
304 {
305 char *t;
306
307 if (strcmp(fs->type, "auto") != 0)
308 return;
309 t = fsprobe_get_fstype_by_devname(fs->device);
310 if (t) {
311 free(fs->type);
312 fs->type = t;
313 }
314 }
315
316 /*
317 * Load the filesystem database from /etc/fstab
318 */
319 static void load_fs_info(const char *filename)
320 {
321 FILE *f;
322 char buf[1024];
323 int lineno = 0;
324 int old_fstab = 1;
325 struct fs_info *fs;
326
327 if ((f = fopen(filename, "r")) == NULL) {
328 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
329 filename, strerror(errno));
330 return;
331 }
332 while (!feof(f)) {
333 lineno++;
334 if (!fgets(buf, sizeof(buf), f))
335 break;
336 buf[sizeof(buf)-1] = 0;
337 if (parse_fstab_line(buf, &fs) < 0) {
338 fprintf(stderr, _("WARNING: bad format "
339 "on line %d of %s\n"), lineno, filename);
340 continue;
341 }
342 if (!fs)
343 continue;
344 if (fs->passno < 0)
345 fs->passno = 0;
346 else
347 old_fstab = 0;
348 }
349
350 fclose(f);
351
352 if (old_fstab && filesys_info) {
353 fputs(_(
354 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
355 " field. I will kludge around things for you, but you\n"
356 " should fix your /etc/fstab file as soon as you can.\n\n"), stderr);
357
358 for (fs = filesys_info; fs; fs = fs->next) {
359 fs->passno = 1;
360 }
361 }
362 }
363
364 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
365 static struct fs_info *lookup(char *filesys)
366 {
367 struct fs_info *fs;
368
369 /* No filesys name given. */
370 if (filesys == NULL)
371 return NULL;
372
373 for (fs = filesys_info; fs; fs = fs->next) {
374 if (!strcmp(filesys, fs->device) ||
375 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
376 break;
377 }
378
379 return fs;
380 }
381
382 /* Find fsck program for a given fs type. */
383 static char *find_fsck(char *type)
384 {
385 char *s;
386 const char *tpl;
387 static char prog[256];
388 char *p = string_copy(fsck_path);
389 struct stat st;
390
391 /* Are we looking for a program or just a type? */
392 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
393
394 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
395 sprintf(prog, tpl, s, type);
396 if (stat(prog, &st) == 0) break;
397 }
398 free(p);
399 return(s ? prog : NULL);
400 }
401
402 static int progress_active(NOARGS)
403 {
404 struct fsck_instance *inst;
405
406 for (inst = instance_list; inst; inst = inst->next) {
407 if (inst->flags & FLAG_DONE)
408 continue;
409 if (inst->flags & FLAG_PROGRESS)
410 return 1;
411 }
412 return 0;
413 }
414
415 /*
416 * Execute a particular fsck program, and link it into the list of
417 * child processes we are waiting for.
418 */
419 static int execute(const char *type, struct fs_info *fs, int interactive)
420 {
421 char *s, *argv[80], prog[80];
422 int argc, i;
423 struct fsck_instance *inst, *p;
424 pid_t pid;
425
426 inst = malloc(sizeof(struct fsck_instance));
427 if (!inst)
428 return ENOMEM;
429 memset(inst, 0, sizeof(struct fsck_instance));
430
431 sprintf(prog, "fsck.%s", type);
432 argv[0] = string_copy(prog);
433 argc = 1;
434
435 for (i=0; i <num_args; i++)
436 argv[argc++] = string_copy(args[i]);
437
438 if (progress) {
439 if ((strcmp(type, "ext2") == 0) ||
440 (strcmp(type, "ext3") == 0) ||
441 (strcmp(type, "ext4") == 0) ||
442 (strcmp(type, "ext4dev") == 0)) {
443 char tmp[80];
444
445 tmp[0] = 0;
446 if (!progress_active()) {
447 snprintf(tmp, 80, "-C%d", progress_fd);
448 inst->flags |= FLAG_PROGRESS;
449 } else if (progress_fd)
450 snprintf(tmp, 80, "-C%d", progress_fd * -1);
451 if (tmp[0])
452 argv[argc++] = string_copy(tmp);
453 }
454 }
455
456 argv[argc++] = string_copy(fs->device);
457 argv[argc] = 0;
458
459 s = find_fsck(prog);
460 if (s == NULL) {
461 fprintf(stderr, _("fsck: %s: not found\n"), prog);
462 free(inst);
463 return ENOENT;
464 }
465
466 if (verbose || noexecute) {
467 printf("[%s (%d) -- %s] ", s, num_running,
468 fs->mountpt ? fs->mountpt : fs->device);
469 for (i=0; i < argc; i++)
470 printf("%s ", argv[i]);
471 printf("\n");
472 }
473
474 /* Fork and execute the correct program. */
475 if (noexecute)
476 pid = -1;
477 else if ((pid = fork()) < 0) {
478 perror("fork");
479 free(inst);
480 return errno;
481 } else if (pid == 0) {
482 if (!interactive)
483 close(0);
484 (void) execv(s, argv);
485 perror(argv[0]);
486 free(inst);
487 exit(EXIT_ERROR);
488 }
489
490 for (i=0; i < argc; i++)
491 free(argv[i]);
492
493 inst->pid = pid;
494 inst->prog = string_copy(prog);
495 inst->type = string_copy(type);
496 inst->start_time = time(0);
497 inst->fs = fs;
498 inst->next = NULL;
499
500 /*
501 * Find the end of the list, so we add the instance on at the end.
502 */
503 for (p = instance_list; p && p->next; p = p->next);
504
505 if (p)
506 p->next = inst;
507 else
508 instance_list = inst;
509
510 return 0;
511 }
512
513 /*
514 * Send a signal to all outstanding fsck child processes
515 */
516 static int kill_all(int signum)
517 {
518 struct fsck_instance *inst;
519 int n = 0;
520
521 for (inst = instance_list; inst; inst = inst->next) {
522 if (inst->flags & FLAG_DONE)
523 continue;
524 kill(inst->pid, signum);
525 n++;
526 }
527 return n;
528 }
529
530 /*
531 * Wait for one child process to exit; when it does, unlink it from
532 * the list of executing child processes, and return it.
533 */
534 static struct fsck_instance *wait_one(int flags)
535 {
536 int status;
537 int sig;
538 struct fsck_instance *inst, *inst2, *prev;
539 pid_t pid;
540
541 if (!instance_list)
542 return NULL;
543
544 if (noexecute) {
545 inst = instance_list;
546 prev = 0;
547 #ifdef RANDOM_DEBUG
548 while (inst->next && (random() & 1)) {
549 prev = inst;
550 inst = inst->next;
551 }
552 #endif
553 inst->exit_status = 0;
554 goto ret_inst;
555 }
556
557 /*
558 * gcc -Wall fails saving throw against stupidity
559 * (inst and prev are thought to be uninitialized variables)
560 */
561 inst = prev = NULL;
562
563 do {
564 pid = waitpid(-1, &status, flags);
565 if (cancel_requested && !kill_sent) {
566 kill_all(SIGTERM);
567 kill_sent++;
568 }
569 if ((pid == 0) && (flags & WNOHANG))
570 return NULL;
571 if (pid < 0) {
572 if ((errno == EINTR) || (errno == EAGAIN))
573 continue;
574 if (errno == ECHILD) {
575 fprintf(stderr,
576 _("%s: wait: No more child process?!?\n"),
577 progname);
578 return NULL;
579 }
580 perror("wait");
581 continue;
582 }
583 for (prev = 0, inst = instance_list;
584 inst;
585 prev = inst, inst = inst->next) {
586 if (inst->pid == pid)
587 break;
588 }
589 } while (!inst);
590
591 if (WIFEXITED(status))
592 status = WEXITSTATUS(status);
593 else if (WIFSIGNALED(status)) {
594 sig = WTERMSIG(status);
595 if (sig == SIGINT) {
596 status = EXIT_UNCORRECTED;
597 } else {
598 printf(_("Warning... %s for device %s exited "
599 "with signal %d.\n"),
600 inst->prog, inst->fs->device, sig);
601 status = EXIT_ERROR;
602 }
603 } else {
604 printf(_("%s %s: status is %x, should never happen.\n"),
605 inst->prog, inst->fs->device, status);
606 status = EXIT_ERROR;
607 }
608 inst->exit_status = status;
609 inst->flags |= FLAG_DONE;
610 if (progress && (inst->flags & FLAG_PROGRESS) &&
611 !progress_active()) {
612 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
613 if (inst2->flags & FLAG_DONE)
614 continue;
615 if (strcmp(inst2->type, "ext2") &&
616 strcmp(inst2->type, "ext3") &&
617 strcmp(inst2->type, "ext4") &&
618 strcmp(inst2->type, "ext4dev"))
619 continue;
620 /*
621 * If we've just started the fsck, wait a tiny
622 * bit before sending the kill, to give it
623 * time to set up the signal handler
624 */
625 if (inst2->start_time < time(0)+2) {
626 if (fork() == 0) {
627 sleep(1);
628 kill(inst2->pid, SIGUSR1);
629 exit(0);
630 }
631 } else
632 kill(inst2->pid, SIGUSR1);
633 inst2->flags |= FLAG_PROGRESS;
634 break;
635 }
636 }
637 ret_inst:
638 if (prev)
639 prev->next = inst->next;
640 else
641 instance_list = inst->next;
642 if (verbose > 1)
643 printf(_("Finished with %s (exit status %d)\n"),
644 inst->fs->device, inst->exit_status);
645 num_running--;
646 return inst;
647 }
648
649 #define FLAG_WAIT_ALL 0
650 #define FLAG_WAIT_ATLEAST_ONE 1
651 /*
652 * Wait until all executing child processes have exited; return the
653 * logical OR of all of their exit code values.
654 */
655 static int wait_many(int flags)
656 {
657 struct fsck_instance *inst;
658 int global_status = 0;
659 int wait_flags = 0;
660
661 while ((inst = wait_one(wait_flags))) {
662 global_status |= inst->exit_status;
663 free_instance(inst);
664 #ifdef RANDOM_DEBUG
665 if (noexecute && (flags & WNOHANG) && !(random() % 3))
666 break;
667 #endif
668 if (flags & FLAG_WAIT_ATLEAST_ONE)
669 wait_flags = WNOHANG;
670 }
671 return global_status;
672 }
673
674 /*
675 * Run the fsck program on a particular device
676 *
677 * If the type is specified using -t, and it isn't prefixed with "no"
678 * (as in "noext2") and only one filesystem type is specified, then
679 * use that type regardless of what is specified in /etc/fstab.
680 *
681 * If the type isn't specified by the user, then use either the type
682 * specified in /etc/fstab, or DEFAULT_FSTYPE.
683 */
684 static void fsck_device(struct fs_info *fs, int interactive)
685 {
686 const char *type;
687 int retval;
688
689 interpret_type(fs);
690
691 if (strcmp(fs->type, "auto") != 0)
692 type = fs->type;
693 else if (fstype && strncmp(fstype, "no", 2) &&
694 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
695 !strchr(fstype, ','))
696 type = fstype;
697 else
698 type = DEFAULT_FSTYPE;
699
700 num_running++;
701 retval = execute(type, fs, interactive);
702 if (retval) {
703 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
704 "for %s\n"), progname, retval, type, fs->device);
705 num_running--;
706 }
707 }
708
709
710 /*
711 * Deal with the fsck -t argument.
712 */
713 struct fs_type_compile {
714 char **list;
715 int *type;
716 int negate;
717 } fs_type_compiled;
718
719 #define FS_TYPE_NORMAL 0
720 #define FS_TYPE_OPT 1
721 #define FS_TYPE_NEGOPT 2
722
723 static const char *fs_type_syntax_error =
724 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
725 "with 'no' or '!'.\n");
726
727 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
728 {
729 char *cp, *list, *s;
730 int num = 2;
731 int negate, first_negate = 1;
732
733 if (fs_type) {
734 for (cp=fs_type; *cp; cp++) {
735 if (*cp == ',')
736 num++;
737 }
738 }
739
740 cmp->list = malloc(num * sizeof(char *));
741 cmp->type = malloc(num * sizeof(int));
742 if (!cmp->list || !cmp->type) {
743 fputs(_("Couldn't allocate memory for filesystem types\n"),
744 stderr);
745 exit(EXIT_ERROR);
746 }
747 memset(cmp->list, 0, num * sizeof(char *));
748 memset(cmp->type, 0, num * sizeof(int));
749 cmp->negate = 0;
750
751 if (!fs_type)
752 return;
753
754 list = string_copy(fs_type);
755 num = 0;
756 s = strtok(list, ",");
757 while(s) {
758 negate = 0;
759 if (strncmp(s, "no", 2) == 0) {
760 s += 2;
761 negate = 1;
762 } else if (*s == '!') {
763 s++;
764 negate = 1;
765 }
766 if (strcmp(s, "loop") == 0)
767 /* loop is really short-hand for opts=loop */
768 goto loop_special_case;
769 else if (strncmp(s, "opts=", 5) == 0) {
770 s += 5;
771 loop_special_case:
772 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
773 } else {
774 if (first_negate) {
775 cmp->negate = negate;
776 first_negate = 0;
777 }
778 if ((negate && !cmp->negate) ||
779 (!negate && cmp->negate)) {
780 fputs(_(fs_type_syntax_error), stderr);
781 exit(EXIT_USAGE);
782 }
783 }
784 #if 0
785 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
786 #endif
787 cmp->list[num++] = string_copy(s);
788 s = strtok(NULL, ",");
789 }
790 free(list);
791 }
792
793 /*
794 * This function returns true if a particular option appears in a
795 * comma-delimited options list
796 */
797 static int opt_in_list(const char *opt, char *optlist)
798 {
799 char *list, *s;
800
801 if (!optlist)
802 return 0;
803 list = string_copy(optlist);
804
805 s = strtok(list, ",");
806 while(s) {
807 if (strcmp(s, opt) == 0) {
808 free(list);
809 return 1;
810 }
811 s = strtok(NULL, ",");
812 }
813 free(list);
814 return 0;
815 }
816
817 /* See if the filesystem matches the criteria given by the -t option */
818 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
819 {
820 int n, ret = 0, checked_type = 0;
821 char *cp;
822
823 if (cmp->list == 0 || cmp->list[0] == 0)
824 return 1;
825
826 for (n=0; (cp = cmp->list[n]); n++) {
827 switch (cmp->type[n]) {
828 case FS_TYPE_NORMAL:
829 checked_type++;
830 if (strcmp(cp, fs->type) == 0) {
831 ret = 1;
832 }
833 break;
834 case FS_TYPE_NEGOPT:
835 if (opt_in_list(cp, fs->opts))
836 return 0;
837 break;
838 case FS_TYPE_OPT:
839 if (!opt_in_list(cp, fs->opts))
840 return 0;
841 break;
842 }
843 }
844 if (checked_type == 0)
845 return 1;
846 return (cmp->negate ? !ret : ret);
847 }
848
849 /*
850 * Check if a device exists
851 */
852 static int device_exists(const char *device)
853 {
854 struct stat st;
855
856 if (stat(device, &st) == -1)
857 return 0;
858
859 if (!S_ISBLK(st.st_mode))
860 return 0;
861
862 return 1;
863 }
864
865 /* Check if we should ignore this filesystem. */
866 static int ignore(struct fs_info *fs)
867 {
868 const char **ip;
869 int wanted = 0;
870
871 /*
872 * If the pass number is 0, ignore it.
873 */
874 if (fs->passno == 0)
875 return 1;
876
877 /*
878 * If this is a bind mount, ignore it.
879 */
880 if (opt_in_list("bind", fs->opts)) {
881 fprintf(stderr,
882 _("%s: skipping bad line in /etc/fstab: bind mount with nonzero fsck pass number\n"),
883 fs->mountpt);
884 return 1;
885 }
886
887 /*
888 * ignore devices that don't exist and have the "nofail" mount option
889 */
890 if (!device_exists(fs->device) && opt_in_list("nofail", fs->opts)) {
891 if (verbose)
892 printf(_("%s: skipping nonexistent device\n"), fs->device);
893 return 1;
894 }
895
896 interpret_type(fs);
897
898 /*
899 * If a specific fstype is specified, and it doesn't match,
900 * ignore it.
901 */
902 if (!fs_match(fs, &fs_type_compiled)) return 1;
903
904 /* Are we ignoring this type? */
905 for(ip = ignored_types; *ip; ip++)
906 if (strcmp(fs->type, *ip) == 0) return 1;
907
908 /* Do we really really want to check this fs? */
909 for(ip = really_wanted; *ip; ip++)
910 if (strcmp(fs->type, *ip) == 0) {
911 wanted = 1;
912 break;
913 }
914
915 /* See if the <fsck.fs> program is available. */
916 if (find_fsck(fs->type) == NULL) {
917 if (wanted)
918 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
919 fs->device, fs->type);
920 return 1;
921 }
922
923 /* We can and want to check this file system type. */
924 return 0;
925 }
926
927 static int count_slaves(dev_t disk)
928 {
929 DIR *dir;
930 struct dirent *dp;
931 char dirname[PATH_MAX];
932 int count = 0;
933
934 snprintf(dirname, sizeof(dirname),
935 "/sys/dev/block/%u:%u/slaves/",
936 major(disk), minor(disk));
937
938 if (!(dir = opendir(dirname)))
939 return -1;
940
941 while ((dp = readdir(dir)) != 0) {
942 #ifdef _DIRENT_HAVE_D_TYPE
943 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
944 continue;
945 #endif
946 if (dp->d_name[0] == '.' &&
947 ((dp->d_name[1] == 0) ||
948 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
949 continue;
950
951 count++;
952 }
953 closedir(dir);
954 return count;
955 }
956
957 /*
958 * Returns TRUE if a partition on the same disk is already being
959 * checked.
960 */
961 static int disk_already_active(struct fs_info *fs)
962 {
963 struct fsck_instance *inst;
964
965 if (force_all_parallel)
966 return 0;
967
968 if (instance_list && instance_list->fs->stacked)
969 /* any instance for a stacked device is already running */
970 return 1;
971
972 if (!fs->disk) {
973 struct stat st;
974 dev_t disk;
975
976 if (!stat(fs->device, &st) &&
977 !blkid_devno_to_wholedisk(st.st_rdev, NULL, 0, &disk)) {
978 fs->disk = disk;
979 fs->stacked = count_slaves(disk);
980 }
981 }
982
983 /*
984 * If we don't know the base device, assume that the device is
985 * already active if there are any fsck instances running.
986 *
987 * Don't check a stacked device with any other disk too.
988 */
989 if (!fs->disk || fs->stacked)
990 return (instance_list != 0);
991
992 for (inst = instance_list; inst; inst = inst->next) {
993 if (!inst->fs->disk || fs->disk == inst->fs->disk)
994 return 1;
995 }
996 return 0;
997 }
998
999 /* Check all file systems, using the /etc/fstab table. */
1000 static int check_all(NOARGS)
1001 {
1002 struct fs_info *fs = NULL;
1003 int status = EXIT_OK;
1004 int not_done_yet = 1;
1005 int passno = 1;
1006 int pass_done;
1007
1008 if (verbose)
1009 fputs(_("Checking all file systems.\n"), stdout);
1010
1011 /*
1012 * Do an initial scan over the filesystem; mark filesystems
1013 * which should be ignored as done, and resolve any "auto"
1014 * filesystem types (done as a side-effect of calling ignore()).
1015 */
1016 for (fs = filesys_info; fs; fs = fs->next) {
1017 if (ignore(fs))
1018 fs->flags |= FLAG_DONE;
1019 }
1020
1021 /*
1022 * Find and check the root filesystem.
1023 */
1024 if (!parallel_root) {
1025 for (fs = filesys_info; fs; fs = fs->next) {
1026 if (!strcmp(fs->mountpt, "/"))
1027 break;
1028 }
1029 if (fs) {
1030 if (!skip_root && !ignore(fs) &&
1031 !(ignore_mounted && is_mounted(fs->device))) {
1032 fsck_device(fs, 1);
1033 status |= wait_many(FLAG_WAIT_ALL);
1034 if (status > EXIT_NONDESTRUCT)
1035 return status;
1036 }
1037 fs->flags |= FLAG_DONE;
1038 }
1039 }
1040 /*
1041 * This is for the bone-headed user who enters the root
1042 * filesystem twice. Skip root will skep all root entries.
1043 */
1044 if (skip_root)
1045 for (fs = filesys_info; fs; fs = fs->next)
1046 if (!strcmp(fs->mountpt, "/"))
1047 fs->flags |= FLAG_DONE;
1048
1049 while (not_done_yet) {
1050 not_done_yet = 0;
1051 pass_done = 1;
1052
1053 for (fs = filesys_info; fs; fs = fs->next) {
1054 if (cancel_requested)
1055 break;
1056 if (fs->flags & FLAG_DONE)
1057 continue;
1058 /*
1059 * If the filesystem's pass number is higher
1060 * than the current pass number, then we don't
1061 * do it yet.
1062 */
1063 if (fs->passno > passno) {
1064 not_done_yet++;
1065 continue;
1066 }
1067 if (ignore_mounted && is_mounted(fs->device)) {
1068 fs->flags |= FLAG_DONE;
1069 continue;
1070 }
1071 /*
1072 * If a filesystem on a particular device has
1073 * already been spawned, then we need to defer
1074 * this to another pass.
1075 */
1076 if (disk_already_active(fs)) {
1077 pass_done = 0;
1078 continue;
1079 }
1080 /*
1081 * Spawn off the fsck process
1082 */
1083 fsck_device(fs, serialize);
1084 fs->flags |= FLAG_DONE;
1085
1086 /*
1087 * Only do one filesystem at a time, or if we
1088 * have a limit on the number of fsck's extant
1089 * at one time, apply that limit.
1090 */
1091 if (serialize ||
1092 (max_running && (num_running >= max_running))) {
1093 pass_done = 0;
1094 break;
1095 }
1096 }
1097 if (cancel_requested)
1098 break;
1099 if (verbose > 1)
1100 printf(_("--waiting-- (pass %d)\n"), passno);
1101 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1102 FLAG_WAIT_ATLEAST_ONE);
1103 if (pass_done) {
1104 if (verbose > 1)
1105 printf("----------------------------------\n");
1106 passno++;
1107 } else
1108 not_done_yet++;
1109 }
1110 if (cancel_requested && !kill_sent) {
1111 kill_all(SIGTERM);
1112 kill_sent++;
1113 }
1114 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1115 return status;
1116 }
1117
1118 static void usage(NOARGS)
1119 {
1120 fputs(_("Usage: fsck [-AMNPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n"), stderr);
1121 exit(EXIT_USAGE);
1122 }
1123
1124 static void signal_cancel(int sig FSCK_ATTR((unused)))
1125 {
1126 cancel_requested++;
1127 }
1128
1129 static void PRS(int argc, char *argv[])
1130 {
1131 int i, j;
1132 char *arg, *dev, *tmp = 0;
1133 char options[128];
1134 int opt = 0;
1135 int opts_for_fsck = 0;
1136 struct sigaction sa;
1137
1138 /*
1139 * Set up signal action
1140 */
1141 memset(&sa, 0, sizeof(struct sigaction));
1142 sa.sa_handler = signal_cancel;
1143 sigaction(SIGINT, &sa, 0);
1144 sigaction(SIGTERM, &sa, 0);
1145
1146 num_devices = 0;
1147 num_args = 0;
1148 instance_list = 0;
1149
1150 progname = argv[0];
1151
1152 for (i=1; i < argc; i++) {
1153 arg = argv[i];
1154 if (!arg)
1155 continue;
1156 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1157 if (num_devices >= MAX_DEVICES) {
1158 fprintf(stderr, _("%s: too many devices\n"),
1159 progname);
1160 exit(EXIT_ERROR);
1161 }
1162 dev = fsprobe_get_devname_by_spec(arg);
1163 if (!dev && strchr(arg, '=')) {
1164 /*
1165 * Check to see if we failed because
1166 * /proc/partitions isn't found.
1167 */
1168 if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) {
1169 fprintf(stderr, _("Couldn't open %s: %s\n"),
1170 _PATH_PROC_PARTITIONS, strerror(errno));
1171 fprintf(stderr, _("Is /proc mounted?\n"));
1172 exit(EXIT_ERROR);
1173 }
1174 /*
1175 * Check to see if this is because
1176 * we're not running as root
1177 */
1178 if (geteuid())
1179 fprintf(stderr,
1180 _("Must be root to scan for matching filesystems: %s\n"), arg);
1181 else
1182 fprintf(stderr,
1183 _("Couldn't find matching filesystem: %s\n"), arg);
1184 exit(EXIT_ERROR);
1185 }
1186 devices[num_devices++] = dev ? dev : string_copy(arg);
1187 continue;
1188 }
1189 if (arg[0] != '-' || opts_for_fsck) {
1190 if (num_args >= MAX_ARGS) {
1191 fprintf(stderr, _("%s: too many arguments\n"),
1192 progname);
1193 exit(EXIT_ERROR);
1194 }
1195 args[num_args++] = string_copy(arg);
1196 continue;
1197 }
1198 for (j=1; arg[j]; j++) {
1199 if (opts_for_fsck) {
1200 options[++opt] = arg[j];
1201 continue;
1202 }
1203 switch (arg[j]) {
1204 case 'A':
1205 doall++;
1206 break;
1207 case 'C':
1208 progress++;
1209 if (arg[j+1]) {
1210 progress_fd = string_to_int(arg+j+1);
1211 if (progress_fd < 0)
1212 progress_fd = 0;
1213 else
1214 goto next_arg;
1215 } else if ((i+1) < argc &&
1216 !strncmp(argv[i+1], "-", 1) == 0) {
1217 progress_fd = string_to_int(argv[i]);
1218 if (progress_fd < 0)
1219 progress_fd = 0;
1220 else {
1221 goto next_arg;
1222 i++;
1223 }
1224 }
1225 break;
1226 case 'V':
1227 verbose++;
1228 break;
1229 case 'N':
1230 noexecute++;
1231 break;
1232 case 'R':
1233 skip_root++;
1234 break;
1235 case 'T':
1236 notitle++;
1237 break;
1238 case 'M':
1239 ignore_mounted++;
1240 break;
1241 case 'P':
1242 parallel_root++;
1243 break;
1244 case 's':
1245 serialize++;
1246 break;
1247 case 't':
1248 tmp = 0;
1249 if (fstype)
1250 usage();
1251 if (arg[j+1])
1252 tmp = arg+j+1;
1253 else if ((i+1) < argc)
1254 tmp = argv[++i];
1255 else
1256 usage();
1257 fstype = string_copy(tmp);
1258 compile_fs_type(fstype, &fs_type_compiled);
1259 goto next_arg;
1260 case '-':
1261 opts_for_fsck++;
1262 break;
1263 case '?':
1264 usage();
1265 break;
1266 default:
1267 options[++opt] = arg[j];
1268 break;
1269 }
1270 }
1271 next_arg:
1272 if (opt) {
1273 options[0] = '-';
1274 options[++opt] = '\0';
1275 if (num_args >= MAX_ARGS) {
1276 fprintf(stderr,
1277 _("%s: too many arguments\n"),
1278 progname);
1279 exit(EXIT_ERROR);
1280 }
1281 args[num_args++] = string_copy(options);
1282 opt = 0;
1283 }
1284 }
1285 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1286 force_all_parallel++;
1287 if ((tmp = getenv("FSCK_MAX_INST")))
1288 max_running = atoi(tmp);
1289 }
1290
1291 int main(int argc, char *argv[])
1292 {
1293 int i, status = 0;
1294 int interactive = 0;
1295 char *oldpath = getenv("PATH");
1296 const char *fstab;
1297 struct fs_info *fs;
1298
1299 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1300 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1301
1302 setlocale(LC_MESSAGES, "");
1303 setlocale(LC_CTYPE, "");
1304 bindtextdomain(PACKAGE, LOCALEDIR);
1305 textdomain(PACKAGE);
1306
1307 fsprobe_init();
1308 PRS(argc, argv);
1309
1310 if (!notitle)
1311 printf(_("fsck from %s\n"), PACKAGE_STRING);
1312
1313 fstab = getenv("FSTAB_FILE");
1314 if (!fstab)
1315 fstab = _PATH_MNTTAB;
1316 load_fs_info(fstab);
1317
1318 /* Update our search path to include uncommon directories. */
1319 if (oldpath) {
1320 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1321 strlen (oldpath) + 1);
1322 if (!fsck_path) {
1323 fprintf(stderr, _("%s: Unable to allocate memory for fsck_path\n"), progname);
1324 exit(EXIT_ERROR);
1325 }
1326 strcpy (fsck_path, fsck_prefix_path);
1327 strcat (fsck_path, ":");
1328 strcat (fsck_path, oldpath);
1329 } else {
1330 fsck_path = string_copy(fsck_prefix_path);
1331 }
1332
1333 if ((num_devices == 1) || (serialize))
1334 interactive = 1;
1335
1336 /* If -A was specified ("check all"), do that! */
1337 if (doall)
1338 return check_all();
1339
1340 if (num_devices == 0) {
1341 serialize++;
1342 interactive++;
1343 return check_all();
1344 }
1345 for (i = 0 ; i < num_devices; i++) {
1346 if (cancel_requested) {
1347 if (!kill_sent) {
1348 kill_all(SIGTERM);
1349 kill_sent++;
1350 }
1351 break;
1352 }
1353 fs = lookup(devices[i]);
1354 if (!fs) {
1355 fs = create_fs_device(devices[i], 0, "auto",
1356 0, -1, -1);
1357 if (!fs)
1358 continue;
1359 }
1360 if (ignore_mounted && is_mounted(fs->device))
1361 continue;
1362 fsck_device(fs, interactive);
1363 if (serialize ||
1364 (max_running && (num_running >= max_running))) {
1365 struct fsck_instance *inst;
1366
1367 inst = wait_one(0);
1368 if (inst) {
1369 status |= inst->exit_status;
1370 free_instance(inst);
1371 }
1372 if (verbose > 1)
1373 printf("----------------------------------\n");
1374 }
1375 }
1376 status |= wait_many(FLAG_WAIT_ALL);
1377 free(fsck_path);
1378 fsprobe_exit();
1379 return status;
1380 }
1381