blob: 59514a1a6c629c6bd63d71c8c6befd92a76da76d [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 */
Denys Vlasenko000eda42015-10-18 22:40:23 +020036//config:config FSCK
37//config: bool "fsck"
38//config: default y
39//config: help
40//config: fsck is used to check and optionally repair one or more filesystems.
41//config: In actuality, fsck is simply a front-end for the various file system
42//config: checkers (fsck.fstype) available under Linux.
43
44//applet:IF_FSCK(APPLET(fsck, BB_DIR_SBIN, BB_SUID_DROP))
45
46//kbuild:lib-$(CONFIG_FSCK) += fsck.o
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000047
Pere Orga6a3e01d2011-04-01 22:56:30 +020048//usage:#define fsck_trivial_usage
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +020049//usage: "[-ANPRTV] [-t FSTYPE] [FS_OPTS] [BLOCKDEV]..."
Pere Orga6a3e01d2011-04-01 22:56:30 +020050//usage:#define fsck_full_usage "\n\n"
51//usage: "Check and repair filesystems\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020052//usage: "\n -A Walk /etc/fstab and check all filesystems"
53//usage: "\n -N Don't execute, just show what would be done"
54//usage: "\n -P With -A, check filesystems in parallel"
55//usage: "\n -R With -A, skip the root filesystem"
56//usage: "\n -T Don't show title on startup"
57//usage: "\n -V Verbose"
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +020058//DO_PROGRESS_INDICATOR is off:
59////usage: "\n -C FD Write status information to specified file descriptor"
Pere Orga6a3e01d2011-04-01 22:56:30 +020060//usage: "\n -t TYPE List of filesystem types to check"
61
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000062#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020063#include "common_bufsiz.h"
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000064
Denis Vlasenkoa55bd052008-03-17 08:59:19 +000065/* "progress indicator" code is somewhat buggy and ext[23] specific.
66 * We should be filesystem agnostic. IOW: there should be a well-defined
67 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
68#define DO_PROGRESS_INDICATOR 0
69
Denys Vlasenko0d7e2e72009-11-15 02:36:49 +010070/* fsck 1.41.4 (27-Jan-2009) manpage says:
71 * 0 - No errors
72 * 1 - File system errors corrected
73 * 2 - System should be rebooted
74 * 4 - File system errors left uncorrected
75 * 8 - Operational error
76 * 16 - Usage or syntax error
77 * 32 - Fsck canceled by user request
78 * 128 - Shared library error
79 */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000080#define EXIT_OK 0
81#define EXIT_NONDESTRUCT 1
82#define EXIT_DESTRUCT 2
83#define EXIT_UNCORRECTED 4
84#define EXIT_ERROR 8
85#define EXIT_USAGE 16
86#define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
87
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000088/*
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +000089 * Internal structure for mount table entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000090 */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000091struct fs_info {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +000092 struct fs_info *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000093 char *device;
94 char *mountpt;
95 char *type;
96 char *opts;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000097 int passno;
98 int flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000099};
100
101#define FLAG_DONE 1
102#define FLAG_PROGRESS 2
103/*
104 * Structure to allow exit codes to be stored
105 */
106struct fsck_instance {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000107 struct fsck_instance *next;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000108 int pid;
109 int flags;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000110#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000111 time_t start_time;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000112#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000113 char *prog;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000114 char *device;
115 char *base_device; /* /dev/hda for /dev/hdaN etc */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000116};
117
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000118static const char ignored_types[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000119 "ignore\0"
120 "iso9660\0"
121 "nfs\0"
122 "proc\0"
123 "sw\0"
124 "swap\0"
125 "tmpfs\0"
126 "devpts\0";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000127
Denis Vlasenko0de93752006-12-26 02:51:29 +0000128#if 0
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000129static const char really_wanted[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000130 "minix\0"
131 "ext2\0"
132 "ext3\0"
133 "jfs\0"
134 "reiserfs\0"
135 "xiafs\0"
136 "xfs\0";
Denis Vlasenko0de93752006-12-26 02:51:29 +0000137#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000138
139#define BASE_MD "/dev/md"
140
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200141struct globals {
142 char **args;
143 int num_args;
144 int verbose;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000145
146#define FS_TYPE_FLAG_NORMAL 0
147#define FS_TYPE_FLAG_OPT 1
148#define FS_TYPE_FLAG_NEGOPT 2
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200149 char **fs_type_list;
150 uint8_t *fs_type_flag;
151 smallint fs_type_negated;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000152
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200153 smallint noexecute;
154 smallint serialize;
155 smallint skip_root;
156 /* smallint like_mount; */
157 smallint parallel_root;
158 smallint force_all_parallel;
159 smallint kill_sent;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000160
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000161#if DO_PROGRESS_INDICATOR
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200162 smallint progress;
163 int progress_fd;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000164#endif
165
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200166 int num_running;
167 int max_running;
168 char *fstype;
169 struct fs_info *filesys_info;
170 struct fs_info *filesys_last;
171 struct fsck_instance *instance_list;
172} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200173#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200174#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200175 setup_common_bufsiz(); \
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200176 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
177} while (0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000178
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000179/*
180 * Return the "base device" given a particular device; this is used to
181 * assure that we only fsck one partition on a particular drive at any
182 * one time. Otherwise, the disk heads will be seeking all over the
183 * place. If the base device cannot be determined, return NULL.
184 *
185 * The base_device() function returns an allocated string which must
186 * be freed.
187 */
188#if ENABLE_FEATURE_DEVFS
189/*
190 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
191 * pathames.
192 */
193static const char *const devfs_hier[] = {
194 "host", "bus", "target", "lun", NULL
195};
196#endif
197
198static char *base_device(const char *device)
199{
200 char *str, *cp;
201#if ENABLE_FEATURE_DEVFS
202 const char *const *hier;
203 const char *disk;
204 int len;
205#endif
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200206 str = xstrdup(device);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000207
Denys Vlasenkof8d8aa12010-04-06 18:50:05 +0200208 /* Skip over "/dev/"; if it's not present, give up */
209 cp = skip_dev_pfx(str);
210 if (cp == str)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000211 goto errout;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000212
213 /*
214 * For md devices, we treat them all as if they were all
215 * on one disk, since we don't know how to parallelize them.
216 */
217 if (cp[0] == 'm' && cp[1] == 'd') {
218 cp[2] = 0;
219 return str;
220 }
221
222 /* Handle DAC 960 devices */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100223 if (is_prefixed_with(cp, "rd/")) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000224 cp += 3;
225 if (cp[0] != 'c' || !isdigit(cp[1])
226 || cp[2] != 'd' || !isdigit(cp[3]))
227 goto errout;
228 cp[4] = 0;
229 return str;
230 }
231
232 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
233 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
234 cp += 2;
235 /* If there's a single number after /dev/hd, skip it */
236 if (isdigit(*cp))
237 cp++;
238 /* What follows must be an alpha char, or give up */
239 if (!isalpha(*cp))
240 goto errout;
241 cp[1] = 0;
242 return str;
243 }
244
245#if ENABLE_FEATURE_DEVFS
246 /* Now let's handle devfs (ugh) names */
247 len = 0;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100248 if (is_prefixed_with(cp, "ide/"))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000249 len = 4;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100250 if (is_prefixed_with(cp, "scsi/"))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000251 len = 5;
252 if (len) {
253 cp += len;
254 /*
255 * Now we proceed down the expected devfs hierarchy.
256 * i.e., .../host1/bus2/target3/lun4/...
257 * If we don't find the expected token, followed by
258 * some number of digits at each level, abort.
259 */
260 for (hier = devfs_hier; *hier; hier++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100261 cp = is_prefixed_with(cp, *hier);
262 if (!cp)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000263 goto errout;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100264 while (*cp != '/' && *cp != '\0') {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000265 if (!isdigit(*cp))
266 goto errout;
267 cp++;
268 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100269//FIXME: what if *cp = '\0' now? cp++ moves past it!!!
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000270 cp++;
271 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100272 cp[-1] = '\0';
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000273 return str;
274 }
275
276 /* Now handle devfs /dev/disc or /dev/disk names */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100277 disk = NULL;
278 if (is_prefixed_with(cp, "discs/"))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000279 disk = "disc";
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100280 else if (is_prefixed_with(cp, "disks/"))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000281 disk = "disk";
282 if (disk) {
283 cp += 6;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100284 cp = is_prefixed_with(cp, disk);
285 if (!cp)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000286 goto errout;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100287 while (*cp != '/' && *cp != '\0') {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000288 if (!isdigit(*cp))
289 goto errout;
290 cp++;
291 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100292 *cp = '\0';
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000293 return str;
294 }
295#endif
296 errout:
297 free(str);
298 return NULL;
299}
300
301static void free_instance(struct fsck_instance *p)
302{
303 free(p->prog);
304 free(p->device);
305 free(p->base_device);
306 free(p);
307}
308
309static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
310 const char *type, const char *opts,
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000311 int passno)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000312{
313 struct fs_info *fs;
314
315 fs = xzalloc(sizeof(*fs));
316 fs->device = xstrdup(device);
317 fs->mountpt = xstrdup(mntpnt);
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000318 if (strchr(type, ','))
319 type = (char *)"auto";
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000320 fs->type = xstrdup(type);
321 fs->opts = xstrdup(opts ? opts : "");
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000322 fs->passno = passno < 0 ? 1 : passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000323 /*fs->flags = 0; */
324 /*fs->next = NULL; */
325
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200326 if (!G.filesys_info)
327 G.filesys_info = fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000328 else
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200329 G.filesys_last->next = fs;
330 G.filesys_last = fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000331
332 return fs;
333}
334
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000335/* Load the filesystem database from /etc/fstab */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000336static void load_fs_info(const char *filename)
337{
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000338 FILE *fstab;
339 struct mntent mte;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200340 char buf[1024];
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000341
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000342 fstab = setmntent(filename, "r");
343 if (!fstab) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100344 bb_perror_msg("can't read '%s'", filename);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000345 return;
346 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000347
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000348 // Loop through entries
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200349 while (getmntent_r(fstab, &mte, buf, sizeof(buf))) {
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200350 //bb_error_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000351 // mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000352 // mte.mnt_passno);
Denys Vlasenko60a94142011-05-13 20:57:01 +0200353 create_fs_device(mte.mnt_fsname, mte.mnt_dir,
Denis Vlasenkoea7c9b32008-09-25 10:39:10 +0000354 mte.mnt_type, mte.mnt_opts,
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000355 mte.mnt_passno);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000356 }
Denis Vlasenkob9f5d592008-08-20 02:38:48 +0000357 endmntent(fstab);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000358}
359
360/* Lookup filesys in /etc/fstab and return the corresponding entry. */
361static struct fs_info *lookup(char *filesys)
362{
363 struct fs_info *fs;
364
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200365 for (fs = G.filesys_info; fs; fs = fs->next) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000366 if (strcmp(filesys, fs->device) == 0
367 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
368 )
369 break;
370 }
371
372 return fs;
373}
374
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000375#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000376static int progress_active(void)
377{
378 struct fsck_instance *inst;
379
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200380 for (inst = G.instance_list; inst; inst = inst->next) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000381 if (inst->flags & FLAG_DONE)
382 continue;
383 if (inst->flags & FLAG_PROGRESS)
384 return 1;
385 }
386 return 0;
387}
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000388#endif
389
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000390
391/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000392 * Send a signal to all outstanding fsck child processes
393 */
Denys Vlasenko66837452009-09-13 05:52:03 +0200394static void kill_all_if_got_signal(void)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000395{
396 struct fsck_instance *inst;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000397
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200398 if (!bb_got_signal || G.kill_sent)
Denis Vlasenko0de93752006-12-26 02:51:29 +0000399 return;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000400
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200401 for (inst = G.instance_list; inst; inst = inst->next) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000402 if (inst->flags & FLAG_DONE)
403 continue;
Denis Vlasenko0de93752006-12-26 02:51:29 +0000404 kill(inst->pid, SIGTERM);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000405 }
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200406 G.kill_sent = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000407}
408
409/*
410 * Wait for one child process to exit; when it does, unlink it from
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000411 * the list of executing child processes, free, and return its exit status.
412 * If there is no exited child, return -1.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000413 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000414static int wait_one(int flags)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000415{
416 int status;
417 int sig;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000418 struct fsck_instance *inst, *prev;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000419 pid_t pid;
420
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200421 if (!G.instance_list)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000422 return -1;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200423 /* if (G.noexecute) { already returned -1; } */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000424
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000425 while (1) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000426 pid = waitpid(-1, &status, flags);
Denys Vlasenko66837452009-09-13 05:52:03 +0200427 kill_all_if_got_signal();
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000428 if (pid == 0) /* flags == WNOHANG and no children exited */
429 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000430 if (pid < 0) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000431 if (errno == EINTR)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000432 continue;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000433 if (errno == ECHILD) { /* paranoia */
434 bb_error_msg("wait: no more children");
435 return -1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000436 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000437 bb_perror_msg("wait");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000438 continue;
439 }
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000440 prev = NULL;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200441 inst = G.instance_list;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000442 do {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000443 if (inst->pid == pid)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000444 goto child_died;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000445 prev = inst;
446 inst = inst->next;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000447 } while (inst);
448 }
449 child_died:
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000450
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200451 status = WEXITSTATUS(status);
452 if (WIFSIGNALED(status)) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000453 sig = WTERMSIG(status);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000454 status = EXIT_UNCORRECTED;
455 if (sig != SIGINT) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000456 printf("Warning: %s %s terminated "
457 "by signal %d\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000458 inst->prog, inst->device, sig);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000459 status = EXIT_ERROR;
460 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000461 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000462
463#if DO_PROGRESS_INDICATOR
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000464 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000465 struct fsck_instance *inst2;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200466 for (inst2 = G.instance_list; inst2; inst2 = inst2->next) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000467 if (inst2->flags & FLAG_DONE)
468 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000469 if (strcmp(inst2->type, "ext2") != 0
470 && strcmp(inst2->type, "ext3") != 0
471 ) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000472 continue;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000473 }
474 /* ext[23], we will send USR1
475 * (request to start displaying progress bar)
476 *
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000477 * If we've just started the fsck, wait a tiny
478 * bit before sending the kill, to give it
479 * time to set up the signal handler
480 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000481 if (inst2->start_time >= time(NULL) - 1)
482 sleep(1);
483 kill(inst2->pid, SIGUSR1);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000484 inst2->flags |= FLAG_PROGRESS;
485 break;
486 }
487 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000488#endif
489
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000490 if (prev)
491 prev->next = inst->next;
492 else
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200493 G.instance_list = inst->next;
494 if (G.verbose > 1)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000495 printf("Finished with %s (exit status %d)\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100496 inst->device, status);
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200497 G.num_running--;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000498 free_instance(inst);
499
500 return status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000501}
502
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000503/*
504 * Wait until all executing child processes have exited; return the
505 * logical OR of all of their exit code values.
506 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000507#define FLAG_WAIT_ALL 0
508#define FLAG_WAIT_ATLEAST_ONE WNOHANG
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000509static int wait_many(int flags)
510{
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000511 int exit_status;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000512 int global_status = 0;
513 int wait_flags = 0;
514
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000515 while ((exit_status = wait_one(wait_flags)) != -1) {
516 global_status |= exit_status;
517 wait_flags |= flags;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000518 }
519 return global_status;
520}
521
522/*
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000523 * Execute a particular fsck program, and link it into the list of
524 * child processes we are waiting for.
525 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000526static void execute(const char *type, const char *device,
527 const char *mntpt /*, int interactive */)
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000528{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000529 int i;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000530 struct fsck_instance *inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000531 pid_t pid;
532
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200533 G.args[0] = xasprintf("fsck.%s", type);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000534
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000535#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000536 if (progress && !progress_active()) {
537 if (strcmp(type, "ext2") == 0
538 || strcmp(type, "ext3") == 0
539 ) {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200540 G.args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000541 inst->flags |= FLAG_PROGRESS;
542 }
543 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000544#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000545
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200546 G.args[G.num_args - 2] = (char*)device;
547 /* G.args[G.num_args - 1] = NULL; - already is */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000548
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200549 if (G.verbose || G.noexecute) {
550 printf("[%s (%d) -- %s]", G.args[0], G.num_running,
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000551 mntpt ? mntpt : device);
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200552 for (i = 0; G.args[i]; i++)
553 printf(" %s", G.args[i]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000554 bb_putchar('\n');
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000555 }
556
557 /* Fork and execute the correct program. */
558 pid = -1;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200559 if (!G.noexecute) {
560 pid = spawn(G.args);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000561 if (pid < 0)
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200562 bb_simple_perror_msg(G.args[0]);
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000563 }
564
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000565#if DO_PROGRESS_INDICATOR
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200566 free(G.args[XXX]);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000567#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000568
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000569 /* No child, so don't record an instance */
570 if (pid <= 0) {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200571 free(G.args[0]);
Denis Vlasenko30eb3192008-02-02 18:54:58 +0000572 return;
573 }
574
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000575 inst = xzalloc(sizeof(*inst));
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000576 inst->pid = pid;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200577 inst->prog = G.args[0];
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000578 inst->device = xstrdup(device);
579 inst->base_device = base_device(device);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000580#if DO_PROGRESS_INDICATOR
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000581 inst->start_time = time(NULL);
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000582#endif
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000583
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000584 /* Add to the list of running fsck's.
585 * (was adding to the end, but adding to the front is simpler...) */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200586 inst->next = G.instance_list;
587 G.instance_list = inst;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000588}
589
590/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000591 * Run the fsck program on a particular device
592 *
593 * If the type is specified using -t, and it isn't prefixed with "no"
594 * (as in "noext2") and only one filesystem type is specified, then
595 * use that type regardless of what is specified in /etc/fstab.
596 *
597 * If the type isn't specified by the user, then use either the type
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000598 * specified in /etc/fstab, or "auto".
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000599 */
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000600static void fsck_device(struct fs_info *fs /*, int interactive */)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000601{
602 const char *type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000603
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000604 if (strcmp(fs->type, "auto") != 0) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000605 type = fs->type;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200606 if (G.verbose > 2)
Denys Vlasenkoc418f482016-03-30 16:30:24 +0200607 printf("using filesystem type '%s' %s\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000608 type, "from fstab");
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200609 } else if (G.fstype
610 && (G.fstype[0] != 'n' || G.fstype[1] != 'o') /* != "no" */
611 && !is_prefixed_with(G.fstype, "opts=")
612 && !is_prefixed_with(G.fstype, "loop")
613 && !strchr(G.fstype, ',')
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000614 ) {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200615 type = G.fstype;
616 if (G.verbose > 2)
Denys Vlasenkoc418f482016-03-30 16:30:24 +0200617 printf("using filesystem type '%s' %s\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000618 type, "from -t");
619 } else {
620 type = "auto";
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200621 if (G.verbose > 2)
Denys Vlasenkoc418f482016-03-30 16:30:24 +0200622 printf("using filesystem type '%s' %s\n",
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000623 type, "(default)");
624 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000625
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200626 G.num_running++;
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000627 execute(type, fs->device, fs->mountpt /*, interactive */);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000628}
629
630/*
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000631 * Returns TRUE if a partition on the same disk is already being
632 * checked.
633 */
634static int device_already_active(char *device)
635{
636 struct fsck_instance *inst;
637 char *base;
638
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200639 if (G.force_all_parallel)
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000640 return 0;
641
642#ifdef BASE_MD
643 /* Don't check a soft raid disk with any other disk */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200644 if (G.instance_list
645 && (is_prefixed_with(G.instance_list->device, BASE_MD)
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100646 || is_prefixed_with(device, BASE_MD))
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000647 ) {
648 return 1;
649 }
650#endif
651
652 base = base_device(device);
653 /*
654 * If we don't know the base device, assume that the device is
655 * already active if there are any fsck instances running.
656 */
657 if (!base)
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200658 return (G.instance_list != NULL);
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000659
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200660 for (inst = G.instance_list; inst; inst = inst->next) {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000661 if (!inst->base_device || !strcmp(base, inst->base_device)) {
662 free(base);
663 return 1;
664 }
665 }
666
667 free(base);
668 return 0;
669}
670
671/*
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000672 * This function returns true if a particular option appears in a
673 * comma-delimited options list
674 */
675static int opt_in_list(char *opt, char *optlist)
676{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000677 char *s;
678 int len;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000679
680 if (!optlist)
681 return 0;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000682
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000683 len = strlen(opt);
684 s = optlist - 1;
685 while (1) {
686 s = strstr(s + 1, opt);
687 if (!s)
688 return 0;
689 /* neither "opt.." nor "xxx,opt.."? */
690 if (s != optlist && s[-1] != ',')
691 continue;
692 /* neither "..opt" nor "..opt,xxx"? */
693 if (s[len] != '\0' && s[len] != ',')
694 continue;
695 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000696 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000697}
698
699/* See if the filesystem matches the criteria given by the -t option */
700static int fs_match(struct fs_info *fs)
701{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000702 int n, ret, checked_type;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000703 char *cp;
704
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200705 if (!G.fs_type_list)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000706 return 1;
707
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000708 ret = 0;
709 checked_type = 0;
710 n = 0;
711 while (1) {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200712 cp = G.fs_type_list[n];
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000713 if (!cp)
714 break;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200715 switch (G.fs_type_flag[n]) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000716 case FS_TYPE_FLAG_NORMAL:
717 checked_type++;
718 if (strcmp(cp, fs->type) == 0)
719 ret = 1;
720 break;
721 case FS_TYPE_FLAG_NEGOPT:
722 if (opt_in_list(cp, fs->opts))
723 return 0;
724 break;
725 case FS_TYPE_FLAG_OPT:
726 if (!opt_in_list(cp, fs->opts))
727 return 0;
728 break;
729 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000730 n++;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000731 }
732 if (checked_type == 0)
733 return 1;
734
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200735 return (G.fs_type_negated ? !ret : ret);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000736}
737
738/* Check if we should ignore this filesystem. */
739static int ignore(struct fs_info *fs)
740{
741 /*
742 * If the pass number is 0, ignore it.
743 */
744 if (fs->passno == 0)
745 return 1;
746
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000747 /*
748 * If a specific fstype is specified, and it doesn't match,
749 * ignore it.
750 */
751 if (!fs_match(fs))
752 return 1;
753
754 /* Are we ignoring this type? */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000755 if (index_in_strings(ignored_types, fs->type) >= 0)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000756 return 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000757
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000758 /* We can and want to check this file system type. */
759 return 0;
760}
761
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000762/* Check all file systems, using the /etc/fstab table. */
763static int check_all(void)
764{
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000765 struct fs_info *fs;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000766 int status = EXIT_OK;
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000767 smallint not_done_yet;
768 smallint pass_done;
769 int passno;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000770
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200771 if (G.verbose)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000772 puts("Checking all filesystems");
773
774 /*
775 * Do an initial scan over the filesystem; mark filesystems
776 * which should be ignored as done, and resolve any "auto"
777 * filesystem types (done as a side-effect of calling ignore()).
778 */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200779 for (fs = G.filesys_info; fs; fs = fs->next)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000780 if (ignore(fs))
781 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000782
783 /*
784 * Find and check the root filesystem.
785 */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200786 if (!G.parallel_root) {
787 for (fs = G.filesys_info; fs; fs = fs->next) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000788 if (LONE_CHAR(fs->mountpt, '/')) {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200789 if (!G.skip_root && !ignore(fs)) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000790 fsck_device(fs /*, 1*/);
791 status |= wait_many(FLAG_WAIT_ALL);
792 if (status > EXIT_NONDESTRUCT)
793 return status;
794 }
795 fs->flags |= FLAG_DONE;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000796 break;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000797 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000798 }
799 }
800 /*
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000801 * This is for the bone-headed user who has root
802 * filesystem listed twice.
803 * "Skip root" will skip _all_ root entries.
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000804 */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200805 if (G.skip_root)
806 for (fs = G.filesys_info; fs; fs = fs->next)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000807 if (LONE_CHAR(fs->mountpt, '/'))
808 fs->flags |= FLAG_DONE;
809
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000810 not_done_yet = 1;
811 passno = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000812 while (not_done_yet) {
813 not_done_yet = 0;
814 pass_done = 1;
815
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200816 for (fs = G.filesys_info; fs; fs = fs->next) {
Denys Vlasenko66837452009-09-13 05:52:03 +0200817 if (bb_got_signal)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000818 break;
819 if (fs->flags & FLAG_DONE)
820 continue;
821 /*
822 * If the filesystem's pass number is higher
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000823 * than the current pass number, then we didn't
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000824 * do it yet.
825 */
826 if (fs->passno > passno) {
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000827 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000828 continue;
829 }
830 /*
831 * If a filesystem on a particular device has
832 * already been spawned, then we need to defer
833 * this to another pass.
834 */
835 if (device_already_active(fs->device)) {
836 pass_done = 0;
837 continue;
838 }
839 /*
840 * Spawn off the fsck process
841 */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200842 fsck_device(fs /*, G.serialize*/);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000843 fs->flags |= FLAG_DONE;
844
845 /*
846 * Only do one filesystem at a time, or if we
847 * have a limit on the number of fsck's extant
848 * at one time, apply that limit.
849 */
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200850 if (G.serialize
851 || (G.num_running >= G.max_running)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000852 ) {
853 pass_done = 0;
854 break;
855 }
856 }
Denys Vlasenko66837452009-09-13 05:52:03 +0200857 if (bb_got_signal)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000858 break;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200859 if (G.verbose > 1)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000860 printf("--waiting-- (pass %d)\n", passno);
861 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
Denys Vlasenko69675782013-01-14 01:34:48 +0100862 FLAG_WAIT_ATLEAST_ONE);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000863 if (pass_done) {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200864 if (G.verbose > 1)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000865 puts("----------------------------------");
866 passno++;
867 } else
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000868 not_done_yet = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000869 }
Denys Vlasenko66837452009-09-13 05:52:03 +0200870 kill_all_if_got_signal();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000871 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
872 return status;
873}
874
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000875/*
876 * Deal with the fsck -t argument.
877 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
878 * Why here we require "-t novfat,nonfs" ??
879 */
880static void compile_fs_type(char *fs_type)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000881{
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000882 char *s;
883 int num = 2;
884 smallint negate;
885
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000886 s = fs_type;
887 while ((s = strchr(s, ','))) {
888 num++;
889 s++;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000890 }
891
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200892 G.fs_type_list = xzalloc(num * sizeof(G.fs_type_list[0]));
893 G.fs_type_flag = xzalloc(num * sizeof(G.fs_type_flag[0]));
894 G.fs_type_negated = -1; /* not yet known is it negated or not */
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000895
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000896 num = 0;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000897 s = fs_type;
898 while (1) {
899 char *comma;
900
901 negate = 0;
902 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
903 s += 2;
904 negate = 1;
905 } else if (s[0] == '!') {
906 s++;
907 negate = 1;
908 }
Denis Vlasenkoe18a2932007-01-19 02:03:14 +0000909
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000910 if (strcmp(s, "loop") == 0)
911 /* loop is really short-hand for opts=loop */
912 goto loop_special_case;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100913 if (is_prefixed_with(s, "opts=")) {
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000914 s += 5;
915 loop_special_case:
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200916 G.fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000917 } else {
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200918 if (G.fs_type_negated == -1)
919 G.fs_type_negated = negate;
920 if (G.fs_type_negated != negate)
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000921 bb_error_msg_and_die(
922"either all or none of the filesystem types passed to -t must be prefixed "
923"with 'no' or '!'");
924 }
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200925 comma = strchrnul(s, ',');
926 G.fs_type_list[num++] = xstrndup(s, comma-s);
927 if (*comma == '\0')
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000928 break;
929 s = comma + 1;
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000930 }
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000931}
932
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200933static char **new_args(void)
934{
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200935 G.args = xrealloc_vector(G.args, 2, G.num_args);
936 return &G.args[G.num_args++];
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200937}
938
Denys Vlasenko66837452009-09-13 05:52:03 +0200939int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
940int fsck_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000941{
Denys Vlasenko66837452009-09-13 05:52:03 +0200942 int i, status;
943 /*int interactive;*/
944 struct fs_info *fs;
945 const char *fstab;
946 char *tmp;
947 char **devices;
948 int num_devices;
949 smallint opts_for_fsck;
950 smallint doall;
951 smallint notitle;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000952
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200953 INIT_G();
954
Denys Vlasenko66837452009-09-13 05:52:03 +0200955 /* we want wait() to be interruptible */
956 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
957 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
958
959 setbuf(stdout, NULL);
960
961 opts_for_fsck = doall = notitle = 0;
962 devices = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000963 num_devices = 0;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +0200964 new_args(); /* G.args[0] = NULL, will be replaced by fsck.<type> */
965 /* G.instance_list = NULL; - in bss, so already zeroed */
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200966
Denys Vlasenko66837452009-09-13 05:52:03 +0200967 while (*++argv) {
968 int j;
969 int optpos;
970 char *options;
971 char *arg = *argv;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000972
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000973 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000974 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
Denis Vlasenko1abf91a2007-01-19 02:02:33 +0000975// FIXME: must check that arg is a blkdev, or resolve
976// "/path", "UUID=xxx" or "LABEL=xxx" into block device name
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000977// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000978 devices = xrealloc_vector(devices, 2, num_devices);
Denys Vlasenko66837452009-09-13 05:52:03 +0200979 devices[num_devices++] = arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000980 continue;
981 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000982
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000983 if (arg[0] != '-' || opts_for_fsck) {
Denys Vlasenko7649bd02009-09-13 05:52:46 +0200984 *new_args() = arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000985 continue;
986 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +0000987
Denis Vlasenkoa55bd052008-03-17 08:59:19 +0000988 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
989 opts_for_fsck = 1;
990 continue;
991 }
992
993 optpos = 0;
994 options = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000995 for (j = 1; arg[j]; j++) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000996 switch (arg[j]) {
997 case 'A':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +0000998 doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000999 break;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001000#if DO_PROGRESS_INDICATOR
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001001 case 'C':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001002 progress = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001003 if (arg[++j]) { /* -Cn */
Denys Vlasenko77832482010-08-12 14:14:45 +02001004 progress_fd = xatoi_positive(&arg[j]);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001005 goto next_arg;
1006 }
1007 /* -C n */
Denys Vlasenko66837452009-09-13 05:52:03 +02001008 if (!*++argv)
1009 bb_show_usage();
Denys Vlasenko77832482010-08-12 14:14:45 +02001010 progress_fd = xatoi_positive(*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001011 goto next_arg;
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001012#endif
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001013 case 'V':
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001014 G.verbose++;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001015 break;
1016 case 'N':
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001017 G.noexecute = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001018 break;
1019 case 'R':
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001020 G.skip_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001021 break;
1022 case 'T':
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001023 notitle = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001024 break;
Denis Vlasenkoa0e701d2007-01-19 02:01:19 +00001025/* case 'M':
1026 like_mount = 1;
1027 break; */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001028 case 'P':
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001029 G.parallel_root = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001030 break;
1031 case 's':
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001032 G.serialize = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001033 break;
1034 case 't':
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001035 if (G.fstype)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001036 bb_show_usage();
1037 if (arg[++j])
1038 tmp = &arg[j];
Denys Vlasenko66837452009-09-13 05:52:03 +02001039 else if (*++argv)
1040 tmp = *argv;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001041 else
1042 bb_show_usage();
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001043 G.fstype = xstrdup(tmp);
1044 compile_fs_type(G.fstype);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001045 goto next_arg;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001046 case '?':
1047 bb_show_usage();
1048 break;
1049 default:
1050 optpos++;
1051 /* one extra for '\0' */
1052 options = xrealloc(options, optpos + 2);
1053 options[optpos] = arg[j];
1054 break;
1055 }
1056 }
1057 next_arg:
1058 if (optpos) {
1059 options[0] = '-';
1060 options[optpos + 1] = '\0';
Denys Vlasenko7649bd02009-09-13 05:52:46 +02001061 *new_args() = options;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001062 }
1063 }
1064 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001065 G.force_all_parallel = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001066 tmp = getenv("FSCK_MAX_INST");
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001067 G.max_running = INT_MAX;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001068 if (tmp)
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001069 G.max_running = xatoi(tmp);
1070 new_args(); /* G.args[G.num_args - 2] will be replaced by <device> */
1071 new_args(); /* G.args[G.num_args - 1] is the last, NULL element */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001072
1073 if (!notitle)
1074 puts("fsck (busybox "BB_VER", "BB_BT")");
1075
Denis Vlasenko1abf91a2007-01-19 02:02:33 +00001076 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1077 * so we are scanning it anyway */
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001078 fstab = getenv("FSTAB_FILE");
1079 if (!fstab)
1080 fstab = "/etc/fstab";
1081 load_fs_info(fstab);
1082
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001083 /*interactive = (num_devices == 1) | G.serialize;*/
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001084
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001085 if (num_devices == 0)
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001086 /*interactive =*/ G.serialize = doall = 1;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001087 if (doall)
1088 return check_all();
1089
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001090 status = 0;
Denis Vlasenko0de93752006-12-26 02:51:29 +00001091 for (i = 0; i < num_devices; i++) {
Denys Vlasenko66837452009-09-13 05:52:03 +02001092 if (bb_got_signal) {
1093 kill_all_if_got_signal();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001094 break;
1095 }
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001096
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001097 fs = lookup(devices[i]);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001098 if (!fs)
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001099 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1100 fsck_device(fs /*, interactive */);
Denis Vlasenkof8c11aa2007-01-19 02:04:09 +00001101
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001102 if (G.serialize
1103 || (G.num_running >= G.max_running)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001104 ) {
Denis Vlasenkoa55bd052008-03-17 08:59:19 +00001105 int exit_status = wait_one(0);
1106 if (exit_status >= 0)
1107 status |= exit_status;
Denys Vlasenkoc4fb8c62015-10-19 02:24:14 +02001108 if (G.verbose > 1)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001109 puts("----------------------------------");
1110 }
1111 }
1112 status |= wait_many(FLAG_WAIT_ALL);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001113 return status;
1114}