blob: 229c7504a4b4b156fa3192bcc79ce60f3d97b21f [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",
Theodore Ts'o19c68912000-07-07 03:25:13 +000077 "ext3",
Theodore Ts'of3db3561997-04-26 13:34:30 +000078 "xiafs",
79 NULL
80};
81
82#ifdef DEV_DSK_DEVICES
83static const char *base_devices[] = {
84 "/dev/dsk/hda",
85 "/dev/dsk/hdb",
86 "/dev/dsk/hdc",
87 "/dev/dsk/hdd",
88 "/dev/dsk/hd1a",
89 "/dev/dsk/hd1b",
90 "/dev/dsk/hd1c",
91 "/dev/dsk/hd1d",
92 "/dev/dsk/sda",
93 "/dev/dsk/sdb",
94 "/dev/dsk/sdc",
95 "/dev/dsk/sdd",
96 "/dev/dsk/sde",
97 "/dev/dsk/sdf",
98 "/dev/dsk/sdg",
99 NULL
100};
101#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102static const char *base_devices[] = {
103 "/dev/hda",
104 "/dev/hdb",
105 "/dev/hdc",
106 "/dev/hdd",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000107 "/dev/hd1a",
108 "/dev/hd1b",
109 "/dev/hd1c",
110 "/dev/hd1d",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 "/dev/sda",
112 "/dev/sdb",
113 "/dev/sdc",
114 "/dev/sdd",
115 "/dev/sde",
116 "/dev/sdf",
117 "/dev/sdg",
118 NULL
119};
Theodore Ts'of3db3561997-04-26 13:34:30 +0000120#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121
122/*
123 * Global variables for options
124 */
125char *devices[MAX_DEVICES];
126char *args[MAX_ARGS];
127int num_devices, num_args;
128
129int verbose = 0;
130int doall = 0;
131int noexecute = 0;
132int serialize = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000133int skip_root = 0;
134int like_mount = 0;
135int notitle = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000136int parallel_root = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000137int progress = 0;
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000138int force_all_parallel = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139char *progname;
140char *fstype = NULL;
141struct fs_info *filesys_info;
142struct fsck_instance *instance_list;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000143const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
144char *fsck_path = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000145static int ignore(struct fs_info *);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146
Theodore Ts'o818180c1998-06-27 05:11:14 +0000147static char *string_copy(const char *s)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148{
149 char *ret;
150
151 ret = malloc(strlen(s)+1);
152 if (ret)
153 strcpy(ret, s);
154 return ret;
155}
156
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000157static char *skip_over_blank(char *cp)
158{
159 while (*cp && isspace(*cp))
160 cp++;
161 return cp;
162}
163
164static char *skip_over_word(char *cp)
165{
166 while (*cp && !isspace(*cp))
167 cp++;
168 return cp;
169}
170
171static void strip_line(char *line)
172{
173 char *p;
174
175 while (*line) {
176 p = line + strlen(line) - 1;
177 if ((*p == '\n') || (*p == '\r'))
178 *p = 0;
179 else
180 break;
181 }
182}
183
184static char *parse_word(char **buf)
185{
186 char *word, *next;
187
188 word = *buf;
189 if (*word == 0)
190 return 0;
191
192 word = skip_over_blank(word);
193 next = skip_over_word(word);
194 if (*next)
195 *next++ = 0;
196 *buf = next;
197 return word;
198}
199
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200static void free_instance(struct fsck_instance *i)
201{
202 if (i->prog)
203 free(i->prog);
204 if (i->device)
205 free(i->device);
206 free(i);
207 return;
208}
209
Theodore Ts'ob5135711999-10-26 14:33:24 +0000210static int parse_fstab_line(char *line, struct fs_info **ret_fs)
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000211{
212 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
213 struct fs_info *fs;
214
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000215 *ret_fs = 0;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000216 strip_line(line);
Theodore Ts'ob5135711999-10-26 14:33:24 +0000217 if ((cp = strchr(line, '#')))
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000218 *cp = 0; /* Ignore everything after the comment char */
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000219 cp = line;
220
221 device = parse_word(&cp);
222 mntpnt = parse_word(&cp);
223 type = parse_word(&cp);
224 opts = parse_word(&cp);
225 freq = parse_word(&cp);
226 passno = parse_word(&cp);
227
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000228 if (!device)
229 return 0; /* Allow blank lines */
230
231 if (!mntpnt || !type)
232 return -1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000233
234 if (!(fs = malloc(sizeof(struct fs_info))))
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000235 return -1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000236
237 fs->device = string_copy(device);
238 fs->mountpt = string_copy(mntpnt);
239 fs->type = string_copy(type);
240 fs->opts = string_copy(opts ? opts : "");
241 fs->freq = freq ? atoi(freq) : -1;
242 fs->passno = passno ? atoi(passno) : -1;
243 fs->flags = 0;
244 fs->next = NULL;
245
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000246 *ret_fs = fs;
247
248 return 0;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000249}
250
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251/*
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000252 * Interpret the device name if necessary
253 */
254static char *interpret_device(char *spec)
255{
256 char *dev = NULL;
257
258 if (!strncmp(spec, "UUID=", 5))
259 dev = get_spec_by_uuid(spec+5);
260 else if (!strncmp(spec, "LABEL=", 6))
261 dev = get_spec_by_volume_label(spec+6);
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000262 else
263 return spec;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000264 if (dev) {
265 free(spec);
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000266 return (dev);
267 }
268 /*
269 * Check to see if this was because /proc/partitions isn't
270 * found.
271 */
272 if (access("/proc/partitions", R_OK) < 0) {
273 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
274 strerror(errno));
275 fprintf(stderr, "Is /proc mounted?\n");
276 exit(1);
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000277 }
Theodore Ts'oc96e5112000-05-08 14:47:15 +0000278 /*
279 * Check to see if this is because we're not running as root
280 */
281 if (geteuid())
282 fprintf(stderr, "Must be root to scan for matching "
283 "filesystems: %s\n", spec);
284 else
285 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
286 spec);
287 exit(1);
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000288}
289
290/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 * Load the filesystem database from /etc/fstab
292 */
Theodore Ts'ob5135711999-10-26 14:33:24 +0000293static void load_fs_info(const char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294{
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000295 FILE *f;
296 char buf[1024];
297 int lineno = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000298 int old_fstab = 1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000299 struct fs_info *fs, *fs_last = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300
301 filesys_info = NULL;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000302 if ((f = fopen(filename, "r")) == NULL) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000303 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000304 filename, strerror(errno));
305 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 }
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000307 while (!feof(f)) {
308 lineno++;
309 if (!fgets(buf, sizeof(buf), f))
310 break;
311 buf[sizeof(buf)-1] = 0;
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000312 if (parse_fstab_line(buf, &fs) < 0) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000313 fprintf(stderr, _("WARNING: bad format "
314 "on line %d of %s\n"), lineno, filename);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000315 continue;
316 }
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000317 if (!fs)
318 continue;
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;
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000348 int try_again = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349
350 /* No filesys name given. */
351 if (filesys == NULL)
352 return NULL;
353
354 for (fs = filesys_info; fs; fs = fs->next) {
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000355 if (strchr(fs->device, '='))
356 try_again++;
357 if (!strcmp(filesys, fs->device) ||
358 !strcmp(filesys, fs->mountpt))
359 break;
360 }
Theodore Ts'o19c68912000-07-07 03:25:13 +0000361 if (fs && strchr(fs->device, '='))
362 fs->device = interpret_device(fs->device);
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000363
364 if (fs || !try_again)
365 return fs;
366
367 for (fs = filesys_info; fs; fs = fs->next) {
368 fs->device = interpret_device(fs->device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 if (!strcmp(filesys, fs->device) ||
370 !strcmp(filesys, fs->mountpt))
371 break;
372 }
373
374 return fs;
375}
376
Theodore Ts'of3db3561997-04-26 13:34:30 +0000377/* Find fsck program for a given fs type. */
378static char *find_fsck(char *type)
379{
380 char *s;
381 const char *tpl;
382 static char prog[256];
Theodore Ts'o818180c1998-06-27 05:11:14 +0000383 char *p = string_copy(fsck_path);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000384 struct stat st;
385
386 /* Are we looking for a program or just a type? */
387 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
388
389 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
390 sprintf(prog, tpl, s, type);
391 if (stat(prog, &st) == 0) break;
392 }
393 free(p);
394 return(s ? prog : NULL);
395}
396
Theodore Ts'ob5135711999-10-26 14:33:24 +0000397static int progress_active(NOARGS)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000398{
399 struct fsck_instance *inst;
400
401 for (inst = instance_list; inst; inst = inst->next) {
402 if (inst->flags & FLAG_DONE)
403 continue;
404 if (inst->flags & FLAG_PROGRESS)
405 return 1;
406 }
407 return 0;
408}
409
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410/*
411 * Execute a particular fsck program, and link it into the list of
412 * child processes we are waiting for.
413 */
Theodore Ts'ob5135711999-10-26 14:33:24 +0000414static int execute(const char *type, char *device, char *mntpt,
415 int interactive)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416{
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000417 char *s, *argv[80], prog[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 int argc, i;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000419 struct fsck_instance *inst, *p;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 pid_t pid;
421
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000422 inst = malloc(sizeof(struct fsck_instance));
423 if (!inst)
424 return ENOMEM;
425 memset(inst, 0, sizeof(struct fsck_instance));
426
427 sprintf(prog, "fsck.%s", type);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000428 argv[0] = string_copy(prog);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 argc = 1;
430
431 for (i=0; i <num_args; i++)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000432 argv[argc++] = string_copy(args[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000434 if (progress & !progress_active()) {
Theodore Ts'o19c68912000-07-07 03:25:13 +0000435 if ((strcmp(type, "ext2") == 0) ||
436 (strcmp(type, "ext3") == 0)) {
Theodore Ts'ob5135711999-10-26 14:33:24 +0000437 argv[argc++] = string_copy("-C0");
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000438 inst->flags |= FLAG_PROGRESS;
439 }
440 }
441
Theodore Ts'o818180c1998-06-27 05:11:14 +0000442 argv[argc++] = string_copy(device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443 argv[argc] = 0;
444
Theodore Ts'of3db3561997-04-26 13:34:30 +0000445 s = find_fsck(prog);
446 if (s == NULL) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000447 fprintf(stderr, _("fsck: %s: not found\n"), prog);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000448 return ENOENT;
449 }
450
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 if (verbose || noexecute) {
Theodore Ts'o5d45d801999-03-16 19:35:19 +0000452 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 for (i=0; i < argc; i++)
454 printf("%s ", argv[i]);
455 printf("\n");
456 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457
458 /* Fork and execute the correct program. */
Theodore Ts'oee922991999-01-16 05:39:12 +0000459 if (noexecute)
460 pid = -1;
461 else if ((pid = fork()) < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 perror("fork");
463 return errno;
464 } else if (pid == 0) {
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000465 if (!interactive)
466 close(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000467 (void) execv(s, argv);
468 perror(argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 exit(EXIT_ERROR);
470 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000471
Theodore Ts'ob5135711999-10-26 14:33:24 +0000472 for (i=0; i < argc; i++)
473 free(argv[i]);
474
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 inst->pid = pid;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000476 inst->prog = string_copy(prog);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000477 inst->type = string_copy(type);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000478 inst->device = string_copy(device);
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000479 inst->start_time = time(0);
480 inst->next = NULL;
481
482 /*
483 * Find the end of the list, so we add the instance on at the end.
484 */
485 for (p = instance_list; p && p->next; p = p->next);
486
487 if (p)
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000488 p->next = inst;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000489 else
490 instance_list = inst;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491
492 return 0;
493}
494
495/*
496 * Wait for one child process to exit; when it does, unlink it from
497 * the list of executing child processes, and return it.
498 */
499static struct fsck_instance *wait_one(NOARGS)
500{
501 int status;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000502 int sig;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000503 struct fsck_instance *inst, *inst2, *prev;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504 pid_t pid;
505
506 if (!instance_list)
507 return NULL;
508
Theodore Ts'oee922991999-01-16 05:39:12 +0000509 if (noexecute) {
510 inst = instance_list;
511 instance_list = inst->next;
512 inst->exit_status = 0;
513 return(inst);
514 }
515
Theodore Ts'ob5135711999-10-26 14:33:24 +0000516 /*
517 * gcc -Wall fails saving throw against stupidity
518 * (inst and prev are thought to be uninitialized variables)
519 */
520 inst = prev = NULL;
521
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000522 do {
523 pid = wait(&status);
524 if (pid < 0) {
525 if ((errno == EINTR) || (errno == EAGAIN))
526 continue;
527 if (errno == ECHILD) {
528 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000529 _("%s: wait: No more child process?!?\n"),
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000530 progname);
531 return NULL;
532 }
533 perror("wait");
534 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535 }
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000536 for (prev = 0, inst = instance_list;
537 inst;
538 prev = inst, inst = inst->next) {
539 if (inst->pid == pid)
540 break;
541 }
542 } while (!inst);
543
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 if (WIFEXITED(status))
545 status = WEXITSTATUS(status);
546 else if (WIFSIGNALED(status)) {
547 sig = WTERMSIG(status);
548 if (sig == SIGINT) {
549 status = EXIT_UNCORRECTED;
550 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000551 printf(_("Warning... %s for device %s exited "
552 "with signal %d.\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000553 inst->prog, inst->device, sig);
554 status = EXIT_ERROR;
555 }
556 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000557 printf(_("%s %s: status is %x, should never happen.\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000558 inst->prog, inst->device, status);
559 status = EXIT_ERROR;
560 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 inst->exit_status = status;
562 if (prev)
563 prev->next = inst->next;
564 else
565 instance_list = inst->next;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000566 if (progress && (inst->flags & FLAG_PROGRESS) &&
567 !progress_active()) {
568 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
569 if (inst2->flags & FLAG_DONE)
570 continue;
Theodore Ts'o19c68912000-07-07 03:25:13 +0000571 if (strcmp(inst2->type, "ext2") &&
572 strcmp(inst2->type, "ext3"))
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000573 continue;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000574 /*
575 * If we've just started the fsck, wait a tiny
576 * bit before sending the kill, to give it
577 * time to set up the signal handler
578 */
579 if (inst2->start_time < time(0)+2) {
580 if (fork() == 0) {
581 sleep(1);
582 kill(inst2->pid, SIGUSR1);
583 exit(0);
584 }
585 } else
586 kill(inst2->pid, SIGUSR1);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000587 break;
588 }
589 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590 return inst;
591}
592
593/*
594 * Wait until all executing child processes have exited; return the
595 * logical OR of all of their exit code values.
596 */
597static int wait_all(NOARGS)
598{
599 struct fsck_instance *inst;
600 int global_status = 0;
601
602 while (instance_list) {
603 inst = wait_one();
604 if (!inst)
605 break;
606 global_status |= inst->exit_status;
607 free_instance(inst);
608 }
609 return global_status;
610}
611
612/*
613 * Run the fsck program on a particular device
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000614 *
615 * If the type is specified using -t, and it isn't prefixed with "no"
616 * (as in "noext2") and only one filesystem type is specified, then
617 * use that type regardless of what is specified in /etc/fstab.
618 *
619 * If the type isn't specified by the user, then use either the type
620 * specified in /etc/fstab, or DEFAULT_FSTYPE.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 */
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000622static void fsck_device(char *device, int interactive)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623{
Theodore Ts'ob5135711999-10-26 14:33:24 +0000624 const char *type = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625 struct fs_info *fsent;
626 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627
Theodore Ts'o297f47a1997-04-26 14:25:20 +0000628 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000629 type = fstype;
630
Theodore Ts'of3db3561997-04-26 13:34:30 +0000631 if ((fsent = lookup(device))) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 device = fsent->device;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000633 if (!type)
634 type = fsent->type;
635 }
636 if (!type)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 type = DEFAULT_FSTYPE;
638
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000639 retval = execute(type, device, fsent ? fsent->mountpt : 0,
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000640 interactive);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000642 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
643 "for %s\n"), progname, retval, type, device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644 }
645}
646
Theodore Ts'of3db3561997-04-26 13:34:30 +0000647/* See if filesystem type matches the list. */
648static int fs_match(char *type, char *fs_type)
649{
650 int ret = 0, negate = 0;
651 char list[128];
652 char *s;
653
654 if (!fs_type) return(1);
655
656 if (strncmp(fs_type, "no", 2) == 0) {
657 fs_type += 2;
658 negate = 1;
659 }
660 strcpy(list, fs_type);
661 s = strtok(list, ",");
662 while(s) {
663 if (strcmp(s, type) == 0) {
664 ret = 1;
665 break;
666 }
667 s = strtok(NULL, ",");
668 }
669 return(negate ? !ret : ret);
670}
671
672
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673/* Check if we should ignore this filesystem. */
674static int ignore(struct fs_info *fs)
675{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676 const char **ip;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000677 int wanted = 0;
678
679 /*
680 * If the pass number is 0, ignore it.
681 */
682 if (fs->passno == 0)
683 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684
685 /*
686 * If a specific fstype is specified, and it doesn't match,
687 * ignore it.
688 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000689 if (!fs_match(fs->type, fstype)) return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690
Theodore Ts'of3db3561997-04-26 13:34:30 +0000691 /* Are we ignoring this type? */
692 for(ip = ignored_types; *ip; ip++)
693 if (strcmp(fs->type, *ip) == 0) return(1);
694
695 /* Do we really really want to check this fs? */
696 for(ip = really_wanted; *ip; ip++)
697 if (strcmp(fs->type, *ip) == 0) {
698 wanted = 1;
699 break;
700 }
701
702 /* See if the <fsck.fs> program is available. */
703 if (find_fsck(fs->type) == NULL) {
704 if (wanted)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000705 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000706 fs->device, fs->type);
707 return(1);
708 }
709
710 /* We can and want to check this file system type. */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711 return 0;
712}
713
714/*
715 * Return the "base device" given a particular device; this is used to
716 * assure that we only fsck one partition on a particular drive at any
717 * one time. Otherwise, the disk heads will be seeking all over the
718 * place.
719 */
720static const char *base_device(char *device)
721{
722 const char **base;
723
724 for (base = base_devices; *base; base++) {
725 if (!strncmp(*base, device, strlen(*base)))
726 return *base;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727 }
728 return device;
729}
730
731/*
732 * Returns TRUE if a partition on the same disk is already being
733 * checked.
734 */
735static int device_already_active(char *device)
736{
737 struct fsck_instance *inst;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000738 const char *base = base_device(device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000739
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000740 if (force_all_parallel)
741 return 0;
742
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743 for (inst = instance_list; inst; inst = inst->next) {
744 if (!strcmp(base, base_device(inst->device)))
745 return 1;
746 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 return 0;
748}
749
750/* Check all file systems, using the /etc/fstab table. */
751static int check_all(NOARGS)
752{
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000753 struct fs_info *fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754 struct fsck_instance *inst;
755 int status = EXIT_OK;
756 int not_done_yet = 1;
Theodore Ts'oee922991999-01-16 05:39:12 +0000757 int passno = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758 int pass_done;
759
760 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000761 printf(_("Checking all file systems.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000762
763 /*
764 * Find and check the root filesystem first.
765 */
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000766 if (!parallel_root) {
767 for (fs = filesys_info; fs; fs = fs->next) {
768 if (!strcmp(fs->mountpt, "/"))
769 break;
770 }
771 if (fs && !skip_root && !ignore(fs)) {
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000772 fs->device = interpret_device(fs->device);
Theodore Ts'o3cb77842000-01-18 16:30:27 +0000773 fsck_device(fs->device, 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000774 fs->flags |= FLAG_DONE;
775 status |= wait_all();
776 if (status > EXIT_NONDESTRUCT)
777 return status;
778 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000779 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000780 if (fs) fs->flags |= FLAG_DONE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781
782 /*
783 * Mark filesystems that should be ignored as done.
784 */
785 for (fs = filesys_info; fs; fs = fs->next) {
786 if (ignore(fs))
787 fs->flags |= FLAG_DONE;
788 }
789
790 while (not_done_yet) {
791 not_done_yet = 0;
792 pass_done = 1;
793
794 for (fs = filesys_info; fs; fs = fs->next) {
795 if (fs->flags & FLAG_DONE)
796 continue;
797 /*
798 * If the filesystem's pass number is higher
799 * than the current pass number, then we don't
800 * do it yet.
801 */
802 if (fs->passno > passno) {
803 not_done_yet++;
804 continue;
805 }
806 /*
807 * If a filesystem on a particular device has
808 * already been spawned, then we need to defer
809 * this to another pass.
810 */
811 if (device_already_active(fs->device)) {
812 pass_done = 0;
813 continue;
814 }
815 /*
816 * Spawn off the fsck process
817 */
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000818 fs->device = interpret_device(fs->device);
Theodore Ts'o3cb77842000-01-18 16:30:27 +0000819 fsck_device(fs->device, serialize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 fs->flags |= FLAG_DONE;
821
Theodore Ts'o74becf31997-04-26 14:37:06 +0000822 if (serialize) {
823 pass_done = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000824 break; /* Only do one filesystem at a time */
Theodore Ts'o74becf31997-04-26 14:37:06 +0000825 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000826 }
Theodore Ts'oee922991999-01-16 05:39:12 +0000827 if (verbose > 1)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000828 printf(_("--waiting-- (pass %d)\n"), passno);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829 inst = wait_one();
830 if (inst) {
831 status |= inst->exit_status;
832 free_instance(inst);
833 }
834 if (pass_done) {
835 status |= wait_all();
Theodore Ts'oee922991999-01-16 05:39:12 +0000836 if (verbose > 1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000837 printf("----------------------------------\n");
838 passno++;
839 } else
840 not_done_yet++;
841 }
842 status |= wait_all();
843 return status;
844}
845
846static void usage(NOARGS)
847{
848 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000849 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000850 exit(EXIT_USAGE);
851}
852
853static void PRS(int argc, char *argv[])
854{
855 int i, j;
856 char *arg;
857 char options[128];
858 int opt = 0;
859 int opts_for_fsck = 0;
860
861 num_devices = 0;
862 num_args = 0;
863 instance_list = 0;
864
865 progname = argv[0];
866
Theodore Ts'o3839e651997-04-26 13:21:57 +0000867 for (i=1; i < argc; i++) {
868 arg = argv[i];
869 if (!arg)
870 continue;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000871 if ((arg[0] == '/' && !opts_for_fsck) ||
872 (strncmp(arg, "LABEL=", 6) == 0) ||
873 (strncmp(arg, "UUID=", 5) == 0)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000874 if (num_devices >= MAX_DEVICES) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000875 fprintf(stderr, _("%s: too many devices\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000876 progname);
877 exit(1);
878 }
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000879 devices[num_devices++] =
880 interpret_device(string_copy(arg));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000881 continue;
882 }
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000883 if (arg[0] != '-' || opts_for_fsck) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000884 if (num_args >= MAX_ARGS) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000885 fprintf(stderr, _("%s: too many arguments\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 progname);
887 exit(1);
888 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000889 args[num_args++] = string_copy(arg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000890 continue;
891 }
892 for (j=1; arg[j]; j++) {
893 if (opts_for_fsck) {
894 options[++opt] = arg[j];
895 continue;
896 }
897 switch (arg[j]) {
898 case 'A':
899 doall++;
900 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000901 case 'C':
902 progress++;
903 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 case 'V':
905 verbose++;
906 break;
907 case 'N':
908 noexecute++;
909 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000910 case 'R':
911 skip_root++;
912 break;
913 case 'T':
914 notitle++;
915 break;
916 case 'M':
917 like_mount++;
918 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000919 case 'P':
920 parallel_root++;
921 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922 case 's':
923 serialize++;
924 break;
925 case 't':
926 if (arg[j+1]) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000927 fstype = string_copy(arg+j+1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000928 goto next_arg;
929 }
930 if ((i+1) < argc) {
931 i++;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000932 fstype = string_copy(argv[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933 goto next_arg;
934 }
935 usage();
936 break;
937 case '-':
938 opts_for_fsck++;
939 break;
940 default:
941 options[++opt] = arg[j];
942 break;
943 }
944 }
945 next_arg:
946 if (opt) {
947 options[0] = '-';
948 options[++opt] = '\0';
949 if (num_args >= MAX_ARGS) {
950 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000951 _("%s: too many arguments\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000952 progname);
953 exit(1);
954 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000955 args[num_args++] = string_copy(options);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956 opt = 0;
957 }
958 }
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000959 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
960 force_all_parallel++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000961}
962
963int main(int argc, char *argv[])
964{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965 int i;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000966 int status = 0;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000967 int interactive = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000968 char *oldpath = getenv("PATH");
Theodore Ts'ob5135711999-10-26 14:33:24 +0000969 const char *fstab;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000971#ifdef ENABLE_NLS
972 setlocale(LC_MESSAGES, "");
973 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
974 textdomain(NLS_CAT_NAME);
975#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976 PRS(argc, argv);
977
Theodore Ts'of3db3561997-04-26 13:34:30 +0000978 if (!notitle)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000979 printf(_("Parallelizing fsck version %s (%s)\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000980 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000981
Theodore Ts'o7d5633c1999-02-09 08:14:28 +0000982 fstab = getenv("FSTAB_FILE");
983 if (!fstab)
984 fstab = _PATH_MNTTAB;
985 load_fs_info(fstab);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000986
Theodore Ts'of3db3561997-04-26 13:34:30 +0000987 /* Update our search path to include uncommon directories. */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000988 if (oldpath) {
989 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
990 strlen (oldpath) + 1);
991 strcpy (fsck_path, fsck_prefix_path);
992 strcat (fsck_path, ":");
993 strcat (fsck_path, oldpath);
994 } else {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000995 fsck_path = string_copy(fsck_prefix_path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000996 }
997
Theodore Ts'o6a35ffa1999-11-04 22:34:43 +0000998 if ((num_devices == 1) || (serialize))
999 interactive = 1;
1000
Theodore Ts'o3839e651997-04-26 13:21:57 +00001001 /* If -A was specified ("check all"), do that! */
1002 if (doall)
1003 return check_all();
1004
1005 for (i = 0 ; i < num_devices; i++) {
Theodore Ts'o2d8defd1999-07-03 01:59:42 +00001006 fsck_device(devices[i], interactive);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007 if (serialize) {
1008 struct fsck_instance *inst;
1009
1010 inst = wait_one();
Theodore Ts'of3db3561997-04-26 13:34:30 +00001011 if (inst) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001012 status |= inst->exit_status;
1013 free_instance(inst);
1014 }
1015 }
1016 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001017 status |= wait_all();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001018 free(fsck_path);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 return status;
1020}