blob: 5202711982e6f292becdde16a5485327003efad4 [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",
Theodore Ts'o3f1e9a52000-09-12 21:21:52 +000088 "/dev/dsk/hde",
89 "/dev/dsk/hdf",
90 "/dev/dsk/hdg",
91 "/dev/dsk/hdh",
Theodore Ts'of3db3561997-04-26 13:34:30 +000092 "/dev/dsk/hd1a",
93 "/dev/dsk/hd1b",
94 "/dev/dsk/hd1c",
95 "/dev/dsk/hd1d",
96 "/dev/dsk/sda",
97 "/dev/dsk/sdb",
98 "/dev/dsk/sdc",
99 "/dev/dsk/sdd",
100 "/dev/dsk/sde",
101 "/dev/dsk/sdf",
102 "/dev/dsk/sdg",
103 NULL
104};
105#else
Theodore Ts'o43819662000-10-25 02:06:09 +0000106#define BASE_MD "/dev/md"
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107static const char *base_devices[] = {
108 "/dev/hda",
109 "/dev/hdb",
110 "/dev/hdc",
111 "/dev/hdd",
Theodore Ts'o3f1e9a52000-09-12 21:21:52 +0000112 "/dev/hde",
113 "/dev/hdf",
114 "/dev/hdg",
115 "/dev/hdh",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000116 "/dev/hd1a",
117 "/dev/hd1b",
118 "/dev/hd1c",
119 "/dev/hd1d",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120 "/dev/sda",
121 "/dev/sdb",
122 "/dev/sdc",
123 "/dev/sdd",
124 "/dev/sde",
125 "/dev/sdf",
126 "/dev/sdg",
Theodore Ts'o43819662000-10-25 02:06:09 +0000127 BASE_MD,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 NULL
129};
Theodore Ts'of3db3561997-04-26 13:34:30 +0000130#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131
132/*
133 * Global variables for options
134 */
135char *devices[MAX_DEVICES];
136char *args[MAX_ARGS];
137int num_devices, num_args;
138
139int verbose = 0;
140int doall = 0;
141int noexecute = 0;
142int serialize = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000143int skip_root = 0;
144int like_mount = 0;
145int notitle = 0;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000146int parallel_root = 0;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000147int progress = 0;
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000148int force_all_parallel = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000149char *progname;
150char *fstype = NULL;
151struct fs_info *filesys_info;
152struct fsck_instance *instance_list;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000153const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
154char *fsck_path = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000155static int ignore(struct fs_info *);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156
Theodore Ts'o818180c1998-06-27 05:11:14 +0000157static char *string_copy(const char *s)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158{
159 char *ret;
160
161 ret = malloc(strlen(s)+1);
162 if (ret)
163 strcpy(ret, s);
164 return ret;
165}
166
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000167static char *skip_over_blank(char *cp)
168{
169 while (*cp && isspace(*cp))
170 cp++;
171 return cp;
172}
173
174static char *skip_over_word(char *cp)
175{
176 while (*cp && !isspace(*cp))
177 cp++;
178 return cp;
179}
180
181static void strip_line(char *line)
182{
183 char *p;
184
185 while (*line) {
186 p = line + strlen(line) - 1;
187 if ((*p == '\n') || (*p == '\r'))
188 *p = 0;
189 else
190 break;
191 }
192}
193
194static char *parse_word(char **buf)
195{
196 char *word, *next;
197
198 word = *buf;
199 if (*word == 0)
200 return 0;
201
202 word = skip_over_blank(word);
203 next = skip_over_word(word);
204 if (*next)
205 *next++ = 0;
206 *buf = next;
207 return word;
208}
209
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210static void free_instance(struct fsck_instance *i)
211{
212 if (i->prog)
213 free(i->prog);
214 if (i->device)
215 free(i->device);
216 free(i);
217 return;
218}
219
Theodore Ts'ob5135711999-10-26 14:33:24 +0000220static int parse_fstab_line(char *line, struct fs_info **ret_fs)
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000221{
222 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
223 struct fs_info *fs;
224
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000225 *ret_fs = 0;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000226 strip_line(line);
Theodore Ts'ob5135711999-10-26 14:33:24 +0000227 if ((cp = strchr(line, '#')))
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000228 *cp = 0; /* Ignore everything after the comment char */
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000229 cp = line;
230
231 device = parse_word(&cp);
232 mntpnt = parse_word(&cp);
233 type = parse_word(&cp);
234 opts = parse_word(&cp);
235 freq = parse_word(&cp);
236 passno = parse_word(&cp);
237
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000238 if (!device)
239 return 0; /* Allow blank lines */
240
241 if (!mntpnt || !type)
242 return -1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000243
244 if (!(fs = malloc(sizeof(struct fs_info))))
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000245 return -1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000246
247 fs->device = string_copy(device);
248 fs->mountpt = string_copy(mntpnt);
249 fs->type = string_copy(type);
250 fs->opts = string_copy(opts ? opts : "");
251 fs->freq = freq ? atoi(freq) : -1;
252 fs->passno = passno ? atoi(passno) : -1;
253 fs->flags = 0;
254 fs->next = NULL;
255
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000256 *ret_fs = fs;
257
258 return 0;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000259}
260
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261/*
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000262 * Interpret the device name if necessary
263 */
264static char *interpret_device(char *spec)
265{
266 char *dev = NULL;
267
268 if (!strncmp(spec, "UUID=", 5))
269 dev = get_spec_by_uuid(spec+5);
270 else if (!strncmp(spec, "LABEL=", 6))
271 dev = get_spec_by_volume_label(spec+6);
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000272 else
273 return spec;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000274 if (dev) {
275 free(spec);
Theodore Ts'ob4ee1fb2000-02-02 19:08:51 +0000276 return (dev);
277 }
278 /*
279 * Check to see if this was because /proc/partitions isn't
280 * found.
281 */
282 if (access("/proc/partitions", R_OK) < 0) {
283 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
284 strerror(errno));
285 fprintf(stderr, "Is /proc mounted?\n");
286 exit(1);
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000287 }
Theodore Ts'oc96e5112000-05-08 14:47:15 +0000288 /*
289 * Check to see if this is because we're not running as root
290 */
291 if (geteuid())
292 fprintf(stderr, "Must be root to scan for matching "
293 "filesystems: %s\n", spec);
294 else
295 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
296 spec);
297 exit(1);
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000298}
299
300/*
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301 * Load the filesystem database from /etc/fstab
302 */
Theodore Ts'ob5135711999-10-26 14:33:24 +0000303static void load_fs_info(const char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304{
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000305 FILE *f;
306 char buf[1024];
307 int lineno = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000308 int old_fstab = 1;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000309 struct fs_info *fs, *fs_last = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310
311 filesys_info = NULL;
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000312 if ((f = fopen(filename, "r")) == NULL) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000313 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000314 filename, strerror(errno));
315 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316 }
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000317 while (!feof(f)) {
318 lineno++;
319 if (!fgets(buf, sizeof(buf), f))
320 break;
321 buf[sizeof(buf)-1] = 0;
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000322 if (parse_fstab_line(buf, &fs) < 0) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000323 fprintf(stderr, _("WARNING: bad format "
324 "on line %d of %s\n"), lineno, filename);
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000325 continue;
326 }
Theodore Ts'o93ab9d71999-01-02 04:04:42 +0000327 if (!fs)
328 continue;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000329 if (!filesys_info)
330 filesys_info = fs;
331 else
332 fs_last->next = fs;
333 fs_last = fs;
Theodore Ts'o665f7101999-01-08 13:33:39 +0000334 if (fs->passno < 0)
335 fs->passno = 0;
336 else
Theodore Ts'of3db3561997-04-26 13:34:30 +0000337 old_fstab = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 }
Theodore Ts'o5a679c81998-12-03 16:40:38 +0000339
340 fclose(f);
341
Theodore Ts'of3db3561997-04-26 13:34:30 +0000342 if (old_fstab) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000343 fprintf(stderr, _("\007\007\007"
344 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
345 " field. I will kludge around things for you, but you\n"
346 " should fix your /etc/fstab file as soon as you can.\n\n"));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000347
348 for (fs = filesys_info; fs; fs = fs->next) {
349 fs->passno = 1;
350 }
351 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000352}
353
354/* Lookup filesys in /etc/fstab and return the corresponding entry. */
355static struct fs_info *lookup(char *filesys)
356{
357 struct fs_info *fs;
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000358 int try_again = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359
360 /* No filesys name given. */
361 if (filesys == NULL)
362 return NULL;
363
364 for (fs = filesys_info; fs; fs = fs->next) {
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000365 if (strchr(fs->device, '='))
366 try_again++;
367 if (!strcmp(filesys, fs->device) ||
368 !strcmp(filesys, fs->mountpt))
369 break;
370 }
Theodore Ts'o19c68912000-07-07 03:25:13 +0000371 if (fs && strchr(fs->device, '='))
372 fs->device = interpret_device(fs->device);
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000373
374 if (fs || !try_again)
375 return fs;
376
377 for (fs = filesys_info; fs; fs = fs->next) {
378 fs->device = interpret_device(fs->device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000379 if (!strcmp(filesys, fs->device) ||
380 !strcmp(filesys, fs->mountpt))
381 break;
382 }
383
384 return fs;
385}
386
Theodore Ts'of3db3561997-04-26 13:34:30 +0000387/* Find fsck program for a given fs type. */
388static char *find_fsck(char *type)
389{
390 char *s;
391 const char *tpl;
392 static char prog[256];
Theodore Ts'o818180c1998-06-27 05:11:14 +0000393 char *p = string_copy(fsck_path);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000394 struct stat st;
395
396 /* Are we looking for a program or just a type? */
397 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
398
399 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
400 sprintf(prog, tpl, s, type);
401 if (stat(prog, &st) == 0) break;
402 }
403 free(p);
404 return(s ? prog : NULL);
405}
406
Theodore Ts'ob5135711999-10-26 14:33:24 +0000407static int progress_active(NOARGS)
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000408{
409 struct fsck_instance *inst;
410
411 for (inst = instance_list; inst; inst = inst->next) {
412 if (inst->flags & FLAG_DONE)
413 continue;
414 if (inst->flags & FLAG_PROGRESS)
415 return 1;
416 }
417 return 0;
418}
419
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420/*
421 * Execute a particular fsck program, and link it into the list of
422 * child processes we are waiting for.
423 */
Theodore Ts'ob5135711999-10-26 14:33:24 +0000424static int execute(const char *type, char *device, char *mntpt,
425 int interactive)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426{
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000427 char *s, *argv[80], prog[80];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 int argc, i;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000429 struct fsck_instance *inst, *p;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 pid_t pid;
431
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000432 inst = malloc(sizeof(struct fsck_instance));
433 if (!inst)
434 return ENOMEM;
435 memset(inst, 0, sizeof(struct fsck_instance));
436
437 sprintf(prog, "fsck.%s", type);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000438 argv[0] = string_copy(prog);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 argc = 1;
440
441 for (i=0; i <num_args; i++)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000442 argv[argc++] = string_copy(args[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000444 if (progress & !progress_active()) {
Theodore Ts'o19c68912000-07-07 03:25:13 +0000445 if ((strcmp(type, "ext2") == 0) ||
446 (strcmp(type, "ext3") == 0)) {
Theodore Ts'ob5135711999-10-26 14:33:24 +0000447 argv[argc++] = string_copy("-C0");
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000448 inst->flags |= FLAG_PROGRESS;
449 }
450 }
451
Theodore Ts'o818180c1998-06-27 05:11:14 +0000452 argv[argc++] = string_copy(device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000453 argv[argc] = 0;
454
Theodore Ts'of3db3561997-04-26 13:34:30 +0000455 s = find_fsck(prog);
456 if (s == NULL) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000457 fprintf(stderr, _("fsck: %s: not found\n"), prog);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000458 return ENOENT;
459 }
460
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 if (verbose || noexecute) {
Theodore Ts'o5d45d801999-03-16 19:35:19 +0000462 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 for (i=0; i < argc; i++)
464 printf("%s ", argv[i]);
465 printf("\n");
466 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467
468 /* Fork and execute the correct program. */
Theodore Ts'oee922991999-01-16 05:39:12 +0000469 if (noexecute)
470 pid = -1;
471 else if ((pid = fork()) < 0) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 perror("fork");
473 return errno;
474 } else if (pid == 0) {
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000475 if (!interactive)
476 close(0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 (void) execv(s, argv);
478 perror(argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479 exit(EXIT_ERROR);
480 }
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000481
Theodore Ts'ob5135711999-10-26 14:33:24 +0000482 for (i=0; i < argc; i++)
483 free(argv[i]);
484
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 inst->pid = pid;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000486 inst->prog = string_copy(prog);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000487 inst->type = string_copy(type);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000488 inst->device = string_copy(device);
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000489 inst->start_time = time(0);
490 inst->next = NULL;
491
492 /*
493 * Find the end of the list, so we add the instance on at the end.
494 */
495 for (p = instance_list; p && p->next; p = p->next);
496
497 if (p)
Theodore Ts'oad6783d1999-10-26 01:58:54 +0000498 p->next = inst;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000499 else
500 instance_list = inst;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501
502 return 0;
503}
504
505/*
506 * Wait for one child process to exit; when it does, unlink it from
507 * the list of executing child processes, and return it.
508 */
509static struct fsck_instance *wait_one(NOARGS)
510{
511 int status;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000512 int sig;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000513 struct fsck_instance *inst, *inst2, *prev;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 pid_t pid;
515
516 if (!instance_list)
517 return NULL;
518
Theodore Ts'oee922991999-01-16 05:39:12 +0000519 if (noexecute) {
520 inst = instance_list;
521 instance_list = inst->next;
522 inst->exit_status = 0;
523 return(inst);
524 }
525
Theodore Ts'ob5135711999-10-26 14:33:24 +0000526 /*
527 * gcc -Wall fails saving throw against stupidity
528 * (inst and prev are thought to be uninitialized variables)
529 */
530 inst = prev = NULL;
531
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000532 do {
533 pid = wait(&status);
534 if (pid < 0) {
535 if ((errno == EINTR) || (errno == EAGAIN))
536 continue;
537 if (errno == ECHILD) {
538 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000539 _("%s: wait: No more child process?!?\n"),
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000540 progname);
541 return NULL;
542 }
543 perror("wait");
544 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 }
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000546 for (prev = 0, inst = instance_list;
547 inst;
548 prev = inst, inst = inst->next) {
549 if (inst->pid == pid)
550 break;
551 }
552 } while (!inst);
553
Theodore Ts'of3db3561997-04-26 13:34:30 +0000554 if (WIFEXITED(status))
555 status = WEXITSTATUS(status);
556 else if (WIFSIGNALED(status)) {
557 sig = WTERMSIG(status);
558 if (sig == SIGINT) {
559 status = EXIT_UNCORRECTED;
560 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000561 printf(_("Warning... %s for device %s exited "
562 "with signal %d.\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563 inst->prog, inst->device, sig);
564 status = EXIT_ERROR;
565 }
566 } else {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000567 printf(_("%s %s: status is %x, should never happen.\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000568 inst->prog, inst->device, status);
569 status = EXIT_ERROR;
570 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 inst->exit_status = status;
572 if (prev)
573 prev->next = inst->next;
574 else
575 instance_list = inst->next;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000576 if (progress && (inst->flags & FLAG_PROGRESS) &&
577 !progress_active()) {
578 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
579 if (inst2->flags & FLAG_DONE)
580 continue;
Theodore Ts'o19c68912000-07-07 03:25:13 +0000581 if (strcmp(inst2->type, "ext2") &&
582 strcmp(inst2->type, "ext3"))
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000583 continue;
Theodore Ts'o7f4bb6c1999-10-20 18:11:01 +0000584 /*
585 * If we've just started the fsck, wait a tiny
586 * bit before sending the kill, to give it
587 * time to set up the signal handler
588 */
589 if (inst2->start_time < time(0)+2) {
590 if (fork() == 0) {
591 sleep(1);
592 kill(inst2->pid, SIGUSR1);
593 exit(0);
594 }
595 } else
596 kill(inst2->pid, SIGUSR1);
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000597 break;
598 }
599 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 return inst;
601}
602
603/*
604 * Wait until all executing child processes have exited; return the
605 * logical OR of all of their exit code values.
606 */
607static int wait_all(NOARGS)
608{
609 struct fsck_instance *inst;
610 int global_status = 0;
611
612 while (instance_list) {
613 inst = wait_one();
614 if (!inst)
615 break;
616 global_status |= inst->exit_status;
617 free_instance(inst);
618 }
619 return global_status;
620}
621
622/*
623 * Run the fsck program on a particular device
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000624 *
625 * If the type is specified using -t, and it isn't prefixed with "no"
626 * (as in "noext2") and only one filesystem type is specified, then
627 * use that type regardless of what is specified in /etc/fstab.
628 *
629 * If the type isn't specified by the user, then use either the type
630 * specified in /etc/fstab, or DEFAULT_FSTYPE.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 */
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000632static void fsck_device(char *device, int interactive)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633{
Theodore Ts'ob5135711999-10-26 14:33:24 +0000634 const char *type = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 struct fs_info *fsent;
636 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637
Theodore Ts'o297f47a1997-04-26 14:25:20 +0000638 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000639 type = fstype;
640
Theodore Ts'of3db3561997-04-26 13:34:30 +0000641 if ((fsent = lookup(device))) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 device = fsent->device;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000643 if (!type)
644 type = fsent->type;
645 }
646 if (!type)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 type = DEFAULT_FSTYPE;
648
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000649 retval = execute(type, device, fsent ? fsent->mountpt : 0,
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000650 interactive);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000652 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
653 "for %s\n"), progname, retval, type, device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 }
655}
656
Theodore Ts'of3db3561997-04-26 13:34:30 +0000657/* See if filesystem type matches the list. */
658static int fs_match(char *type, char *fs_type)
659{
660 int ret = 0, negate = 0;
661 char list[128];
662 char *s;
663
664 if (!fs_type) return(1);
665
666 if (strncmp(fs_type, "no", 2) == 0) {
667 fs_type += 2;
668 negate = 1;
669 }
670 strcpy(list, fs_type);
671 s = strtok(list, ",");
672 while(s) {
673 if (strcmp(s, type) == 0) {
674 ret = 1;
675 break;
676 }
677 s = strtok(NULL, ",");
678 }
679 return(negate ? !ret : ret);
680}
681
682
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683/* Check if we should ignore this filesystem. */
684static int ignore(struct fs_info *fs)
685{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000686 const char **ip;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000687 int wanted = 0;
688
689 /*
690 * If the pass number is 0, ignore it.
691 */
692 if (fs->passno == 0)
693 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694
695 /*
696 * If a specific fstype is specified, and it doesn't match,
697 * ignore it.
698 */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000699 if (!fs_match(fs->type, fstype)) return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700
Theodore Ts'of3db3561997-04-26 13:34:30 +0000701 /* Are we ignoring this type? */
702 for(ip = ignored_types; *ip; ip++)
703 if (strcmp(fs->type, *ip) == 0) return(1);
704
705 /* Do we really really want to check this fs? */
706 for(ip = really_wanted; *ip; ip++)
707 if (strcmp(fs->type, *ip) == 0) {
708 wanted = 1;
709 break;
710 }
711
712 /* See if the <fsck.fs> program is available. */
713 if (find_fsck(fs->type) == NULL) {
714 if (wanted)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000715 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000716 fs->device, fs->type);
717 return(1);
718 }
719
720 /* We can and want to check this file system type. */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000721 return 0;
722}
723
724/*
725 * Return the "base device" given a particular device; this is used to
726 * assure that we only fsck one partition on a particular drive at any
727 * one time. Otherwise, the disk heads will be seeking all over the
728 * place.
729 */
730static const char *base_device(char *device)
731{
732 const char **base;
733
734 for (base = base_devices; *base; base++) {
735 if (!strncmp(*base, device, strlen(*base)))
736 return *base;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 }
738 return device;
739}
740
741/*
742 * Returns TRUE if a partition on the same disk is already being
743 * checked.
744 */
745static int device_already_active(char *device)
746{
747 struct fsck_instance *inst;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000748 const char *base = base_device(device);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000750 if (force_all_parallel)
751 return 0;
752
Theodore Ts'o43819662000-10-25 02:06:09 +0000753#ifdef BASE_MD
754 /* Don't check a soft raid disk with any other disk */
755 if (instance_list &&
756 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
757 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
758 return 1;
759#endif
760
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761 for (inst = instance_list; inst; inst = inst->next) {
762 if (!strcmp(base, base_device(inst->device)))
763 return 1;
764 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000765 return 0;
766}
767
768/* Check all file systems, using the /etc/fstab table. */
769static int check_all(NOARGS)
770{
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000771 struct fs_info *fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000772 struct fsck_instance *inst;
773 int status = EXIT_OK;
774 int not_done_yet = 1;
Theodore Ts'oee922991999-01-16 05:39:12 +0000775 int passno = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000776 int pass_done;
777
778 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000779 printf(_("Checking all file systems.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000780
781 /*
782 * Find and check the root filesystem first.
783 */
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000784 if (!parallel_root) {
785 for (fs = filesys_info; fs; fs = fs->next) {
786 if (!strcmp(fs->mountpt, "/"))
787 break;
788 }
789 if (fs && !skip_root && !ignore(fs)) {
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000790 fs->device = interpret_device(fs->device);
Theodore Ts'o3cb77842000-01-18 16:30:27 +0000791 fsck_device(fs->device, 1);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000792 fs->flags |= FLAG_DONE;
793 status |= wait_all();
794 if (status > EXIT_NONDESTRUCT)
795 return status;
796 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000797 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000798 if (fs) fs->flags |= FLAG_DONE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799
800 /*
801 * Mark filesystems that should be ignored as done.
802 */
803 for (fs = filesys_info; fs; fs = fs->next) {
804 if (ignore(fs))
805 fs->flags |= FLAG_DONE;
806 }
807
808 while (not_done_yet) {
809 not_done_yet = 0;
810 pass_done = 1;
811
812 for (fs = filesys_info; fs; fs = fs->next) {
813 if (fs->flags & FLAG_DONE)
814 continue;
815 /*
816 * If the filesystem's pass number is higher
817 * than the current pass number, then we don't
818 * do it yet.
819 */
820 if (fs->passno > passno) {
821 not_done_yet++;
822 continue;
823 }
824 /*
825 * If a filesystem on a particular device has
826 * already been spawned, then we need to defer
827 * this to another pass.
828 */
829 if (device_already_active(fs->device)) {
830 pass_done = 0;
831 continue;
832 }
833 /*
834 * Spawn off the fsck process
835 */
Theodore Ts'o5b1519b2000-07-06 14:16:08 +0000836 fs->device = interpret_device(fs->device);
Theodore Ts'o3cb77842000-01-18 16:30:27 +0000837 fsck_device(fs->device, serialize);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 fs->flags |= FLAG_DONE;
839
Theodore Ts'o74becf31997-04-26 14:37:06 +0000840 if (serialize) {
841 pass_done = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000842 break; /* Only do one filesystem at a time */
Theodore Ts'o74becf31997-04-26 14:37:06 +0000843 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844 }
Theodore Ts'oee922991999-01-16 05:39:12 +0000845 if (verbose > 1)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000846 printf(_("--waiting-- (pass %d)\n"), passno);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847 inst = wait_one();
848 if (inst) {
849 status |= inst->exit_status;
850 free_instance(inst);
851 }
852 if (pass_done) {
853 status |= wait_all();
Theodore Ts'oee922991999-01-16 05:39:12 +0000854 if (verbose > 1)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855 printf("----------------------------------\n");
856 passno++;
857 } else
858 not_done_yet++;
859 }
860 status |= wait_all();
861 return status;
862}
863
864static void usage(NOARGS)
865{
866 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000867 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868 exit(EXIT_USAGE);
869}
870
871static void PRS(int argc, char *argv[])
872{
873 int i, j;
874 char *arg;
875 char options[128];
876 int opt = 0;
877 int opts_for_fsck = 0;
878
879 num_devices = 0;
880 num_args = 0;
881 instance_list = 0;
882
883 progname = argv[0];
884
Theodore Ts'o3839e651997-04-26 13:21:57 +0000885 for (i=1; i < argc; i++) {
886 arg = argv[i];
887 if (!arg)
888 continue;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000889 if ((arg[0] == '/' && !opts_for_fsck) ||
890 (strncmp(arg, "LABEL=", 6) == 0) ||
891 (strncmp(arg, "UUID=", 5) == 0)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000892 if (num_devices >= MAX_DEVICES) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000893 fprintf(stderr, _("%s: too many devices\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000894 progname);
895 exit(1);
896 }
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000897 devices[num_devices++] =
898 interpret_device(string_copy(arg));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 continue;
900 }
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000901 if (arg[0] != '-' || opts_for_fsck) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000902 if (num_args >= MAX_ARGS) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000903 fprintf(stderr, _("%s: too many arguments\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 progname);
905 exit(1);
906 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000907 args[num_args++] = string_copy(arg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000908 continue;
909 }
910 for (j=1; arg[j]; j++) {
911 if (opts_for_fsck) {
912 options[++opt] = arg[j];
913 continue;
914 }
915 switch (arg[j]) {
916 case 'A':
917 doall++;
918 break;
Theodore Ts'o50787ea1999-07-19 15:30:21 +0000919 case 'C':
920 progress++;
921 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922 case 'V':
923 verbose++;
924 break;
925 case 'N':
926 noexecute++;
927 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000928 case 'R':
929 skip_root++;
930 break;
931 case 'T':
932 notitle++;
933 break;
934 case 'M':
935 like_mount++;
936 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000937 case 'P':
938 parallel_root++;
939 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000940 case 's':
941 serialize++;
942 break;
943 case 't':
944 if (arg[j+1]) {
Theodore Ts'o818180c1998-06-27 05:11:14 +0000945 fstype = string_copy(arg+j+1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946 goto next_arg;
947 }
948 if ((i+1) < argc) {
949 i++;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000950 fstype = string_copy(argv[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951 goto next_arg;
952 }
953 usage();
954 break;
955 case '-':
956 opts_for_fsck++;
957 break;
958 default:
959 options[++opt] = arg[j];
960 break;
961 }
962 }
963 next_arg:
964 if (opt) {
965 options[0] = '-';
966 options[++opt] = '\0';
967 if (num_args >= MAX_ARGS) {
968 fprintf(stderr,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000969 _("%s: too many arguments\n"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970 progname);
971 exit(1);
972 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000973 args[num_args++] = string_copy(options);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000974 opt = 0;
975 }
976 }
Theodore Ts'o3bc03661999-10-26 17:17:00 +0000977 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
978 force_all_parallel++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000979}
980
981int main(int argc, char *argv[])
982{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000983 int i;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000984 int status = 0;
Theodore Ts'o2d8defd1999-07-03 01:59:42 +0000985 int interactive = 0;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000986 char *oldpath = getenv("PATH");
Theodore Ts'ob5135711999-10-26 14:33:24 +0000987 const char *fstab;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000988
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000989#ifdef ENABLE_NLS
990 setlocale(LC_MESSAGES, "");
991 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
992 textdomain(NLS_CAT_NAME);
993#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000994 PRS(argc, argv);
995
Theodore Ts'of3db3561997-04-26 13:34:30 +0000996 if (!notitle)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000997 printf(_("Parallelizing fsck version %s (%s)\n"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000998 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000999
Theodore Ts'o7d5633c1999-02-09 08:14:28 +00001000 fstab = getenv("FSTAB_FILE");
1001 if (!fstab)
1002 fstab = _PATH_MNTTAB;
1003 load_fs_info(fstab);
Theodore Ts'o5a679c81998-12-03 16:40:38 +00001004
Theodore Ts'of3db3561997-04-26 13:34:30 +00001005 /* Update our search path to include uncommon directories. */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001006 if (oldpath) {
1007 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1008 strlen (oldpath) + 1);
1009 strcpy (fsck_path, fsck_prefix_path);
1010 strcat (fsck_path, ":");
1011 strcat (fsck_path, oldpath);
1012 } else {
Theodore Ts'o818180c1998-06-27 05:11:14 +00001013 fsck_path = string_copy(fsck_prefix_path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001014 }
1015
Theodore Ts'o6a35ffa1999-11-04 22:34:43 +00001016 if ((num_devices == 1) || (serialize))
1017 interactive = 1;
1018
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 /* If -A was specified ("check all"), do that! */
1020 if (doall)
1021 return check_all();
1022
1023 for (i = 0 ; i < num_devices; i++) {
Theodore Ts'o2d8defd1999-07-03 01:59:42 +00001024 fsck_device(devices[i], interactive);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001025 if (serialize) {
1026 struct fsck_instance *inst;
1027
1028 inst = wait_one();
Theodore Ts'of3db3561997-04-26 13:34:30 +00001029 if (inst) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001030 status |= inst->exit_status;
1031 free_instance(inst);
1032 }
1033 }
1034 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001035 status |= wait_all();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001036 free(fsck_path);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001037 return status;
1038}