fsck.c 25 KB

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