3
0

fsck.c 24 KB

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