blob: a86a9d96f6577ad13df4e6a9b691da368284e7e8 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * fsck --- A generic, parallelizing front-end for the fsck program.
4 * It will automatically try to run fsck programs in parallel if the
5 * devices are on separate spindles. It is based on the same ideas as
6 * the generic front end for fsck by David Engel and Fred van Kempen,
7 * but it has been completely rewritten from scratch to support
8 * parallel execution.
9 *
10 * Written by Theodore Ts'o, <tytso@mit.edu>
11 *
12 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
13 * o Changed -t fstype to behave like with mount when -A (all file
14 * systems) or -M (like mount) is specified.
15 * o fsck looks if it can find the fsck.type program to decide
16 * if it should ignore the fs type. This way more fsck programs
17 * can be added without changing this front-end.
18 * o -R flag skip root file system.
19 *
20 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
21 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
22 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020023 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000024 */
25
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000026/* All filesystem specific hooks have been removed.
27 * If filesystem cannot be determined, we will execute
28 * "fsck.auto". Currently this also happens if you specify
29 * UUID=xxx or LABEL=xxx as an object to check.
30 * Detection code for that is also probably has to be in fsck.auto.
31 *
32 * In other words, this is _really_ is just a driver program which
33 * spawns actual fsck.something for each filesystem to check.
34 * It doesn't guess filesystem types from on-disk format.
35 */
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000038
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000039/* "progress indicator" code is somewhat buggy and ext[23] specific.
40 * We should be filesystem agnostic. IOW: there should be a well-defined
41 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
42#define DO_PROGRESS_INDICATOR 0
43
Denys Vlasenko0d7e2e72009-11-15 02:36:49 +010044/* fsck 1.41.4 (27-Jan-2009) manpage says:
45 * 0 - No errors
46 * 1 - File system errors corrected
47 * 2 - System should be rebooted
48 * 4 - File system errors left uncorrected
49 * 8 - Operational error
50 * 16 - Usage or syntax error
51 * 32 - Fsck canceled by user request
52 * 128 - Shared library error
53 */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000054#define EXIT_OK 0
55#define EXIT_NONDESTRUCT 1
56#define EXIT_DESTRUCT 2
57#define EXIT_UNCORRECTED 4
58#define EXIT_ERROR 8
59#define EXIT_USAGE 16
60#define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
61
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000062/*
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000063 * Internal structure for mount table entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000064 */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000065struct fs_info {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000066 struct fs_info *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000067 char *device;
68 char *mountpt;
69 char *type;
70 char *opts;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000071 int passno;
72 int flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000073};
74
75#define FLAG_DONE 1
76#define FLAG_PROGRESS 2
77/*
78 * Structure to allow exit codes to be stored
79 */
80struct fsck_instance {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000081 struct fsck_instance *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000082 int pid;
83 int flags;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000084#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000085 time_t start_time;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000086#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000087 char *prog;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000088 char *device;
89 char *base_device; /* /dev/hda for /dev/hdaN etc */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000090};
91
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000092static const char ignored_types[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000093 "ignore\0"
94 "iso9660\0"
95 "nfs\0"
96 "proc\0"
97 "sw\0"
98 "swap\0"
99 "tmpfs\0"
100 "devpts\0";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000101
Denis Vlasenko0de93752006-12-26 02:51:29 +0000102#if 0
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000103static const char really_wanted[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000104 "minix\0"
105 "ext2\0"
106 "ext3\0"
107 "jfs\0"
108 "reiserfs\0"
109 "xiafs\0"
110 "xfs\0";
Denis Vlasenko0de93752006-12-26 02:51:29 +0000111#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000112
113#define BASE_MD "/dev/md"
114
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000115static char **args;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000116static int num_args;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000117static int verbose;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000118
119#define FS_TYPE_FLAG_NORMAL 0
120#define FS_TYPE_FLAG_OPT 1
121#define FS_TYPE_FLAG_NEGOPT 2
122static char **fs_type_list;
123static uint8_t *fs_type_flag;
124static smallint fs_type_negated;
125
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000126static smallint noexecute;
127static smallint serialize;
128static smallint skip_root;
129/* static smallint like_mount; */
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000130static smallint parallel_root;
131static smallint force_all_parallel;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000132
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000133#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000134static smallint progress;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000135static int progress_fd;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000136#endif
137
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000138static int num_running;
139static int max_running;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000140static char *fstype;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000141static struct fs_info *filesys_info;
142static struct fs_info *filesys_last;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000143static struct fsck_instance *instance_list;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000144
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000145/*
146 * Return the "base device" given a particular device; this is used to
147 * assure that we only fsck one partition on a particular drive at any
148 * one time. Otherwise, the disk heads will be seeking all over the
149 * place. If the base device cannot be determined, return NULL.
150 *
151 * The base_device() function returns an allocated string which must
152 * be freed.
153 */
154#if ENABLE_FEATURE_DEVFS
155/*
156 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
157 * pathames.
158 */
159static const char *const devfs_hier[] = {
160 "host", "bus", "target", "lun", NULL
161};
162#endif
163
164static char *base_device(const char *device)
165{
166 char *str, *cp;
167#if ENABLE_FEATURE_DEVFS
168 const char *const *hier;
169 const char *disk;
170 int len;
171#endif
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200172 str = xstrdup(device);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000173
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200174 /* Skip over "/dev/"; if it's not present, give up */
175 cp = skip_dev_pfx(str);
176 if (cp == str)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000177 goto errout;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000178
179 /*
180 * For md devices, we treat them all as if they were all
181 * on one disk, since we don't know how to parallelize them.
182 */
183 if (cp[0] == 'm' && cp[1] == 'd') {
184 cp[2] = 0;
185 return str;
186 }
187
188 /* Handle DAC 960 devices */
189 if (strncmp(cp, "rd/", 3) == 0) {
190 cp += 3;
191 if (cp[0] != 'c' || !isdigit(cp[1])
192 || cp[2] != 'd' || !isdigit(cp[3]))
193 goto errout;
194 cp[4] = 0;
195 return str;
196 }
197
198 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
199 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
200 cp += 2;
201 /* If there's a single number after /dev/hd, skip it */
202 if (isdigit(*cp))
203 cp++;
204 /* What follows must be an alpha char, or give up */
205 if (!isalpha(*cp))
206 goto errout;
207 cp[1] = 0;
208 return str;
209 }
210
211#if ENABLE_FEATURE_DEVFS
212 /* Now let's handle devfs (ugh) names */
213 len = 0;
214 if (strncmp(cp, "ide/", 4) == 0)
215 len = 4;
216 if (strncmp(cp, "scsi/", 5) == 0)
217 len = 5;
218 if (len) {
219 cp += len;
220 /*
221 * Now we proceed down the expected devfs hierarchy.
222 * i.e., .../host1/bus2/target3/lun4/...
223 * If we don't find the expected token, followed by
224 * some number of digits at each level, abort.
225 */
226 for (hier = devfs_hier; *hier; hier++) {
227 len = strlen(*hier);
228 if (strncmp(cp, *hier, len) != 0)
229 goto errout;
230 cp += len;
231 while (*cp != '/' && *cp != 0) {
232 if (!isdigit(*cp))
233 goto errout;
234 cp++;
235 }
236 cp++;
237 }
238 cp[-1] = 0;
239 return str;
240 }
241
242 /* Now handle devfs /dev/disc or /dev/disk names */
243 disk = 0;
244 if (strncmp(cp, "discs/", 6) == 0)
245 disk = "disc";
246 else if (strncmp(cp, "disks/", 6) == 0)
247 disk = "disk";
248 if (disk) {
249 cp += 6;
250 if (strncmp(cp, disk, 4) != 0)
251 goto errout;
252 cp += 4;
253 while (*cp != '/' && *cp != 0) {
254 if (!isdigit(*cp))
255 goto errout;
256 cp++;
257 }
258 *cp = 0;
259 return str;
260 }
261#endif
262 errout:
263 free(str);
264 return NULL;
265}
266
267static void free_instance(struct fsck_instance *p)
268{
269 free(p->prog);
270 free(p->device);
271 free(p->base_device);
272 free(p);
273}
274
275static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
276 const char *type, const char *opts,
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000277 int passno)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000278{
279 struct fs_info *fs;
280
281 fs = xzalloc(sizeof(*fs));
282 fs->device = xstrdup(device);
283 fs->mountpt = xstrdup(mntpnt);
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000284 if (strchr(type, ','))
285 type = (char *)"auto";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000286 fs->type = xstrdup(type);
287 fs->opts = xstrdup(opts ? opts : "");
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000288 fs->passno = passno < 0 ? 1 : passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000289 /*fs->flags = 0; */
290 /*fs->next = NULL; */
291
292 if (!filesys_info)
293 filesys_info = fs;
294 else
295 filesys_last->next = fs;
296 filesys_last = fs;
297
298 return fs;
299}
300
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000301/* Load the filesystem database from /etc/fstab */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000302static void load_fs_info(const char *filename)
303{
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000304 FILE *fstab;
305 struct mntent mte;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000306 struct fs_info *fs;
307
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000308 fstab = setmntent(filename, "r");
309 if (!fstab) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100310 bb_perror_msg("can't read '%s'", filename);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000311 return;
312 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000313
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000314 // Loop through entries
315 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
316 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000317 // mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000318 // mte.mnt_passno);
319 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000320 mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000321 mte.mnt_passno);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000322 }
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000323 endmntent(fstab);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000324}
325
326/* Lookup filesys in /etc/fstab and return the corresponding entry. */
327static struct fs_info *lookup(char *filesys)
328{
329 struct fs_info *fs;
330
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000331 for (fs = filesys_info; fs; fs = fs->next) {
332 if (strcmp(filesys, fs->device) == 0
333 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
334 )
335 break;
336 }
337
338 return fs;
339}
340
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000341#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000342static int progress_active(void)
343{
344 struct fsck_instance *inst;
345
346 for (inst = instance_list; inst; inst = inst->next) {
347 if (inst->flags & FLAG_DONE)
348 continue;
349 if (inst->flags & FLAG_PROGRESS)
350 return 1;
351 }
352 return 0;
353}
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000354#endif
355
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000356
357/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000358 * Send a signal to all outstanding fsck child processes
359 */
Denys Vlasenko66837452009-09-13 05:52:03 +0200360static void kill_all_if_got_signal(void)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000361{
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000362 static smallint kill_sent;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000363
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000364 struct fsck_instance *inst;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000365
Denys Vlasenko66837452009-09-13 05:52:03 +0200366 if (!bb_got_signal || kill_sent)
Denis Vlasenko0de93752006-12-26 02:51:29 +0000367 return;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000368
369 for (inst = instance_list; inst; inst = inst->next) {
370 if (inst->flags & FLAG_DONE)
371 continue;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000372 kill(inst->pid, SIGTERM);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000373 }
Denis Vlasenko0de93752006-12-26 02:51:29 +0000374 kill_sent = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000375}
376
377/*
378 * Wait for one child process to exit; when it does, unlink it from
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000379 * the list of executing child processes, free, and return its exit status.
380 * If there is no exited child, return -1.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000381 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000382static int wait_one(int flags)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000383{
384 int status;
385 int sig;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000386 struct fsck_instance *inst, *prev;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000387 pid_t pid;
388
389 if (!instance_list)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000390 return -1;
391 /* if (noexecute) { already returned -1; } */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000392
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000393 while (1) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000394 pid = waitpid(-1, &status, flags);
Denys Vlasenko66837452009-09-13 05:52:03 +0200395 kill_all_if_got_signal();
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000396 if (pid == 0) /* flags == WNOHANG and no children exited */
397 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000398 if (pid < 0) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000399 if (errno == EINTR)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000400 continue;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000401 if (errno == ECHILD) { /* paranoia */
402 bb_error_msg("wait: no more children");
403 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000404 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000405 bb_perror_msg("wait");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000406 continue;
407 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000408 prev = NULL;
409 inst = instance_list;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000410 do {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000411 if (inst->pid == pid)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000412 goto child_died;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000413 prev = inst;
414 inst = inst->next;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000415 } while (inst);
416 }
417 child_died:
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000418
419 if (WIFEXITED(status))
420 status = WEXITSTATUS(status);
421 else if (WIFSIGNALED(status)) {
422 sig = WTERMSIG(status);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000423 status = EXIT_UNCORRECTED;
424 if (sig != SIGINT) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000425 printf("Warning: %s %s terminated "
426 "by signal %d\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000427 inst->prog, inst->device, sig);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000428 status = EXIT_ERROR;
429 }
430 } else {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000431 printf("%s %s: status is %x, should never happen\n",
432 inst->prog, inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000433 status = EXIT_ERROR;
434 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000435
436#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000437 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000438 struct fsck_instance *inst2;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000439 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
440 if (inst2->flags & FLAG_DONE)
441 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000442 if (strcmp(inst2->type, "ext2") != 0
443 && strcmp(inst2->type, "ext3") != 0
444 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000445 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000446 }
447 /* ext[23], we will send USR1
448 * (request to start displaying progress bar)
449 *
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000450 * If we've just started the fsck, wait a tiny
451 * bit before sending the kill, to give it
452 * time to set up the signal handler
453 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000454 if (inst2->start_time >= time(NULL) - 1)
455 sleep(1);
456 kill(inst2->pid, SIGUSR1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000457 inst2->flags |= FLAG_PROGRESS;
458 break;
459 }
460 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000461#endif
462
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000463 if (prev)
464 prev->next = inst->next;
465 else
466 instance_list = inst->next;
467 if (verbose > 1)
468 printf("Finished with %s (exit status %d)\n",
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000469 inst->device, status);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000470 num_running--;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000471 free_instance(inst);
472
473 return status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000474}
475
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000476/*
477 * Wait until all executing child processes have exited; return the
478 * logical OR of all of their exit code values.
479 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000480#define FLAG_WAIT_ALL 0
481#define FLAG_WAIT_ATLEAST_ONE WNOHANG
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000482static int wait_many(int flags)
483{
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000484 int exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000485 int global_status = 0;
486 int wait_flags = 0;
487
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000488 while ((exit_status = wait_one(wait_flags)) != -1) {
489 global_status |= exit_status;
490 wait_flags |= flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000491 }
492 return global_status;
493}
494
495/*
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000496 * Execute a particular fsck program, and link it into the list of
497 * child processes we are waiting for.
498 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000499static void execute(const char *type, const char *device,
500 const char *mntpt /*, int interactive */)
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000501{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000502 int i;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000503 struct fsck_instance *inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000504 pid_t pid;
505
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200506 args[0] = xasprintf("fsck.%s", type);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000507
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000508#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000509 if (progress && !progress_active()) {
510 if (strcmp(type, "ext2") == 0
511 || strcmp(type, "ext3") == 0
512 ) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200513 args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000514 inst->flags |= FLAG_PROGRESS;
515 }
516 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000517#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000518
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200519 args[num_args - 2] = (char*)device;
520 /* args[num_args - 1] = NULL; - already is */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000521
522 if (verbose || noexecute) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200523 printf("[%s (%d) -- %s]", args[0], num_running,
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000524 mntpt ? mntpt : device);
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200525 for (i = 0; args[i]; i++)
526 printf(" %s", args[i]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000527 bb_putchar('\n');
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000528 }
529
530 /* Fork and execute the correct program. */
531 pid = -1;
532 if (!noexecute) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200533 pid = spawn(args);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000534 if (pid < 0)
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200535 bb_simple_perror_msg(args[0]);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000536 }
537
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000538#if DO_PROGRESS_INDICATOR
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200539 free(args[XXX]);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000540#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000541
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000542 /* No child, so don't record an instance */
543 if (pid <= 0) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200544 free(args[0]);
Denis Vlasenko30eb3192008-02-02 18:54:58 +0000545 return;
546 }
547
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000548 inst = xzalloc(sizeof(*inst));
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000549 inst->pid = pid;
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200550 inst->prog = args[0];
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000551 inst->device = xstrdup(device);
552 inst->base_device = base_device(device);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000553#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000554 inst->start_time = time(NULL);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000555#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000556
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000557 /* Add to the list of running fsck's.
558 * (was adding to the end, but adding to the front is simpler...) */
559 inst->next = instance_list;
560 instance_list = inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000561}
562
563/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000564 * Run the fsck program on a particular device
565 *
566 * If the type is specified using -t, and it isn't prefixed with "no"
567 * (as in "noext2") and only one filesystem type is specified, then
568 * use that type regardless of what is specified in /etc/fstab.
569 *
570 * If the type isn't specified by the user, then use either the type
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000571 * specified in /etc/fstab, or "auto".
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000572 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000573static void fsck_device(struct fs_info *fs /*, int interactive */)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000574{
575 const char *type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000576
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000577 if (strcmp(fs->type, "auto") != 0) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000578 type = fs->type;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000579 if (verbose > 2)
580 bb_info_msg("using filesystem type '%s' %s",
581 type, "from fstab");
582 } else if (fstype
Denis Vlasenko0de93752006-12-26 02:51:29 +0000583 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
584 && strncmp(fstype, "opts=", 5) != 0
585 && strncmp(fstype, "loop", 4) != 0
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000586 && !strchr(fstype, ',')
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000587 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000588 type = fstype;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000589 if (verbose > 2)
590 bb_info_msg("using filesystem type '%s' %s",
591 type, "from -t");
592 } else {
593 type = "auto";
594 if (verbose > 2)
595 bb_info_msg("using filesystem type '%s' %s",
596 type, "(default)");
597 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000598
599 num_running++;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000600 execute(type, fs->device, fs->mountpt /*, interactive */);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000601}
602
603/*
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000604 * Returns TRUE if a partition on the same disk is already being
605 * checked.
606 */
607static int device_already_active(char *device)
608{
609 struct fsck_instance *inst;
610 char *base;
611
612 if (force_all_parallel)
613 return 0;
614
615#ifdef BASE_MD
616 /* Don't check a soft raid disk with any other disk */
617 if (instance_list
618 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
619 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
620 ) {
621 return 1;
622 }
623#endif
624
625 base = base_device(device);
626 /*
627 * If we don't know the base device, assume that the device is
628 * already active if there are any fsck instances running.
629 */
630 if (!base)
631 return (instance_list != NULL);
632
633 for (inst = instance_list; inst; inst = inst->next) {
634 if (!inst->base_device || !strcmp(base, inst->base_device)) {
635 free(base);
636 return 1;
637 }
638 }
639
640 free(base);
641 return 0;
642}
643
644/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000645 * This function returns true if a particular option appears in a
646 * comma-delimited options list
647 */
648static int opt_in_list(char *opt, char *optlist)
649{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000650 char *s;
651 int len;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000652
653 if (!optlist)
654 return 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000655
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000656 len = strlen(opt);
657 s = optlist - 1;
658 while (1) {
659 s = strstr(s + 1, opt);
660 if (!s)
661 return 0;
662 /* neither "opt.." nor "xxx,opt.."? */
663 if (s != optlist && s[-1] != ',')
664 continue;
665 /* neither "..opt" nor "..opt,xxx"? */
666 if (s[len] != '\0' && s[len] != ',')
667 continue;
668 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000669 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000670}
671
672/* See if the filesystem matches the criteria given by the -t option */
673static int fs_match(struct fs_info *fs)
674{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000675 int n, ret, checked_type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000676 char *cp;
677
678 if (!fs_type_list)
679 return 1;
680
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000681 ret = 0;
682 checked_type = 0;
683 n = 0;
684 while (1) {
685 cp = fs_type_list[n];
686 if (!cp)
687 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000688 switch (fs_type_flag[n]) {
689 case FS_TYPE_FLAG_NORMAL:
690 checked_type++;
691 if (strcmp(cp, fs->type) == 0)
692 ret = 1;
693 break;
694 case FS_TYPE_FLAG_NEGOPT:
695 if (opt_in_list(cp, fs->opts))
696 return 0;
697 break;
698 case FS_TYPE_FLAG_OPT:
699 if (!opt_in_list(cp, fs->opts))
700 return 0;
701 break;
702 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000703 n++;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000704 }
705 if (checked_type == 0)
706 return 1;
707
708 return (fs_type_negated ? !ret : ret);
709}
710
711/* Check if we should ignore this filesystem. */
712static int ignore(struct fs_info *fs)
713{
714 /*
715 * If the pass number is 0, ignore it.
716 */
717 if (fs->passno == 0)
718 return 1;
719
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000720 /*
721 * If a specific fstype is specified, and it doesn't match,
722 * ignore it.
723 */
724 if (!fs_match(fs))
725 return 1;
726
727 /* Are we ignoring this type? */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000728 if (index_in_strings(ignored_types, fs->type) >= 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000729 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000730
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000731 /* We can and want to check this file system type. */
732 return 0;
733}
734
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000735/* Check all file systems, using the /etc/fstab table. */
736static int check_all(void)
737{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000738 struct fs_info *fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000739 int status = EXIT_OK;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000740 smallint not_done_yet;
741 smallint pass_done;
742 int passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000743
744 if (verbose)
745 puts("Checking all filesystems");
746
747 /*
748 * Do an initial scan over the filesystem; mark filesystems
749 * which should be ignored as done, and resolve any "auto"
750 * filesystem types (done as a side-effect of calling ignore()).
751 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000752 for (fs = filesys_info; fs; fs = fs->next)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000753 if (ignore(fs))
754 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000755
756 /*
757 * Find and check the root filesystem.
758 */
759 if (!parallel_root) {
760 for (fs = filesys_info; fs; fs = fs->next) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000761 if (LONE_CHAR(fs->mountpt, '/')) {
762 if (!skip_root && !ignore(fs)) {
763 fsck_device(fs /*, 1*/);
764 status |= wait_many(FLAG_WAIT_ALL);
765 if (status > EXIT_NONDESTRUCT)
766 return status;
767 }
768 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000769 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000770 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000771 }
772 }
773 /*
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000774 * This is for the bone-headed user who has root
775 * filesystem listed twice.
776 * "Skip root" will skip _all_ root entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000777 */
778 if (skip_root)
779 for (fs = filesys_info; fs; fs = fs->next)
780 if (LONE_CHAR(fs->mountpt, '/'))
781 fs->flags |= FLAG_DONE;
782
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000783 not_done_yet = 1;
784 passno = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000785 while (not_done_yet) {
786 not_done_yet = 0;
787 pass_done = 1;
788
789 for (fs = filesys_info; fs; fs = fs->next) {
Denys Vlasenko66837452009-09-13 05:52:03 +0200790 if (bb_got_signal)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000791 break;
792 if (fs->flags & FLAG_DONE)
793 continue;
794 /*
795 * If the filesystem's pass number is higher
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000796 * than the current pass number, then we didn't
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000797 * do it yet.
798 */
799 if (fs->passno > passno) {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000800 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000801 continue;
802 }
803 /*
804 * If a filesystem on a particular device has
805 * already been spawned, then we need to defer
806 * this to another pass.
807 */
808 if (device_already_active(fs->device)) {
809 pass_done = 0;
810 continue;
811 }
812 /*
813 * Spawn off the fsck process
814 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000815 fsck_device(fs /*, serialize*/);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000816 fs->flags |= FLAG_DONE;
817
818 /*
819 * Only do one filesystem at a time, or if we
820 * have a limit on the number of fsck's extant
821 * at one time, apply that limit.
822 */
823 if (serialize
824 || (max_running && (num_running >= max_running))
825 ) {
826 pass_done = 0;
827 break;
828 }
829 }
Denys Vlasenko66837452009-09-13 05:52:03 +0200830 if (bb_got_signal)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000831 break;
832 if (verbose > 1)
833 printf("--waiting-- (pass %d)\n", passno);
834 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
835 FLAG_WAIT_ATLEAST_ONE);
836 if (pass_done) {
837 if (verbose > 1)
838 puts("----------------------------------");
839 passno++;
840 } else
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000841 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000842 }
Denys Vlasenko66837452009-09-13 05:52:03 +0200843 kill_all_if_got_signal();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000844 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
845 return status;
846}
847
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000848/*
849 * Deal with the fsck -t argument.
850 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
851 * Why here we require "-t novfat,nonfs" ??
852 */
853static void compile_fs_type(char *fs_type)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000854{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000855 char *s;
856 int num = 2;
857 smallint negate;
858
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000859 s = fs_type;
860 while ((s = strchr(s, ','))) {
861 num++;
862 s++;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000863 }
864
865 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
866 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
867 fs_type_negated = -1; /* not yet known is it negated or not */
868
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000869 num = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000870 s = fs_type;
871 while (1) {
872 char *comma;
873
874 negate = 0;
875 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
876 s += 2;
877 negate = 1;
878 } else if (s[0] == '!') {
879 s++;
880 negate = 1;
881 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000882
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000883 if (strcmp(s, "loop") == 0)
884 /* loop is really short-hand for opts=loop */
885 goto loop_special_case;
886 if (strncmp(s, "opts=", 5) == 0) {
887 s += 5;
888 loop_special_case:
889 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
890 } else {
891 if (fs_type_negated == -1)
892 fs_type_negated = negate;
893 if (fs_type_negated != negate)
894 bb_error_msg_and_die(
895"either all or none of the filesystem types passed to -t must be prefixed "
896"with 'no' or '!'");
897 }
898 comma = strchr(s, ',');
899 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
900 if (!comma)
901 break;
902 s = comma + 1;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000903 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000904}
905
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200906static char **new_args(void)
907{
908 args = xrealloc_vector(args, 2, num_args);
909 return &args[num_args++];
910}
911
Denys Vlasenko66837452009-09-13 05:52:03 +0200912int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
913int fsck_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000914{
Denys Vlasenko66837452009-09-13 05:52:03 +0200915 int i, status;
916 /*int interactive;*/
917 struct fs_info *fs;
918 const char *fstab;
919 char *tmp;
920 char **devices;
921 int num_devices;
922 smallint opts_for_fsck;
923 smallint doall;
924 smallint notitle;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000925
Denys Vlasenko66837452009-09-13 05:52:03 +0200926 /* we want wait() to be interruptible */
927 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
928 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
929
930 setbuf(stdout, NULL);
931
932 opts_for_fsck = doall = notitle = 0;
933 devices = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000934 num_devices = 0;
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200935 new_args(); /* args[0] = NULL, will be replaced by fsck.<type> */
936 /* instance_list = NULL; - in bss, so already zeroed */
937
Denys Vlasenko66837452009-09-13 05:52:03 +0200938 while (*++argv) {
939 int j;
940 int optpos;
941 char *options;
942 char *arg = *argv;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000943
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000944 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000945 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000946// FIXME: must check that arg is a blkdev, or resolve
947// "/path", "UUID=xxx" or "LABEL=xxx" into block device name
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000948// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000949 devices = xrealloc_vector(devices, 2, num_devices);
Denys Vlasenko66837452009-09-13 05:52:03 +0200950 devices[num_devices++] = arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000951 continue;
952 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000953
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000954 if (arg[0] != '-' || opts_for_fsck) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200955 *new_args() = arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000956 continue;
957 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000958
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000959 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
960 opts_for_fsck = 1;
961 continue;
962 }
963
964 optpos = 0;
965 options = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000966 for (j = 1; arg[j]; j++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000967 switch (arg[j]) {
968 case 'A':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000969 doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000970 break;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000971#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000972 case 'C':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000973 progress = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000974 if (arg[++j]) { /* -Cn */
Denys Vlasenko77832482010-08-12 14:14:45 +0200975 progress_fd = xatoi_positive(&arg[j]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000976 goto next_arg;
977 }
978 /* -C n */
Denys Vlasenko66837452009-09-13 05:52:03 +0200979 if (!*++argv)
980 bb_show_usage();
Denys Vlasenko77832482010-08-12 14:14:45 +0200981 progress_fd = xatoi_positive(*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000982 goto next_arg;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000983#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000984 case 'V':
985 verbose++;
986 break;
987 case 'N':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000988 noexecute = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000989 break;
990 case 'R':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000991 skip_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000992 break;
993 case 'T':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000994 notitle = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000995 break;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000996/* case 'M':
997 like_mount = 1;
998 break; */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000999 case 'P':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001000 parallel_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001001 break;
1002 case 's':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001003 serialize = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001004 break;
1005 case 't':
1006 if (fstype)
1007 bb_show_usage();
1008 if (arg[++j])
1009 tmp = &arg[j];
Denys Vlasenko66837452009-09-13 05:52:03 +02001010 else if (*++argv)
1011 tmp = *argv;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001012 else
1013 bb_show_usage();
1014 fstype = xstrdup(tmp);
1015 compile_fs_type(fstype);
1016 goto next_arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001017 case '?':
1018 bb_show_usage();
1019 break;
1020 default:
1021 optpos++;
1022 /* one extra for '\0' */
1023 options = xrealloc(options, optpos + 2);
1024 options[optpos] = arg[j];
1025 break;
1026 }
1027 }
1028 next_arg:
1029 if (optpos) {
1030 options[0] = '-';
1031 options[optpos + 1] = '\0';
Denys Vlasenko7649bd02009-09-13 05:52:46 +02001032 *new_args() = options;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001033 }
1034 }
1035 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001036 force_all_parallel = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001037 tmp = getenv("FSCK_MAX_INST");
1038 if (tmp)
1039 max_running = xatoi(tmp);
Denys Vlasenko7649bd02009-09-13 05:52:46 +02001040 new_args(); /* args[num_args - 2] will be replaced by <device> */
1041 new_args(); /* args[num_args - 1] is the last, NULL element */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001042
1043 if (!notitle)
1044 puts("fsck (busybox "BB_VER", "BB_BT")");
1045
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001046 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1047 * so we are scanning it anyway */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001048 fstab = getenv("FSTAB_FILE");
1049 if (!fstab)
1050 fstab = "/etc/fstab";
1051 load_fs_info(fstab);
1052
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001053 /*interactive = (num_devices == 1) | serialize;*/
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001054
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001055 if (num_devices == 0)
1056 /*interactive =*/ serialize = doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001057 if (doall)
1058 return check_all();
1059
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001060 status = 0;
Denis Vlasenko0de93752006-12-26 02:51:29 +00001061 for (i = 0; i < num_devices; i++) {
Denys Vlasenko66837452009-09-13 05:52:03 +02001062 if (bb_got_signal) {
1063 kill_all_if_got_signal();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001064 break;
1065 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001066
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001067 fs = lookup(devices[i]);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001068 if (!fs)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001069 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1070 fsck_device(fs /*, interactive */);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001071
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001072 if (serialize
1073 || (max_running && (num_running >= max_running))
1074 ) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001075 int exit_status = wait_one(0);
1076 if (exit_status >= 0)
1077 status |= exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001078 if (verbose > 1)
1079 puts("----------------------------------");
1080 }
1081 }
1082 status |= wait_many(FLAG_WAIT_ALL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001083 return status;
1084}