fsck.c 25 KB

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