blob: c9ed76947f1f865825ab623a6f020f3fdaf27c45 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +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>
10 *
Theodore Ts'o50787ea1999-07-19 15:30:21 +000011 * Usage: fsck [-ACVRNTM] [-s] [-t fstype] [fs-options] device
Theodore Ts'o3839e651997-04-26 13:21:57 +000012 *
Theodore Ts'of3db3561997-04-26 13:34:30 +000013 * 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 *
Theodore Ts'o50787ea1999-07-19 15:30:21 +000021 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000022 *
23 * %Begin-Header%
24 * This file may be redistributed under the terms of the GNU Public
25 * License.
26 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000027 */
28
29#include <sys/types.h>
30#include <sys/wait.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000031#include <sys/signal.h>
32#include <sys/stat.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <limits.h>
34#include <stdio.h>
Theodore Ts'ob5135711999-10-26 14:33:24 +000035#include <ctype.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#include <string.h>
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +000037#include <time.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000038#if HAVE_STDLIB_H
39#include <stdlib.h>
40#endif
41#if HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <errno.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'o5a679c81998-12-03 16:40:38 +000044#if HAVE_PATHS_H
45#include <paths.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000046#endif
47#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000048#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000049#endif
50#if HAVE_ERRNO_H
51#include <errno.h>
52#endif
53#include <malloc.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000054
55#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000056#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057#include "fsck.h"
Theodore Ts'o2d8defd1999-07-03 01:59:42 +000058#include "get_device_by_label.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
Theodore Ts'o5a679c81998-12-03 16:40:38 +000060#ifndef _PATH_MNTTAB
61#define _PATH_MNTTAB "/etc/fstab"
62#endif
63
Theodore Ts'o3839e651997-04-26 13:21:57 +000064static const char *ignored_types[] = {
65 "ignore",
66 "iso9660",
Theodore Ts'o3839e651997-04-26 13:21:57 +000067 "nfs",
68 "proc",
69 "sw",
70 "swap",
71 NULL
72};
73
Theodore Ts'of3db3561997-04-26 13:34:30 +000074static const char *really_wanted[] = {
75 "minix",
76 "ext2",
77 "xiafs",
78 NULL
79};
80
81#ifdef DEV_DSK_DEVICES
82static const char *base_devices[] = {
83 "/dev/dsk/hda",
84 "/dev/dsk/hdb",
85 "/dev/dsk/hdc",
86 "/dev/dsk/hdd",
87 "/dev/dsk/hd1a",
88 "/dev/dsk/hd1b",
89 "/dev/dsk/hd1c",
90 "/dev/dsk/hd1d",
91 "/dev/dsk/sda",
92 "/dev/dsk/sdb",
93 "/dev/dsk/sdc",
94 "/dev/dsk/sdd",
95 "/dev/dsk/sde",
96 "/dev/dsk/sdf",
97 "/dev/dsk/sdg",
98 NULL
99};
100#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101static const char *base_devices[] = {
102 "/dev/hda",
103 "/dev/hdb",
104 "/dev/hdc",
105 "/dev/hdd",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000106 "/dev/hd1a",
107 "/dev/hd1b",
108 "/dev/hd1c",
109 "/dev/hd1d",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 "/dev/sda",
111 "/dev/sdb",
112 "/dev/sdc",
113 "/dev/sdd",
114 "/dev/sde",
115 "/dev/sdf",
116 "/dev/sdg",
117 NULL
118};
Theodore Ts'of3db3561997-04-26 13:34:30 +0000119#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120
121/*
122 * Global variables for options
123 */
124char *devices[MAX_DEVICES];
125char *args[MAX_ARGS];
126int num_devices, num_args;
127
128int verbose = 0;
129int doall = 0;
130int noexecute = 0;
131int serialize = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000132int skip_root = 0;
133int like_mount = 0;
134int notitle = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000135int parallel_root = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000136int progress = 0;
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000137int force_all_parallel = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138char *progname;
139char *fstype = NULL;
140struct fs_info *filesys_info;
141struct fsck_instance *instance_list;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000142const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
143char *fsck_path = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000144static int ignore(struct fs_info *);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145
Theodore Ts'o818180c1998-06-27 05:11:14 +0000146static char *string_copy(const char *s)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147{
148 char *ret;
149
150 ret = malloc(strlen(s)+1);
151 if (ret)
152 strcpy(ret, s);
153 return ret;
154}
155
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000156static char *skip_over_blank(char *cp)
157{
158 while (*cp && isspace(*cp))
159 cp++;
160 return cp;
161}
162
163static char *skip_over_word(char *cp)
164{
165 while (*cp && !isspace(*cp))
166 cp++;
167 return cp;
168}
169
170static void strip_line(char *line)
171{
172 char *p;
173
174 while (*line) {
175 p = line + strlen(line) - 1;
176 if ((*p == '\n') || (*p == '\r'))
177 *p = 0;
178 else
179 break;
180 }
181}
182
183static char *parse_word(char **buf)
184{
185 char *word, *next;
186
187 word = *buf;
188 if (*word == 0)
189 return 0;
190
191 word = skip_over_blank(word);
192 next = skip_over_word(word);
193 if (*next)
194 *next++ = 0;
195 *buf = next;
196 return word;
197}
198
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199static void free_instance(struct fsck_instance *i)
200{
201 if (i->prog)
202 free(i->prog);
203 if (i->device)
204 free(i->device);
205 free(i);
206 return;
207}
208
Theodore Ts'ob5135711999-10-26 14:33:24 +0000209static int parse_fstab_line(char *line, struct fs_info **ret_fs)
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000210{
211 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
212 struct fs_info *fs;
213
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000214 *ret_fs = 0;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000215 strip_line(line);
Theodore Ts'ob5135711999-10-26 14:33:24 +0000216 if ((cp = strchr(line, '#')))
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000217 *cp = 0; /* Ignore everything after the comment char */
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000218 cp = line;
219
220 device = parse_word(&cp);
221 mntpnt = parse_word(&cp);
222 type = parse_word(&cp);
223 opts = parse_word(&cp);
224 freq = parse_word(&cp);
225 passno = parse_word(&cp);
226
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000227 if (!device)
228 return 0; /* Allow blank lines */
229
230 if (!mntpnt || !type)
231 return -1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000232
233 if (!(fs = malloc(sizeof(struct fs_info))))
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000234 return -1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000235
236 fs->device = string_copy(device);
237 fs->mountpt = string_copy(mntpnt);
238 fs->type = string_copy(type);
239 fs->opts = string_copy(opts ? opts : "");
240 fs->freq = freq ? atoi(freq) : -1;
241 fs->passno = passno ? atoi(passno) : -1;
242 fs->flags = 0;
243 fs->next = NULL;
244
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000245 *ret_fs = fs;
246
247 return 0;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000248}
249
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250/*
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000251 * Interpret the device name if necessary
252 */
253static char *interpret_device(char *spec)
254{
255 char *dev = NULL;
256
257 if (!strncmp(spec, "UUID=", 5))
258 dev = get_spec_by_uuid(spec+5);
259 else if (!strncmp(spec, "LABEL=", 6))
260 dev = get_spec_by_volume_label(spec+6);
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000261 else
262 return spec;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000263 if (dev) {
264 free(spec);
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000265 return (dev);
266 }
267 /*
268 * Check to see if this was because /proc/partitions isn't
269 * found.
270 */
271 if (access("/proc/partitions", R_OK) < 0) {
272 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
273 strerror(errno));
274 fprintf(stderr, "Is /proc mounted?\n");
275 exit(1);
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000276 }
Theodore Ts'oc96e5112000-05-08 14:47:15 +0000277 /*
278 * Check to see if this is because we're not running as root
279 */
280 if (geteuid())
281 fprintf(stderr, "Must be root to scan for matching "
282 "filesystems: %s\n", spec);
283 else
284 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
285 spec);
286 exit(1);
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000287}
288
289/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290 * Load the filesystem database from /etc/fstab
291 */
Theodore Ts'ob5135711999-10-26 14:33:24 +0000292static void load_fs_info(const char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293{
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000294 FILE *f;
295 char buf[1024];
296 int lineno = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000297 int old_fstab = 1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000298 struct fs_info *fs, *fs_last = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299
300 filesys_info = NULL;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000301 if ((f = fopen(filename, "r")) == NULL) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000302 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000303 filename, strerror(errno));
304 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 }
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000306 while (!feof(f)) {
307 lineno++;
308 if (!fgets(buf, sizeof(buf), f))
309 break;
310 buf[sizeof(buf)-1] = 0;
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000311 if (parse_fstab_line(buf, &fs) < 0) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000312 fprintf(stderr, _("WARNING: bad format "
313 "on line %d of %s\n"), lineno, filename);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000314 continue;
315 }
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000316 if (!fs)
317 continue;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000318 fs->device = interpret_device(fs->device);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000319 if (!filesys_info)
320 filesys_info = fs;
321 else
322 fs_last->next = fs;
323 fs_last = fs;
Theodore Ts'o665f7101999-01-08 13:33:39 +0000324 if (fs->passno < 0)
325 fs->passno = 0;
326 else
Theodore Ts'of3db3561997-04-26 13:34:30 +0000327 old_fstab = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328 }
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000329
330 fclose(f);
331
Theodore Ts'of3db3561997-04-26 13:34:30 +0000332 if (old_fstab) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000333 fprintf(stderr, _("\007\007\007"
334 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
335 " field. I will kludge around things for you, but you\n"
336 " should fix your /etc/fstab file as soon as you can.\n\n"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000337
338 for (fs = filesys_info; fs; fs = fs->next) {
339 fs->passno = 1;
340 }
341 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342}
343
344/* Lookup filesys in /etc/fstab and return the corresponding entry. */
345static struct fs_info *lookup(char *filesys)
346{
347 struct fs_info *fs;
348
349 /* No filesys name given. */
350 if (filesys == NULL)
351 return NULL;
352
353 for (fs = filesys_info; fs; fs = fs->next) {
354 if (!strcmp(filesys, fs->device) ||
355 !strcmp(filesys, fs->mountpt))
356 break;
357 }
358
359 return fs;
360}
361
Theodore Ts'of3db3561997-04-26 13:34:30 +0000362/* Find fsck program for a given fs type. */
363static char *find_fsck(char *type)
364{
365 char *s;
366 const char *tpl;
367 static char prog[256];
Theodore Ts'o818180c1998-06-27 05:11:14 +0000368 char *p = string_copy(fsck_path);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000369 struct stat st;
370
371 /* Are we looking for a program or just a type? */
372 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
373
374 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
375 sprintf(prog, tpl, s, type);
376 if (stat(prog, &st) == 0) break;
377 }
378 free(p);
379 return(s ? prog : NULL);
380}
381
Theodore Ts'ob5135711999-10-26 14:33:24 +0000382static int progress_active(NOARGS)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000383{
384 struct fsck_instance *inst;
385
386 for (inst = instance_list; inst; inst = inst->next) {
387 if (inst->flags & FLAG_DONE)
388 continue;
389 if (inst->flags & FLAG_PROGRESS)
390 return 1;
391 }
392 return 0;
393}
394
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395/*
396 * Execute a particular fsck program, and link it into the list of
397 * child processes we are waiting for.
398 */
Theodore Ts'ob5135711999-10-26 14:33:24 +0000399static int execute(const char *type, char *device, char *mntpt,
400 int interactive)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401{
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000402 char *s, *argv[80], prog[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 int argc, i;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000404 struct fsck_instance *inst, *p;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 pid_t pid;
406
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000407 inst = malloc(sizeof(struct fsck_instance));
408 if (!inst)
409 return ENOMEM;
410 memset(inst, 0, sizeof(struct fsck_instance));
411
412 sprintf(prog, "fsck.%s", type);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000413 argv[0] = string_copy(prog);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 argc = 1;
415
416 for (i=0; i <num_args; i++)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000417 argv[argc++] = string_copy(args[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000419 if (progress & !progress_active()) {
420 if (strcmp(type, "ext2") == 0) {
Theodore Ts'ob5135711999-10-26 14:33:24 +0000421 argv[argc++] = string_copy("-C0");
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000422 inst->flags |= FLAG_PROGRESS;
423 }
424 }
425
Theodore Ts'o818180c1998-06-27 05:11:14 +0000426 argv[argc++] = string_copy(device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 argv[argc] = 0;
428
Theodore Ts'of3db3561997-04-26 13:34:30 +0000429 s = find_fsck(prog);
430 if (s == NULL) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000431 fprintf(stderr, _("fsck: %s: not found\n"), prog);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000432 return ENOENT;
433 }
434
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 if (verbose || noexecute) {
Theodore Ts'o5d45d801999-03-16 19:35:19 +0000436 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 for (i=0; i < argc; i++)
438 printf("%s ", argv[i]);
439 printf("\n");
440 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441
442 /* Fork and execute the correct program. */
Theodore Ts'oee922991999-01-16 05:39:12 +0000443 if (noexecute)
444 pid = -1;
445 else if ((pid = fork()) < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446 perror("fork");
447 return errno;
448 } else if (pid == 0) {
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000449 if (!interactive)
450 close(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000451 (void) execv(s, argv);
452 perror(argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 exit(EXIT_ERROR);
454 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000455
Theodore Ts'ob5135711999-10-26 14:33:24 +0000456 for (i=0; i < argc; i++)
457 free(argv[i]);
458
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 inst->pid = pid;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000460 inst->prog = string_copy(prog);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000461 inst->type = string_copy(type);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000462 inst->device = string_copy(device);
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000463 inst->start_time = time(0);
464 inst->next = NULL;
465
466 /*
467 * Find the end of the list, so we add the instance on at the end.
468 */
469 for (p = instance_list; p && p->next; p = p->next);
470
471 if (p)
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000472 p->next = inst;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000473 else
474 instance_list = inst;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475
476 return 0;
477}
478
479/*
480 * Wait for one child process to exit; when it does, unlink it from
481 * the list of executing child processes, and return it.
482 */
483static struct fsck_instance *wait_one(NOARGS)
484{
485 int status;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000486 int sig;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000487 struct fsck_instance *inst, *inst2, *prev;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 pid_t pid;
489
490 if (!instance_list)
491 return NULL;
492
Theodore Ts'oee922991999-01-16 05:39:12 +0000493 if (noexecute) {
494 inst = instance_list;
495 instance_list = inst->next;
496 inst->exit_status = 0;
497 return(inst);
498 }
499
Theodore Ts'ob5135711999-10-26 14:33:24 +0000500 /*
501 * gcc -Wall fails saving throw against stupidity
502 * (inst and prev are thought to be uninitialized variables)
503 */
504 inst = prev = NULL;
505
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000506 do {
507 pid = wait(&status);
508 if (pid < 0) {
509 if ((errno == EINTR) || (errno == EAGAIN))
510 continue;
511 if (errno == ECHILD) {
512 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000513 _("%s: wait: No more child process?!?\n"),
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000514 progname);
515 return NULL;
516 }
517 perror("wait");
518 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 }
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000520 for (prev = 0, inst = instance_list;
521 inst;
522 prev = inst, inst = inst->next) {
523 if (inst->pid == pid)
524 break;
525 }
526 } while (!inst);
527
Theodore Ts'of3db3561997-04-26 13:34:30 +0000528 if (WIFEXITED(status))
529 status = WEXITSTATUS(status);
530 else if (WIFSIGNALED(status)) {
531 sig = WTERMSIG(status);
532 if (sig == SIGINT) {
533 status = EXIT_UNCORRECTED;
534 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000535 printf(_("Warning... %s for device %s exited "
536 "with signal %d.\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000537 inst->prog, inst->device, sig);
538 status = EXIT_ERROR;
539 }
540 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000541 printf(_("%s %s: status is %x, should never happen.\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000542 inst->prog, inst->device, status);
543 status = EXIT_ERROR;
544 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 inst->exit_status = status;
546 if (prev)
547 prev->next = inst->next;
548 else
549 instance_list = inst->next;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000550 if (progress && (inst->flags & FLAG_PROGRESS) &&
551 !progress_active()) {
552 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
553 if (inst2->flags & FLAG_DONE)
554 continue;
555 if (strcmp(inst2->type, "ext2"))
556 continue;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000557 /*
558 * If we've just started the fsck, wait a tiny
559 * bit before sending the kill, to give it
560 * time to set up the signal handler
561 */
562 if (inst2->start_time < time(0)+2) {
563 if (fork() == 0) {
564 sleep(1);
565 kill(inst2->pid, SIGUSR1);
566 exit(0);
567 }
568 } else
569 kill(inst2->pid, SIGUSR1);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000570 break;
571 }
572 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 return inst;
574}
575
576/*
577 * Wait until all executing child processes have exited; return the
578 * logical OR of all of their exit code values.
579 */
580static int wait_all(NOARGS)
581{
582 struct fsck_instance *inst;
583 int global_status = 0;
584
585 while (instance_list) {
586 inst = wait_one();
587 if (!inst)
588 break;
589 global_status |= inst->exit_status;
590 free_instance(inst);
591 }
592 return global_status;
593}
594
595/*
596 * Run the fsck program on a particular device
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000597 *
598 * If the type is specified using -t, and it isn't prefixed with "no"
599 * (as in "noext2") and only one filesystem type is specified, then
600 * use that type regardless of what is specified in /etc/fstab.
601 *
602 * If the type isn't specified by the user, then use either the type
603 * specified in /etc/fstab, or DEFAULT_FSTYPE.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 */
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000605static void fsck_device(char *device, int interactive)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606{
Theodore Ts'ob5135711999-10-26 14:33:24 +0000607 const char *type = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 struct fs_info *fsent;
609 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610
Theodore Ts'o297f47a1997-04-26 14:25:20 +0000611 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000612 type = fstype;
613
Theodore Ts'of3db3561997-04-26 13:34:30 +0000614 if ((fsent = lookup(device))) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 device = fsent->device;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000616 if (!type)
617 type = fsent->type;
618 }
619 if (!type)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620 type = DEFAULT_FSTYPE;
621
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000622 retval = execute(type, device, fsent ? fsent->mountpt : 0,
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000623 interactive);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000625 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
626 "for %s\n"), progname, retval, type, device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 }
628}
629
Theodore Ts'of3db3561997-04-26 13:34:30 +0000630/* See if filesystem type matches the list. */
631static int fs_match(char *type, char *fs_type)
632{
633 int ret = 0, negate = 0;
634 char list[128];
635 char *s;
636
637 if (!fs_type) return(1);
638
639 if (strncmp(fs_type, "no", 2) == 0) {
640 fs_type += 2;
641 negate = 1;
642 }
643 strcpy(list, fs_type);
644 s = strtok(list, ",");
645 while(s) {
646 if (strcmp(s, type) == 0) {
647 ret = 1;
648 break;
649 }
650 s = strtok(NULL, ",");
651 }
652 return(negate ? !ret : ret);
653}
654
655
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656/* Check if we should ignore this filesystem. */
657static int ignore(struct fs_info *fs)
658{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659 const char **ip;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000660 int wanted = 0;
661
662 /*
663 * If the pass number is 0, ignore it.
664 */
665 if (fs->passno == 0)
666 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667
668 /*
669 * If a specific fstype is specified, and it doesn't match,
670 * ignore it.
671 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000672 if (!fs_match(fs->type, fstype)) return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673
Theodore Ts'of3db3561997-04-26 13:34:30 +0000674 /* Are we ignoring this type? */
675 for(ip = ignored_types; *ip; ip++)
676 if (strcmp(fs->type, *ip) == 0) return(1);
677
678 /* Do we really really want to check this fs? */
679 for(ip = really_wanted; *ip; ip++)
680 if (strcmp(fs->type, *ip) == 0) {
681 wanted = 1;
682 break;
683 }
684
685 /* See if the <fsck.fs> program is available. */
686 if (find_fsck(fs->type) == NULL) {
687 if (wanted)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000688 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000689 fs->device, fs->type);
690 return(1);
691 }
692
693 /* We can and want to check this file system type. */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 return 0;
695}
696
697/*
698 * Return the "base device" given a particular device; this is used to
699 * assure that we only fsck one partition on a particular drive at any
700 * one time. Otherwise, the disk heads will be seeking all over the
701 * place.
702 */
703static const char *base_device(char *device)
704{
705 const char **base;
706
707 for (base = base_devices; *base; base++) {
708 if (!strncmp(*base, device, strlen(*base)))
709 return *base;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000710 }
711 return device;
712}
713
714/*
715 * Returns TRUE if a partition on the same disk is already being
716 * checked.
717 */
718static int device_already_active(char *device)
719{
720 struct fsck_instance *inst;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000721 const char *base = base_device(device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000723 if (force_all_parallel)
724 return 0;
725
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 for (inst = instance_list; inst; inst = inst->next) {
727 if (!strcmp(base, base_device(inst->device)))
728 return 1;
729 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 return 0;
731}
732
733/* Check all file systems, using the /etc/fstab table. */
734static int check_all(NOARGS)
735{
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000736 struct fs_info *fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 struct fsck_instance *inst;
738 int status = EXIT_OK;
739 int not_done_yet = 1;
Theodore Ts'oee922991999-01-16 05:39:12 +0000740 int passno = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000741 int pass_done;
742
743 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000744 printf(_("Checking all file systems.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745
746 /*
747 * Find and check the root filesystem first.
748 */
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000749 if (!parallel_root) {
750 for (fs = filesys_info; fs; fs = fs->next) {
751 if (!strcmp(fs->mountpt, "/"))
752 break;
753 }
754 if (fs && !skip_root && !ignore(fs)) {
Theodore Ts'o3cb77842000-01-18 16:30:27 +0000755 fsck_device(fs->device, 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000756 fs->flags |= FLAG_DONE;
757 status |= wait_all();
758 if (status > EXIT_NONDESTRUCT)
759 return status;
760 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000762 if (fs) fs->flags |= FLAG_DONE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000763
764 /*
765 * Mark filesystems that should be ignored as done.
766 */
767 for (fs = filesys_info; fs; fs = fs->next) {
768 if (ignore(fs))
769 fs->flags |= FLAG_DONE;
770 }
771
772 while (not_done_yet) {
773 not_done_yet = 0;
774 pass_done = 1;
775
776 for (fs = filesys_info; fs; fs = fs->next) {
777 if (fs->flags & FLAG_DONE)
778 continue;
779 /*
780 * If the filesystem's pass number is higher
781 * than the current pass number, then we don't
782 * do it yet.
783 */
784 if (fs->passno > passno) {
785 not_done_yet++;
786 continue;
787 }
788 /*
789 * If a filesystem on a particular device has
790 * already been spawned, then we need to defer
791 * this to another pass.
792 */
793 if (device_already_active(fs->device)) {
794 pass_done = 0;
795 continue;
796 }
797 /*
798 * Spawn off the fsck process
799 */
Theodore Ts'o3cb77842000-01-18 16:30:27 +0000800 fsck_device(fs->device, serialize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801 fs->flags |= FLAG_DONE;
802
Theodore Ts'o74becf31997-04-26 14:37:06 +0000803 if (serialize) {
804 pass_done = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000805 break; /* Only do one filesystem at a time */
Theodore Ts'o74becf31997-04-26 14:37:06 +0000806 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000807 }
Theodore Ts'oee922991999-01-16 05:39:12 +0000808 if (verbose > 1)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000809 printf(_("--waiting-- (pass %d)\n"), passno);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810 inst = wait_one();
811 if (inst) {
812 status |= inst->exit_status;
813 free_instance(inst);
814 }
815 if (pass_done) {
816 status |= wait_all();
Theodore Ts'oee922991999-01-16 05:39:12 +0000817 if (verbose > 1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000818 printf("----------------------------------\n");
819 passno++;
820 } else
821 not_done_yet++;
822 }
823 status |= wait_all();
824 return status;
825}
826
827static void usage(NOARGS)
828{
829 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000830 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000831 exit(EXIT_USAGE);
832}
833
834static void PRS(int argc, char *argv[])
835{
836 int i, j;
837 char *arg;
838 char options[128];
839 int opt = 0;
840 int opts_for_fsck = 0;
841
842 num_devices = 0;
843 num_args = 0;
844 instance_list = 0;
845
846 progname = argv[0];
847
Theodore Ts'o3839e651997-04-26 13:21:57 +0000848 for (i=1; i < argc; i++) {
849 arg = argv[i];
850 if (!arg)
851 continue;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000852 if ((arg[0] == '/' && !opts_for_fsck) ||
853 (strncmp(arg, "LABEL=", 6) == 0) ||
854 (strncmp(arg, "UUID=", 5) == 0)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855 if (num_devices >= MAX_DEVICES) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000856 fprintf(stderr, _("%s: too many devices\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000857 progname);
858 exit(1);
859 }
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000860 devices[num_devices++] =
861 interpret_device(string_copy(arg));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862 continue;
863 }
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000864 if (arg[0] != '-' || opts_for_fsck) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000865 if (num_args >= MAX_ARGS) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000866 fprintf(stderr, _("%s: too many arguments\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000867 progname);
868 exit(1);
869 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000870 args[num_args++] = string_copy(arg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000871 continue;
872 }
873 for (j=1; arg[j]; j++) {
874 if (opts_for_fsck) {
875 options[++opt] = arg[j];
876 continue;
877 }
878 switch (arg[j]) {
879 case 'A':
880 doall++;
881 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000882 case 'C':
883 progress++;
884 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000885 case 'V':
886 verbose++;
887 break;
888 case 'N':
889 noexecute++;
890 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000891 case 'R':
892 skip_root++;
893 break;
894 case 'T':
895 notitle++;
896 break;
897 case 'M':
898 like_mount++;
899 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000900 case 'P':
901 parallel_root++;
902 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000903 case 's':
904 serialize++;
905 break;
906 case 't':
907 if (arg[j+1]) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000908 fstype = string_copy(arg+j+1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 goto next_arg;
910 }
911 if ((i+1) < argc) {
912 i++;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000913 fstype = string_copy(argv[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914 goto next_arg;
915 }
916 usage();
917 break;
918 case '-':
919 opts_for_fsck++;
920 break;
921 default:
922 options[++opt] = arg[j];
923 break;
924 }
925 }
926 next_arg:
927 if (opt) {
928 options[0] = '-';
929 options[++opt] = '\0';
930 if (num_args >= MAX_ARGS) {
931 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000932 _("%s: too many arguments\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933 progname);
934 exit(1);
935 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000936 args[num_args++] = string_copy(options);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000937 opt = 0;
938 }
939 }
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000940 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
941 force_all_parallel++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942}
943
944int main(int argc, char *argv[])
945{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946 int i;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000947 int status = 0;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000948 int interactive = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000949 char *oldpath = getenv("PATH");
Theodore Ts'ob5135711999-10-26 14:33:24 +0000950 const char *fstab;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000952#ifdef ENABLE_NLS
953 setlocale(LC_MESSAGES, "");
954 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
955 textdomain(NLS_CAT_NAME);
956#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000957 PRS(argc, argv);
958
Theodore Ts'of3db3561997-04-26 13:34:30 +0000959 if (!notitle)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000960 printf(_("Parallelizing fsck version %s (%s)\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000961 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000962
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000963 fstab = getenv("FSTAB_FILE");
964 if (!fstab)
965 fstab = _PATH_MNTTAB;
966 load_fs_info(fstab);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000967
Theodore Ts'of3db3561997-04-26 13:34:30 +0000968 /* Update our search path to include uncommon directories. */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000969 if (oldpath) {
970 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
971 strlen (oldpath) + 1);
972 strcpy (fsck_path, fsck_prefix_path);
973 strcat (fsck_path, ":");
974 strcat (fsck_path, oldpath);
975 } else {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000976 fsck_path = string_copy(fsck_prefix_path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000977 }
978
Theodore Ts'o6a35ffa1999-11-04 22:34:43 +0000979 if ((num_devices == 1) || (serialize))
980 interactive = 1;
981
Theodore Ts'o3839e651997-04-26 13:21:57 +0000982 /* If -A was specified ("check all"), do that! */
983 if (doall)
984 return check_all();
985
986 for (i = 0 ; i < num_devices; i++) {
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000987 fsck_device(devices[i], interactive);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000988 if (serialize) {
989 struct fsck_instance *inst;
990
991 inst = wait_one();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000992 if (inst) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993 status |= inst->exit_status;
994 free_instance(inst);
995 }
996 }
997 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000998 status |= wait_all();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000999 free(fsck_path);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 return status;
1001}