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