fsck.c 24 KB

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