blob: 93514a3912991da4c7a07541dc818c4c0b1a17ea [file] [log] [blame]
Mike Frysinger51a43b42005-09-24 07:11:16 +00001/*
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>
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000010 *
Mike Frysinger51a43b42005-09-24 07:11:16 +000011 * 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 *
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000019 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
Mike Frysinger51a43b42005-09-24 07:11:16 +000021 *
22 * %Begin-Header%
23 * This file may be redistributed under the terms of the GNU Public
24 * License.
25 * %End-Header%
26 */
27
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/signal.h>
31#include <sys/stat.h>
32#include <limits.h>
33#include <stdio.h>
34#include <ctype.h>
35#include <string.h>
36#include <time.h>
37#include <stdlib.h>
38#include <errno.h>
39#include <paths.h>
40#include <unistd.h>
41#include <errno.h>
42#include <malloc.h>
43#include <signal.h>
44
45#include "fsck.h"
46#include "blkid/blkid.h"
47
48#include "e2fsbb.h"
49
50#ifndef _PATH_MNTTAB
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000051#define _PATH_MNTTAB "/etc/fstab"
Mike Frysinger51a43b42005-09-24 07:11:16 +000052#endif
53
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000054static const char * const ignored_types[] = {
Mike Frysinger51a43b42005-09-24 07:11:16 +000055 "ignore",
56 "iso9660",
57 "nfs",
58 "proc",
59 "sw",
60 "swap",
61 "tmpfs",
62 "devpts",
63 NULL
64};
65
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000066static const char * const really_wanted[] = {
Mike Frysinger51a43b42005-09-24 07:11:16 +000067 "minix",
68 "ext2",
69 "ext3",
70 "jfs",
71 "reiserfs",
72 "xiafs",
73 "xfs",
74 NULL
75};
76
77#define BASE_MD "/dev/md"
78
79/*
80 * Global variables for options
81 */
82static char *devices[MAX_DEVICES];
83static char *args[MAX_ARGS];
84static int num_devices, num_args;
85
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +000086static int verbose;
87static int doall;
88static int noexecute;
89static int serialize;
90static int skip_root;
91static int like_mount;
92static int notitle;
93static int parallel_root;
94static int progress;
95static int progress_fd;
96static int force_all_parallel;
97static int num_running;
98static int max_running;
99static volatile int cancel_requested;
100static int kill_sent;
101static char *fstype;
102static struct fs_info *filesys_info, *filesys_last;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000103static struct fsck_instance *instance_list;
104static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000105static char *fsck_path;
106static blkid_cache cache;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000107
108static char *string_copy(const char *s)
109{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000110 char *ret;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000111
112 if (!s)
113 return 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000114 ret = strdup(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000115 return ret;
116}
117
118static int string_to_int(const char *s)
119{
120 long l;
121 char *p;
122
123 l = strtol(s, &p, 0);
124 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
125 return -1;
126 else
127 return (int) l;
128}
129
Mike Frysinger51a43b42005-09-24 07:11:16 +0000130static char *skip_over_blank(char *cp)
131{
132 while (*cp && isspace(*cp))
133 cp++;
134 return cp;
135}
136
137static char *skip_over_word(char *cp)
138{
139 while (*cp && !isspace(*cp))
140 cp++;
141 return cp;
142}
143
144static void strip_line(char *line)
145{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000146 char *p;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000147
148 while (*line) {
149 p = line + strlen(line) - 1;
150 if ((*p == '\n') || (*p == '\r'))
151 *p = 0;
152 else
153 break;
154 }
155}
156
157static char *parse_word(char **buf)
158{
159 char *word, *next;
160
161 word = *buf;
162 if (*word == 0)
163 return 0;
164
165 word = skip_over_blank(word);
166 next = skip_over_word(word);
167 if (*next)
168 *next++ = 0;
169 *buf = next;
170 return word;
171}
172
173static void parse_escape(char *word)
174{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000175 char *q, c;
176 const char *p;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000177
178 if (!word)
179 return;
180
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000181 for (p = q = word; *p; q++) {
182 c = *p++;
183 if (c != '\\') {
184 *q = c;
185 } else {
186 *q = bb_process_escape_sequence(&p);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000187 }
Mike Frysinger51a43b42005-09-24 07:11:16 +0000188 }
189 *q = 0;
190}
191
192static void free_instance(struct fsck_instance *i)
193{
194 if (i->prog)
195 free(i->prog);
196 if (i->device)
197 free(i->device);
198 if (i->base_device)
199 free(i->base_device);
200 free(i);
201 return;
202}
203
204static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000205 const char *type, const char *opts,
Mike Frysinger51a43b42005-09-24 07:11:16 +0000206 int freq, int passno)
207{
208 struct fs_info *fs;
209
210 if (!(fs = malloc(sizeof(struct fs_info))))
211 return NULL;
212
213 fs->device = string_copy(device);
214 fs->mountpt = string_copy(mntpnt);
215 fs->type = string_copy(type);
216 fs->opts = string_copy(opts ? opts : "");
217 fs->freq = freq;
218 fs->passno = passno;
219 fs->flags = 0;
220 fs->next = NULL;
221
222 if (!filesys_info)
223 filesys_info = fs;
224 else
225 filesys_last->next = fs;
226 filesys_last = fs;
227
228 return fs;
229}
230
231
232
233static int parse_fstab_line(char *line, struct fs_info **ret_fs)
234{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000235 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000236 struct fs_info *fs;
237
238 *ret_fs = 0;
239 strip_line(line);
240 if ((cp = strchr(line, '#')))
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000241 *cp = 0; /* Ignore everything after the comment char */
Mike Frysinger51a43b42005-09-24 07:11:16 +0000242 cp = line;
243
244 device = parse_word(&cp);
245 mntpnt = parse_word(&cp);
246 type = parse_word(&cp);
247 opts = parse_word(&cp);
248 freq = parse_word(&cp);
249 passno = parse_word(&cp);
250
251 if (!device)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000252 return 0; /* Allow blank lines */
253
Mike Frysinger51a43b42005-09-24 07:11:16 +0000254 if (!mntpnt || !type)
255 return -1;
256
257 parse_escape(device);
258 parse_escape(mntpnt);
259 parse_escape(type);
260 parse_escape(opts);
261 parse_escape(freq);
262 parse_escape(passno);
263
264 dev = blkid_get_devname(cache, device, NULL);
265 if (dev)
266 device = dev;
267
268 if (strchr(type, ','))
269 type = 0;
270
271 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
272 freq ? atoi(freq) : -1,
273 passno ? atoi(passno) : -1);
274 if (dev)
275 free(dev);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000276
Mike Frysinger51a43b42005-09-24 07:11:16 +0000277 if (!fs)
278 return -1;
279 *ret_fs = fs;
280 return 0;
281}
282
283static void interpret_type(struct fs_info *fs)
284{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000285 char *t;
286
Mike Frysinger51a43b42005-09-24 07:11:16 +0000287 if (strcmp(fs->type, "auto") != 0)
288 return;
289 t = blkid_get_tag_value(cache, "TYPE", fs->device);
290 if (t) {
291 free(fs->type);
292 fs->type = t;
293 }
294}
295
296/*
297 * Load the filesystem database from /etc/fstab
298 */
299static void load_fs_info(const char *filename)
300{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000301 FILE *f;
302 char buf[1024];
303 int lineno = 0;
304 int old_fstab = 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000305 struct fs_info *fs;
306
307 if ((f = fopen(filename, "r")) == NULL) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000308 bb_perror_msg("WARNING: couldn't open %s: %m", filename);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000309 return;
310 }
311 while (!feof(f)) {
312 lineno++;
313 if (!fgets(buf, sizeof(buf), f))
314 break;
315 buf[sizeof(buf)-1] = 0;
316 if (parse_fstab_line(buf, &fs) < 0) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000317 bb_error_msg("WARNING: bad format "
318 "on line %d of %s\n", lineno, filename);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000319 continue;
320 }
321 if (!fs)
322 continue;
323 if (fs->passno < 0)
324 fs->passno = 0;
325 else
326 old_fstab = 0;
327 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000328
Mike Frysinger51a43b42005-09-24 07:11:16 +0000329 fclose(f);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000330
Mike Frysinger51a43b42005-09-24 07:11:16 +0000331 if (old_fstab) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000332 fputs("\007\007\007"
Mike Frysinger51a43b42005-09-24 07:11:16 +0000333 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000334 " field. I will kludge around things for you, but you\n"
335 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
336
Mike Frysinger51a43b42005-09-24 07:11:16 +0000337 for (fs = filesys_info; fs; fs = fs->next) {
338 fs->passno = 1;
339 }
340 }
341}
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000342
Mike Frysinger51a43b42005-09-24 07:11:16 +0000343/* Lookup filesys in /etc/fstab and return the corresponding entry. */
344static struct fs_info *lookup(char *filesys)
345{
346 struct fs_info *fs;
347
348 /* No filesys name given. */
349 if (filesys == NULL)
350 return NULL;
351
352 for (fs = filesys_info; fs; fs = fs->next) {
353 if (!strcmp(filesys, fs->device) ||
354 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
355 break;
356 }
357
358 return fs;
359}
360
361/* Find fsck program for a given fs type. */
362static char *find_fsck(char *type)
363{
364 char *s;
365 const char *tpl;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000366 char *p = string_copy(fsck_path);
367 struct stat st;
368
369 /* Are we looking for a program or just a type? */
370 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
371
372 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000373 s = bb_xasprintf(tpl, s, type);
374 if (stat(s, &st) == 0) break;
375 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000376 }
377 free(p);
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000378 return(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000379}
380
381static int progress_active(void)
382{
383 struct fsck_instance *inst;
384
385 for (inst = instance_list; inst; inst = inst->next) {
386 if (inst->flags & FLAG_DONE)
387 continue;
388 if (inst->flags & FLAG_PROGRESS)
389 return 1;
390 }
391 return 0;
392}
393
394/*
395 * Execute a particular fsck program, and link it into the list of
396 * child processes we are waiting for.
397 */
398static int execute(const char *type, const char *device, const char *mntpt,
399 int interactive)
400{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000401 char *s, *argv[80];
402 char *prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000403 int argc, i;
404 struct fsck_instance *inst, *p;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000405 pid_t pid;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000406
407 inst = malloc(sizeof(struct fsck_instance));
408 if (!inst)
409 return ENOMEM;
410 memset(inst, 0, sizeof(struct fsck_instance));
411
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000412 prog = bb_xasprintf("fsck.%s", type);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000413 argv[0] = prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000414 argc = 1;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000415
Mike Frysinger51a43b42005-09-24 07:11:16 +0000416 for (i=0; i <num_args; i++)
417 argv[argc++] = string_copy(args[i]);
418
419 if (progress && !progress_active()) {
420 if ((strcmp(type, "ext2") == 0) ||
421 (strcmp(type, "ext3") == 0)) {
422 char tmp[80];
423 snprintf(tmp, 80, "-C%d", progress_fd);
424 argv[argc++] = string_copy(tmp);
425 inst->flags |= FLAG_PROGRESS;
426 }
427 }
428
429 argv[argc++] = string_copy(device);
430 argv[argc] = 0;
431
432 s = find_fsck(prog);
433 if (s == NULL) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000434 bb_error_msg("%s: not found", prog);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000435 return ENOENT;
436 }
437
438 if (verbose || noexecute) {
439 printf("[%s (%d) -- %s] ", s, num_running,
440 mntpt ? mntpt : device);
441 for (i=0; i < argc; i++)
442 printf("%s ", argv[i]);
443 printf("\n");
444 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000445
Mike Frysinger51a43b42005-09-24 07:11:16 +0000446 /* Fork and execute the correct program. */
447 if (noexecute)
448 pid = -1;
449 else if ((pid = fork()) < 0) {
450 perror("fork");
451 return errno;
452 } else if (pid == 0) {
453 if (!interactive)
454 close(0);
455 (void) execv(s, argv);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000456 bb_perror_msg_and_die("%s", argv[0]);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000457 }
458
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000459 for (i = 1; i < argc; i++)
Mike Frysinger51a43b42005-09-24 07:11:16 +0000460 free(argv[i]);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000461
462 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000463 inst->pid = pid;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000464 inst->prog = prog;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000465 inst->type = string_copy(type);
466 inst->device = string_copy(device);
467 inst->base_device = base_device(device);
468 inst->start_time = time(0);
469 inst->next = NULL;
470
471 /*
472 * Find the end of the list, so we add the instance on at the end.
473 */
474 for (p = instance_list; p && p->next; p = p->next);
475
476 if (p)
477 p->next = inst;
478 else
479 instance_list = inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000480
Mike Frysinger51a43b42005-09-24 07:11:16 +0000481 return 0;
482}
483
484/*
485 * Send a signal to all outstanding fsck child processes
486 */
487static int kill_all(int signum)
488{
489 struct fsck_instance *inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000490 int n = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000491
492 for (inst = instance_list; inst; inst = inst->next) {
493 if (inst->flags & FLAG_DONE)
494 continue;
495 kill(inst->pid, signum);
496 n++;
497 }
498 return n;
499}
500
501/*
502 * Wait for one child process to exit; when it does, unlink it from
503 * the list of executing child processes, and return it.
504 */
505static struct fsck_instance *wait_one(int flags)
506{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000507 int status;
508 int sig;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000509 struct fsck_instance *inst, *inst2, *prev;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000510 pid_t pid;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000511
512 if (!instance_list)
513 return NULL;
514
515 if (noexecute) {
516 inst = instance_list;
517 prev = 0;
518#ifdef RANDOM_DEBUG
519 while (inst->next && (random() & 1)) {
520 prev = inst;
521 inst = inst->next;
522 }
523#endif
524 inst->exit_status = 0;
525 goto ret_inst;
526 }
527
528 /*
529 * gcc -Wall fails saving throw against stupidity
530 * (inst and prev are thought to be uninitialized variables)
531 */
532 inst = prev = NULL;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000533
Mike Frysinger51a43b42005-09-24 07:11:16 +0000534 do {
535 pid = waitpid(-1, &status, flags);
536 if (cancel_requested && !kill_sent) {
537 kill_all(SIGTERM);
538 kill_sent++;
539 }
540 if ((pid == 0) && (flags & WNOHANG))
541 return NULL;
542 if (pid < 0) {
543 if ((errno == EINTR) || (errno == EAGAIN))
544 continue;
545 if (errno == ECHILD) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000546 bb_error_msg("wait: No more child process?!?");
Mike Frysinger51a43b42005-09-24 07:11:16 +0000547 return NULL;
548 }
549 perror("wait");
550 continue;
551 }
552 for (prev = 0, inst = instance_list;
553 inst;
554 prev = inst, inst = inst->next) {
555 if (inst->pid == pid)
556 break;
557 }
558 } while (!inst);
559
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000560 if (WIFEXITED(status))
Mike Frysinger51a43b42005-09-24 07:11:16 +0000561 status = WEXITSTATUS(status);
562 else if (WIFSIGNALED(status)) {
563 sig = WTERMSIG(status);
564 if (sig == SIGINT) {
565 status = EXIT_UNCORRECTED;
566 } else {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000567 printf("Warning... %s for device %s exited "
568 "with signal %d.\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000569 inst->prog, inst->device, sig);
570 status = EXIT_ERROR;
571 }
572 } else {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000573 printf("%s %s: status is %x, should never happen.\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000574 inst->prog, inst->device, status);
575 status = EXIT_ERROR;
576 }
577 inst->exit_status = status;
578 if (progress && (inst->flags & FLAG_PROGRESS) &&
579 !progress_active()) {
580 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
581 if (inst2->flags & FLAG_DONE)
582 continue;
583 if (strcmp(inst2->type, "ext2") &&
584 strcmp(inst2->type, "ext3"))
585 continue;
586 /*
587 * If we've just started the fsck, wait a tiny
588 * bit before sending the kill, to give it
589 * time to set up the signal handler
590 */
591 if (inst2->start_time < time(0)+2) {
592 if (fork() == 0) {
593 sleep(1);
594 kill(inst2->pid, SIGUSR1);
595 exit(0);
596 }
597 } else
598 kill(inst2->pid, SIGUSR1);
599 inst2->flags |= FLAG_PROGRESS;
600 break;
601 }
602 }
603ret_inst:
604 if (prev)
605 prev->next = inst->next;
606 else
607 instance_list = inst->next;
608 if (verbose > 1)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000609 printf("Finished with %s (exit status %d)\n",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000610 inst->device, inst->exit_status);
611 num_running--;
612 return inst;
613}
614
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000615#define FLAG_WAIT_ALL 0
616#define FLAG_WAIT_ATLEAST_ONE 1
Mike Frysinger51a43b42005-09-24 07:11:16 +0000617/*
618 * Wait until all executing child processes have exited; return the
619 * logical OR of all of their exit code values.
620 */
621static int wait_many(int flags)
622{
623 struct fsck_instance *inst;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000624 int global_status = 0;
625 int wait_flags = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000626
627 while ((inst = wait_one(wait_flags))) {
628 global_status |= inst->exit_status;
629 free_instance(inst);
630#ifdef RANDOM_DEBUG
631 if (noexecute && (flags & WNOHANG) && !(random() % 3))
632 break;
633#endif
634 if (flags & FLAG_WAIT_ATLEAST_ONE)
635 wait_flags = WNOHANG;
636 }
637 return global_status;
638}
639
640/*
641 * Run the fsck program on a particular device
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000642 *
Mike Frysinger51a43b42005-09-24 07:11:16 +0000643 * If the type is specified using -t, and it isn't prefixed with "no"
644 * (as in "noext2") and only one filesystem type is specified, then
645 * use that type regardless of what is specified in /etc/fstab.
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000646 *
Mike Frysinger51a43b42005-09-24 07:11:16 +0000647 * If the type isn't specified by the user, then use either the type
648 * specified in /etc/fstab, or DEFAULT_FSTYPE.
649 */
650static void fsck_device(struct fs_info *fs, int interactive)
651{
652 const char *type;
653 int retval;
654
655 interpret_type(fs);
656
657 if (strcmp(fs->type, "auto") != 0)
658 type = fs->type;
659 else if (fstype && strncmp(fstype, "no", 2) &&
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000660 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
Mike Frysinger51a43b42005-09-24 07:11:16 +0000661 !strchr(fstype, ','))
662 type = fstype;
663 else
664 type = DEFAULT_FSTYPE;
665
666 num_running++;
667 retval = execute(type, fs->device, fs->mountpt, interactive);
668 if (retval) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000669 bb_error_msg("Error %d while executing fsck.%s for %s",
670 retval, type, fs->device);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000671 num_running--;
672 }
673}
674
675
676/*
677 * Deal with the fsck -t argument.
678 */
679struct fs_type_compile {
680 char **list;
681 int *type;
682 int negate;
683} fs_type_compiled;
684
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000685#define FS_TYPE_NORMAL 0
686#define FS_TYPE_OPT 1
687#define FS_TYPE_NEGOPT 2
Mike Frysinger51a43b42005-09-24 07:11:16 +0000688
689static const char *fs_type_syntax_error =
690N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
691 "with 'no' or '!'.\n");
692
693static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
694{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000695 char *cp, *list, *s;
696 int num = 2;
697 int negate, first_negate = 1;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000698
699 if (fs_type) {
700 for (cp=fs_type; *cp; cp++) {
701 if (*cp == ',')
702 num++;
703 }
704 }
705
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000706 cmp->list = xcalloc(num, sizeof(char *));
707 cmp->type = xcalloc(num, sizeof(int));
Mike Frysinger51a43b42005-09-24 07:11:16 +0000708 cmp->negate = 0;
709
710 if (!fs_type)
711 return;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000712
Mike Frysinger51a43b42005-09-24 07:11:16 +0000713 list = string_copy(fs_type);
714 num = 0;
715 s = strtok(list, ",");
716 while(s) {
717 negate = 0;
718 if (strncmp(s, "no", 2) == 0) {
719 s += 2;
720 negate = 1;
721 } else if (*s == '!') {
722 s++;
723 negate = 1;
724 }
725 if (strcmp(s, "loop") == 0)
726 /* loop is really short-hand for opts=loop */
727 goto loop_special_case;
728 else if (strncmp(s, "opts=", 5) == 0) {
729 s += 5;
730 loop_special_case:
731 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
732 } else {
733 if (first_negate) {
734 cmp->negate = negate;
735 first_negate = 0;
736 }
737 if ((negate && !cmp->negate) ||
738 (!negate && cmp->negate)) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000739 bb_error_msg_and_die("%s", fs_type_syntax_error);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000740 }
741 }
742#if 0
743 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
744#endif
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000745 cmp->list[num++] = string_copy(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000746 s = strtok(NULL, ",");
747 }
748 free(list);
749}
750
751/*
752 * This function returns true if a particular option appears in a
753 * comma-delimited options list
754 */
755static int opt_in_list(char *opt, char *optlist)
756{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000757 char *list, *s;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000758
759 if (!optlist)
760 return 0;
761 list = string_copy(optlist);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000762
Mike Frysinger51a43b42005-09-24 07:11:16 +0000763 s = strtok(list, ",");
764 while(s) {
765 if (strcmp(s, opt) == 0) {
766 free(list);
767 return 1;
768 }
769 s = strtok(NULL, ",");
770 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000771 free(list);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000772 return 0;
773}
774
775/* See if the filesystem matches the criteria given by the -t option */
776static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
777{
778 int n, ret = 0, checked_type = 0;
779 char *cp;
780
781 if (cmp->list == 0 || cmp->list[0] == 0)
782 return 1;
783
784 for (n=0; (cp = cmp->list[n]); n++) {
785 switch (cmp->type[n]) {
786 case FS_TYPE_NORMAL:
787 checked_type++;
788 if (strcmp(cp, fs->type) == 0) {
789 ret = 1;
790 }
791 break;
792 case FS_TYPE_NEGOPT:
793 if (opt_in_list(cp, fs->opts))
794 return 0;
795 break;
796 case FS_TYPE_OPT:
797 if (!opt_in_list(cp, fs->opts))
798 return 0;
799 break;
800 }
801 }
802 if (checked_type == 0)
803 return 1;
804 return (cmp->negate ? !ret : ret);
805}
806
807/* Check if we should ignore this filesystem. */
808static int ignore(struct fs_info *fs)
809{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000810 const char * const *ip;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000811 int wanted = 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000812 char *s;
Mike Frysinger51a43b42005-09-24 07:11:16 +0000813
814 /*
815 * If the pass number is 0, ignore it.
816 */
817 if (fs->passno == 0)
818 return 1;
819
820 interpret_type(fs);
821
822 /*
823 * If a specific fstype is specified, and it doesn't match,
824 * ignore it.
825 */
826 if (!fs_match(fs, &fs_type_compiled)) return 1;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000827
Mike Frysinger51a43b42005-09-24 07:11:16 +0000828 /* Are we ignoring this type? */
829 for(ip = ignored_types; *ip; ip++)
830 if (strcmp(fs->type, *ip) == 0) return 1;
831
832 /* Do we really really want to check this fs? */
833 for(ip = really_wanted; *ip; ip++)
834 if (strcmp(fs->type, *ip) == 0) {
835 wanted = 1;
836 break;
837 }
838
839 /* See if the <fsck.fs> program is available. */
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000840 s = find_fsck(fs->type);
841 if (s == NULL) {
Mike Frysinger51a43b42005-09-24 07:11:16 +0000842 if (wanted)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000843 bb_error_msg("cannot check %s: fsck.%s not found",
Mike Frysinger51a43b42005-09-24 07:11:16 +0000844 fs->device, fs->type);
845 return 1;
846 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000847 free(s);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000848
849 /* We can and want to check this file system type. */
850 return 0;
851}
852
853/*
854 * Returns TRUE if a partition on the same disk is already being
855 * checked.
856 */
857static int device_already_active(char *device)
858{
859 struct fsck_instance *inst;
860 char *base;
861
862 if (force_all_parallel)
863 return 0;
864
865#ifdef BASE_MD
866 /* Don't check a soft raid disk with any other disk */
867 if (instance_list &&
868 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
869 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
870 return 1;
871#endif
872
873 base = base_device(device);
874 /*
875 * If we don't know the base device, assume that the device is
876 * already active if there are any fsck instances running.
877 */
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000878 if (!base)
Mike Frysinger51a43b42005-09-24 07:11:16 +0000879 return (instance_list != 0);
880 for (inst = instance_list; inst; inst = inst->next) {
881 if (!inst->base_device || !strcmp(base, inst->base_device)) {
882 free(base);
883 return 1;
884 }
885 }
886 free(base);
887 return 0;
888}
889
890/* Check all file systems, using the /etc/fstab table. */
891static int check_all(void)
892{
893 struct fs_info *fs = NULL;
894 int status = EXIT_OK;
895 int not_done_yet = 1;
896 int passno = 1;
897 int pass_done;
898
899 if (verbose)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000900 fputs("Checking all file systems.\n", stdout);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000901
902 /*
903 * Do an initial scan over the filesystem; mark filesystems
904 * which should be ignored as done, and resolve any "auto"
905 * filesystem types (done as a side-effect of calling ignore()).
906 */
907 for (fs = filesys_info; fs; fs = fs->next) {
908 if (ignore(fs))
909 fs->flags |= FLAG_DONE;
910 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000911
Mike Frysinger51a43b42005-09-24 07:11:16 +0000912 /*
913 * Find and check the root filesystem.
914 */
915 if (!parallel_root) {
916 for (fs = filesys_info; fs; fs = fs->next) {
917 if (!strcmp(fs->mountpt, "/"))
918 break;
919 }
920 if (fs) {
921 if (!skip_root && !ignore(fs)) {
922 fsck_device(fs, 1);
923 status |= wait_many(FLAG_WAIT_ALL);
924 if (status > EXIT_NONDESTRUCT)
925 return status;
926 }
927 fs->flags |= FLAG_DONE;
928 }
929 }
930 /*
931 * This is for the bone-headed user who enters the root
932 * filesystem twice. Skip root will skep all root entries.
933 */
934 if (skip_root)
935 for (fs = filesys_info; fs; fs = fs->next)
936 if (!strcmp(fs->mountpt, "/"))
937 fs->flags |= FLAG_DONE;
938
939 while (not_done_yet) {
940 not_done_yet = 0;
941 pass_done = 1;
942
943 for (fs = filesys_info; fs; fs = fs->next) {
944 if (cancel_requested)
945 break;
946 if (fs->flags & FLAG_DONE)
947 continue;
948 /*
949 * If the filesystem's pass number is higher
950 * than the current pass number, then we don't
951 * do it yet.
952 */
953 if (fs->passno > passno) {
954 not_done_yet++;
955 continue;
956 }
957 /*
958 * If a filesystem on a particular device has
959 * already been spawned, then we need to defer
960 * this to another pass.
961 */
962 if (device_already_active(fs->device)) {
963 pass_done = 0;
964 continue;
965 }
966 /*
967 * Spawn off the fsck process
968 */
969 fsck_device(fs, serialize);
970 fs->flags |= FLAG_DONE;
971
972 /*
973 * Only do one filesystem at a time, or if we
974 * have a limit on the number of fsck's extant
975 * at one time, apply that limit.
976 */
977 if (serialize ||
978 (max_running && (num_running >= max_running))) {
979 pass_done = 0;
980 break;
981 }
982 }
983 if (cancel_requested)
984 break;
985 if (verbose > 1)
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000986 printf("--waiting-- (pass %d)\n", passno);
Mike Frysinger51a43b42005-09-24 07:11:16 +0000987 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
988 FLAG_WAIT_ATLEAST_ONE);
989 if (pass_done) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +0000990 if (verbose > 1)
Mike Frysinger51a43b42005-09-24 07:11:16 +0000991 printf("----------------------------------\n");
992 passno++;
993 } else
994 not_done_yet++;
995 }
996 if (cancel_requested && !kill_sent) {
997 kill_all(SIGTERM);
998 kill_sent++;
999 }
1000 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1001 return status;
1002}
1003
1004#if 0
1005static void usage(void)
1006{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001007 fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001008 exit(EXIT_USAGE);
1009}
1010#endif
1011
Mike Frysinger51a43b42005-09-24 07:11:16 +00001012static void signal_cancel(int sig FSCK_ATTR((unused)))
1013{
1014 cancel_requested++;
1015}
Mike Frysinger51a43b42005-09-24 07:11:16 +00001016
1017static void PRS(int argc, char *argv[])
1018{
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001019 int i, j;
1020 char *arg, *dev, *tmp = 0;
1021 char options[128];
1022 int opt = 0;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001023 int opts_for_fsck = 0;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001024 struct sigaction sa;
Mike Frysinger51a43b42005-09-24 07:11:16 +00001025
1026 /*
1027 * Set up signal action
1028 */
1029 memset(&sa, 0, sizeof(struct sigaction));
1030 sa.sa_handler = signal_cancel;
1031 sigaction(SIGINT, &sa, 0);
1032 sigaction(SIGTERM, &sa, 0);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001033
Mike Frysinger51a43b42005-09-24 07:11:16 +00001034 num_devices = 0;
1035 num_args = 0;
1036 instance_list = 0;
1037
Mike Frysinger51a43b42005-09-24 07:11:16 +00001038 for (i=1; i < argc; i++) {
1039 arg = argv[i];
1040 if (!arg)
1041 continue;
1042 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1043 if (num_devices >= MAX_DEVICES) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001044 bb_error_msg_and_die("too many devices");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001045 }
1046 dev = blkid_get_devname(cache, arg, NULL);
1047 if (!dev && strchr(arg, '=')) {
1048 /*
1049 * Check to see if we failed because
1050 * /proc/partitions isn't found.
1051 */
1052 if (access("/proc/partitions", R_OK) < 0) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001053 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1054 "Is /proc mounted?");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001055 }
1056 /*
1057 * Check to see if this is because
1058 * we're not running as root
1059 */
1060 if (geteuid())
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001061 bb_error_msg_and_die(
Mike Frysinger51a43b42005-09-24 07:11:16 +00001062 "Must be root to scan for matching filesystems: %s\n", arg);
1063 else
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001064 bb_error_msg_and_die(
1065 "Couldn't find matching filesystem: %s", arg);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001066 }
1067 devices[num_devices++] = dev ? dev : string_copy(arg);
1068 continue;
1069 }
1070 if (arg[0] != '-' || opts_for_fsck) {
1071 if (num_args >= MAX_ARGS) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001072 bb_error_msg_and_die("too many arguments");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001073 }
1074 args[num_args++] = string_copy(arg);
1075 continue;
1076 }
1077 for (j=1; arg[j]; j++) {
1078 if (opts_for_fsck) {
1079 options[++opt] = arg[j];
1080 continue;
1081 }
1082 switch (arg[j]) {
1083 case 'A':
1084 doall++;
1085 break;
1086 case 'C':
1087 progress++;
1088 if (arg[j+1]) {
1089 progress_fd = string_to_int(arg+j+1);
1090 if (progress_fd < 0)
1091 progress_fd = 0;
1092 else
1093 goto next_arg;
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001094 } else if ((i+1) < argc &&
Mike Frysinger51a43b42005-09-24 07:11:16 +00001095 !strncmp(argv[i+1], "-", 1) == 0) {
1096 progress_fd = string_to_int(argv[i]);
1097 if (progress_fd < 0)
1098 progress_fd = 0;
1099 else {
1100 goto next_arg;
1101 i++;
1102 }
1103 }
1104 break;
1105 case 'V':
1106 verbose++;
1107 break;
1108 case 'N':
1109 noexecute++;
1110 break;
1111 case 'R':
1112 skip_root++;
1113 break;
1114 case 'T':
1115 notitle++;
1116 break;
1117 case 'M':
1118 like_mount++;
1119 break;
1120 case 'P':
1121 parallel_root++;
1122 break;
1123 case 's':
1124 serialize++;
1125 break;
1126 case 't':
1127 tmp = 0;
1128 if (fstype)
1129 usage();
1130 if (arg[j+1])
1131 tmp = arg+j+1;
1132 else if ((i+1) < argc)
1133 tmp = argv[++i];
1134 else
1135 usage();
1136 fstype = string_copy(tmp);
1137 compile_fs_type(fstype, &fs_type_compiled);
1138 goto next_arg;
1139 case '-':
1140 opts_for_fsck++;
1141 break;
1142 case '?':
1143 usage();
1144 break;
1145 default:
1146 options[++opt] = arg[j];
1147 break;
1148 }
1149 }
1150 next_arg:
1151 if (opt) {
1152 options[0] = '-';
1153 options[++opt] = '\0';
1154 if (num_args >= MAX_ARGS) {
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001155 bb_error_msg("too many arguments");
Mike Frysinger51a43b42005-09-24 07:11:16 +00001156 }
1157 args[num_args++] = string_copy(options);
1158 opt = 0;
1159 }
1160 }
1161 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1162 force_all_parallel++;
1163 if ((tmp = getenv("FSCK_MAX_INST")))
1164 max_running = atoi(tmp);
1165}
1166
1167int fsck_main(int argc, char *argv[])
1168{
1169 int i, status = 0;
1170 int interactive = 0;
1171 char *oldpath = getenv("PATH");
1172 const char *fstab;
1173 struct fs_info *fs;
1174
1175 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1176 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1177
Mike Frysinger51a43b42005-09-24 07:11:16 +00001178 blkid_get_cache(&cache, NULL);
1179 PRS(argc, argv);
1180
1181 if (!notitle)
1182 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1183
1184 fstab = getenv("FSTAB_FILE");
1185 if (!fstab)
1186 fstab = _PATH_MNTTAB;
1187 load_fs_info(fstab);
1188
1189 /* Update our search path to include uncommon directories. */
1190 if (oldpath) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +00001191 fsck_path = bb_xasprintf("%s:%s", fsck_prefix_path, oldpath);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001192 } else {
1193 fsck_path = string_copy(fsck_prefix_path);
1194 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001195
Mike Frysinger51a43b42005-09-24 07:11:16 +00001196 if ((num_devices == 1) || (serialize))
1197 interactive = 1;
1198
1199 /* If -A was specified ("check all"), do that! */
1200 if (doall)
1201 return check_all();
1202
1203 if (num_devices == 0) {
1204 serialize++;
1205 interactive++;
1206 return check_all();
1207 }
1208 for (i = 0 ; i < num_devices; i++) {
1209 if (cancel_requested) {
1210 if (!kill_sent) {
1211 kill_all(SIGTERM);
1212 kill_sent++;
1213 }
1214 break;
1215 }
1216 fs = lookup(devices[i]);
1217 if (!fs) {
1218 fs = create_fs_device(devices[i], 0, "auto",
1219 0, -1, -1);
1220 if (!fs)
1221 continue;
1222 }
1223 fsck_device(fs, interactive);
1224 if (serialize ||
1225 (max_running && (num_running >= max_running))) {
1226 struct fsck_instance *inst;
1227
1228 inst = wait_one(0);
1229 if (inst) {
1230 status |= inst->exit_status;
1231 free_instance(inst);
1232 }
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001233 if (verbose > 1)
Mike Frysinger51a43b42005-09-24 07:11:16 +00001234 printf("----------------------------------\n");
1235 }
1236 }
1237 status |= wait_many(FLAG_WAIT_ALL);
"Vladimir N. Oleynik"6160d452005-09-29 09:45:22 +00001238 if (ENABLE_FEATURE_CLEAN_UP)
1239 free(fsck_path);
Mike Frysinger51a43b42005-09-24 07:11:16 +00001240 blkid_put_cache(cache);
1241 return status;
1242}