Whamcloud - gitweb
ChangeLog, debugfs.8.in, debugfs.c:
[tools/e2fsprogs.git] / misc / 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  * Usage:       fsck [-AVRNTM] [-s] [-t fstype] [fs-options] device
12  * 
13  * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
14  *   o Changed -t fstype to behave like with mount when -A (all file
15  *     systems) or -M (like mount) is specified.
16  *   o fsck looks if it can find the fsck.type program to decide
17  *     if it should ignore the fs type. This way more fsck programs
18  *     can be added without changing this front-end.
19  *   o -R flag skip root file system.
20  *
21  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
22  *
23  * %Begin-Header%
24  * This file may be redistributed under the terms of the GNU Public
25  * License.
26  * %End-Header%
27  */
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/signal.h>
32 #include <sys/stat.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <string.h>
36 #if HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #if HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #if HAVE_MNTENT_H
43 #include <mntent.h>
44 #endif
45 #if HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #if HAVE_ERRNO_H
49 #include <errno.h>
50 #endif
51 #include <malloc.h>
52
53 #include "../version.h"
54 #include "fsck.h"
55
56 static const char *ignored_types[] = {
57         "ignore",
58         "iso9660",
59         "nfs",
60         "proc",
61         "sw",
62         "swap",
63         NULL
64 };
65
66 static const char *really_wanted[] = {
67         "minix",
68         "ext2",
69         "xiafs",
70         NULL
71 };
72
73 #ifdef DEV_DSK_DEVICES
74 static const char *base_devices[] = {
75         "/dev/dsk/hda",
76         "/dev/dsk/hdb",
77         "/dev/dsk/hdc",
78         "/dev/dsk/hdd",
79         "/dev/dsk/hd1a",
80         "/dev/dsk/hd1b",
81         "/dev/dsk/hd1c",
82         "/dev/dsk/hd1d",
83         "/dev/dsk/sda",
84         "/dev/dsk/sdb",
85         "/dev/dsk/sdc",
86         "/dev/dsk/sdd",
87         "/dev/dsk/sde",
88         "/dev/dsk/sdf",
89         "/dev/dsk/sdg",
90         NULL
91 };
92 #else
93 static const char *base_devices[] = {
94         "/dev/hda",
95         "/dev/hdb",
96         "/dev/hdc",
97         "/dev/hdd",
98         "/dev/hd1a",
99         "/dev/hd1b",
100         "/dev/hd1c",
101         "/dev/hd1d",
102         "/dev/sda",
103         "/dev/sdb",
104         "/dev/sdc",
105         "/dev/sdd",
106         "/dev/sde",
107         "/dev/sdf",
108         "/dev/sdg",
109         NULL
110 };
111 #endif
112
113 /*
114  * Global variables for options
115  */
116 char *devices[MAX_DEVICES];
117 char *args[MAX_ARGS];
118 int num_devices, num_args;
119
120 int verbose = 0;
121 int doall = 0;
122 int noexecute = 0;
123 int serialize = 0;
124 int skip_root = 0;
125 int like_mount = 0;
126 int notitle = 0;
127 int parallel_root = 0;
128 char *progname;
129 char *fstype = NULL;
130 struct fs_info *filesys_info;
131 struct fsck_instance *instance_list;
132 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
133 char *fsck_path = 0;
134 static int ignore(struct fs_info *);
135
136 static char *string_copy(const char *s)
137 {
138         char    *ret;
139
140         ret = malloc(strlen(s)+1);
141         if (ret)
142                 strcpy(ret, s);
143         return ret;
144 }
145
146 static void free_instance(struct fsck_instance *i)
147 {
148         if (i->prog)
149                 free(i->prog);
150         if (i->device)
151                 free(i->device);
152         free(i);
153         return;
154 }
155
156 /*
157  * Load the filesystem database from /etc/fstab
158  */
159 static void load_fs_info(NOARGS)
160 {
161 #if HAVE_MNTENT_H
162         FILE *mntfile;
163         struct mntent *mp;
164         struct fs_info *fs;
165         struct fs_info *fs_last = NULL;
166         int     old_fstab = 1;
167
168         filesys_info = NULL;
169         
170         /* Open the mount table. */
171         if ((mntfile = setmntent(MNTTAB, "r")) == NULL) {
172                 perror(MNTTAB);
173                 exit(EXIT_ERROR);
174         }
175
176         while ((mp = getmntent(mntfile)) != NULL) {
177                 fs = malloc(sizeof(struct fs_info));
178                 memset(fs, 0, sizeof(struct fs_info));
179                 fs->device = string_copy(mp->mnt_fsname);
180                 fs->mountpt = string_copy(mp->mnt_dir);
181                 fs->type = string_copy(mp->mnt_type);
182                 fs->opts = string_copy(mp->mnt_opts);
183                 fs->freq = mp->mnt_freq;
184                 fs->passno = mp->mnt_passno;
185                 fs->next = NULL;
186                 if (!filesys_info)
187                         filesys_info = fs;
188                 else
189                         fs_last->next = fs;
190                 fs_last = fs;
191                 if (fs->passno)
192                         old_fstab = 0;
193         }
194
195         (void) endmntent(mntfile);
196
197         if (old_fstab) {
198                 fprintf(stderr, "\007\007\007"
199         "WARNING: Your /etc/fstab does not contain the fsck passno\n");
200                 fprintf(stderr,
201         "       field.  I will kludge around things for you, but you\n");
202                 fprintf(stderr,
203         "       should fix your /etc/fstab file as soon as you can.\n\n");
204                 
205                 for (fs = filesys_info; fs; fs = fs->next) {
206                         fs->passno = 1;
207                 }
208         }
209 #else
210         filesys_info = NULL;
211 #endif /* HAVE_MNTENT_H */
212 }
213         
214 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
215 static struct fs_info *lookup(char *filesys)
216 {
217         struct fs_info *fs;
218
219         /* No filesys name given. */
220         if (filesys == NULL)
221                 return NULL;
222
223         for (fs = filesys_info; fs; fs = fs->next) {
224                 if (!strcmp(filesys, fs->device) ||
225                     !strcmp(filesys, fs->mountpt))
226                         break;
227         }
228
229         return fs;
230 }
231
232 /* Find fsck program for a given fs type. */
233 static char *find_fsck(char *type)
234 {
235   char *s;
236   const char *tpl;
237   static char prog[256];
238   char *p = string_copy(fsck_path);
239   struct stat st;
240
241   /* Are we looking for a program or just a type? */
242   tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
243
244   for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
245         sprintf(prog, tpl, s, type);
246         if (stat(prog, &st) == 0) break;
247   }
248   free(p);
249   return(s ? prog : NULL);
250 }
251
252 /*
253  * Execute a particular fsck program, and link it into the list of
254  * child processes we are waiting for.
255  */
256 static int execute(char *prog, char *device)
257 {
258         char *s, *argv[80];
259         int  argc, i;
260         struct fsck_instance *inst;
261         pid_t   pid;
262
263         argv[0] = string_copy(prog);
264         argc = 1;
265         
266         for (i=0; i <num_args; i++)
267                 argv[argc++] = string_copy(args[i]);
268
269         argv[argc++] = string_copy(device);
270         argv[argc] = 0;
271
272         s = find_fsck(prog);
273         if (s == NULL) {
274                 fprintf(stderr, "fsck: %s: not found\n", prog);
275                 return ENOENT;
276         }
277
278         if (verbose || noexecute) {
279                 printf("[%s] ", s);
280                 for (i=0; i < argc; i++)
281                         printf("%s ", argv[i]);
282                 printf("\n");
283         }
284         if (noexecute)
285                 return 0;
286         
287         /* Fork and execute the correct program. */
288         if ((pid = fork()) < 0) {
289                 perror("fork");
290                 return errno;
291         } else if (pid == 0) {
292                 (void) execv(s, argv);
293                 perror(argv[0]);
294                 exit(EXIT_ERROR);
295         }
296         inst = malloc(sizeof(struct fsck_instance));
297         if (!inst)
298                 return ENOMEM;
299         memset(inst, 0, sizeof(struct fsck_instance));
300         inst->pid = pid;
301         inst->prog = string_copy(prog);
302         inst->device = string_copy(device);
303         inst->next = instance_list;
304         instance_list = inst;
305         
306         return 0;
307 }
308
309 /*
310  * Wait for one child process to exit; when it does, unlink it from
311  * the list of executing child processes, and return it.
312  */
313 static struct fsck_instance *wait_one(NOARGS)
314 {
315         int     status;
316         int     sig;
317         struct fsck_instance *inst, *prev;
318         pid_t   pid;
319
320         if (!instance_list)
321                 return NULL;
322
323 retry:
324         pid = wait(&status);
325         if (pid < 0) {
326                 if ((errno == EINTR) || (errno == EAGAIN))
327                         goto retry;
328                 if (errno == ECHILD) {
329                         fprintf(stderr,
330                                 "%s: wait: No more child process?!?\n",
331                                 progname);
332                         return NULL;
333                 }
334                 perror("wait");
335                 goto retry;
336         }
337         for (prev = 0, inst = instance_list;
338              inst;
339              prev = inst, inst = inst->next) {
340                 if (inst->pid == pid)
341                         break;
342         }
343         if (!inst) {
344                 printf("Unexpected child process %d, status = 0x%x\n",
345                        pid, status);
346                 goto retry;
347         }
348         if (WIFEXITED(status)) 
349                 status = WEXITSTATUS(status);
350         else if (WIFSIGNALED(status)) {
351                 sig = WTERMSIG(status);
352                 if (sig == SIGINT) {
353                         status = EXIT_UNCORRECTED;
354                 } else {
355                         printf("Warning... %s for device %s exited "
356                                "with signal %d.\n",
357                                inst->prog, inst->device, sig);
358                         status = EXIT_ERROR;
359                 }
360         } else {
361                 printf("%s %s: status is %x, should never happen.\n",
362                        inst->prog, inst->device, status);
363                 status = EXIT_ERROR;
364         }
365         inst->exit_status = status;
366         if (prev)
367                 prev->next = inst->next;
368         else
369                 instance_list = inst->next;
370         return inst;
371 }
372
373 /*
374  * Wait until all executing child processes have exited; return the
375  * logical OR of all of their exit code values.
376  */
377 static int wait_all(NOARGS)
378 {
379         struct fsck_instance *inst;
380         int     global_status = 0;
381
382         while (instance_list) {
383                 inst = wait_one();
384                 if (!inst)
385                         break;
386                 global_status |= inst->exit_status;
387                 free_instance(inst);
388         }
389         return global_status;
390 }
391
392 /*
393  * Run the fsck program on a particular device
394  * 
395  * If the type is specified using -t, and it isn't prefixed with "no"
396  * (as in "noext2") and only one filesystem type is specified, then
397  * use that type regardless of what is specified in /etc/fstab.
398  * 
399  * If the type isn't specified by the user, then use either the type
400  * specified in /etc/fstab, or DEFAULT_FSTYPE.
401  */
402 static void fsck_device(char *device)
403 {
404         const char      *type = 0;
405         struct fs_info *fsent;
406         int retval;
407         char prog[80];
408
409         if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
410                 type = fstype;
411
412         if ((fsent = lookup(device))) {
413                 device = fsent->device;
414                 if (!type)
415                         type = fsent->type;
416         }
417         if (!type)
418                 type = DEFAULT_FSTYPE;
419
420         sprintf(prog, "fsck.%s", type);
421         retval = execute(prog, device);
422         if (retval) {
423                 fprintf(stderr, "%s: Error %d while executing %s for %s\n",
424                         progname, retval, prog, device);
425         }
426 }
427
428 /* See if filesystem type matches the list. */
429 static int fs_match(char *type, char *fs_type)
430 {
431   int ret = 0, negate = 0;
432   char list[128];
433   char *s;
434
435   if (!fs_type) return(1);
436
437   if (strncmp(fs_type, "no", 2) == 0) {
438         fs_type += 2;
439         negate = 1;
440   }
441   strcpy(list, fs_type);
442   s = strtok(list, ",");
443   while(s) {
444         if (strcmp(s, type) == 0) {
445                 ret = 1;
446                 break;
447         }
448         s = strtok(NULL, ",");
449   }
450   return(negate ? !ret : ret);
451 }
452
453
454 /* Check if we should ignore this filesystem. */
455 static int ignore(struct fs_info *fs)
456 {
457         const char **ip;
458         int wanted = 0;
459
460         /*
461          * If the pass number is 0, ignore it.
462          */
463         if (fs->passno == 0)
464                 return 1;
465
466         /*
467          * If a specific fstype is specified, and it doesn't match,
468          * ignore it.
469          */
470         if (!fs_match(fs->type, fstype)) return 1;
471         
472         /* Are we ignoring this type? */
473         for(ip = ignored_types; *ip; ip++)
474                 if (strcmp(fs->type, *ip) == 0) return(1);
475
476         /* Do we really really want to check this fs? */
477         for(ip = really_wanted; *ip; ip++)
478                 if (strcmp(fs->type, *ip) == 0) {
479                         wanted = 1;
480                         break;
481                 }
482
483         /* See if the <fsck.fs> program is available. */
484         if (find_fsck(fs->type) == NULL) {
485                 if (wanted)
486                         fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
487                                 fs->device, fs->type);
488                 return(1);
489         }
490
491         /* We can and want to check this file system type. */
492         return 0;
493 }
494
495 /*
496  * Return the "base device" given a particular device; this is used to
497  * assure that we only fsck one partition on a particular drive at any
498  * one time.  Otherwise, the disk heads will be seeking all over the
499  * place.
500  */
501 static const char *base_device(char *device)
502 {
503         const char **base;
504
505         for (base = base_devices; *base; base++) {
506                 if (!strncmp(*base, device, strlen(*base)))
507                         return *base;
508         }
509         return device;
510 }
511
512 /*
513  * Returns TRUE if a partition on the same disk is already being
514  * checked.
515  */
516 static int device_already_active(char *device)
517 {
518         struct fsck_instance *inst;
519         const char *base;
520
521         base = base_device(device);
522
523         for (inst = instance_list; inst; inst = inst->next) {
524                 if (!strcmp(base, base_device(inst->device)))
525                         return 1;
526         }
527
528         return 0;
529 }
530
531 /* Check all file systems, using the /etc/fstab table. */
532 static int check_all(NOARGS)
533 {
534         struct fs_info *fs = NULL;
535         struct fsck_instance *inst;
536         int status = EXIT_OK;
537         int not_done_yet = 1;
538         int passno = 0;
539         int pass_done;
540
541         if (verbose)
542                 printf("Checking all file systems.\n");
543
544         /*
545          * Find and check the root filesystem first.
546          */
547         if (!parallel_root) {
548                 for (fs = filesys_info; fs; fs = fs->next) {
549                         if (!strcmp(fs->mountpt, "/"))
550                                 break;
551                 }
552                 if (fs && !skip_root && !ignore(fs)) {
553                         fsck_device(fs->device);
554                         fs->flags |= FLAG_DONE;
555                         status |= wait_all();
556                         if (status > EXIT_NONDESTRUCT)
557                                 return status;
558                 }
559         }
560         if (fs) fs->flags |= FLAG_DONE;
561
562         /*
563          * Mark filesystems that should be ignored as done.
564          */
565         for (fs = filesys_info; fs; fs = fs->next) {
566                 if (ignore(fs))
567                         fs->flags |= FLAG_DONE;
568         }
569                 
570         while (not_done_yet) {
571                 not_done_yet = 0;
572                 pass_done = 1;
573
574                 for (fs = filesys_info; fs; fs = fs->next) {
575                         if (fs->flags & FLAG_DONE)
576                                 continue;
577                         /*
578                          * If the filesystem's pass number is higher
579                          * than the current pass number, then we don't
580                          * do it yet.
581                          */
582                         if (fs->passno > passno) {
583                                 not_done_yet++;
584                                 continue;
585                         }
586                         /*
587                          * If a filesystem on a particular device has
588                          * already been spawned, then we need to defer
589                          * this to another pass.
590                          */
591                         if (device_already_active(fs->device)) {
592                                 pass_done = 0;
593                                 continue;
594                         }
595                         /*
596                          * Spawn off the fsck process
597                          */
598                         fsck_device(fs->device);
599                         fs->flags |= FLAG_DONE;
600
601                         if (serialize) {
602                                 pass_done = 0;
603                                 break; /* Only do one filesystem at a time */
604
605                         }
606                 }
607                 inst = wait_one();
608                 if (inst) {
609                         status |= inst->exit_status;
610                         free_instance(inst);
611                 }
612                 if (pass_done) {
613                         status |= wait_all();
614                         if (verbose) 
615                                 printf("----------------------------------\n");
616                         passno++;
617                 } else
618                         not_done_yet++;
619         }
620         status |= wait_all();
621         return status;
622 }
623
624 static void usage(NOARGS)
625 {
626         fprintf(stderr,
627                 "Usage: fsck [-AV] [-t fstype] [fs-options] filesys\n");
628         exit(EXIT_USAGE);
629 }
630
631 static void PRS(int argc, char *argv[])
632 {
633         int     i, j;
634         char    *arg;
635         char    options[128];
636         int     opt = 0;
637         int     opts_for_fsck = 0;
638         
639         num_devices = 0;
640         num_args = 0;
641         instance_list = 0;
642
643         progname = argv[0];
644
645         load_fs_info();
646
647         for (i=1; i < argc; i++) {
648                 arg = argv[i];
649                 if (!arg)
650                         continue;
651                 if (arg[0] == '/') {
652                         if (num_devices >= MAX_DEVICES) {
653                                 fprintf(stderr, "%s: too many devices\n",
654                                         progname);
655                                 exit(1);
656                         }
657                         devices[num_devices++] = string_copy(arg);
658                         continue;
659                 }
660                 if (arg[0] != '-') {
661                         if (num_args >= MAX_ARGS) {
662                                 fprintf(stderr, "%s: too many arguments\n",
663                                         progname);
664                                 exit(1);
665                         }
666                         args[num_args++] = string_copy(arg);
667                         continue;
668                 }
669                 for (j=1; arg[j]; j++) {
670                         if (opts_for_fsck) {
671                                 options[++opt] = arg[j];
672                                 continue;
673                         }
674                         switch (arg[j]) {
675                         case 'A':
676                                 doall++;
677                                 break;
678                         case 'V':
679                                 verbose++;
680                                 break;
681                         case 'N':
682                                 noexecute++;
683                                 break;
684                         case 'R':
685                                 skip_root++;
686                                 break;
687                         case 'T':
688                                 notitle++;
689                                 break;
690                         case 'M':
691                                 like_mount++;
692                                 break;
693                         case 'P':
694                                 parallel_root++;
695                                 break;
696                         case 's':
697                                 serialize++;
698                                 break;
699                         case 't':
700                                 if (arg[j+1]) {
701                                         fstype = string_copy(arg+j+1);
702                                         goto next_arg;
703                                 }
704                                 if ((i+1) < argc) {
705                                         i++;
706                                         fstype = string_copy(argv[i]);
707                                         goto next_arg;
708                                 }
709                                 usage();
710                                 break;
711                         case '-':
712                                 opts_for_fsck++;
713                                 break;
714                         default:
715                                 options[++opt] = arg[j];
716                                 break;
717                         }
718                 }
719         next_arg:
720                 if (opt) {
721                         options[0] = '-';
722                         options[++opt] = '\0';
723                         if (num_args >= MAX_ARGS) {
724                                 fprintf(stderr,
725                                         "%s: too many arguments\n",
726                                         progname);
727                                 exit(1);
728                         }
729                         args[num_args++] = string_copy(options);
730                         opt = 0;
731                 }
732         }
733 }
734
735 int main(int argc, char *argv[])
736 {
737         int i;
738         int status = 0;
739         char *oldpath = getenv("PATH");
740
741         PRS(argc, argv);
742
743         if (!notitle)
744                 printf("Parallelizing fsck version %s (%s)\n",
745                         E2FSPROGS_VERSION, E2FSPROGS_DATE);
746
747         /* Update our search path to include uncommon directories. */
748         if (oldpath) {
749                 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
750                                     strlen (oldpath) + 1);
751                 strcpy (fsck_path, fsck_prefix_path);
752                 strcat (fsck_path, ":");
753                 strcat (fsck_path, oldpath);
754         } else {
755                 fsck_path = string_copy(fsck_prefix_path);
756         }
757         
758         /* If -A was specified ("check all"), do that! */
759         if (doall)
760                 return check_all();
761
762         for (i = 0 ; i < num_devices; i++) {
763                 fsck_device(devices[i]);
764                 if (serialize) {
765                         struct fsck_instance *inst;
766
767                         inst = wait_one();
768                         if (inst) {
769                                 status |= inst->exit_status;
770                                 free_instance(inst);
771                         }
772                 }
773         }
774         status |= wait_all();
775         free(fsck_path);
776         return status;
777 }