fsck.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /* 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. *
  23. * Licensed under GPLv2, see file LICENSE in this source tree.
  24. */
  25. /* All filesystem specific hooks have been removed.
  26. * If filesystem cannot be determined, we will execute
  27. * "fsck.auto". Currently this also happens if you specify
  28. * UUID=xxx or LABEL=xxx as an object to check.
  29. * Detection code for that is also probably has to be in fsck.auto.
  30. *
  31. * In other words, this is _really_ is just a driver program which
  32. * spawns actual fsck.something for each filesystem to check.
  33. * It doesn't guess filesystem types from on-disk format.
  34. */
  35. //config:config FSCK
  36. //config: bool "fsck"
  37. //config: default y
  38. //config: help
  39. //config: fsck is used to check and optionally repair one or more filesystems.
  40. //config: In actuality, fsck is simply a front-end for the various file system
  41. //config: checkers (fsck.fstype) available under Linux.
  42. //applet:IF_FSCK(APPLET(fsck, BB_DIR_SBIN, BB_SUID_DROP))
  43. //kbuild:lib-$(CONFIG_FSCK) += fsck.o
  44. //usage:#define fsck_trivial_usage
  45. //usage: "[-ANPRTV] [-t FSTYPE] [FS_OPTS] [BLOCKDEV]..."
  46. //usage:#define fsck_full_usage "\n\n"
  47. //usage: "Check and repair filesystems\n"
  48. //usage: "\n -A Walk /etc/fstab and check all filesystems"
  49. //usage: "\n -N Don't execute, just show what would be done"
  50. //usage: "\n -P With -A, check filesystems in parallel"
  51. //usage: "\n -R With -A, skip the root filesystem"
  52. //usage: "\n -T Don't show title on startup"
  53. //usage: "\n -V Verbose"
  54. //DO_PROGRESS_INDICATOR is off:
  55. ////usage: "\n -C FD Write status information to specified file descriptor"
  56. //usage: "\n -t TYPE List of filesystem types to check"
  57. #include "libbb.h"
  58. #include "common_bufsiz.h"
  59. /* "progress indicator" code is somewhat buggy and ext[23] specific.
  60. * We should be filesystem agnostic. IOW: there should be a well-defined
  61. * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
  62. #define DO_PROGRESS_INDICATOR 0
  63. /* fsck 1.41.4 (27-Jan-2009) manpage says:
  64. * 0 - No errors
  65. * 1 - File system errors corrected
  66. * 2 - System should be rebooted
  67. * 4 - File system errors left uncorrected
  68. * 8 - Operational error
  69. * 16 - Usage or syntax error
  70. * 32 - Fsck canceled by user request
  71. * 128 - Shared library error
  72. */
  73. #define EXIT_OK 0
  74. #define EXIT_NONDESTRUCT 1
  75. #define EXIT_DESTRUCT 2
  76. #define EXIT_UNCORRECTED 4
  77. #define EXIT_ERROR 8
  78. #define EXIT_USAGE 16
  79. #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
  80. /*
  81. * Internal structure for mount table entries.
  82. */
  83. struct fs_info {
  84. struct fs_info *next;
  85. char *device;
  86. char *mountpt;
  87. char *type;
  88. char *opts;
  89. int passno;
  90. int flags;
  91. };
  92. #define FLAG_DONE 1
  93. #define FLAG_PROGRESS 2
  94. /*
  95. * Structure to allow exit codes to be stored
  96. */
  97. struct fsck_instance {
  98. struct fsck_instance *next;
  99. int pid;
  100. int flags;
  101. #if DO_PROGRESS_INDICATOR
  102. time_t start_time;
  103. #endif
  104. char *prog;
  105. char *device;
  106. char *base_device; /* /dev/hda for /dev/hdaN etc */
  107. };
  108. static const char ignored_types[] ALIGN1 =
  109. "ignore\0"
  110. "iso9660\0"
  111. "nfs\0"
  112. "proc\0"
  113. "sw\0"
  114. "swap\0"
  115. "tmpfs\0"
  116. "devpts\0";
  117. #if 0
  118. static const char really_wanted[] ALIGN1 =
  119. "minix\0"
  120. "ext2\0"
  121. "ext3\0"
  122. "jfs\0"
  123. "reiserfs\0"
  124. "xiafs\0"
  125. "xfs\0";
  126. #endif
  127. #define BASE_MD "/dev/md"
  128. struct globals {
  129. char **args;
  130. int num_args;
  131. int verbose;
  132. #define FS_TYPE_FLAG_NORMAL 0
  133. #define FS_TYPE_FLAG_OPT 1
  134. #define FS_TYPE_FLAG_NEGOPT 2
  135. char **fs_type_list;
  136. uint8_t *fs_type_flag;
  137. smallint fs_type_negated;
  138. smallint noexecute;
  139. smallint serialize;
  140. smallint skip_root;
  141. /* smallint like_mount; */
  142. smallint parallel_root;
  143. smallint force_all_parallel;
  144. smallint kill_sent;
  145. #if DO_PROGRESS_INDICATOR
  146. smallint progress;
  147. int progress_fd;
  148. #endif
  149. int num_running;
  150. int max_running;
  151. char *fstype;
  152. struct fs_info *filesys_info;
  153. struct fs_info *filesys_last;
  154. struct fsck_instance *instance_list;
  155. } FIX_ALIASING;
  156. #define G (*(struct globals*)bb_common_bufsiz1)
  157. #define INIT_G() do { \
  158. setup_common_bufsiz(); \
  159. BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
  160. } while (0)
  161. /*
  162. * Return the "base device" given a particular device; this is used to
  163. * assure that we only fsck one partition on a particular drive at any
  164. * one time. Otherwise, the disk heads will be seeking all over the
  165. * place. If the base device cannot be determined, return NULL.
  166. *
  167. * The base_device() function returns an allocated string which must
  168. * be freed.
  169. */
  170. #if ENABLE_FEATURE_DEVFS
  171. /*
  172. * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
  173. * pathames.
  174. */
  175. static const char *const devfs_hier[] = {
  176. "host", "bus", "target", "lun", NULL
  177. };
  178. #endif
  179. static char *base_device(const char *device)
  180. {
  181. char *str, *cp;
  182. #if ENABLE_FEATURE_DEVFS
  183. const char *const *hier;
  184. const char *disk;
  185. int len;
  186. #endif
  187. str = xstrdup(device);
  188. /* Skip over "/dev/"; if it's not present, give up */
  189. cp = skip_dev_pfx(str);
  190. if (cp == str)
  191. goto errout;
  192. /*
  193. * For md devices, we treat them all as if they were all
  194. * on one disk, since we don't know how to parallelize them.
  195. */
  196. if (cp[0] == 'm' && cp[1] == 'd') {
  197. cp[2] = 0;
  198. return str;
  199. }
  200. /* Handle DAC 960 devices */
  201. if (is_prefixed_with(cp, "rd/")) {
  202. cp += 3;
  203. if (cp[0] != 'c' || !isdigit(cp[1])
  204. || cp[2] != 'd' || !isdigit(cp[3]))
  205. goto errout;
  206. cp[4] = 0;
  207. return str;
  208. }
  209. /* Now let's handle /dev/hd* and /dev/sd* devices.... */
  210. if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
  211. cp += 2;
  212. /* If there's a single number after /dev/hd, skip it */
  213. if (isdigit(*cp))
  214. cp++;
  215. /* What follows must be an alpha char, or give up */
  216. if (!isalpha(*cp))
  217. goto errout;
  218. cp[1] = 0;
  219. return str;
  220. }
  221. #if ENABLE_FEATURE_DEVFS
  222. /* Now let's handle devfs (ugh) names */
  223. len = 0;
  224. if (is_prefixed_with(cp, "ide/"))
  225. len = 4;
  226. if (is_prefixed_with(cp, "scsi/"))
  227. len = 5;
  228. if (len) {
  229. cp += len;
  230. /*
  231. * Now we proceed down the expected devfs hierarchy.
  232. * i.e., .../host1/bus2/target3/lun4/...
  233. * If we don't find the expected token, followed by
  234. * some number of digits at each level, abort.
  235. */
  236. for (hier = devfs_hier; *hier; hier++) {
  237. cp = is_prefixed_with(cp, *hier);
  238. if (!cp)
  239. goto errout;
  240. while (*cp != '/' && *cp != '\0') {
  241. if (!isdigit(*cp))
  242. goto errout;
  243. cp++;
  244. }
  245. //FIXME: what if *cp = '\0' now? cp++ moves past it!!!
  246. cp++;
  247. }
  248. cp[-1] = '\0';
  249. return str;
  250. }
  251. /* Now handle devfs /dev/disc or /dev/disk names */
  252. disk = NULL;
  253. if (is_prefixed_with(cp, "discs/"))
  254. disk = "disc";
  255. else if (is_prefixed_with(cp, "disks/"))
  256. disk = "disk";
  257. if (disk) {
  258. cp += 6;
  259. cp = is_prefixed_with(cp, disk);
  260. if (!cp)
  261. goto errout;
  262. while (*cp != '/' && *cp != '\0') {
  263. if (!isdigit(*cp))
  264. goto errout;
  265. cp++;
  266. }
  267. *cp = '\0';
  268. return str;
  269. }
  270. #endif
  271. errout:
  272. free(str);
  273. return NULL;
  274. }
  275. static void free_instance(struct fsck_instance *p)
  276. {
  277. free(p->prog);
  278. free(p->device);
  279. free(p->base_device);
  280. free(p);
  281. }
  282. static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
  283. const char *type, const char *opts,
  284. int passno)
  285. {
  286. struct fs_info *fs;
  287. fs = xzalloc(sizeof(*fs));
  288. fs->device = xstrdup(device);
  289. fs->mountpt = xstrdup(mntpnt);
  290. if (strchr(type, ','))
  291. type = (char *)"auto";
  292. fs->type = xstrdup(type);
  293. fs->opts = xstrdup(opts ? opts : "");
  294. fs->passno = passno < 0 ? 1 : passno;
  295. /*fs->flags = 0; */
  296. /*fs->next = NULL; */
  297. if (!G.filesys_info)
  298. G.filesys_info = fs;
  299. else
  300. G.filesys_last->next = fs;
  301. G.filesys_last = fs;
  302. return fs;
  303. }
  304. /* Load the filesystem database from /etc/fstab */
  305. static void load_fs_info(const char *filename)
  306. {
  307. FILE *fstab;
  308. struct mntent mte;
  309. char buf[1024];
  310. fstab = setmntent(filename, "r");
  311. if (!fstab) {
  312. bb_perror_msg("can't read '%s'", filename);
  313. return;
  314. }
  315. // Loop through entries
  316. while (getmntent_r(fstab, &mte, buf, sizeof(buf))) {
  317. //bb_error_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
  318. // mte.mnt_type, mte.mnt_opts,
  319. // mte.mnt_passno);
  320. create_fs_device(mte.mnt_fsname, mte.mnt_dir,
  321. mte.mnt_type, mte.mnt_opts,
  322. mte.mnt_passno);
  323. }
  324. endmntent(fstab);
  325. }
  326. /* Lookup filesys in /etc/fstab and return the corresponding entry. */
  327. static struct fs_info *lookup(char *filesys)
  328. {
  329. struct fs_info *fs;
  330. for (fs = G.filesys_info; fs; fs = fs->next) {
  331. if (strcmp(filesys, fs->device) == 0
  332. || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
  333. )
  334. break;
  335. }
  336. return fs;
  337. }
  338. #if DO_PROGRESS_INDICATOR
  339. static int progress_active(void)
  340. {
  341. struct fsck_instance *inst;
  342. for (inst = G.instance_list; inst; inst = inst->next) {
  343. if (inst->flags & FLAG_DONE)
  344. continue;
  345. if (inst->flags & FLAG_PROGRESS)
  346. return 1;
  347. }
  348. return 0;
  349. }
  350. #endif
  351. /*
  352. * Send a signal to all outstanding fsck child processes
  353. */
  354. static void kill_all_if_got_signal(void)
  355. {
  356. struct fsck_instance *inst;
  357. if (!bb_got_signal || G.kill_sent)
  358. return;
  359. for (inst = G.instance_list; inst; inst = inst->next) {
  360. if (inst->flags & FLAG_DONE)
  361. continue;
  362. kill(inst->pid, SIGTERM);
  363. }
  364. G.kill_sent = 1;
  365. }
  366. /*
  367. * Wait for one child process to exit; when it does, unlink it from
  368. * the list of executing child processes, free, and return its exit status.
  369. * If there is no exited child, return -1.
  370. */
  371. static int wait_one(int flags)
  372. {
  373. int status;
  374. int sig;
  375. struct fsck_instance *inst, *prev;
  376. pid_t pid;
  377. if (!G.instance_list)
  378. return -1;
  379. /* if (G.noexecute) { already returned -1; } */
  380. while (1) {
  381. pid = waitpid(-1, &status, flags);
  382. kill_all_if_got_signal();
  383. if (pid == 0) /* flags == WNOHANG and no children exited */
  384. return -1;
  385. if (pid < 0) {
  386. if (errno == EINTR)
  387. continue;
  388. if (errno == ECHILD) { /* paranoia */
  389. bb_error_msg("wait: no more children");
  390. return -1;
  391. }
  392. bb_perror_msg("wait");
  393. continue;
  394. }
  395. prev = NULL;
  396. inst = G.instance_list;
  397. do {
  398. if (inst->pid == pid)
  399. goto child_died;
  400. prev = inst;
  401. inst = inst->next;
  402. } while (inst);
  403. }
  404. child_died:
  405. status = WEXITSTATUS(status);
  406. if (WIFSIGNALED(status)) {
  407. sig = WTERMSIG(status);
  408. status = EXIT_UNCORRECTED;
  409. if (sig != SIGINT) {
  410. printf("Warning: %s %s terminated "
  411. "by signal %d\n",
  412. inst->prog, inst->device, sig);
  413. status = EXIT_ERROR;
  414. }
  415. }
  416. #if DO_PROGRESS_INDICATOR
  417. if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
  418. struct fsck_instance *inst2;
  419. for (inst2 = G.instance_list; inst2; inst2 = inst2->next) {
  420. if (inst2->flags & FLAG_DONE)
  421. continue;
  422. if (strcmp(inst2->type, "ext2") != 0
  423. && strcmp(inst2->type, "ext3") != 0
  424. ) {
  425. continue;
  426. }
  427. /* ext[23], we will send USR1
  428. * (request to start displaying progress bar)
  429. *
  430. * If we've just started the fsck, wait a tiny
  431. * bit before sending the kill, to give it
  432. * time to set up the signal handler
  433. */
  434. if (inst2->start_time >= time(NULL) - 1)
  435. sleep(1);
  436. kill(inst2->pid, SIGUSR1);
  437. inst2->flags |= FLAG_PROGRESS;
  438. break;
  439. }
  440. }
  441. #endif
  442. if (prev)
  443. prev->next = inst->next;
  444. else
  445. G.instance_list = inst->next;
  446. if (G.verbose > 1)
  447. printf("Finished with %s (exit status %d)\n",
  448. inst->device, status);
  449. G.num_running--;
  450. free_instance(inst);
  451. return status;
  452. }
  453. /*
  454. * Wait until all executing child processes have exited; return the
  455. * logical OR of all of their exit code values.
  456. */
  457. #define FLAG_WAIT_ALL 0
  458. #define FLAG_WAIT_ATLEAST_ONE WNOHANG
  459. static int wait_many(int flags)
  460. {
  461. int exit_status;
  462. int global_status = 0;
  463. int wait_flags = 0;
  464. while ((exit_status = wait_one(wait_flags)) != -1) {
  465. global_status |= exit_status;
  466. wait_flags |= flags;
  467. }
  468. return global_status;
  469. }
  470. /*
  471. * Execute a particular fsck program, and link it into the list of
  472. * child processes we are waiting for.
  473. */
  474. static void execute(const char *type, const char *device,
  475. const char *mntpt /*, int interactive */)
  476. {
  477. int i;
  478. struct fsck_instance *inst;
  479. pid_t pid;
  480. G.args[0] = xasprintf("fsck.%s", type);
  481. #if DO_PROGRESS_INDICATOR
  482. if (progress && !progress_active()) {
  483. if (strcmp(type, "ext2") == 0
  484. || strcmp(type, "ext3") == 0
  485. ) {
  486. G.args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
  487. inst->flags |= FLAG_PROGRESS;
  488. }
  489. }
  490. #endif
  491. G.args[G.num_args - 2] = (char*)device;
  492. /* G.args[G.num_args - 1] = NULL; - already is */
  493. if (G.verbose || G.noexecute) {
  494. printf("[%s (%d) -- %s]", G.args[0], G.num_running,
  495. mntpt ? mntpt : device);
  496. for (i = 0; G.args[i]; i++)
  497. printf(" %s", G.args[i]);
  498. bb_putchar('\n');
  499. }
  500. /* Fork and execute the correct program. */
  501. pid = -1;
  502. if (!G.noexecute) {
  503. pid = spawn(G.args);
  504. if (pid < 0)
  505. bb_simple_perror_msg(G.args[0]);
  506. }
  507. #if DO_PROGRESS_INDICATOR
  508. free(G.args[XXX]);
  509. #endif
  510. /* No child, so don't record an instance */
  511. if (pid <= 0) {
  512. free(G.args[0]);
  513. return;
  514. }
  515. inst = xzalloc(sizeof(*inst));
  516. inst->pid = pid;
  517. inst->prog = G.args[0];
  518. inst->device = xstrdup(device);
  519. inst->base_device = base_device(device);
  520. #if DO_PROGRESS_INDICATOR
  521. inst->start_time = time(NULL);
  522. #endif
  523. /* Add to the list of running fsck's.
  524. * (was adding to the end, but adding to the front is simpler...) */
  525. inst->next = G.instance_list;
  526. G.instance_list = inst;
  527. }
  528. /*
  529. * Run the fsck program on a particular device
  530. *
  531. * If the type is specified using -t, and it isn't prefixed with "no"
  532. * (as in "noext2") and only one filesystem type is specified, then
  533. * use that type regardless of what is specified in /etc/fstab.
  534. *
  535. * If the type isn't specified by the user, then use either the type
  536. * specified in /etc/fstab, or "auto".
  537. */
  538. static void fsck_device(struct fs_info *fs /*, int interactive */)
  539. {
  540. const char *type;
  541. if (strcmp(fs->type, "auto") != 0) {
  542. type = fs->type;
  543. if (G.verbose > 2)
  544. printf("using filesystem type '%s' %s\n",
  545. type, "from fstab");
  546. } else if (G.fstype
  547. && (G.fstype[0] != 'n' || G.fstype[1] != 'o') /* != "no" */
  548. && !is_prefixed_with(G.fstype, "opts=")
  549. && !is_prefixed_with(G.fstype, "loop")
  550. && !strchr(G.fstype, ',')
  551. ) {
  552. type = G.fstype;
  553. if (G.verbose > 2)
  554. printf("using filesystem type '%s' %s\n",
  555. type, "from -t");
  556. } else {
  557. type = "auto";
  558. if (G.verbose > 2)
  559. printf("using filesystem type '%s' %s\n",
  560. type, "(default)");
  561. }
  562. G.num_running++;
  563. execute(type, fs->device, fs->mountpt /*, interactive */);
  564. }
  565. /*
  566. * Returns TRUE if a partition on the same disk is already being
  567. * checked.
  568. */
  569. static int device_already_active(char *device)
  570. {
  571. struct fsck_instance *inst;
  572. char *base;
  573. if (G.force_all_parallel)
  574. return 0;
  575. #ifdef BASE_MD
  576. /* Don't check a soft raid disk with any other disk */
  577. if (G.instance_list
  578. && (is_prefixed_with(G.instance_list->device, BASE_MD)
  579. || is_prefixed_with(device, BASE_MD))
  580. ) {
  581. return 1;
  582. }
  583. #endif
  584. base = base_device(device);
  585. /*
  586. * If we don't know the base device, assume that the device is
  587. * already active if there are any fsck instances running.
  588. */
  589. if (!base)
  590. return (G.instance_list != NULL);
  591. for (inst = G.instance_list; inst; inst = inst->next) {
  592. if (!inst->base_device || strcmp(base, inst->base_device) == 0) {
  593. free(base);
  594. return 1;
  595. }
  596. }
  597. free(base);
  598. return 0;
  599. }
  600. /*
  601. * This function returns true if a particular option appears in a
  602. * comma-delimited options list
  603. */
  604. static int opt_in_list(char *opt, char *optlist)
  605. {
  606. char *s;
  607. int len;
  608. if (!optlist)
  609. return 0;
  610. len = strlen(opt);
  611. s = optlist - 1;
  612. while (1) {
  613. s = strstr(s + 1, opt);
  614. if (!s)
  615. return 0;
  616. /* neither "opt.." nor "xxx,opt.."? */
  617. if (s != optlist && s[-1] != ',')
  618. continue;
  619. /* neither "..opt" nor "..opt,xxx"? */
  620. if (s[len] != '\0' && s[len] != ',')
  621. continue;
  622. return 1;
  623. }
  624. }
  625. /* See if the filesystem matches the criteria given by the -t option */
  626. static int fs_match(struct fs_info *fs)
  627. {
  628. int n, ret, checked_type;
  629. char *cp;
  630. if (!G.fs_type_list)
  631. return 1;
  632. ret = 0;
  633. checked_type = 0;
  634. n = 0;
  635. while (1) {
  636. cp = G.fs_type_list[n];
  637. if (!cp)
  638. break;
  639. switch (G.fs_type_flag[n]) {
  640. case FS_TYPE_FLAG_NORMAL:
  641. checked_type++;
  642. if (strcmp(cp, fs->type) == 0)
  643. ret = 1;
  644. break;
  645. case FS_TYPE_FLAG_NEGOPT:
  646. if (opt_in_list(cp, fs->opts))
  647. return 0;
  648. break;
  649. case FS_TYPE_FLAG_OPT:
  650. if (!opt_in_list(cp, fs->opts))
  651. return 0;
  652. break;
  653. }
  654. n++;
  655. }
  656. if (checked_type == 0)
  657. return 1;
  658. return (G.fs_type_negated ? !ret : ret);
  659. }
  660. /* Check if we should ignore this filesystem. */
  661. static int ignore(struct fs_info *fs)
  662. {
  663. /*
  664. * If the pass number is 0, ignore it.
  665. */
  666. if (fs->passno == 0)
  667. return 1;
  668. /*
  669. * If a specific fstype is specified, and it doesn't match,
  670. * ignore it.
  671. */
  672. if (!fs_match(fs))
  673. return 1;
  674. /* Are we ignoring this type? */
  675. if (index_in_strings(ignored_types, fs->type) >= 0)
  676. return 1;
  677. /* We can and want to check this file system type. */
  678. return 0;
  679. }
  680. /* Check all file systems, using the /etc/fstab table. */
  681. static int check_all(void)
  682. {
  683. struct fs_info *fs;
  684. int status = EXIT_OK;
  685. smallint not_done_yet;
  686. smallint pass_done;
  687. int passno;
  688. if (G.verbose)
  689. puts("Checking all filesystems");
  690. /*
  691. * Do an initial scan over the filesystem; mark filesystems
  692. * which should be ignored as done, and resolve any "auto"
  693. * filesystem types (done as a side-effect of calling ignore()).
  694. */
  695. for (fs = G.filesys_info; fs; fs = fs->next)
  696. if (ignore(fs))
  697. fs->flags |= FLAG_DONE;
  698. /*
  699. * Find and check the root filesystem.
  700. */
  701. if (!G.parallel_root) {
  702. for (fs = G.filesys_info; fs; fs = fs->next) {
  703. if (LONE_CHAR(fs->mountpt, '/')) {
  704. if (!G.skip_root && !ignore(fs)) {
  705. fsck_device(fs /*, 1*/);
  706. status |= wait_many(FLAG_WAIT_ALL);
  707. if (status > EXIT_NONDESTRUCT)
  708. return status;
  709. }
  710. fs->flags |= FLAG_DONE;
  711. break;
  712. }
  713. }
  714. }
  715. /*
  716. * This is for the bone-headed user who has root
  717. * filesystem listed twice.
  718. * "Skip root" will skip _all_ root entries.
  719. */
  720. if (G.skip_root)
  721. for (fs = G.filesys_info; fs; fs = fs->next)
  722. if (LONE_CHAR(fs->mountpt, '/'))
  723. fs->flags |= FLAG_DONE;
  724. not_done_yet = 1;
  725. passno = 1;
  726. while (not_done_yet) {
  727. not_done_yet = 0;
  728. pass_done = 1;
  729. for (fs = G.filesys_info; fs; fs = fs->next) {
  730. if (bb_got_signal)
  731. break;
  732. if (fs->flags & FLAG_DONE)
  733. continue;
  734. /*
  735. * If the filesystem's pass number is higher
  736. * than the current pass number, then we didn't
  737. * do it yet.
  738. */
  739. if (fs->passno > passno) {
  740. not_done_yet = 1;
  741. continue;
  742. }
  743. /*
  744. * If a filesystem on a particular device has
  745. * already been spawned, then we need to defer
  746. * this to another pass.
  747. */
  748. if (device_already_active(fs->device)) {
  749. pass_done = 0;
  750. continue;
  751. }
  752. /*
  753. * Spawn off the fsck process
  754. */
  755. fsck_device(fs /*, G.serialize*/);
  756. fs->flags |= FLAG_DONE;
  757. /*
  758. * Only do one filesystem at a time, or if we
  759. * have a limit on the number of fsck's extant
  760. * at one time, apply that limit.
  761. */
  762. if (G.serialize
  763. || (G.num_running >= G.max_running)
  764. ) {
  765. pass_done = 0;
  766. break;
  767. }
  768. }
  769. if (bb_got_signal)
  770. break;
  771. if (G.verbose > 1)
  772. printf("--waiting-- (pass %d)\n", passno);
  773. status |= wait_many(pass_done ? FLAG_WAIT_ALL :
  774. FLAG_WAIT_ATLEAST_ONE);
  775. if (pass_done) {
  776. if (G.verbose > 1)
  777. puts("----------------------------------");
  778. passno++;
  779. } else
  780. not_done_yet = 1;
  781. }
  782. kill_all_if_got_signal();
  783. status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
  784. return status;
  785. }
  786. /*
  787. * Deal with the fsck -t argument.
  788. * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
  789. * Why here we require "-t novfat,nonfs" ??
  790. */
  791. static void compile_fs_type(char *fs_type)
  792. {
  793. char *s;
  794. int num = 2;
  795. smallint negate;
  796. s = fs_type;
  797. while ((s = strchr(s, ','))) {
  798. num++;
  799. s++;
  800. }
  801. G.fs_type_list = xzalloc(num * sizeof(G.fs_type_list[0]));
  802. G.fs_type_flag = xzalloc(num * sizeof(G.fs_type_flag[0]));
  803. G.fs_type_negated = -1; /* not yet known is it negated or not */
  804. num = 0;
  805. s = fs_type;
  806. while (1) {
  807. char *comma;
  808. negate = 0;
  809. if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
  810. s += 2;
  811. negate = 1;
  812. } else if (s[0] == '!') {
  813. s++;
  814. negate = 1;
  815. }
  816. if (strcmp(s, "loop") == 0)
  817. /* loop is really short-hand for opts=loop */
  818. goto loop_special_case;
  819. if (is_prefixed_with(s, "opts=")) {
  820. s += 5;
  821. loop_special_case:
  822. G.fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
  823. } else {
  824. if (G.fs_type_negated == -1)
  825. G.fs_type_negated = negate;
  826. if (G.fs_type_negated != negate)
  827. bb_error_msg_and_die(
  828. "either all or none of the filesystem types passed to -t must be prefixed "
  829. "with 'no' or '!'");
  830. }
  831. comma = strchrnul(s, ',');
  832. G.fs_type_list[num++] = xstrndup(s, comma-s);
  833. if (*comma == '\0')
  834. break;
  835. s = comma + 1;
  836. }
  837. }
  838. static char **new_args(void)
  839. {
  840. G.args = xrealloc_vector(G.args, 2, G.num_args);
  841. return &G.args[G.num_args++];
  842. }
  843. int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  844. int fsck_main(int argc UNUSED_PARAM, char **argv)
  845. {
  846. int i, status;
  847. /*int interactive;*/
  848. struct fs_info *fs;
  849. const char *fstab;
  850. char *tmp;
  851. char **devices;
  852. int num_devices;
  853. smallint opts_for_fsck;
  854. smallint doall;
  855. smallint notitle;
  856. INIT_G();
  857. /* we want wait() to be interruptible */
  858. signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
  859. signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
  860. setbuf(stdout, NULL);
  861. opts_for_fsck = doall = notitle = 0;
  862. devices = NULL;
  863. num_devices = 0;
  864. new_args(); /* G.args[0] = NULL, will be replaced by fsck.<type> */
  865. /* G.instance_list = NULL; - in bss, so already zeroed */
  866. while (*++argv) {
  867. int j;
  868. int optpos;
  869. char *options;
  870. char *arg = *argv;
  871. /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
  872. if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
  873. // FIXME: must check that arg is a blkdev, or resolve
  874. // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
  875. // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
  876. devices = xrealloc_vector(devices, 2, num_devices);
  877. devices[num_devices++] = arg;
  878. continue;
  879. }
  880. if (arg[0] != '-' || opts_for_fsck) {
  881. *new_args() = arg;
  882. continue;
  883. }
  884. if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
  885. opts_for_fsck = 1;
  886. continue;
  887. }
  888. optpos = 0;
  889. options = NULL;
  890. for (j = 1; arg[j]; j++) {
  891. switch (arg[j]) {
  892. case 'A':
  893. doall = 1;
  894. break;
  895. #if DO_PROGRESS_INDICATOR
  896. case 'C':
  897. progress = 1;
  898. if (arg[++j]) { /* -Cn */
  899. progress_fd = xatoi_positive(&arg[j]);
  900. goto next_arg;
  901. }
  902. /* -C n */
  903. if (!*++argv)
  904. bb_show_usage();
  905. progress_fd = xatoi_positive(*argv);
  906. goto next_arg;
  907. #endif
  908. case 'V':
  909. G.verbose++;
  910. break;
  911. case 'N':
  912. G.noexecute = 1;
  913. break;
  914. case 'R':
  915. G.skip_root = 1;
  916. break;
  917. case 'T':
  918. notitle = 1;
  919. break;
  920. /* case 'M':
  921. like_mount = 1;
  922. break; */
  923. case 'P':
  924. G.parallel_root = 1;
  925. break;
  926. case 's':
  927. G.serialize = 1;
  928. break;
  929. case 't':
  930. if (G.fstype)
  931. bb_show_usage();
  932. if (arg[++j])
  933. tmp = &arg[j];
  934. else if (*++argv)
  935. tmp = *argv;
  936. else
  937. bb_show_usage();
  938. G.fstype = xstrdup(tmp);
  939. compile_fs_type(G.fstype);
  940. goto next_arg;
  941. case '?':
  942. bb_show_usage();
  943. break;
  944. default:
  945. optpos++;
  946. /* one extra for '\0' */
  947. options = xrealloc(options, optpos + 2);
  948. options[optpos] = arg[j];
  949. break;
  950. }
  951. }
  952. next_arg:
  953. if (optpos) {
  954. options[0] = '-';
  955. options[optpos + 1] = '\0';
  956. *new_args() = options;
  957. }
  958. }
  959. if (getenv("FSCK_FORCE_ALL_PARALLEL"))
  960. G.force_all_parallel = 1;
  961. tmp = getenv("FSCK_MAX_INST");
  962. G.max_running = INT_MAX;
  963. if (tmp)
  964. G.max_running = xatoi(tmp);
  965. new_args(); /* G.args[G.num_args - 2] will be replaced by <device> */
  966. new_args(); /* G.args[G.num_args - 1] is the last, NULL element */
  967. if (!notitle)
  968. puts("fsck (busybox "BB_VER")");
  969. /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
  970. * so we are scanning it anyway */
  971. fstab = getenv("FSTAB_FILE");
  972. if (!fstab)
  973. fstab = "/etc/fstab";
  974. load_fs_info(fstab);
  975. /*interactive = (num_devices == 1) | G.serialize;*/
  976. if (num_devices == 0)
  977. /*interactive =*/ G.serialize = doall = 1;
  978. if (doall)
  979. return check_all();
  980. status = 0;
  981. for (i = 0; i < num_devices; i++) {
  982. if (bb_got_signal) {
  983. kill_all_if_got_signal();
  984. break;
  985. }
  986. fs = lookup(devices[i]);
  987. if (!fs)
  988. fs = create_fs_device(devices[i], "", "auto", NULL, -1);
  989. fsck_device(fs /*, interactive */);
  990. if (G.serialize
  991. || (G.num_running >= G.max_running)
  992. ) {
  993. int exit_status = wait_one(0);
  994. if (exit_status >= 0)
  995. status |= exit_status;
  996. if (G.verbose > 1)
  997. puts("----------------------------------");
  998. }
  999. }
  1000. status |= wait_many(FLAG_WAIT_ALL);
  1001. return status;
  1002. }